Skip to content

Instantly share code, notes, and snippets.

@florimondmanca
Created July 15, 2018 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florimondmanca/92a9a31f2f04e4db245c7fd695dd4a48 to your computer and use it in GitHub Desktop.
Save florimondmanca/92a9a31f2f04e4db245c7fd695dd4a48 to your computer and use it in GitHub Desktop.
Build-time environment variable injection script for Angular apps
/* 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