/
ExternalCommand.js
50 lines (45 loc) · 1.73 KB
/
ExternalCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { yellow, cyan } = require('colorette');
const logger = require('../utils/logger');
const execa = require('execa');
const { packageExists, promptInstallation } = require('@webpack-cli/package-utils');
const packagePrefix = '@webpack-cli';
class ExternalCommand {
static async runCommand(command, args = []) {
const executedCommand = await execa(command, args, {
stdio: 'inherit',
shell: true,
});
return new Promise((resolve, reject) => {
executedCommand.on('error', (error) => {
reject(error);
});
executedCommand.on('exit', () => {
resolve();
});
});
}
static async run(name, ...args) {
const scopeName = packagePrefix + '/' + name;
let pkgLoc = packageExists(scopeName);
if (!pkgLoc) {
try {
pkgLoc = await promptInstallation(`${scopeName}`, () => {
logger.error(`The command moved into a separate package: ${yellow(scopeName)}\n`);
});
} catch (err) {
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible commands.`);
}
}
// Check dev server availability before running serve
if (name === 'serve') {
try {
// eslint-disable-next-line node/no-extraneous-require
require.resolve('webpack-dev-server');
} catch (error) {
throw new Error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`);
}
}
return pkgLoc ? require(scopeName).default(...args) : null;
}
}
module.exports = ExternalCommand;