-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathenv-cmd.js
71 lines (71 loc) · 2.44 KB
/
env-cmd.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvCmd = exports.CLI = void 0;
const spawn_1 = require("./spawn");
const signal_termination_1 = require("./signal-termination");
const parse_args_1 = require("./parse-args");
const get_env_vars_1 = require("./get-env-vars");
const expand_envs_1 = require("./expand-envs");
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<{ [key: string]: any }>}
*/
async function CLI(args) {
// Parse the args from the command line
const parsedArgs = parse_args_1.parseArgs(args);
// Run EnvCmd
try {
return await exports.EnvCmd(parsedArgs);
}
catch (e) {
console.error(e);
return process.exit(1);
}
}
exports.CLI = CLI;
/**
* The main env-cmd program. This will spawn a new process and run the given command using
* various environment file solutions.
*
* @export
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
* @returns {Promise<{ [key: string]: any }>} Returns an object containing [environment variable name]: value
*/
async function EnvCmd({ command, commandArgs, envFile, rc, options = {} }) {
var _a;
let env = {};
try {
env = await get_env_vars_1.getEnvVars({ envFile, rc, verbose: options.verbose });
}
catch (e) {
if (!((_a = options.silent) !== null && _a !== void 0 ? _a : false)) {
throw e;
}
}
// Override the merge order if --no-override flag set
if (options.noOverride === true) {
env = Object.assign({}, env, process.env);
}
else {
// Add in the system environment variables to our environment list
env = Object.assign({}, process.env, env);
}
if (options.expandEnvs === true) {
command = expand_envs_1.expandEnvs(command, env);
commandArgs = commandArgs.map(arg => expand_envs_1.expandEnvs(arg, env));
}
// Execute the command with the given environment variables
const proc = spawn_1.spawn(command, commandArgs, {
stdio: 'inherit',
shell: options.useShell,
env
});
// Handle any termination signals for parent and child proceses
const signals = new signal_termination_1.TermSignals({ verbose: options.verbose });
signals.handleUncaughtExceptions();
signals.handleTermSignals(proc);
return env;
}
exports.EnvCmd = EnvCmd;