Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom configs #699

Merged
merged 3 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
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;
configLocation?: 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,
configLocation,
name,
type
}: LoadConfigSettings): Promise<ConfigResult<ApolloConfig>> => {
const explorer = cosmiconfig(MODULE_NAME, {
searchPlaces,
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"
? {
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({ configLocation: 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,
configLocation: flags.config,
name: service,
type: this.type
});
Expand Down