Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/commands/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,7 +72,7 @@ export async function getSettings(options: Options = {}): Promise<Settings> {
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(
Expand All @@ -86,7 +88,7 @@ export async function getSettings(options: Options = {}): Promise<Settings> {
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);
Expand All @@ -95,6 +97,10 @@ export async function getSettings(options: Options = {}): Promise<Settings> {
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.",
Expand Down