2022-05-23 09:56:11 +00:00
import * as fs from 'fs' ;
import * as path from 'path' ;
import * as log from 'npmlog' ;
2022-07-06 15:53:35 +00:00
import * as dotenv from 'dotenv' ;
2022-05-23 09:56:11 +00:00
import type { ExecutorContext } from '@nrwl/devkit' ;
2022-07-08 10:19:05 +00:00
process . env [ 'NX_GIT_COMMIT_HASH' ] = process . env [ 'COMMIT_REF' ] ? ? 'dev' ;
process . env [ 'NX_GIT_BRANCH' ] = process . env [ 'BRANCH' ] ? ? 'dev' ;
process . env [ 'NX_GIT_ORIGIN_URL' ] = process . env [ 'REPOSITORY_URL' ] ? ? '' ;
2022-05-23 09:56:11 +00:00
const logEnvData = (
envMap : Record < string , string > ,
envFiles : string [ ] ,
env? : string ,
2022-07-06 15:53:35 +00:00
defaultEnvFile? : string ,
loggerScope? : string
2022-05-23 09:56:11 +00:00
) = > {
if ( env && ! envMap [ env ] ) {
2022-07-06 15:53:35 +00:00
log . warn ( loggerScope , ` No environment called " ${ env } " found. ` ) ;
2022-05-23 09:56:11 +00:00
log . info (
2022-07-06 15:53:35 +00:00
loggerScope ,
2022-05-23 09:56:11 +00:00
envFiles . length > 0
? ` You can create a new environment by putting an ".env. ${ env } " file in your project root, or you can use the following available ones: ${ envFiles . join (
', '
) } . `
: 'To get started with environments, you can create an ".env" file in your project root with the desired variables.'
) ;
}
if ( ! envMap [ env ] ) {
log . info (
2022-07-06 15:53:35 +00:00
loggerScope ,
2022-05-23 09:56:11 +00:00
defaultEnvFile
? ` Using " ${ defaultEnvFile } " as the default project environment. `
: 'Serving the project only using the environment variables scoped to your CLI.'
) ;
} else {
log . info (
2022-07-06 15:53:35 +00:00
loggerScope ,
2022-05-23 09:56:11 +00:00
` Using " ${ envMap [ env ] } " as the default project environment. `
) ;
}
} ;
const filenameToEnv = ( filename : string ) = > filename . replace ( '.env.' , '' ) ;
const getDefaultEnvFile = ( envMap : Record < string , string > ) = > {
return envMap [ 'local' ] || envMap [ '.env' ] ;
} ;
2022-07-06 15:53:35 +00:00
const getEnvFile = ( env : string , envFiles : string [ ] , loggerScope? : string ) = > {
2022-05-23 09:56:11 +00:00
const envMap = envFiles . reduce (
( acc , filename ) = > ( {
. . . acc ,
[ filenameToEnv ( filename ) ] : filename ,
} ) ,
{ }
) ;
const defaultEnvFile = getDefaultEnvFile ( envMap ) ;
2022-07-06 15:53:35 +00:00
logEnvData ( envMap , envFiles , env , defaultEnvFile , loggerScope ) ;
2022-05-23 09:56:11 +00:00
return envMap [ env ] || defaultEnvFile ;
} ;
2022-07-06 15:53:35 +00:00
export default async function setup (
env : string ,
context : ExecutorContext ,
loggerScope? : string
) {
2022-05-23 09:56:11 +00:00
const { root } = context . workspace . projects [ context . projectName ] ;
const workspacePath = path . join ( context . cwd , root ) ;
const files = await fs . promises . readdir ( workspacePath ) ;
2022-07-06 15:53:35 +00:00
2022-05-23 09:56:11 +00:00
const envFile = getEnvFile (
env ,
2022-07-06 15:53:35 +00:00
files . filter ( ( f ) = > f . startsWith ( '.env' ) ) ,
loggerScope
2022-05-23 09:56:11 +00:00
) ;
if ( env && envFile ) {
dotenv . config ( { path : path.join ( workspacePath , envFile ) , override : true } ) ;
}
}