Skip to content

Commit 5b98884

Browse files
author
evilebottnawi
committed
refactor: internal
1 parent 2c10ddb commit 5b98884

3 files changed

Lines changed: 89 additions & 31 deletions

File tree

__tests__/index.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,62 @@ describe("execa-webpack-plugin", () => {
420420
});
421421
});
422422

423+
it("should not throw error with `bail: false` option (sync event)", () => {
424+
expect.assertions(1);
425+
426+
let catchError = null;
427+
428+
return run({
429+
bail: false,
430+
logLevel: "silent",
431+
onCompile: [
432+
{
433+
cmd: "not-found"
434+
}
435+
]
436+
})
437+
.catch(error => {
438+
catchError = error;
439+
440+
return Promise.resolve();
441+
})
442+
.then(() => {
443+
// execa not return error instanceOf Error
444+
// expect(catchError).toBeInstanceOf(Error);
445+
expect(catchError).toBeNull();
446+
447+
return Promise.resolve();
448+
});
449+
});
450+
451+
it("should not throw error with `bail: false` option (async event)", () => {
452+
expect.assertions(1);
453+
454+
let catchError = null;
455+
456+
return run({
457+
bail: false,
458+
logLevel: "silent",
459+
onDone: [
460+
{
461+
cmd: "not-found"
462+
}
463+
]
464+
})
465+
.catch(error => {
466+
catchError = error;
467+
468+
return Promise.resolve();
469+
})
470+
.then(() => {
471+
// execa not return error instanceOf Error
472+
// expect(catchError).toBeInstanceOf(Error);
473+
expect(catchError).toBeNull();
474+
475+
return Promise.resolve();
476+
});
477+
});
478+
423479
it("should works and output 'stdout' and 'stderr' with `logLevel: 'info'` command (sync event)", () =>
424480
run({
425481
logLevel: "info",

src/CommandRunner.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class CommandRunner {
1313
}
1414

1515
static buildError(error, command) {
16-
const { cmd } = command;
17-
const args = command.args || [];
16+
const { cmd, args } = command;
1817

1918
return new Error(
2019
`Command "${cmd}${args.length > 0 ? ` ${args.join(" ")}` : ""}" return ${
@@ -24,6 +23,10 @@ class CommandRunner {
2423
}
2524

2625
handleResult(result) {
26+
if (!result) {
27+
return;
28+
}
29+
2730
const { stdout, stderr } = result;
2831

2932
if (stdout) {
@@ -44,9 +47,7 @@ class CommandRunner {
4447
}
4548

4649
run(command, async) {
47-
const { cmd } = command;
48-
const args = command.args || [];
49-
const opts = command.opts || {};
50+
const { cmd, args = [], opts = {} } = command;
5051

5152
opts.stdio = ["ignore", "pipe", "pipe"];
5253

@@ -76,7 +77,7 @@ class CommandRunner {
7677

7778
this.handleResult(result, cmd, args);
7879

79-
return result.stdout;
80+
return result ? result.stdout : null;
8081
}
8182
}
8283

src/ExecaWebpackPlugin.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,42 @@ class ExecaWebpackPlugin {
6161

6262
execute(commands, async) {
6363
const results = [];
64-
65-
commands.forEach(command => {
66-
const args = command.args || [];
67-
68-
args.forEach((arg, index) => {
69-
if (
70-
typeof arg === "object" &&
71-
Boolean(arg) &&
72-
Object.keys(arg).length > 0
73-
) {
74-
const commandResult = this.execute([arg], async);
75-
76-
command.args[index] = Array.isArray(commandResult)
77-
? commandResult[0]
78-
: commandResult;
79-
} else {
80-
command.args[index] = async ? Promise.resolve(arg) : arg;
81-
}
82-
});
83-
84-
let result = null;
85-
64+
const runCommandRunner = command => {
8665
if (async) {
87-
result = Promise.all(args).then(resolvedArgs => {
66+
return Promise.all(command.args).then(resolvedArgs => {
8867
command.args = resolvedArgs.map(
8968
item => (item[0] && item[0].stdout ? item[0].stdout : item)
9069
);
9170

9271
return new CommandRunner(this.options).run(command, async);
9372
});
73+
}
74+
75+
return new CommandRunner(this.options).run(command, async);
76+
};
77+
78+
commands.forEach(command => {
79+
if (command.args) {
80+
command.args = command.args.map(arg => {
81+
if (
82+
typeof arg === "object" &&
83+
Boolean(arg) &&
84+
Object.keys(arg).length > 0
85+
) {
86+
const commandResult = this.execute([arg], async);
87+
88+
return Array.isArray(commandResult)
89+
? commandResult[0]
90+
: commandResult;
91+
}
92+
93+
return async ? Promise.resolve(arg) : arg;
94+
});
9495
} else {
95-
result = new CommandRunner(this.options).run(command, async);
96+
command.args = [];
9697
}
9798

98-
results.push(result);
99+
results.push(runCommandRunner(command));
99100
});
100101

101102
return async ? Promise.all(results) : results;

0 commit comments

Comments
 (0)