This repository has been archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·152 lines (128 loc) · 4.06 KB
/
index.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
#!/usr/bin/env node
'use strict';
const merge = require('deepmerge');
function resolveConfigCommand(command, args, config, options) {
if (options.command.enabled && typeof command === 'string') {
const commandAndArgs = [command, ...args];
command = config[options.command.property];
let argIndex = 0;
for (; argIndex < commandAndArgs.length && typeof command === 'object'; argIndex++) {
command = command[commandAndArgs[argIndex]];
}
args = commandAndArgs.slice(argIndex);
if (command === undefined) {
command = commandAndArgs[0];
args = commandAndArgs.slice(1);
}
}
return {command, args};
}
function normalizeCommand(command, args, config) {
function throwCommand() {
throw new Error('"command" parameter has invalid type');
}
if (typeof args === 'function') {
args = args(config);
}
if (typeof command === 'function') {
command = command(config);
if (command === undefined) {
command = () => undefined;
return;
}
}
if (Array.isArray(command)) {
if (command.length === 0) {
throwCommand();
} else {
args = [...command.slice(1), ...args];
command = command[0];
}
} else if (typeof command === 'object') {
if (command.args) {
args = [...command.args, ...args];
}
command = command.command;
} else if (typeof command !== 'string') {
throwCommand();
}
return {args, command};
}
function resolvePlugin(command, options) {
let args = [];
if (command.plugin) {
if (command.args) {
args = command.args;
}
command = command.plugin;
}
if (command.options) {
options = merge(command.options, options);
command = command.command;
}
return {args, command, options};
}
function runCommand(command, args, config, options) {
if (typeof command === 'function') {
return command(args, config, (c, runnerOptions) => resolveAndRunCommand(c, [], config, merge(options, runnerOptions || {})));
}
const child = require('child_process'),
defaultSpawnOptions = {
shell: true
};
return (options.async ? child.spawn : child.spawnSync)(command, args, merge.all([defaultSpawnOptions, {
env: Object.assign({}, process.env, require('flat').flatten(config, {delimiter: '_'})),
}, (options.spawnOptions || {})]));
}
function resolveCommand(command, args, config, options) {
({args, command} = normalizeCommand(command, args, config));
return resolveConfigCommand(command, args, config, options);
}
function resolveAndRunCommand(command, args, config, options) {
({args, command} = resolveCommand(command, args, config, options));
return runCommand(command, args, config, options);
}
module.exports = (command, configName, options) => {
options = merge({
command: {
property: module.exports.commandProperty,
enabled: true
}
}, options);
let args;
({args, command, options} = resolvePlugin(command, options));
return resolveAndRunCommand(command, args, require('flavors')(configName, options), options);
};
module.exports.optionsFile = 'flavorsOptions.js';
module.exports.localOptionsFile = 'flavorsOptions.local.js';
module.exports.commandProperty = 'command';
if (require.main !== module) {
return;
}
function resolveOptionsPath(customPath, optionsFile) {
return customPath
? (customPath.endsWith('.js')
? customPath
: path.resolve(customPath, optionsFile))
: path.resolve(process.cwd(), optionsFile);
}
const path = require('path'),
defaultOptions = {
spawnOptions: {
stdio: 'inherit'
}
},
localOptionsPath = resolveOptionsPath(process.env.FLAVORS_LOCAL_OPTIONS_PATH, module.exports.localOptionsFile),
optionsPath = resolveOptionsPath(process.env.FLAVORS_OPTIONS_PATH, module.exports.optionsFile),
localOptions = (() => {
try {
return require(localOptionsPath);
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
return {};
})(),
options = require(optionsPath);
module.exports(process.argv.slice(2), process.env.FLAVORS_CONFIG_NAME, merge.all([defaultOptions, options, localOptions]));