-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.js
164 lines (136 loc) · 4.61 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
"use strict";
const os = require("os");
const ChildProcessUtilities = require("@lerna/child-process");
const Command = require("@lerna/command");
const batchPackages = require("@lerna/batch-packages");
const runParallelBatches = require("@lerna/run-parallel-batches");
const ValidationError = require("@lerna/validation-error");
const { getFilteredPackages } = require("@lerna/filter-options");
module.exports = factory;
function factory(argv) {
return new ExecCommand(argv);
}
class ExecCommand extends Command {
get requiresGit() {
return false;
}
initialize() {
const dashedArgs = this.options["--"] || [];
this.command = this.options.cmd || dashedArgs.shift();
this.args = (this.options.args || []).concat(dashedArgs);
if (!this.command) {
throw new ValidationError("ENOCOMMAND", "A command to execute is required");
}
// inverted boolean options
this.bail = this.options.bail !== false;
this.prefix = this.options.prefix !== false;
// accessing properties of process.env can be expensive,
// so cache it here to reduce churn during tighter loops
this.env = Object.assign({}, process.env);
let chain = Promise.resolve();
chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options));
chain = chain.then(filteredPackages => {
this.filteredPackages = filteredPackages;
});
return chain.then(() => {
this.count = this.filteredPackages.length;
this.packagePlural = this.count === 1 ? "package" : "packages";
this.joinedCommand = [this.command].concat(this.args).join(" ");
this.batchedPackages = this.toposort
? batchPackages(this.filteredPackages, this.options.rejectCycles)
: [this.filteredPackages];
});
}
execute() {
this.logger.info(
"",
"Executing command in %d %s: %j",
this.count,
this.packagePlural,
this.joinedCommand
);
let chain = Promise.resolve();
if (this.options.parallel) {
chain = chain.then(() => this.runCommandInPackagesParallel());
} else {
chain = chain.then(() => this.runCommandInPackagesBatched());
}
if (this.bail) {
// only the first error is caught
chain = chain.catch(err => {
process.exitCode = err.code;
// rethrow to halt chain and log properly
throw err;
});
} else {
// detect error (if any) from collected results
chain = chain.then(results => {
/* istanbul ignore else */
if (results.some(result => result.failed)) {
// propagate "highest" error code, it's probably the most useful
const codes = results
.filter(result => result.failed)
.map(result => {
switch (typeof result.code) {
case "number":
return result.code;
case "string":
return os.constants.errno[result.code];
default:
throw new TypeError("Received unexpected exit code value");
}
});
const exitCode = Math.max(...codes, 1);
this.logger.error("", "Received non-zero exit code %d during execution", exitCode);
process.exitCode = exitCode;
}
});
}
return chain.then(() => {
this.logger.success(
"exec",
"Executed command in %d %s: %j",
this.count,
this.packagePlural,
this.joinedCommand
);
});
}
getOpts(pkg) {
// these options are passed _directly_ to execa
return {
cwd: pkg.location,
shell: true,
extendEnv: false,
env: Object.assign({}, this.env, {
LERNA_PACKAGE_NAME: pkg.name,
LERNA_ROOT_PATH: this.project.rootPath,
}),
reject: this.bail,
pkg,
};
}
runCommandInPackagesBatched() {
const runner = this.options.stream
? pkg => this.runCommandInPackageStreaming(pkg)
: pkg => this.runCommandInPackageCapturing(pkg);
return runParallelBatches(this.batchedPackages, this.concurrency, runner).then(batchedResults =>
batchedResults.reduce((arr, batch) => arr.concat(batch), [])
);
}
runCommandInPackagesParallel() {
return Promise.all(this.filteredPackages.map(pkg => this.runCommandInPackageStreaming(pkg)));
}
runCommandInPackageStreaming(pkg) {
return ChildProcessUtilities.spawnStreaming(
this.command,
this.args,
this.getOpts(pkg),
this.prefix && pkg.name
);
}
runCommandInPackageCapturing(pkg) {
return ChildProcessUtilities.spawn(this.command, this.args, this.getOpts(pkg));
}
}
module.exports.ExecCommand = ExecCommand;