-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·170 lines (159 loc) · 4.47 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
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
#!/usr/bin/env node
'use strict';
function parseArgs(argv) {
let loaders;
if (argv.loader) {
if (Array.isArray(argv.loader) && argv.loader.length > 0) {
loaders = argv.loader;
} else {
loaders = [argv.loader];
}
loaders = loaders.map(require);
}
let command, plugin;
if (argv.plugin) {
plugin = require(argv.command);
} else {
command = argv.command;
}
const {optionsPath, shell, workingDir} = argv;
return {
command: {
command,
plugin,
args: [...(argv.args || []), ...(argv['--'] || [])]
},
configName: argv.name,
options: {
configDirName: argv.dirName,
configFileName: argv.fileName,
configNameSeparator: argv.separator,
loaders,
spawnOptions: {
shell
},
workingDir
},
optionsPath
};
}
function resolveOptionsPath(customPath, optionsFile) {
const path = require('path');
return customPath
? (customPath.endsWith('.js')
? path.resolve(customPath)
: path.resolve(customPath, optionsFile))
: path.resolve(process.cwd(), optionsFile);
}
function tryOptions(customPath, optionsFile) {
if (customPath || optionsFile) {
try {
return require(resolveOptionsPath(customPath, optionsFile));
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND' || customPath) {
throw e;
}
}
}
return {};
}
function loadOptions(argv) {
const {options: argvOptions, optionsPath, ...rest} = parseArgs(argv);
return {
options: require('deepmerge').all([
{
spawnOptions: {
stdio: 'inherit'
}
},
tryOptions(optionsPath, module.exports.optionsFile),
argvOptions,
tryOptions(process.env.FLAVORS_OPTIONS_PATH, module.exports.optionsFile),
tryOptions(process.env.FLAVORS_LOCAL_OPTIONS_PATH, module.exports.localOptionsFile)
]),
...rest
};
}
function print(argv) {
const {configName, options} = loadOptions(argv);
process.stdout.write(JSON.stringify(require('.')(configName, options), null, 2) + '\n');
}
function run(argv) {
const {configName, command, options} = loadOptions(argv);
return require('./runner')(command, configName || process.env.FLAVORS_CONFIG_NAME, options);
}
module.exports = () => {
const yargs = require('yargs');
// noinspection BadExpressionStatementJS
yargs// eslint-disable-line no-unused-expressions
.strict()
.parserConfiguration({
'duplicate-arguments-array': false,
'populate--': true
})
.usage('flavors - print loaded configuration or configure environment and run command.')
.command({
command: ['print', '*'],
desc: 'Load and print configuration with specified name in JSON format',
handler: print
})
.command({
command: 'run <command> [args..]',
desc: 'Load configuration and run command',
builder: yargs => yargs.options({
'plugin': {
alias: 'p',
boolean: true,
describe: 'Treat command as Node.js module name or path.'
},
'shell': {
boolean: true,
default: require('./runner').defaultOptions.spawnOptions.shell,
describe: 'Runs command inside of a shell. Uses /bin/sh on Unix, and process.env.ComSpec on Windows.',
},
}),
handler: run
})
.options({
'dir-name': {
alias: 'd',
describe: 'Configuration directory name'
},
'file-name': {
alias: 'f',
describe: 'Configuration file name (excluding extension)'
},
'loader': {
alias: 'l',
array: true,
describe: 'Loaders. Names of a Node.js modules or their paths.'
},
'name': {
alias: 'n',
required: !process.env.FLAVORS_CONFIG_NAME,
describe: 'Configuration name. Use this option or FLAVORS_CONFIG_NAME environment variable.'
},
'options-path': {
alias: 'o',
describe: 'Path to options file'
},
'separator': {
alias: 's',
describe: 'Configuration name separator'
},
'working-dir': {
alias: 'w',
describe: 'Directory name where configuration resolving starts from'
}
})
.epilogue('for more information, please read our documentation at https://github.com/flavors-js/flavors.')
.help()
.version()
.wrap(yargs.terminalWidth())
.argv;
};
module.exports.localOptionsFile = 'flavorsOptions.local.js';
module.exports.optionsFile = 'flavorsOptions.js';
if (require.main === module) {
module.exports();
}