forked from entropitor/dotenv-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·122 lines (107 loc) · 3.6 KB
/
Copy pathcli.js
File metadata and controls
executable file
·122 lines (107 loc) · 3.6 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
#!/usr/bin/env node
import spawn from 'cross-spawn';
import path from 'path';
import minimist from 'minimist';
import dotenv from 'dotenv';
import { expand } from '@maximumquiet/dotenv-expand';
import createDebug from 'debug';
import { ContextFactory } from 'icontext';
const debug = createDebug('ldapenv');
var argv = minimist(process.argv.slice(2));
function printHelp() {
console.log(
[
'Usage: ldapenv [--help] [--debug] [-e <path>] [-v <name>=<value>] [-p <variable name>] [-c [environment]] [-- 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',
' --webxml <path> path to web.xml file (default conf/web.xml)',
' --context <path> path to context.xml file (default $HOME/.config/context.xml)',
' -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.local`, `.env.<environment>`, `.env.<environment>.local` files',
' 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();
}
var 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}.${argv.c}`, `${path}.local`, path]
: [`${path}.local`, path]
),
[]
);
}
function validateCmdVariable(param) {
if (!param.match(/^\w+=\w+$/)) {
console.error('Unexpected argument ' + param + '. Expected variable in format variable=value');
process.exit(1);
}
return param;
}
var variables = [];
if (argv.v) {
if (typeof argv.v === 'string') {
variables.push(validateCmdVariable(argv.v));
} else {
variables.push(...argv.v.map(validateCmdVariable));
}
}
var parsedVariables = dotenv.parse(Buffer.from(variables.join('\n')));
if (argv.debug) {
console.log(paths);
console.log(parsedVariables);
process.exit();
}
paths.forEach(function (env) {
expand(dotenv.config({ path: path.resolve(env) }));
});
Object.assign(process.env, parsedVariables);
var command = argv._[0];
if (!command) {
printHelp();
process.exit(1);
}
const args = [];
if (argv.p) {
args.push('-p', argv.p);
}
if (argv.H) {
args.push('-H', argv.H);
}
const contextConfig = {
webXmlPath: argv.webxml,
contextXmlPath: argv.context
};
debug('contextConfig = %o', contextConfig);
const contextFactory = new ContextFactory(contextConfig);
await contextFactory.build().then((parsed) => expand({ parsed }));
spawn(command, [...argv._.slice(1), ...args], { stdio: 'inherit' }).on(
'exit',
function (exitCode, signal) {
if (typeof exitCode === 'number') {
process.exit(exitCode);
} else {
process.kill(process.pid, signal);
}
}
);