Skip to content

Commit

Permalink
feat: add option to parse JSON in environment variables (#24)
Browse files Browse the repository at this point in the history
Co-authored-by: Yohan Lasorsa <[email protected]>
  • Loading branch information
cnuss and sinedied authored Mar 12, 2021
1 parent 954195c commit 7f2c532
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This modules provides CLI commands to be used in [NPM scripts](https://docs.npmj

### Export environment variables to a JSON or JavaScript file

`ngx-scripts env <env_var> [<env_var2> ...] [--out <file>] [--format json|js]`
`ngx-scripts env <env_var> [<env_var2> ...] [--out <file>] [--format json|js] [--parse-json]`

Default output file is `src/environments/.env.ts` with JavaScript format.

Expand All @@ -52,6 +52,7 @@ Any accepted Cordova option can be passed through after `--`.
- `--release`: Create a Cordova release build
- `--verbose`: Show Cordova debug output
- `--yarn`: Use [Yarn](https://yarnpkg.com) instead of NPM to run the `build` script
- `--parse-json`: During `env`, if an environment variable's value is parsable JSON, it will be a proper object in `.env.ts`

> Note: Yarn is automatically used instead of NPM is the environment variable `NGX_PACKAGE_MANAGER` is set to `yarn` or
> if the current project was generated with ngX-Rocket using Yarn option (option is saved in `.yo-rc.json`).
Expand Down
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const help = `${chalk.bold('Usage')} ${appName} ${chalk.blue(
const detailedHelp = `
${chalk.blue(
'env'
)} <env_var> [<env_var2> ...] [--out <file>] [--format json|js]
)} <env_var> [<env_var2> ...] [--out <file>] [--format json|js] [--parse-json]
Export environment variables to a JSON or JavaScript file.
Default output file is ${chalk.cyan('src/environments/.env.ts')}
Expand Down Expand Up @@ -72,7 +72,8 @@ class NgxScriptsCli {
'yarn',
'cordova',
'dist',
'verbose'
'verbose',
'parse-json'
],
string: [
'out',
Expand Down Expand Up @@ -103,7 +104,8 @@ class NgxScriptsCli {
return this._env(
this._options._.slice(1),
this._options.out,
this._options.format
this._options.format,
this._options['parse-json']
);
case 'cordova':
return this._cordova(this._options);
Expand All @@ -114,14 +116,25 @@ class NgxScriptsCli {
}
}

_env(vars, outputFile = 'src/environments/.env.ts', format = 'js') {
_env(
vars,
outputFile = 'src/environments/.env.ts',
format = 'js',
parseJson = false
) {
if (vars.length === 0) {
this._exit(`${chalk.red('Missing arguments')}\n`);
}

let env = JSON.stringify(
vars.reduce((env, v) => {
env[v] = process.env[v] === undefined ? null : process.env[v];
if (parseJson) {
try {
env[v] = JSON.parse(env[v]);
} catch {}
}

return env;
}, {}),
null,
Expand All @@ -134,7 +147,9 @@ class NgxScriptsCli {
const s = v.replace(/'/g, "\\'").replace(/\\"/g, '"');
return `'${s}'`;
});
env = `export const env: { [s: string]: (string | null); } = ${env};\n`;
env = `export const env: { [s: string]: (${
parseJson ? `any` : `string`
} | null); } = ${env};\n`;
}

try {
Expand Down

0 comments on commit 7f2c532

Please sign in to comment.