Created
July 15, 2018 08:09
-
-
Save florimondmanca/92a9a31f2f04e4db245c7fd695dd4a48 to your computer and use it in GitHub Desktop.
Build-time environment variable injection script for Angular apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Generate environment.ts file using environment variables | |
Requires: | |
npm install --save yargs | |
npm install --save dotenv | |
Usage: | |
node set-env.js --env=dev | |
Put variables in a local .env file so they can be | |
read by `dotenv`. | |
Register more env variables for your app in the | |
`envConfigFile` template definition. | |
*/ | |
const writeFile = require('fs').writeFile; | |
const argv = require('yargs').argv; | |
require('dotenv').config(); | |
const environment = argv.env; | |
const isProd = environment === 'prod'; | |
const targetPath = `./src/environments/environment.${environment}.ts`; | |
const envConfigFile = `\ | |
// ${environment} environment | |
export const environment = { | |
production: ${isProd}, | |
myVar: "${process.env.MY_VAR}", | |
}; | |
` | |
writeFile(targetPath, envConfigFile, function (err) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(`Output generated at ${targetPath}:`); | |
console.log(envConfigFile); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment