diff --git a/src/commands/_common.ts b/src/commands/_common.ts index 971c2dd..c27cd25 100644 --- a/src/commands/_common.ts +++ b/src/commands/_common.ts @@ -7,6 +7,8 @@ import { Settings } from "../settings"; export const DEFAULT_GMRC_PATH = `${process.cwd()}/.gmrc`; export const DEFAULT_GMRCJS_PATH = `${DEFAULT_GMRC_PATH}.js`; +export const DEFAULT_GMRCCJS_PATH = `${DEFAULT_GMRC_PATH}.cjs`; +export const DEFAULT_GMRCMJS_PATH = `${DEFAULT_GMRC_PATH}.mjs`; /** * Represents the option flags that are valid for all commands (see @@ -70,7 +72,7 @@ export async function getSettings(options: Options = {}): Promise { const relativePath = resolve(process.cwd(), path); try { - return require(relativePath); + return relativePath.endsWith(".mjs") ? await import(relativePath) : require(relativePath); } catch (e) { throw new Error( `Failed to import '${relativePath}'; error:\n ${e.stack.replace( @@ -86,7 +88,7 @@ export async function getSettings(options: Options = {}): Promise { throw new Error(`Failed to import '${configFile}': file not found`); } - if (configFile.endsWith(".js")) { + if (configFile.endsWith(".js") || configFile.endsWith(".cjs") || configFile.endsWith(".mjs")) { return tryRequire(configFile); } else { return await getSettingsFromJSON(configFile); @@ -95,6 +97,10 @@ export async function getSettings(options: Options = {}): Promise { return await getSettingsFromJSON(DEFAULT_GMRC_PATH); } else if (await exists(DEFAULT_GMRCJS_PATH)) { return tryRequire(DEFAULT_GMRCJS_PATH); + } else if (await exists(DEFAULT_GMRCCJS_PATH)) { + return tryRequire(DEFAULT_GMRCCJS_PATH); + } else if (await exists(DEFAULT_GMRCMJS_PATH)) { + return tryRequire(DEFAULT_GMRCMJS_PATH); } else { throw new Error( "No .gmrc file found; please run `graphile-migrate init` first.",