Skip to content

Commit 7f2c532

Browse files
cnusssinedied
andauthored
feat: add option to parse JSON in environment variables (#24)
Co-authored-by: Yohan Lasorsa <noda@free.fr>
1 parent 954195c commit 7f2c532

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This modules provides CLI commands to be used in [NPM scripts](https://docs.npmj
2828

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

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

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

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

5657
> Note: Yarn is automatically used instead of NPM is the environment variable `NGX_PACKAGE_MANAGER` is set to `yarn` or
5758
> if the current project was generated with ngX-Rocket using Yarn option (option is saved in `.yo-rc.json`).

index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const help = `${chalk.bold('Usage')} ${appName} ${chalk.blue(
1616
const detailedHelp = `
1717
${chalk.blue(
1818
'env'
19-
)} <env_var> [<env_var2> ...] [--out <file>] [--format json|js]
19+
)} <env_var> [<env_var2> ...] [--out <file>] [--format json|js] [--parse-json]
2020
Export environment variables to a JSON or JavaScript file.
2121
Default output file is ${chalk.cyan('src/environments/.env.ts')}
2222
@@ -72,7 +72,8 @@ class NgxScriptsCli {
7272
'yarn',
7373
'cordova',
7474
'dist',
75-
'verbose'
75+
'verbose',
76+
'parse-json'
7677
],
7778
string: [
7879
'out',
@@ -103,7 +104,8 @@ class NgxScriptsCli {
103104
return this._env(
104105
this._options._.slice(1),
105106
this._options.out,
106-
this._options.format
107+
this._options.format,
108+
this._options['parse-json']
107109
);
108110
case 'cordova':
109111
return this._cordova(this._options);
@@ -114,14 +116,25 @@ class NgxScriptsCli {
114116
}
115117
}
116118

117-
_env(vars, outputFile = 'src/environments/.env.ts', format = 'js') {
119+
_env(
120+
vars,
121+
outputFile = 'src/environments/.env.ts',
122+
format = 'js',
123+
parseJson = false
124+
) {
118125
if (vars.length === 0) {
119126
this._exit(`${chalk.red('Missing arguments')}\n`);
120127
}
121128

122129
let env = JSON.stringify(
123130
vars.reduce((env, v) => {
124131
env[v] = process.env[v] === undefined ? null : process.env[v];
132+
if (parseJson) {
133+
try {
134+
env[v] = JSON.parse(env[v]);
135+
} catch {}
136+
}
137+
125138
return env;
126139
}, {}),
127140
null,
@@ -134,7 +147,9 @@ class NgxScriptsCli {
134147
const s = v.replace(/'/g, "\\'").replace(/\\"/g, '"');
135148
return `'${s}'`;
136149
});
137-
env = `export const env: { [s: string]: (string | null); } = ${env};\n`;
150+
env = `export const env: { [s: string]: (${
151+
parseJson ? `any` : `string`
152+
} | null); } = ${env};\n`;
138153
}
139154

140155
try {

0 commit comments

Comments
 (0)