Skip to content

Commit 814819d

Browse files
Allow custom configs (#699)
* 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. * Update variable name * configLocation -> configPath
1 parent 9c9b406 commit 814819d

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

packages/apollo-language-server/src/config.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,18 @@ export type ApolloConfigFormat =
112112

113113
// config settings
114114
const MODULE_NAME = "apollo";
115-
const searchPlaces = [
115+
const defaultSearchPlaces = [
116116
"package.json",
117117
`${MODULE_NAME}.config.js`,
118118
`${MODULE_NAME}.config.ts`
119119
];
120+
121+
// Based on order, a provided config file will take precedence over the defaults
122+
const getSearchPlaces = (configFile?: string) => [
123+
...(configFile ? [configFile] : []),
124+
...defaultSearchPlaces
125+
];
126+
120127
const loaders = {
121128
// XXX improve types for config
122129
".json": (cosmiconfig as any).loadJson as LoaderEntry,
@@ -130,7 +137,7 @@ export interface LoadConfigSettings {
130137
// the current working directory to start looking for the config
131138
// config loading only works on node so we default to
132139
// process.cwd()
133-
cwd: string;
140+
configPath?: string;
134141
name?: string;
135142
type?: "service" | "client";
136143
}
@@ -249,23 +256,23 @@ export function isServiceConfig(config: ApolloConfig): config is ServiceConfig {
249256

250257
// XXX load .env files automatically
251258
export const loadConfig = async ({
252-
cwd,
259+
configPath,
253260
name,
254261
type
255262
}: LoadConfigSettings): Promise<ConfigResult<ApolloConfig>> => {
256263
const explorer = cosmiconfig(MODULE_NAME, {
257-
searchPlaces,
264+
searchPlaces: getSearchPlaces(configPath),
258265
loaders
259266
});
260267

261-
let loadedConfig = (await explorer.search(cwd)) as ConfigResult<
268+
let loadedConfig = (await explorer.search(configPath)) as ConfigResult<
262269
ApolloConfigFormat
263270
>;
264271

265272
if (!loadedConfig) {
266273
loadedConfig = {
267274
isEmpty: false,
268-
filepath: cwd || process.cwd(),
275+
filepath: configPath || process.cwd(),
269276
config:
270277
type === "client"
271278
? {

packages/apollo-language-server/src/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class GraphQLWorkspace {
123123
`Loading Apollo Config in folder ${configFolder}`,
124124
(async () => {
125125
try {
126-
const config = await loadConfig({ cwd: configFolder });
126+
const config = await loadConfig({ configPath: configFolder });
127127
return config && config.config;
128128
} catch (e) {
129129
console.error(e);

packages/apollo/src/Command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export abstract class ProjectCommand extends Command {
102102
service = getServiceFromKey(process.env.ENGINE_API_KEY);
103103
if (flags.key) service = getServiceFromKey(flags.key);
104104
const loadedConfig = await loadConfig({
105-
cwd: flags.config,
105+
configPath: flags.config,
106106
name: service,
107107
type: this.type
108108
});

0 commit comments

Comments
 (0)