Skip to content

Commit e606d88

Browse files
refactor: external command (#1867)
Co-authored-by: Nitin Kumar <[email protected]>
1 parent f653409 commit e606d88

File tree

4 files changed

+41
-44
lines changed

4 files changed

+41
-44
lines changed

packages/package-utils/__tests__/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
jest.mock('@webpack-cli/package-utils');
44

55
import { packageExists, promptInstallation } from '@webpack-cli/package-utils';
6-
import ExternalCommand from '../../webpack-cli/lib/commands/ExternalCommand';
6+
import { run } from '../../webpack-cli/lib/commands/resolveCommand';
77

88
describe('@webpack-cli/package-utils', () => {
99
it('should check existence of package', () => {
@@ -16,6 +16,6 @@ describe('@webpack-cli/package-utils', () => {
1616
(promptInstallation as jest.Mock).mockImplementation(() => {
1717
throw new Error();
1818
});
19-
await expect(ExternalCommand.run('info')).resolves.not.toThrow();
19+
await expect(run('info')).resolves.not.toThrow();
2020
});
2121
});

packages/webpack-cli/lib/commands/ExternalCommand.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { yellow, cyan } = require('colorette');
2+
const logger = require('../utils/logger');
3+
const execa = require('execa');
4+
const { packageExists, promptInstallation } = require('@webpack-cli/package-utils');
5+
6+
const packagePrefix = '@webpack-cli';
7+
const runCommand = async (command, args = []) => {
8+
const executedCommand = await execa(command, args, {
9+
stdio: 'inherit',
10+
shell: true,
11+
});
12+
return new Promise((resolve, reject) => {
13+
executedCommand.on('error', (error) => {
14+
reject(error);
15+
});
16+
17+
executedCommand.on('exit', () => {
18+
resolve();
19+
});
20+
});
21+
};
22+
23+
const run = async (name, ...args) => {
24+
const scopeName = packagePrefix + '/' + name;
25+
let pkgLoc = packageExists(scopeName);
26+
if (!pkgLoc) {
27+
try {
28+
pkgLoc = await promptInstallation(`${scopeName}`, () => {
29+
logger.error(`The command moved into a separate package: ${yellow(scopeName)}\n`);
30+
});
31+
} catch (err) {
32+
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible commands.`);
33+
}
34+
}
35+
return pkgLoc ? require(scopeName).default(...args) : null;
36+
};
37+
38+
module.exports = { run, runCommand };

packages/webpack-cli/lib/utils/arg-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction) {
2727
.allowUnknownOption(true)
2828
.action(async () => {
2929
const cliArgs = args.slice(args.indexOf(cmd.name) + 1 || args.indexOf(cmd.alias) + 1);
30-
return await require('../commands/ExternalCommand').run(defaultCommands[cmd.name], ...cliArgs);
30+
return await require('../commands/resolveCommand').run(defaultCommands[cmd.name], ...cliArgs);
3131
});
3232
return parser;
3333
}, parser);

0 commit comments

Comments
 (0)