-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.js
More file actions
130 lines (108 loc) · 3.24 KB
/
runner.js
File metadata and controls
130 lines (108 loc) · 3.24 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
123
124
125
126
127
128
129
'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');
}
args = args || [];
if (typeof args === 'function') {
args = args(config);
}
if (typeof command === 'function') {
command = command(config);
if (command === undefined) {
command = null;
}
}
if (typeof command === 'string' || command === null) {
// noop
} else 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 {
throwCommand();
}
return {args, command};
}
function resolvePlugin(command, options) {
let args = [];
if (command) {
let isPlugin = false;
if (command.plugin) {
if (command.args) {
args = command.args;
}
command = command.plugin;
isPlugin = true;
}
let pluginOptions = command.options;
if (pluginOptions) {
if (typeof pluginOptions === 'function') {
pluginOptions = pluginOptions(options);
}
options = merge(options, pluginOptions);
isPlugin = true;
}
if (isPlugin) {
command = command.command;
}
}
return {args, command, options};
}
function runCommand(command, args, config, configName, options) {
if (command === null) {
return null;
} else if (typeof command === 'function') {
return command(args, config, (c, runnerOptions) => module.exports(c, configName, merge(options, runnerOptions || {})));
}
const child = require('child_process');
return (options.async ? child.spawn : child.spawnSync)(command, args, merge.all([{
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);
}
module.exports = (command, configName, options) => {
options = merge(module.exports.defaultOptions, options);
let args;
({args, command, options} = resolvePlugin(command, options));
const config = require('.')(configName, options);
({args, command} = resolveCommand(command, args, config, options));
return runCommand(command, args, config, configName, options);
};
module.exports.defaultOptions = {
command: {
property: 'command',
enabled: true
},
spawnOptions: {
shell: true
}
};