Skip to content

Commit 934746d

Browse files
jamesgeorge007haoqunjiang
authored andcommitted
chore: minor refactor (#4205)
eliminate redundancy
1 parent 5460ca4 commit 934746d

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

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

+27-32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ const taobaoDistURL = 'https://npm.taobao.org/dist'
1111

1212
const supportPackageManagerList = ['npm', 'yarn', 'pnpm']
1313

14+
const packageManagerConfig = {
15+
npm: {
16+
installDeps: ['install', '--loglevel', 'error'],
17+
installPackage: ['install', '--loglevel', 'error'],
18+
uninstallPackage: ['uninstall', '--loglevel', 'error'],
19+
updatePackage: ['update', '--loglevel', 'error']
20+
},
21+
22+
pnpm: {
23+
installDeps: ['install', '--loglevel', 'error', '--shamefully-flatten'],
24+
installPackage: ['install', '--loglevel', 'error'],
25+
uninstallPackage: ['uninstall', '--loglevel', 'error'],
26+
updatePackage: ['update', '--loglevel', 'error']
27+
},
28+
29+
yarn: {
30+
installDeps: [],
31+
installPackage: ['add'],
32+
uninstallPackage: ['remove'],
33+
updatePackage: ['upgrade']
34+
}
35+
}
36+
1437
class InstallProgress extends EventEmitter {
1538
constructor () {
1639
super()
@@ -174,17 +197,7 @@ function executeCommand (command, args, targetDir) {
174197
exports.installDeps = async function installDeps (targetDir, command, cliRegistry) {
175198
checkPackageManagerIsSupported(command)
176199

177-
const args = []
178-
179-
if (command === 'npm' || command === 'pnpm') {
180-
args.push('install', '--loglevel', 'error')
181-
} else if (command === 'yarn') {
182-
// do nothing
183-
}
184-
185-
if (command === 'pnpm') {
186-
args.push('--shamefully-flatten')
187-
}
200+
const args = packageManagerConfig[command].installDeps;
188201

189202
await addRegistryToArgs(command, args, cliRegistry)
190203

@@ -197,13 +210,7 @@ exports.installDeps = async function installDeps (targetDir, command, cliRegistr
197210
exports.installPackage = async function (targetDir, command, cliRegistry, packageName, dev = true) {
198211
checkPackageManagerIsSupported(command)
199212

200-
const args = []
201-
202-
if (command === 'npm' || command === 'pnpm') {
203-
args.push('install', '--loglevel', 'error')
204-
} else if (command === 'yarn') {
205-
args.push('add')
206-
}
213+
const args = packageManagerConfig[command].installPackage;
207214

208215
if (dev) args.push('-D')
209216

@@ -220,13 +227,7 @@ exports.installPackage = async function (targetDir, command, cliRegistry, packag
220227
exports.uninstallPackage = async function (targetDir, command, cliRegistry, packageName) {
221228
checkPackageManagerIsSupported(command)
222229

223-
const args = []
224-
225-
if (command === 'npm' || command === 'pnpm') {
226-
args.push('uninstall', '--loglevel', 'error')
227-
} else if (command === 'yarn') {
228-
args.push('remove')
229-
}
230+
const args = packageManagerConfig[command].uninstallPackage;
230231

231232
await addRegistryToArgs(command, args, cliRegistry)
232233

@@ -241,13 +242,7 @@ exports.uninstallPackage = async function (targetDir, command, cliRegistry, pack
241242
exports.updatePackage = async function (targetDir, command, cliRegistry, packageName) {
242243
checkPackageManagerIsSupported(command)
243244

244-
const args = []
245-
246-
if (command === 'npm' || command === 'pnpm') {
247-
args.push('update', '--loglevel', 'error')
248-
} else if (command === 'yarn') {
249-
args.push('upgrade')
250-
}
245+
const args = packageManagerConfig[command].updatePackage;
251246

252247
await addRegistryToArgs(command, args, cliRegistry)
253248

0 commit comments

Comments
 (0)