Skip to content

Commit

Permalink
Allow custom configs (#699)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
trevor-scheer authored Nov 14, 2018
1 parent 9c9b406 commit 814819d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions packages/apollo-language-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,18 @@ export type ApolloConfigFormat =

// config settings
const MODULE_NAME = "apollo";
const searchPlaces = [
const defaultSearchPlaces = [
"package.json",
`${MODULE_NAME}.config.js`,
`${MODULE_NAME}.config.ts`
];

// Based on order, a provided config file will take precedence over the defaults
const getSearchPlaces = (configFile?: string) => [
...(configFile ? [configFile] : []),
...defaultSearchPlaces
];

const loaders = {
// XXX improve types for config
".json": (cosmiconfig as any).loadJson as LoaderEntry,
Expand All @@ -130,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;
configPath?: string;
name?: string;
type?: "service" | "client";
}
Expand Down Expand Up @@ -249,23 +256,23 @@ export function isServiceConfig(config: ApolloConfig): config is ServiceConfig {

// XXX load .env files automatically
export const loadConfig = async ({
cwd,
configPath,
name,
type
}: LoadConfigSettings): Promise<ConfigResult<ApolloConfig>> => {
const explorer = cosmiconfig(MODULE_NAME, {
searchPlaces,
searchPlaces: getSearchPlaces(configPath),
loaders
});

let loadedConfig = (await explorer.search(cwd)) as ConfigResult<
let loadedConfig = (await explorer.search(configPath)) as ConfigResult<
ApolloConfigFormat
>;

if (!loadedConfig) {
loadedConfig = {
isEmpty: false,
filepath: cwd || process.cwd(),
filepath: configPath || process.cwd(),
config:
type === "client"
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({ configPath: configFolder });
return config && config.config;
} catch (e) {
console.error(e);
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
configPath: flags.config,
name: service,
type: this.type
});
Expand Down

0 comments on commit 814819d

Please sign in to comment.