Skip to content

Commit

Permalink
fix(webpack-cli): handle promise rejection with package installation (#…
Browse files Browse the repository at this point in the history
…1284)

* fix: handle promise rejection

* chore: better message

* chore: test case for prompt-installation
  • Loading branch information
jamesgeorge007 authored Mar 9, 2020
1 parent b93ffe4 commit eb1112e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/package-utils/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict';

import { packageExists } from '../src';
jest.mock('@webpack-cli/package-utils')

import { packageExists, promptInstallation } from '@webpack-cli/package-utils';
import ExternalCommand from '../../webpack-cli/lib/commands/ExternalCommand';

describe('@webpack-cli/package-utils', () => {
it('should check existence of package', () => {
(packageExists as any).mockImplementation(() => true);
const exists = packageExists('@webpack-cli/info');

expect(exists).toBeTruthy();
});

it('should not throw if the user interrupts', async () => {
(promptInstallation as any).mockImplementation(() => { throw new Error() });
await expect(ExternalCommand.run('info')).resolves.not.toThrow();
});
});
8 changes: 6 additions & 2 deletions packages/webpack-cli/lib/commands/ExternalCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ class ExternalCommand {
const scopeName = packagePrefix + '/' + name;
let pkgLoc = packageExists(scopeName);
if (!pkgLoc) {
pkgLoc = await promptInstallation(`${scopeName}`, () => {
try {
pkgLoc = await promptInstallation(`${scopeName}`, () => {
logger.error(`The command moved into a separate package: ${chalk.keyword('orange')(scopeName)}\n`);
});
});
} catch (err) {
logger.error(`Action Interrupted, use ${chalk.cyan(`webpack-cli help`)} to see possible commands.`)
}
}
return pkgLoc ? require(scopeName).default(...args) : null;
}
Expand Down

0 comments on commit eb1112e

Please sign in to comment.