-
Notifications
You must be signed in to change notification settings - Fork 51
/
cli.js
executable file
·123 lines (106 loc) · 3.74 KB
/
cli.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
#!/usr/bin/env node
const spawn = require('cross-spawn')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2))
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand').expand
function printHelp () {
console.log([
'Usage: dotenv [--help] [--debug] [-e <path>] [-v <name>=<value>] [-p <variable name>] [-c [environment]] [--no-expand] [-- command]',
' --help print help',
' --debug output the files that would be processed but don\'t actually parse them or run the `command`',
' -e <path> parses the file <path> as a `.env` file and adds the variables to the environment',
' -e <path> multiple -e flags are allowed',
' -v <name>=<value> put variable <name> into environment using value <value>',
' -v <name>=<value> multiple -v flags are allowed',
' -p <variable> print value of <variable> to the console. If you specify this, you do not have to specify a `command`',
' -c [environment] support cascading env variables from `.env`, `.env.<environment>`, `.env.local`, `.env.<environment>.local` files',
' --no-expand skip variable expansion',
' -o, --override override system variables. Cannot be used along with cascade (-c).',
' command `command` is the actual command you want to run. Best practice is to precede this command with ` -- `. Everything after `--` is considered to be your command. So any flags will not be parsed by this tool but be passed to your command. If you do not do it, this tool will strip those flags'
].join('\n'))
}
if (argv.help) {
printHelp()
process.exit()
}
const override = argv.o || argv.override
if (argv.c && override) {
console.error('Invalid arguments. Cascading env variables conflicts with overrides.')
process.exit(1)
}
let paths = []
if (argv.e) {
if (typeof argv.e === 'string') {
paths.push(argv.e)
} else {
paths.push(...argv.e)
}
} else {
paths.push('.env')
}
if (argv.c) {
paths = paths.reduce((accumulator, path) => accumulator.concat(
typeof argv.c === 'string'
? [`${path}.${argv.c}.local`, `${path}.local`, `${path}.${argv.c}`, path]
: [`${path}.local`, path]
), [])
}
function validateCmdVariable (param) {
const [, key, val] = param.match(/^(\w+)=([\s\S]+)$/m) || []
if (!key || !val) {
console.error(`Invalid variable name. Expected variable in format '-v variable=value', but got: \`-v ${param}\`.`)
process.exit(1)
}
return [key, val]
}
const variables = []
if (argv.v) {
if (typeof argv.v === 'string') {
variables.push(validateCmdVariable(argv.v))
} else {
variables.push(...argv.v.map(validateCmdVariable))
}
}
const parsedVariables = Object.fromEntries(variables)
if (argv.debug) {
console.log(paths)
console.log(parsedVariables)
process.exit()
}
paths.forEach(function (env) {
dotenv.config({ path: path.resolve(env), override })
})
// Expand when all path configs are loaded
if (argv.expand !== false) {
dotenvExpand({
parsed: process.env
});
}
Object.assign(process.env, parsedVariables)
if (argv.p) {
let value = process.env[argv.p]
if (typeof value === 'string') {
value = `${value}`
}
console.log(value != null ? value : '')
process.exit()
}
const command = argv._[0]
if (!command) {
printHelp()
process.exit(1)
}
const child = spawn(command, argv._.slice(1), { stdio: 'inherit' })
.on('exit', function (exitCode, signal) {
if (typeof exitCode === 'number') {
process.exit(exitCode)
} else {
process.kill(process.pid, signal)
}
})
for (const signal of ['SIGINT', 'SIGTERM', 'SIGPIPE', 'SIGHUP', 'SIGBREAK', 'SIGWINCH', 'SIGUSR1', 'SIGUSR2']) {
process.on(signal, function () {
child.kill(signal)
})
}