Last active
June 14, 2023 12:35
-
-
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
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
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