Skip to content

Commit 5d1ab5f

Browse files
musicqhaoqunjiang
authored andcommitted
refactor: check support package manager befor install (#3368)
1 parent d5ed280 commit 5d1ab5f

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

packages/@vue/cli/lib/util/installDeps.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const debug = require('debug')('vue-cli:install')
99

1010
const taobaoDistURL = 'https://npm.taobao.org/dist'
1111

12+
const supportPackageManagerList = ['npm', 'yarn']
13+
1214
class InstallProgress extends EventEmitter {
1315
constructor () {
1416
super()
@@ -48,6 +50,12 @@ function toStartOfLine (stream) {
4850
readline.cursorTo(stream, 0)
4951
}
5052

53+
function checkPackageManagerIsSupported (command) {
54+
if (supportPackageManagerList.indexOf(command) === -1) {
55+
throw new Error(`Unknown package manager: ${command}`)
56+
}
57+
}
58+
5159
function renderProgressBar (curr, total) {
5260
const ratio = Math.min(Math.max(curr / total, 0), 1)
5361
const bar = ` ${curr}/${total}`
@@ -164,13 +172,14 @@ function executeCommand (command, args, targetDir) {
164172
}
165173

166174
exports.installDeps = async function installDeps (targetDir, command, cliRegistry) {
175+
checkPackageManagerIsSupported(command)
176+
167177
const args = []
178+
168179
if (command === 'npm') {
169180
args.push('install', '--loglevel', 'error')
170181
} else if (command === 'yarn') {
171182
// do nothing
172-
} else {
173-
throw new Error(`Unknown package manager: ${command}`)
174183
}
175184

176185
await addRegistryToArgs(command, args, cliRegistry)
@@ -182,13 +191,14 @@ exports.installDeps = async function installDeps (targetDir, command, cliRegistr
182191
}
183192

184193
exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) {
194+
checkPackageManagerIsSupported(command)
195+
185196
const args = []
197+
186198
if (command === 'npm') {
187199
args.push('install', '--loglevel', 'error')
188200
} else if (command === 'yarn') {
189201
args.push('add')
190-
} else {
191-
throw new Error(`Unknown package manager: ${command}`)
192202
}
193203

194204
if (dev) args.push('-D')
@@ -204,13 +214,14 @@ exports.installPackage = async function (targetDir, command, cliRegistry, packag
204214
}
205215

206216
exports.uninstallPackage = async function (targetDir, command, cliRegistry, packageName) {
217+
checkPackageManagerIsSupported(command)
218+
207219
const args = []
220+
208221
if (command === 'npm') {
209222
args.push('uninstall', '--loglevel', 'error')
210223
} else if (command === 'yarn') {
211224
args.push('remove')
212-
} else {
213-
throw new Error(`Unknown package manager: ${command}`)
214225
}
215226

216227
await addRegistryToArgs(command, args, cliRegistry)
@@ -224,13 +235,14 @@ exports.uninstallPackage = async function (targetDir, command, cliRegistry, pack
224235
}
225236

226237
exports.updatePackage = async function (targetDir, command, cliRegistry, packageName) {
238+
checkPackageManagerIsSupported(command)
239+
227240
const args = []
241+
228242
if (command === 'npm') {
229243
args.push('update', '--loglevel', 'error')
230244
} else if (command === 'yarn') {
231245
args.push('upgrade')
232-
} else {
233-
throw new Error(`Unknown package manager: ${command}`)
234246
}
235247

236248
await addRegistryToArgs(command, args, cliRegistry)

0 commit comments

Comments
 (0)