-
-
Notifications
You must be signed in to change notification settings - Fork 954
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·171 lines (142 loc) · 3.54 KB
/
Copy pathcli.js
File metadata and controls
executable file
·171 lines (142 loc) · 3.54 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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const cp = require('child_process')
const dotenv = require('./lib/main')
function printHelp () {
console.log([
'Usage: dotenv run [--help] [--quiet] [-f <path>] -- <command>',
'',
'Run a command with environment variables from a .env file.',
'',
'Options:',
' -f <path> path to your .env file (default: .env)',
' --quiet suppress the injected env message'
].join('\n'))
}
function parseRunArgs (args) {
const paths = []
let defaultPath = true
let quiet = false
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') {
quiet = true
continue
}
if (arg === '-f') {
const filepath = args[i + 1]
if (!filepath || filepath === '--') {
return { error: '-f requires a path' }
}
paths.push(filepath)
defaultPath = false
i++
continue
}
if (arg.startsWith('-f=')) {
const filepath = arg.slice(3)
if (!filepath) {
return { error: '-f requires a path' }
}
paths.push(filepath)
defaultPath = false
continue
}
return { error: `unknown option: ${arg}` }
}
const command = commandIndex === -1 ? [] : args.slice(commandIndex)
return {
paths: paths.length > 0 ? paths : ['.env'],
defaultPath,
quiet,
command
}
}
function loadEnvFiles (paths, defaultPath) {
const parsedAll = {}
const loadedPaths = []
for (const filepath of paths) {
const resolvedPath = path.resolve(process.cwd(), filepath)
try {
const parsed = dotenv.parse(fs.readFileSync(resolvedPath, { encoding: 'utf8' }))
dotenv.populate(parsedAll, parsed)
loadedPaths.push(filepath)
} catch (e) {
if (!(defaultPath && e.code === 'ENOENT')) {
throw e
}
}
}
const injected = dotenv.populate(process.env, parsedAll)
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
}
try {
const result = loadEnvFiles(parsed.paths, parsed.defaultPath)
if (!parsed.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 child = cp.spawn(parsed.command[0], parsed.command.slice(1), {
stdio: 'inherit',
shell: process.platform === 'win32'
})
child.on('error', function (e) {
console.error(`dotenv: ${e.message}`)
process.exitCode = 1
})
child.on('exit', function (exitCode, signal) {
if (typeof exitCode === 'number') {
process.exit(exitCode)
} else {
process.kill(process.pid, signal)
}
})
}
module.exports = run
if (require.main === module) {
run(process.argv.slice(2))
}