Skip to content

refactor: check support package manager befor install #3368

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions packages/@vue/cli/lib/util/installDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const debug = require('debug')('vue-cli:install')

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

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

class InstallProgress extends EventEmitter {
constructor () {
super()
Expand Down Expand Up @@ -48,6 +50,12 @@ function toStartOfLine (stream) {
readline.cursorTo(stream, 0)
}

function checkPackageManagerIsSupported (command) {
if (supportPackageManagerList.indexOf(command) === -1) {
throw new Error(`Unknown package manager: ${command}`)
}
}

function renderProgressBar (curr, total) {
const ratio = Math.min(Math.max(curr / total, 0), 1)
const bar = ` ${curr}/${total}`
Expand Down Expand Up @@ -164,13 +172,14 @@ function executeCommand (command, args, targetDir) {
}

exports.installDeps = async function installDeps (targetDir, command, cliRegistry) {
checkPackageManagerIsSupported(command)

const args = []

if (command === 'npm') {
args.push('install', '--loglevel', 'error')
} else if (command === 'yarn') {
// do nothing
} else {
throw new Error(`Unknown package manager: ${command}`)
}

await addRegistryToArgs(command, args, cliRegistry)
Expand All @@ -182,13 +191,14 @@ 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') {
args.push('install', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('add')
} else {
throw new Error(`Unknown package manager: ${command}`)
}

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

exports.uninstallPackage = async function (targetDir, command, cliRegistry, packageName) {
checkPackageManagerIsSupported(command)

const args = []

if (command === 'npm') {
args.push('uninstall', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('remove')
} else {
throw new Error(`Unknown package manager: ${command}`)
}

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

exports.updatePackage = async function (targetDir, command, cliRegistry, packageName) {
checkPackageManagerIsSupported(command)

const args = []

if (command === 'npm') {
args.push('update', '--loglevel', 'error')
} else if (command === 'yarn') {
args.push('upgrade')
} else {
throw new Error(`Unknown package manager: ${command}`)
}

await addRegistryToArgs(command, args, cliRegistry)
Expand Down