@@ -16,7 +16,7 @@ const help = `${chalk.bold('Usage')} ${appName} ${chalk.blue(
16
16
const detailedHelp = `
17
17
${ chalk . blue (
18
18
'env'
19
- ) } <env_var> [<env_var2> ...] [--out <file>] [--format json|js]
19
+ ) } <env_var> [<env_var2> ...] [--out <file>] [--format json|js] [--parse-json]
20
20
Export environment variables to a JSON or JavaScript file.
21
21
Default output file is ${ chalk . cyan ( 'src/environments/.env.ts' ) }
22
22
@@ -72,7 +72,8 @@ class NgxScriptsCli {
72
72
'yarn' ,
73
73
'cordova' ,
74
74
'dist' ,
75
- 'verbose'
75
+ 'verbose' ,
76
+ 'parse-json'
76
77
] ,
77
78
string : [
78
79
'out' ,
@@ -103,7 +104,8 @@ class NgxScriptsCli {
103
104
return this . _env (
104
105
this . _options . _ . slice ( 1 ) ,
105
106
this . _options . out ,
106
- this . _options . format
107
+ this . _options . format ,
108
+ this . _options [ 'parse-json' ]
107
109
) ;
108
110
case 'cordova' :
109
111
return this . _cordova ( this . _options ) ;
@@ -114,14 +116,25 @@ class NgxScriptsCli {
114
116
}
115
117
}
116
118
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
+ ) {
118
125
if ( vars . length === 0 ) {
119
126
this . _exit ( `${ chalk . red ( 'Missing arguments' ) } \n` ) ;
120
127
}
121
128
122
129
let env = JSON . stringify (
123
130
vars . reduce ( ( env , v ) => {
124
131
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
+
125
138
return env ;
126
139
} , { } ) ,
127
140
null ,
@@ -134,7 +147,9 @@ class NgxScriptsCli {
134
147
const s = v . replace ( / ' / g, "\\'" ) . replace ( / \\ " / g, '"' ) ;
135
148
return `'${ s } '` ;
136
149
} ) ;
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`;
138
153
}
139
154
140
155
try {
0 commit comments