Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use logger to throw error with proper exit code #2076

Merged
merged 1 commit into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down