Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eliminate code redundancy #4205

Merged
merged 1 commit into from Jun 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 27 additions & 32 deletions packages/@vue/cli/lib/util/installDeps.js
Expand Up @@ -11,6 +11,29 @@ const taobaoDistURL = 'https://npm.taobao.org/dist'

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

const packageManagerConfig = {
npm: {
installDeps: ['install', '--loglevel', 'error'],
installPackage: ['install', '--loglevel', 'error'],
uninstallPackage: ['uninstall', '--loglevel', 'error'],
updatePackage: ['update', '--loglevel', 'error']
},

pnpm: {
installDeps: ['install', '--loglevel', 'error', '--shamefully-flatten'],
installPackage: ['install', '--loglevel', 'error'],
uninstallPackage: ['uninstall', '--loglevel', 'error'],
updatePackage: ['update', '--loglevel', 'error']
},

yarn: {
installDeps: [],
installPackage: ['add'],
uninstallPackage: ['remove'],
updatePackage: ['upgrade']
}
}

class InstallProgress extends EventEmitter {
constructor () {
super()
Expand Down Expand Up @@ -174,17 +197,7 @@ function executeCommand (command, args, targetDir) {
exports.installDeps = async function installDeps (targetDir, command, cliRegistry) {
checkPackageManagerIsSupported(command)

const args = []

if (command === 'npm' || command === 'pnpm') {
args.push('install', '--loglevel', 'error')
} else if (command === 'yarn') {
// do nothing
}

if (command === 'pnpm') {
args.push('--shamefully-flatten')
}
const args = packageManagerConfig[command].installDeps;

await addRegistryToArgs(command, args, cliRegistry)

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

const args = []

if (command === 'npm' || command === 'pnpm') {
args.push('install', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('add')
}
const args = packageManagerConfig[command].installPackage;

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

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

const args = []

if (command === 'npm' || command === 'pnpm') {
args.push('uninstall', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('remove')
}
const args = packageManagerConfig[command].uninstallPackage;

await addRegistryToArgs(command, args, cliRegistry)

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

const args = []

if (command === 'npm' || command === 'pnpm') {
args.push('update', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('upgrade')
}
const args = packageManagerConfig[command].updatePackage;

await addRegistryToArgs(command, args, cliRegistry)

Expand Down