File tree 3 files changed +49
-24
lines changed
3 files changed +49
-24
lines changed Original file line number Diff line number Diff line change 9
9
{
10
10
"name" : " zx/index" ,
11
11
"path" : " build/*.{js,cjs}" ,
12
- "limit" : " 805 kB" ,
12
+ "limit" : " 805.5 kB" ,
13
13
"brotli" : false ,
14
14
"gzip" : false
15
15
},
30
30
{
31
31
"name" : " all" ,
32
32
"path" : " build/*" ,
33
- "limit" : " 842 kB" ,
33
+ "limit" : " 842.5 kB" ,
34
34
"brotli" : false ,
35
35
"gzip" : false
36
36
}
Original file line number Diff line number Diff line change @@ -358,15 +358,40 @@ export const toCamelCase = (str: string) =>
358
358
export const parseBool = ( v : string ) : boolean | string =>
359
359
( { true : true , false : false } ) [ v ] ?? v
360
360
361
- export const parseDotenv = ( content : string ) : NodeJS . ProcessEnv =>
362
- content . split ( / \r ? \n / ) . reduce < NodeJS . ProcessEnv > ( ( r , line ) => {
363
- if ( line . startsWith ( 'export ' ) ) line = line . slice ( 7 )
364
- const i = line . indexOf ( '=' )
365
- const k = line . slice ( 0 , i ) . trim ( )
366
- const v = line . slice ( i + 1 ) . trim ( )
367
- if ( k && v ) r [ k ] = v
368
- return r
369
- } , { } )
361
+ // prettier-ignore
362
+ export const parseDotenv = ( content : string ) : NodeJS . ProcessEnv => {
363
+ const e : Record < string , string > = { }
364
+ let k = ''
365
+ let c = ''
366
+ let q = ''
367
+ const cap = ( ) => { if ( c && k ) { e [ k ] = c ; c = '' ; k = '' } }
368
+
369
+ for ( const s of content ) {
370
+ if ( s === ' ' && ! q ) {
371
+ if ( ! k && c === 'export' ) c = ''
372
+ continue
373
+ }
374
+ if ( s === '=' && ! q ) {
375
+ if ( ! k ) { k = c ; c = '' ; continue }
376
+ }
377
+ if ( s === '\n' && ! q ) {
378
+ cap ( )
379
+ continue
380
+ }
381
+ if ( s === '"' || s === "'" ) {
382
+ if ( q === s ) {
383
+ q = ''
384
+ cap ( )
385
+ continue
386
+ }
387
+ q = s
388
+ continue
389
+ }
390
+ c += s
391
+ }
392
+
393
+ return e
394
+ }
370
395
371
396
export const readEnvFromFile = (
372
397
filepath : string ,
Original file line number Diff line number Diff line change @@ -143,24 +143,24 @@ describe('util', () => {
143
143
} )
144
144
145
145
test ( 'parseDotenv()' , ( ) => {
146
- assert . deepEqual (
147
- parseDotenv ( 'ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \nexport ENV4=v4' ) ,
148
- {
149
- ENV : 'v1' ,
150
- ENV2 : 'v2' ,
151
- ENV3 : 'v3' ,
152
- ENV4 : 'v4' ,
153
- }
154
- )
155
- assert . deepEqual ( parseDotenv ( '' ) , { } )
156
-
157
- // TBD: multiline
158
146
const multiline = `SIMPLE=xyz123
159
147
NON_INTERPOLATED='raw text without variable interpolation'
160
148
MULTILINE = """
161
149
long text here,
162
150
e.g. a private SSH key
163
- """`
151
+ """
152
+ ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \n export ENV4=v4
153
+ `
154
+
155
+ assert . deepEqual ( parseDotenv ( multiline ) , {
156
+ SIMPLE : 'xyz123' ,
157
+ NON_INTERPOLATED : 'raw text without variable interpolation' ,
158
+ MULTILINE : '\nlong text here,\ne.g. a private SSH key\n' ,
159
+ ENV : 'v1' ,
160
+ ENV2 : 'v2' ,
161
+ ENV3 : 'v3' ,
162
+ ENV4 : 'v4' ,
163
+ } )
164
164
} )
165
165
166
166
describe ( 'readEnvFromFile()' , ( ) => {
You can’t perform that action at this time.
0 commit comments