forked from toddbluhm/env-cmd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse-args.js
98 lines (98 loc) · 3.62 KB
/
parse-args.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseArgsUsingCommander = exports.parseArgs = void 0;
const commander = require("commander");
const utils_1 = require("./utils");
// Use commonjs require to prevent a weird folder hierarchy in dist
const packageJson = require('../package.json'); /* eslint-disable-line */
/**
* Parses the arguments passed into the cli
*/
function parseArgs(args) {
// Run the initial arguments through commander in order to determine
// which value in the args array is the `command` to execute
let program = parseArgsUsingCommander(args);
const command = program.args[0];
// Grab all arguments after the `command` in the args array
const commandArgs = args.splice(args.indexOf(command) + 1);
// Reprocess the args with the command and command arguments removed
program = parseArgsUsingCommander(args.slice(0, args.indexOf(command)));
// Set values for provided options
let noOverride = false;
// In commander `no-` negates the original value `override`
if (program.override === false) {
noOverride = true;
}
let useShell = false;
if (program.useShell === true) {
useShell = true;
}
let expandEnvs = false;
if (program.expandEnvs === true) {
expandEnvs = true;
}
let interpolate = false;
if (program.interpolate === true) {
interpolate = true;
}
let verbose = false;
if (program.verbose === true) {
verbose = true;
}
let silent = false;
if (program.silent === true) {
silent = true;
}
let rc;
if (program.environments !== undefined && program.environments.length !== 0) {
rc = {
environments: program.environments,
filePath: program.rcFile
};
}
let envFile;
if (program.file !== undefined) {
envFile = {
filePath: program.file,
fallback: program.fallback
};
}
const options = {
command,
commandArgs,
envFile,
rc,
options: {
expandEnvs,
noOverride,
silent,
useShell,
verbose,
interpolate,
}
};
if (verbose) {
console.info(`Options: ${JSON.stringify(options, null, 0)}`);
}
return options;
}
exports.parseArgs = parseArgs;
function parseArgsUsingCommander(args) {
const program = new commander.Command();
return program
.version(packageJson.version, '-v, --version')
.usage('[options] <command> [...args]')
.option('-e, --environments [env1,env2,...]', 'The rc file environment(s) to use', utils_1.parseArgList)
.option('-f, --file [path]', 'Custom env file path (default path: ./.env)')
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
.option('-n, --no-override', 'Do not override existing environment variables')
.option('-r, --rc-file [path]', 'Custom rc file path (default path: ./.env-cmdrc(|.js|.json)')
.option('-s, --silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
.option('--use-shell', 'Execute the command in a new shell with the given environment')
.option('--verbose', 'Print helpful debugging information')
.option('-x, --expand-envs', 'Replace $var in args and command with environment variables')
.option('-i, --interpolate', 'Interpolates {{var}} in args and command with environment variables')
.allowUnknownOption(true)
.parse(['_', '_', ...args]);
}
exports.parseArgsUsingCommander = parseArgsUsingCommander;