@@ -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
258266export 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 } ;
0 commit comments