8000 fix: use logger for error with proper exit code (#2076) · webpack/webpack-cli@2c9069f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c9069f

Browse files
authored
fix: use logger for error with proper exit code (#2076)
1 parent 2ae99c2 commit 2c9069f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
jest.setMock('../prompt-installation', jest.fn());
2+
3+
const resolveCommand = require('../resolve-command');
4+
const promptInstallation = require('../prompt-installation');
5+
6+
describe('resolve-command util', () => {
7+
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
8+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
9+
10+
beforeEach(() => {
11+
processExitSpy.mockClear();
12+
consoleErrorSpy.mockClear();
13+
});
14+
15+
it('should not throw error', async () => {
16+
promptInstallation.mockImplementation(() => {});
17+
18+
await expect(resolveCommand('info')).resolves.not.toThrow();
19+
expect(processExitSpy.mock.calls.length).toBe(0);
20+
expect(consoleErrorSpy.mock.calls.length).toBe(0);
21+
});
22+
23+
it('should throw error and exit with invalid command', async () => {
24+
promptInstallation.mockImplementation(() => {
25+
throw new Error();
26+
});
27+
28+
await resolveCommand('invalid');
29+
expect(processExitSpy).toBeCalledWith(2);
30+
expect(consoleErrorSpy.mock.calls.length).toBe(1);
31+
});
32+
});

packages/webpack-cli/lib/utils/resolve-command.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const run = async (name, ...args) => {
1717
});
1818
} catch (err) {
1919
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible commands.`);
20+
process.exit(2);
2021
}
2122
}
2223

packages/webpack-cli/lib/utils/run-command.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const execa = require('execa');
2+
const logger = require('./logger');
23

34
async function runCommand(command, args = []) {
45
try {
@@ -7,7 +8,8 @@ async function runCommand(command, args = []) {
78
shell: true,
89
});
910
} catch (e) {
10-
throw new Error(e);
11+
logger.error(e);
12+
process.exit(2);
1113
}
1214
}
1315

0 commit comments

Comments
 (0)
0