Skip to content

Commit

Permalink
fix: use logger for error with proper exit code (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Nov 10, 2020
1 parent 2ae99c2 commit 2c9069f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/webpack-cli/lib/utils/__tests__/resolve-command.test.js
@@ -0,0 +1,32 @@
jest.setMock('../prompt-installation', jest.fn());

const resolveCommand = require('../resolve-command');
const promptInstallation = require('../prompt-installation');

describe('resolve-command util', () => {
const processExitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

beforeEach(() => {
processExitSpy.mockClear();
consoleErrorSpy.mockClear();
});

it('should not throw error', async () => {
promptInstallation.mockImplementation(() => {});

await expect(resolveCommand('info')).resolves.not.toThrow();
expect(processExitSpy.mock.calls.length).toBe(0);
expect(consoleErrorSpy.mock.calls.length).toBe(0);
});

it('should throw error and exit with invalid command', async () => {
promptInstallation.mockImplementation(() => {
throw new Error();
});

await resolveCommand('invalid');
expect(processExitSpy).toBeCalledWith(2);
expect(consoleErrorSpy.mock.calls.length).toBe(1);
});
});
1 change: 1 addition & 0 deletions packages/webpack-cli/lib/utils/resolve-command.js
Expand Up @@ -17,6 +17,7 @@ const run = async (name, ...args) => {
});
} catch (err) {
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible commands.`);
process.exit(2);
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/webpack-cli/lib/utils/run-command.js
@@ -1,4 +1,5 @@
const execa = require('execa');
const logger = require('./logger');

async function runCommand(command, args = []) {
try {
Expand All @@ -7,7 +8,8 @@ async function runCommand(command, args = []) {
shell: true,
});
} catch (e) {
throw new Error(e);
logger.error(e);
process.exit(2);
}
}

Expand Down

0 comments on commit 2c9069f

Please sign in to comment.