Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·298 lines (253 loc) · 7.68 KB
/
Copy pathcli.js
File metadata and controls
executable file
·298 lines (253 loc) · 7.68 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env node
const fs = require('fs')
const os = require('os')
const path = require('path')
const cp = require('child_process')
const spawnCommand = require('./lib/spawn-command')
const dotenv = require('./lib/main')
const { optionsFromEnv } = require('./lib/config-options')
function printHelp () {
console.log([
'Usage: dotenv run [--help] [-q|--quiet] [--debug] [--override] [--fast] [-f|--file <paths>] [--] <command> [args...]',
'',
'Run a command with environment variables from a .env file.',
'Place dotenv options before the command; all following arguments go to the command.',
'',
'Options:',
' -f, --file <paths> .env paths, comma-separated or repeated (default: .env)',
' -q, --quiet suppress the injected env message',
' --debug enable debug logging',
' --override override existing environment variables',
' --fast use the faster character-scanner parser',
'',
'Environment variables (DOTENV_CONFIG_* names remain as fallbacks):',
' DOTENV_PATH, DOTENV_ENCODING, DOTENV_QUIET,',
' DOTENV_DEBUG, DOTENV_OVERRIDE,',
' DOTENV_FAST'
].join('\n'))
}
function parseRunArgs (args) {
const paths = []
let pathSet = false
let quiet
let debug
let override
let fast
let commandIndex = -1
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg === '--') {
commandIndex = i + 1
break
}
if (arg === '--help' || arg === '-h') {
return { help: true }
}
if (arg === '--quiet' || arg === '-q') {
quiet = true
continue
}
if (arg === '--debug') {
debug = true
continue
}
if (arg === '--override') {
override = true
continue
}
if (arg === '--fast') {
fast = true
continue
}
if (arg === '-f' || arg === '--file' || arg.startsWith('-f=') || arg.startsWith('--file=')) {
const equalsIndex = arg.indexOf('=')
const flag = equalsIndex === -1 ? arg : arg.slice(0, equalsIndex)
const value = equalsIndex === -1 ? args[++i] : arg.slice(equalsIndex + 1)
if (!value || value === '--') {
return { error: `${flag} requires a path` }
}
const filepaths = value.split(',').map(filepath => filepath.trim()).filter(Boolean)
if (filepaths.length === 0) {
return { error: `${flag} requires a path` }
}
paths.push(...filepaths)
pathSet = true
continue
}
if (arg.startsWith('-')) {
return { error: `unknown option: ${arg}` }
}
commandIndex = i
break
}
const command = commandIndex === -1 ? [] : args.slice(commandIndex)
return {
paths,
pathSet,
quiet,
debug,
override,
fast,
command
}
}
function resolveHome (envPath) {
return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
}
function resolveRunOptions (parsed) {
const envOptions = optionsFromEnv()
const options = {
encoding: envOptions.encoding || 'utf8',
quiet: envOptions.quiet,
debug: envOptions.debug === true,
override: envOptions.override === true,
fast: envOptions.fast === true,
paths: ['.env'],
defaultPath: true
}
if (envOptions.path != null) {
options.paths = [envOptions.path]
options.defaultPath = false
}
if (parsed.pathSet) {
options.paths = parsed.paths
options.defaultPath = false
}
if (parsed.quiet != null) options.quiet = parsed.quiet
if (parsed.debug != null) options.debug = parsed.debug
if (parsed.override != null) options.override = parsed.override
if (parsed.fast != null) options.fast = parsed.fast
return options
}
function loadEnvFiles (options) {
const parsedAll = {}
const loadedPaths = []
const populateOptions = {
override: options.override,
debug: options.debug
}
for (const filepath of options.paths) {
const resolvedPath = path.resolve(process.cwd(), resolveHome(filepath))
try {
const parsed = dotenv.parse(fs.readFileSync(resolvedPath, { encoding: options.encoding }), { fast: options.fast })
dotenv.populate(parsedAll, parsed, populateOptions)
loadedPaths.push(filepath)
} catch (e) {
if (options.debug) {
console.log(`┆ failed to load ${filepath} ${e.message}`)
}
if (!(options.defaultPath && e.code === 'ENOENT')) {
throw e
}
}
}
const injected = dotenv.populate(process.env, parsedAll, populateOptions)
return { injected, loadedPaths }
}
function run (argv) {
const command = argv[0]
if (command === '--help' || command === '-h') {
printHelp()
return
}
if (command !== 'run') {
printHelp()
process.exitCode = 1
return
}
const parsed = parseRunArgs(argv.slice(1))
if (parsed.help) {
printHelp()
return
}
if (parsed.error) {
console.error(`dotenv: ${parsed.error}`)
printHelp()
process.exitCode = 1
return
}
if (parsed.command.length === 0) {
printHelp()
process.exitCode = 1
return
}
const options = resolveRunOptions(parsed)
try {
const result = loadEnvFiles(options)
const quiet = options.quiet == null ? optionsFromEnv().quiet : options.quiet
if (!quiet) {
let message = `◇ injected env (${Object.keys(result.injected).length})`
if (result.loadedPaths.length > 0) {
message += ` from ${result.loadedPaths.join(', ')}`
}
console.error(message)
}
} catch (e) {
console.error(`dotenv: ${e.message}`)
process.exitCode = 1
return
}
const interactive = Boolean(process.stdin.isTTY)
// A separate group lets services/CI stop descendants too. Interactive children
// stay in the terminal's foreground group so stdin and Ctrl-C work normally.
const useProcessGroup = process.platform !== 'win32' && !interactive
const child = spawnCommand(parsed.command[0], parsed.command.slice(1), {
stdio: 'inherit',
detached: useProcessGroup
})
const handlers = new Map()
let interrupts = 0
function forward (signal) {
if (!child.pid || child.exitCode !== null || child.signalCode !== null) return
if (process.platform === 'win32') {
// Windows has no POSIX process-group signals; include the shell's children.
cp.spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], { stdio: 'ignore' })
return
}
try {
process.kill(useProcessGroup ? -child.pid : child.pid, signal)
} catch (error) {
if (error.code !== 'ESRCH') throw error
}
}
function cleanup () {
for (const [signal, handler] of handlers) process.removeListener(signal, handler)
}
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT']) {
const handler = () => {
if (signal === 'SIGINT') {
interrupts++
// The terminal already delivers Ctrl-C to the foreground child. Further
// interrupts escalate instead of duplicating its graceful shutdown.
if (interactive && process.platform !== 'win32' && interrupts === 1) return
if (interrupts > 1) {
forward(interrupts === 2 ? 'SIGTERM' : 'SIGKILL')
return
}
}
forward(signal)
}
handlers.set(signal, handler)
process.on(signal, handler)
}
child.on('error', function (e) {
cleanup()
console.error(`dotenv: ${e.message}`)
process.exitCode = 1
})
child.on('exit', function (exitCode, signal) {
cleanup()
if (typeof exitCode === 'number') {
process.exit(exitCode)
} else {
// Keep the event loop alive until the re-raised signal terminates us.
// Otherwise Node can finish normally before delivery and report exit 0.
setInterval(() => {}, 1000)
process.kill(process.pid, signal)
}
})
}
module.exports = run
if (require.main === module) {
run(process.argv.slice(2))
}