Skip to content

Commit 0aeec31

Browse files
committed
use default from existing .env file
1 parent 6038ff6 commit 0aeec31

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"license": "MIT",
2727
"devDependencies": {
2828
"@types/common-tags": "^1.8.0",
29+
"@types/dotenv": "^8.2.0",
2930
"@types/google-libphonenumber": "^7.4.19",
3031
"@types/jest": "^24.0.23",
3132
"@types/mock-fs": "^4.10.0",
@@ -48,6 +49,7 @@
4849
},
4950
"dependencies": {
5051
"@types/node": "^14.0.13",
52+
"dotenv": "^14.1.0",
5153
"email-regex": "^4.0.0",
5254
"google-libphonenumber": "^3.2.10",
5355
"log-symbols": "^3.0.0",

src/cli.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ export async function getExampleContent(fileName: string): Promise<string> {
5656
return readFile(fullPath, 'utf8');
5757
}
5858

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+
5967
export function getOutputStream(fileName: string): fs.WriteStream {
6068
const fullPath = resolve(process.cwd(), fileName);
6169
return fs.createWriteStream(fullPath);
@@ -68,11 +76,13 @@ export async function cli(
6876
) {
6977
const options = parseArgs(args);
7078
const exampleFileContent = await getExampleContent(options.input);
79+
const outputFileContent = await getOutputContent(options.output);
7180
const output = !ttyOutStream.isTTY
7281
? ((ttyOutStream as unknown) as fs.WriteStream)
7382
: getOutputStream(options.output);
7483

7584
const config: Config = {
85+
outputFileContent,
7686
exampleFileContent,
7787
output,
7888
promptStream,

src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ export type Config = {
1111
output: fs.WriteStream;
1212
promptStream: tty.WriteStream;
1313
exampleFileContent: string;
14+
outputFileContent?: string;
1415
};
1516

1617
export async function configureEnv(config: Config) {
1718
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+
}
1931
config.promptStream.write(
2032
`Configuring your environment. Please fill out the following info\n`
2133
);

src/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs';
22
import os from 'os';
3+
import dotenv from 'dotenv';
34

45
export const VALID_BASE_FORMATS = [
56
'text',
@@ -334,3 +335,7 @@ export async function parseFile(
334335
const fileContent = await fs.promises.readFile(filePath, 'utf8');
335336
return parse(fileContent);
336337
}
338+
339+
export async function parseAsObject(fileContent: string): Promise<{ [_: string]: any }> {
340+
return dotenv.parse(Buffer.from(fileContent))
341+
}

0 commit comments

Comments
 (0)