Skip to content

Instantly share code, notes, and snippets.

@natchiketa
Last active June 14, 2023 12:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natchiketa/7036473812257bec7f7a97f436a9c601 to your computer and use it in GitHub Desktop.
Save natchiketa/7036473812257bec7f7a97f436a9c601 to your computer and use it in GitHub Desktop.
Use system environment variables to generate Angular CLI environment files. Uses yargs and dotenv
import { writeFile } from 'fs';
import { argv } from 'yargs';
// This is good for local dev environments, when it's better to
// store a projects environment variables in a .gitignore'd file
require('dotenv').config();
// Would be passed to script like this:
// `ts-node set-env.ts --environment=dev`
// we get it from yargs's argv object
const environment = argv.environment;
const isProd = environment === 'prod';
const targetPath = `./src/environments/environment.${environment}.ts`;
const envConfigFile = `
export const environment = {
production: ${isProd},
superSecretKey: "${process.env.SUPER_SECRET_CRED1}",
superDoubleSecret: "${process.env.SUPER_SECRET_CRED2}"
};
`
writeFile(targetPath, envConfigFile, function (err) {
if (err) {
console.log(err);
}
console.log(`Output generated at ${targetPath}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment