-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenvUpdater.js
190 lines (150 loc) · 4.94 KB
/
dotenvUpdater.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict'
const dotenv = require('dotenv')
const path = require('path')
const fs = require('fs-extra')
const inquirer = require('inquirer')
const DEFAULT_SOURCE_FILENAME = '.env'
const DEFAULT_DIST_FILENAME = '.env.example'
const defaultConfig = {
envFile: DEFAULT_SOURCE_FILENAME,
distFile: DEFAULT_DIST_FILENAME,
checkExtraVars: true,
skipPrompts: false,
};
// Try to load package.json file of project this is being run from to look for configuration settings.
const pkgJson = findPackageJson();
let fileJson;
if (!pkgJson) {
throw new Error("Could not find a package.json file. Run 'npm init' to create one.");
}
try {
fileJson = JSON.parse(fs.readFileSync(pkgJson, "utf8"));
} catch (e) {
const error = new Error(e);
error.messageTemplate = "failed-to-read-json";
error.messageData = {
path: pkgJson,
message: e.message
};
throw error;
}
// Get command line arguments that may override config.
const configArgs = {};
const [envFileArg, skipPrompts] = process.argv.slice(2)
if (envFileArg) {
configArgs.envFile = envFileArg
}
if (skipPrompts && skipPrompts !== 'false') {
configArgs.skipPrompts = true
}
// If an environment variable CI exists and evaluates to true, skip prompts.
if (!skipPrompts && (!!process.env.CI || !!process.env.ci)) {
configArgs.skipPrompts = true
}
const config = Object.assign({}, defaultConfig, fileJson.dotenvUpdater || {}, configArgs);
const sourceFile = path.resolve(process.cwd(), config.envFile)
const distFile = path.resolve(process.cwd(), config.distFile)
/**
* Find the closest package.json file, starting at process.cwd (by default),
* and working up to root.
*
* @param {string} [startDir=process.cwd()] Starting directory
* @returns {string|null} Absolute path to closest package.json file
*/
function findPackageJson (startDir) {
let dir = path.resolve(startDir || process.cwd())
do {
const pkgFile = path.join(dir, 'package.json')
if (!fs.existsSync(pkgFile) || !fs.statSync(pkgFile).isFile()) {
dir = path.join(dir, '..')
continue
}
return pkgFile
} while (dir !== path.resolve(dir, '..'))
return null
}
async function promptForKeyValues (missingKeys, defaultValues = {}) {
const questions = missingKeys.map(key => ({
type: 'input',
name: key,
message: key,
default: defaultValues[key]
}))
return inquirer.prompt(questions)
}
function parseFile (filePath) {
const result = dotenv.config({ path: filePath })
if (result.error) {
throw result.error
}
return result.parsed
}
function loadEnvVariables () {
let env, dist;
try {
env = parseFile(sourceFile);
} catch (e) {
// If no env file found, we'll create one.
env = {};
}
try {
dist = parseFile(distFile);
} catch (e) {
throw new Error(`Failed to load or parse dist file ${distFile}.`);
}
return { env, dist }
}
async function execute () {
// Verify the dist file exists.
try {
await fs.access(distFile);
} catch (e) {
console.error(
`ERROR: Dist file does not exist or could not be read. Error message: ${e.message}
If your dist file is named something different, you can configure the dist file name in your package.json. See the README for more details.`
);
process.exit(1);
}
try {
const { env, dist } = loadEnvVariables();
const envKeys = Object.keys(env)
const distKeys = Object.keys(dist)
const missingKeys = distKeys.filter(x => !envKeys.includes(x))
const extraKeys = envKeys.filter(x => !distKeys.includes(x))
if (missingKeys.length > 0) {
console.log('.env file is missing the following variables:', missingKeys)
let answers = missingKeys.reduce((result, key) => {
result[key] = dist[key] || null
return result
}, {});
if (!config.skipPrompts) {
console.log('Let\'s add these variables now! Enter values or hit enter to accept the defaults.')
answers = await promptForKeyValues(missingKeys, dist)
}
let envString = ''
for (const [key, value] of Object.entries(answers)) {
envString += `${key}=${value}\n`
}
console.log(`${config.envFile} will be appended with the new variables below:`)
console.log(envString)
await fs.appendFile(sourceFile, envString)
console.log(`${config.envFile} updated successfully!`);
} else {
console.log(`${config.envFile} file is up to date!`)
}
if (config.checkExtraVars && extraKeys.length > 0) {
console.log(
`[NOTE] Your ${config.envFile} file has extra variables that are not in ${config.distFile}.`,
'\n If they are no longer used, might want to remove them.',
`\n If they are used, consider adding them to the ${config.distFile} file.`,
'\n Extra keys:',
extraKeys,
'\n(you can disable this warning in config by setting `checkExtraVars` to `false`)'
);
}
} catch (e) {
console.error(e);
process.exit(1);
}
}
execute()