Skip to content

Commit ce42691

Browse files
trevor-scheerJakeDawkins
authored andcommitted
Fix order of .env loading (#815)
Load the env file earlier during configuration in order to infer the service name before it's required.
1 parent b5374cd commit ce42691

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ export function isServiceConfig(config: ApolloConfig): config is ServiceConfig {
254254
return config.isService;
255255
}
256256

257+
const getServiceFromKey = (key: string | undefined): string | undefined => {
258+
if (key) {
259+
const [type, service] = key.split(":");
260+
if (type === "service") return service;
261+
}
262+
return;
263+
};
264+
257265
// XXX load .env files automatically
258266
export const loadConfig = async ({
259267
configPath,
@@ -269,10 +277,29 @@ export const loadConfig = async ({
269277
ApolloConfigFormat
270278
>;
271279

280+
// add API to the env
281+
let engineConfig = {},
282+
nameFromKey;
283+
const dotEnvPath = configPath
284+
? resolve(parse(configPath).dir, ".env")
285+
: resolve(process.cwd(), ".env");
286+
287+
if (existsSync(dotEnvPath)) {
288+
const env: { [key: string]: string } = require("dotenv").parse(
289+
readFileSync(dotEnvPath)
290+
);
291+
292+
if (env["ENGINE_API_KEY"]) {
293+
engineConfig = { engine: { apiKey: env["ENGINE_API_KEY"] } };
294+
nameFromKey = getServiceFromKey(env["ENGINE_API_KEY"]);
295+
}
296+
}
297+
298+
const resolvedName = name || nameFromKey;
272299
// If there's a name passed in (from env/flag), it merges with the config file, to
273300
// overwrite either the client's service (if a client project), or the service's name.
274301
// if there's no config file, it uses the `DefaultConfigBase` to fill these in.
275-
if (!loadedConfig || name) {
302+
if (!loadedConfig || resolvedName) {
276303
loadedConfig = {
277304
isEmpty: false,
278305
filepath: configPath || process.cwd(),
@@ -283,34 +310,22 @@ export const loadConfig = async ({
283310
client: {
284311
...DefaultConfigBase,
285312
...(loadedConfig ? loadedConfig.config.client : {}),
286-
service: name!
313+
service: resolvedName
287314
}
288315
}
289316
: {
290317
...(loadedConfig ? loadedConfig.config : {}),
291318
service: {
292319
...DefaultConfigBase,
293320
...(loadedConfig ? loadedConfig.config.service : {}),
294-
name: name!
321+
name: resolvedName
295322
}
296323
}
297324
};
298325
}
299326

300327
let { config, filepath, isEmpty } = loadedConfig;
301328

302-
// add API to the env
303-
const dotEnvPath = resolve(parse(filepath).dir, ".env");
304-
if (existsSync(dotEnvPath)) {
305-
const env: { [key: string]: string } = require("dotenv").parse(
306-
readFileSync(dotEnvPath)
307-
);
308-
309-
if (env["ENGINE_API_KEY"]) {
310-
config = merge({ engine: { apiKey: env["ENGINE_API_KEY"] } }, config);
311-
}
312-
}
313-
314329
if (isEmpty) {
315330
throw new Error(
316331
`Apollo config found at ${filepath} is empty. Please add either a client or service config`
@@ -320,6 +335,8 @@ export const loadConfig = async ({
320335
// selectivly apply defaults when loading the config
321336
if (config.client) config = merge({ client: DefaultClientConfig }, config);
322337
if (config.service) config = merge({ service: DefaultServiceConfig }, config);
338+
if (engineConfig) config = merge(engineConfig, config);
339+
323340
config = merge({ engine: DefaultEngineConfig }, config);
324341

325342
return { config: new ApolloConfig(config), filepath, isEmpty };

packages/apollo/src/Command.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ export abstract class ProjectCommand extends Command {
119119
}
120120

121121
protected async createConfig(flags: Flags) {
122-
let service;
123-
if (process.env.ENGINE_API_KEY)
124-
service = getServiceFromKey(process.env.ENGINE_API_KEY);
125-
if (flags.key) service = getServiceFromKey(flags.key);
122+
const service = flags.key ? getServiceFromKey(flags.key) : undefined;
126123
const loadedConfig = await loadConfig({
127124
configPath: flags.config,
128125
name: service,

0 commit comments

Comments
 (0)