-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathindex.js
287 lines (261 loc) · 9.78 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @module index
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const shellQuote = require("shell-quote")
const matchTasks = require("./match-tasks")
const readPackageJson = require("./read-package-json")
const runTasks = require("./run-tasks")
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const ARGS_PATTERN = /\{(!)?([*@]|\d+)([^}]+)?}/g
/**
* Converts a given value to an array.
*
* @param {string|string[]|null|undefined} x - A value to convert.
* @returns {string[]} An array.
*/
function toArray(x) {
if (x == null) {
return []
}
return Array.isArray(x) ? x : [x]
}
/**
* Replaces argument placeholders (such as `{1}`) by arguments.
*
* @param {string[]} patterns - Patterns to replace.
* @param {string[]} args - Arguments to replace.
* @returns {string[]} replaced
*/
function applyArguments(patterns, args) {
const defaults = Object.create(null)
return patterns.map(pattern => pattern.replace(ARGS_PATTERN, (whole, indirectionMark, id, options) => {
if (indirectionMark != null) {
throw Error(`Invalid Placeholder: ${whole}`)
}
if (id === "@") {
return shellQuote.quote(args)
}
if (id === "*") {
return shellQuote.quote([args.join(" ")])
}
const position = parseInt(id, 10)
if (position >= 1 && position <= args.length) {
return shellQuote.quote([args[position - 1]])
}
// Address default values
if (options != null) {
const prefix = options.slice(0, 2)
if (prefix === ":=") {
defaults[id] = shellQuote.quote([options.slice(2)])
return defaults[id]
}
if (prefix === ":-") {
return shellQuote.quote([options.slice(2)])
}
throw Error(`Invalid Placeholder: ${whole}`)
}
if (defaults[id] != null) {
return defaults[id]
}
return ""
}))
}
/**
* Parse patterns.
* In parsing process, it replaces argument placeholders (such as `{1}`) by arguments.
*
* @param {string|string[]} patternOrPatterns - Patterns to run.
* A pattern is a npm-script name or a Glob-like pattern.
* @param {string[]} args - Arguments to replace placeholders.
* @returns {string[]} Parsed patterns.
*/
function parsePatterns(patternOrPatterns, args) {
const patterns = toArray(patternOrPatterns)
const hasPlaceholder = patterns.some(pattern => ARGS_PATTERN.test(pattern))
return hasPlaceholder ? applyArguments(patterns, args) : patterns
}
/**
* Converts a given config object to an `--:=` style option array.
*
* @param {object|null} config -
* A map-like object to overwrite package configs.
* Keys are package names.
* Every value is a map-like object (Pairs of variable name and value).
* @returns {string[]} `--:=` style options.
*/
function toOverwriteOptions(config) {
const options = []
for (const packageName of Object.keys(config)) {
const packageConfig = config[packageName]
for (const variableName of Object.keys(packageConfig)) {
const value = packageConfig[variableName]
options.push(`--${packageName}:${variableName}=${value}`)
}
}
return options
}
/**
* Converts a given config object to an `--a=b` style option array.
*
* @param {object|null} config -
* A map-like object to set configs.
* @returns {string[]} `--a=b` style options.
*/
function toConfigOptions(config) {
return Object.keys(config).map(key => `--${key}=${config[key]}`)
}
/**
* Gets the maximum length.
*
* @param {number} length - The current maximum length.
* @param {string} name - A name.
* @returns {number} The maximum length.
*/
function maxLength(length, name) {
return Math.max(name.length, length)
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Runs npm-scripts which are matched with given patterns.
*
* @param {string|string[]} patternOrPatterns - Patterns to run.
* A pattern is a npm-script name or a Glob-like pattern.
* @param {object|undefined} [options] Optional.
* @param {boolean} options.parallel -
* If this is `true`, run scripts in parallel.
* Otherwise, run scripts in sequencial.
* Default is `false`.
* @param {stream.Readable|null} options.stdin -
* A readable stream to send messages to stdin of child process.
* If this is `null`, ignores it.
* If this is `process.stdin`, inherits it.
* Otherwise, makes a pipe.
* Default is `null`.
* @param {stream.Writable|null} options.stdout -
* A writable stream to receive messages from stdout of child process.
* If this is `null`, cannot send.
* If this is `process.stdout`, inherits it.
* Otherwise, makes a pipe.
* Default is `null`.
* @param {stream.Writable|null} options.stderr -
* A writable stream to receive messages from stderr of child process.
* If this is `null`, cannot send.
* If this is `process.stderr`, inherits it.
* Otherwise, makes a pipe.
* Default is `null`.
* @param {string[]} options.taskList -
* Actual name list of npm-scripts.
* This function search npm-script names in this list.
* If this is `null`, this function reads `package.json` of current directly.
* @param {object|null} options.packageConfig -
* A map-like object to overwrite package configs.
* Keys are package names.
* Every value is a map-like object (Pairs of variable name and value).
* e.g. `{"npm-run-all": {"test": 777}}`
* Default is `null`.
* @param {boolean} options.silent -
* The flag to set `silent` to the log level of npm.
* Default is `false`.
* @param {boolean} options.continueOnError -
* The flag to ignore errors.
* Default is `false`.
* @param {boolean} options.printLabel -
* The flag to print task names at the head of each line.
* Default is `false`.
* @param {boolean} options.printName -
* The flag to print task names before running each task.
* Default is `false`.
* @param {number} options.maxParallel -
* The maximum number of parallelism.
* Default is unlimited.
* @param {string} options.npmPath -
* The path to npm.
* Default is `process.env.npm_execpath`.
* @returns {Promise}
* A promise object which becomes fullfilled when all npm-scripts are completed.
*/
module.exports = function npmRunAll(patternOrPatterns, options) { //eslint-disable-line complexity
const stdin = (options && options.stdin) || null
const stdout = (options && options.stdout) || null
const stderr = (options && options.stderr) || null
const taskList = (options && options.taskList) || null
const config = (options && options.config) || null
const packageConfig = (options && options.packageConfig) || null
const args = (options && options.arguments) || []
const parallel = Boolean(options && options.parallel)
const silent = Boolean(options && options.silent)
const continueOnError = Boolean(options && options.continueOnError)
const printLabel = Boolean(options && options.printLabel)
const printName = Boolean(options && options.printName)
const race = Boolean(options && options.race)
const maxParallel = parallel ? ((options && options.maxParallel) || 0) : 1
const aggregateOutput = Boolean(options && options.aggregateOutput)
const npmPath = options && options.npmPath
try {
const patterns = parsePatterns(patternOrPatterns, args)
if (patterns.length === 0) {
return Promise.resolve(null)
}
if (taskList != null && Array.isArray(taskList) === false) {
throw new Error("Invalid options.taskList")
}
if (typeof maxParallel !== "number" || !(maxParallel >= 0)) {
throw new Error("Invalid options.maxParallel")
}
if (!parallel && aggregateOutput) {
throw new Error("Invalid options.aggregateOutput; It requires options.parallel")
}
if (!parallel && race) {
throw new Error("Invalid options.race; It requires options.parallel")
}
const prefixOptions = [].concat(
silent ? ["--silent"] : [],
packageConfig ? toOverwriteOptions(packageConfig) : [],
config ? toConfigOptions(config) : []
)
return Promise.resolve()
.then(() => {
if (taskList != null) {
return { taskList, packageInfo: null }
}
return readPackageJson()
})
.then(x => {
const tasks = matchTasks(x.taskList, patterns)
const labelWidth = tasks.reduce(maxLength, 0)
return runTasks(tasks, {
stdin,
stdout,
stderr,
prefixOptions,
continueOnError,
labelState: {
enabled: printLabel,
width: labelWidth,
lastPrefix: null,
lastIsLinebreak: true,
},
printName,
packageInfo: x.packageInfo,
race,
maxParallel,
npmPath,
aggregateOutput,
})
})
}
catch (err) {
return Promise.reject(new Error(err.message))
}
}