This repository was archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathudk-webpack4.ts
176 lines (150 loc) · 4.71 KB
/
udk-webpack4.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2018 Steven Enten. All rights reserved. Licensed under the MIT license.
process.exitCode = 0;
/**
* @param {string} command process to run
* @param {string[]} args commandline arguments
* @returns {Promise<void>} promise
*/
const runCommand = (command: string, args: string[]) => {
const cp = require('child_process');
return new Promise<void>((resolve, reject) => {
const executedCommand = cp.spawn(command, args, {
stdio: 'inherit',
shell: true,
});
// tslint:disable-next-line:no-any
executedCommand.on('error', (error: any) => {
reject(error);
});
executedCommand.on('exit', (code?: number) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
};
/**
* @param {string} packageName name of the package
* @returns {boolean} is the package installed?
*/
const isInstalled = (packageName: string) => {
try {
require.resolve(packageName);
return true;
} catch (err) {
return false;
}
};
/**
* @typedef {Object} CliOption
* @property {string} name display name
* @property {string} package npm package name
* @property {string} binName name of the executable file
* @property {string} alias shortcut for choice
* @property {boolean} installed currently installed?
* @property {string} url homepage
* @property {string} description description
*/
/** @type {CliOption[]} */
const CLIs = [
{
name: 'webpack-cli',
package: 'webpack-cli',
binName: 'webpack-cli',
alias: 'cli',
installed: isInstalled('webpack-cli'),
url: 'https://github.com/webpack/webpack-cli',
description: 'The original webpack full-featured CLI.',
},
{
name: 'webpack-command',
package: 'webpack-command',
binName: 'webpack-command',
alias: 'command',
installed: isInstalled('webpack-command'),
url: 'https://github.com/webpack-contrib/webpack-command',
description: 'A lightweight, opinionated webpack CLI.',
},
];
const installedClis = CLIs.filter(cli => cli.installed);
if (installedClis.length === 0) {
const path = require('path');
const fs = require('fs');
const readLine = require('readline');
let notify =
// tslint:disable-next-line:max-line-length
'One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:';
for (const item of CLIs) {
notify += `\n - ${item.name} (${item.url})\n ${item.description}`;
}
console.error(notify);
const isYarn = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
const packageManager = isYarn ? 'yarn' : 'npm';
const installOptions = [isYarn ? 'add' : 'install', '-D'];
console.error(
// tslint:disable-next-line:max-line-length
`We will use "${packageManager}" to install the CLI via "${packageManager} ${installOptions.join(
' ',
)}".`,
);
const question = `Which one do you like to install (${CLIs.map(
item => item.name,
).join('/')}):\n`;
const questionInterface = readLine.createInterface({
input: process.stdin,
output: process.stderr,
});
questionInterface.question(question, (answer: string) => {
questionInterface.close();
const normalizedAnswer = answer.toLowerCase();
const selectedPackage = CLIs.find(item => {
return item.name === normalizedAnswer || item.alias === normalizedAnswer;
});
if (!normalizedAnswer) {
console.error(
'One CLI needs to be installed alongside webpack to use the CLI.',
);
process.exitCode = 1;
return;
} else if (!selectedPackage) {
console.error(
'No matching choice.\n' +
'One CLI needs to be installed alongside webpack to use the CLI.\n' +
'Try to installing your CLI of choice manually.',
);
process.exitCode = 1;
return;
}
const packageName = selectedPackage.package;
console.log(
`Installing '${
selectedPackage.name
}' (running '${packageManager} ${installOptions.join(
' ',
)} ${packageName}')...`,
);
runCommand(packageManager, installOptions.concat(packageName))
.then(() => {
require(`./udk-${packageName}`);
})
.catch(error => {
console.error(error);
process.exitCode = 1;
});
});
} else if (installedClis.length === 1) {
if (installedClis[0].package === 'webpack-command') {
require('./udk-webpack-command');
} else {
require('./udk-webpack-cli');
}
} else {
console.warn(
`You have installed ${installedClis
.map(item => item.name)
// tslint:disable-next-line:max-line-length
.join(' and ')} together. To work with the "webpack" command you need only one CLI package, please remove one of them or use them directly via their binary.`,
);
}