From c78b9d64789a959065c6111bcec23b2b7d31bb12 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Wed, 14 Nov 2018 09:10:30 -0800 Subject: [PATCH 1/3] Allow custom configs Cosmiconfig needs to know it can look elsewhere when a custom config is specified. This allows the --config to be something other than apollo.config.js. Note: if a custom config is specified, it will take precedence over our defaults. --- packages/apollo-language-server/src/config.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/apollo-language-server/src/config.ts b/packages/apollo-language-server/src/config.ts index 7633a2d9ae..78cf2a7d9f 100644 --- a/packages/apollo-language-server/src/config.ts +++ b/packages/apollo-language-server/src/config.ts @@ -112,11 +112,17 @@ export type ApolloConfigFormat = // config settings const MODULE_NAME = "apollo"; -const searchPlaces = [ +const defaultSearchPlaces = [ "package.json", `${MODULE_NAME}.config.js`, `${MODULE_NAME}.config.ts` ]; + +const getSearchPlaces = (configFile?: string) => [ + ...(configFile ? [configFile] : []), + ...defaultSearchPlaces +]; + const loaders = { // XXX improve types for config ".json": (cosmiconfig as any).loadJson as LoaderEntry, @@ -130,7 +136,7 @@ export interface LoadConfigSettings { // the current working directory to start looking for the config // config loading only works on node so we default to // process.cwd() - cwd: string; + cwd?: string; name?: string; type?: "service" | "client"; } @@ -254,7 +260,7 @@ export const loadConfig = async ({ type }: LoadConfigSettings): Promise> => { const explorer = cosmiconfig(MODULE_NAME, { - searchPlaces, + searchPlaces: getSearchPlaces(cwd), loaders }); From 0976201d9fc7a109e8b92c0013830fbd409a4a57 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Wed, 14 Nov 2018 09:44:31 -0800 Subject: [PATCH 2/3] Update variable name --- packages/apollo-language-server/src/config.ts | 11 ++++++----- packages/apollo-language-server/src/workspace.ts | 2 +- packages/apollo/src/Command.ts | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/apollo-language-server/src/config.ts b/packages/apollo-language-server/src/config.ts index 78cf2a7d9f..000fcbbac1 100644 --- a/packages/apollo-language-server/src/config.ts +++ b/packages/apollo-language-server/src/config.ts @@ -118,6 +118,7 @@ const defaultSearchPlaces = [ `${MODULE_NAME}.config.ts` ]; +// Based on order, a provided config file will take precedence over the defaults const getSearchPlaces = (configFile?: string) => [ ...(configFile ? [configFile] : []), ...defaultSearchPlaces @@ -136,7 +137,7 @@ export interface LoadConfigSettings { // the current working directory to start looking for the config // config loading only works on node so we default to // process.cwd() - cwd?: string; + configLocation?: string; name?: string; type?: "service" | "client"; } @@ -255,23 +256,23 @@ export function isServiceConfig(config: ApolloConfig): config is ServiceConfig { // XXX load .env files automatically export const loadConfig = async ({ - cwd, + configLocation, name, type }: LoadConfigSettings): Promise> => { const explorer = cosmiconfig(MODULE_NAME, { - searchPlaces: getSearchPlaces(cwd), + searchPlaces: getSearchPlaces(configLocation), loaders }); - let loadedConfig = (await explorer.search(cwd)) as ConfigResult< + let loadedConfig = (await explorer.search(configLocation)) as ConfigResult< ApolloConfigFormat >; if (!loadedConfig) { loadedConfig = { isEmpty: false, - filepath: cwd || process.cwd(), + filepath: configLocation || process.cwd(), config: type === "client" ? { diff --git a/packages/apollo-language-server/src/workspace.ts b/packages/apollo-language-server/src/workspace.ts index ee31eeece9..5de2876bd2 100644 --- a/packages/apollo-language-server/src/workspace.ts +++ b/packages/apollo-language-server/src/workspace.ts @@ -123,7 +123,7 @@ export class GraphQLWorkspace { `Loading Apollo Config in folder ${configFolder}`, (async () => { try { - const config = await loadConfig({ cwd: configFolder }); + const config = await loadConfig({ configLocation: configFolder }); return config && config.config; } catch (e) { console.error(e); diff --git a/packages/apollo/src/Command.ts b/packages/apollo/src/Command.ts index 59684ac673..1944e7ac04 100644 --- a/packages/apollo/src/Command.ts +++ b/packages/apollo/src/Command.ts @@ -102,7 +102,7 @@ export abstract class ProjectCommand extends Command { service = getServiceFromKey(process.env.ENGINE_API_KEY); if (flags.key) service = getServiceFromKey(flags.key); const loadedConfig = await loadConfig({ - cwd: flags.config, + configLocation: flags.config, name: service, type: this.type }); From 297a291ae4021687ed1b160021e38d27261718a1 Mon Sep 17 00:00:00 2001 From: Trevor Scheer Date: Wed, 14 Nov 2018 09:58:31 -0800 Subject: [PATCH 3/3] configLocation -> configPath --- packages/apollo-language-server/src/config.ts | 10 +++++----- packages/apollo-language-server/src/workspace.ts | 2 +- packages/apollo/src/Command.ts | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/apollo-language-server/src/config.ts b/packages/apollo-language-server/src/config.ts index 000fcbbac1..cf16793f11 100644 --- a/packages/apollo-language-server/src/config.ts +++ b/packages/apollo-language-server/src/config.ts @@ -137,7 +137,7 @@ export interface LoadConfigSettings { // the current working directory to start looking for the config // config loading only works on node so we default to // process.cwd() - configLocation?: string; + configPath?: string; name?: string; type?: "service" | "client"; } @@ -256,23 +256,23 @@ export function isServiceConfig(config: ApolloConfig): config is ServiceConfig { // XXX load .env files automatically export const loadConfig = async ({ - configLocation, + configPath, name, type }: LoadConfigSettings): Promise> => { const explorer = cosmiconfig(MODULE_NAME, { - searchPlaces: getSearchPlaces(configLocation), + searchPlaces: getSearchPlaces(configPath), loaders }); - let loadedConfig = (await explorer.search(configLocation)) as ConfigResult< + let loadedConfig = (await explorer.search(configPath)) as ConfigResult< ApolloConfigFormat >; if (!loadedConfig) { loadedConfig = { isEmpty: false, - filepath: configLocation || process.cwd(), + filepath: configPath || process.cwd(), config: type === "client" ? { diff --git a/packages/apollo-language-server/src/workspace.ts b/packages/apollo-language-server/src/workspace.ts index 5de2876bd2..34caafa499 100644 --- a/packages/apollo-language-server/src/workspace.ts +++ b/packages/apollo-language-server/src/workspace.ts @@ -123,7 +123,7 @@ export class GraphQLWorkspace { `Loading Apollo Config in folder ${configFolder}`, (async () => { try { - const config = await loadConfig({ configLocation: configFolder }); + const config = await loadConfig({ configPath: configFolder }); return config && config.config; } catch (e) { console.error(e); diff --git a/packages/apollo/src/Command.ts b/packages/apollo/src/Command.ts index 1944e7ac04..0481c875e6 100644 --- a/packages/apollo/src/Command.ts +++ b/packages/apollo/src/Command.ts @@ -102,7 +102,7 @@ export abstract class ProjectCommand extends Command { service = getServiceFromKey(process.env.ENGINE_API_KEY); if (flags.key) service = getServiceFromKey(flags.key); const loadedConfig = await loadConfig({ - configLocation: flags.config, + configPath: flags.config, name: service, type: this.type });