File tree Expand file tree Collapse file tree 5 files changed +44
-1
lines changed Expand file tree Collapse file tree 5 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 26
26
"license" : " MIT" ,
27
27
"devDependencies" : {
28
28
"@types/common-tags" : " ^1.8.0" ,
29
+ "@types/dotenv" : " ^8.2.0" ,
29
30
"@types/google-libphonenumber" : " ^7.4.19" ,
30
31
"@types/jest" : " ^24.0.23" ,
31
32
"@types/mock-fs" : " ^4.10.0" ,
48
49
},
49
50
"dependencies" : {
50
51
"@types/node" : " ^14.0.13" ,
52
+ "dotenv" : " ^14.1.0" ,
51
53
"email-regex" : " ^4.0.0" ,
52
54
"google-libphonenumber" : " ^3.2.10" ,
53
55
"log-symbols" : " ^3.0.0" ,
Original file line number Diff line number Diff line change @@ -56,6 +56,14 @@ export async function getExampleContent(fileName: string): Promise<string> {
56
56
return readFile ( fullPath , 'utf8' ) ;
57
57
}
58
58
59
+
60
+ export async function getOutputContent ( fileName : string ) : Promise < string | undefined > {
61
+ const fullPath = resolve ( process . cwd ( ) , fileName ) ;
62
+ if ( fs . existsSync ( fullPath ) ) {
63
+ return readFile ( fullPath , 'utf8' ) ;
64
+ }
65
+ }
66
+
59
67
export function getOutputStream ( fileName : string ) : fs . WriteStream {
60
68
const fullPath = resolve ( process . cwd ( ) , fileName ) ;
61
69
return fs . createWriteStream ( fullPath ) ;
@@ -68,11 +76,13 @@ export async function cli(
68
76
) {
69
77
const options = parseArgs ( args ) ;
70
78
const exampleFileContent = await getExampleContent ( options . input ) ;
79
+ const outputFileContent = await getOutputContent ( options . output ) ;
71
80
const output = ! ttyOutStream . isTTY
72
81
? ( ( ttyOutStream as unknown ) as fs . WriteStream )
73
82
: getOutputStream ( options . output ) ;
74
83
75
84
const config : Config = {
85
+ outputFileContent,
76
86
exampleFileContent,
77
87
output,
78
88
promptStream,
Original file line number Diff line number Diff line change @@ -11,11 +11,23 @@ export type Config = {
11
11
output : fs . WriteStream ;
12
12
promptStream : tty . WriteStream ;
13
13
exampleFileContent : string ;
14
+ outputFileContent ?: string ;
14
15
} ;
15
16
16
17
export async function configureEnv ( config : Config ) {
17
18
const parsedExample = parserLib . parse ( config . exampleFileContent ) ;
18
-
19
+ // Use default from existing .env values if file exists
20
+ if ( config . outputFileContent ) {
21
+ config . promptStream . write (
22
+ `${ info } ${ config . output . path } already present using existing values as defaults\n`
23
+ ) ;
24
+ const parsedEnv = await parserLib . parseAsObject ( config . outputFileContent ) ;
25
+ parsedExample . variables . forEach ( variable => {
26
+ if ( variable . configurable && parsedEnv [ variable . key ] ) {
27
+ variable . default = parsedEnv [ variable . key ] ;
28
+ }
29
+ } )
30
+ }
19
31
config . promptStream . write (
20
32
`Configuring your environment. Please fill out the following info\n`
21
33
) ;
Original file line number Diff line number Diff line change 1
1
import fs from 'fs' ;
2
2
import os from 'os' ;
3
+ import dotenv from 'dotenv' ;
3
4
4
5
export const VALID_BASE_FORMATS = [
5
6
'text' ,
@@ -334,3 +335,7 @@ export async function parseFile(
334
335
const fileContent = await fs . promises . readFile ( filePath , 'utf8' ) ;
335
336
return parse ( fileContent ) ;
336
337
}
338
+
339
+ export async function parseAsObject ( fileContent : string ) : Promise < { [ _ : string ] : any } > {
340
+ return dotenv . parse ( Buffer . from ( fileContent ) )
341
+ }
You can’t perform that action at this time.
0 commit comments