Skip to content

refactor: unify package manager related logic #4256

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 7 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
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
32 changes: 13 additions & 19 deletions packages/@vue/cli-ui/apollo-server/connectors/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ const logs = require('./logs')
const getContext = require('../context')
// Utils
const { isPlugin, hasYarn, resolveModule } = require('@vue/cli-shared-utils')
const getPackageVersion = require('@vue/cli/lib/util/getPackageVersion')
const {
progress: installProgress,
installPackage,
uninstallPackage,
updatePackage
} = require('@vue/cli/lib/util/installDeps')
const { getCommand } = require('../util/command')
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
const PackageManager = require('@vue/cli/lib/util/ProjectPackageManager')
const { resolveModuleRoot } = require('../util/resolve-path')
const { notify } = require('../util/notification')
const { log } = require('../util/logger')
Expand Down Expand Up @@ -113,10 +107,7 @@ async function getMetadata (id, context) {

if (!metadata) {
try {
const res = await getPackageVersion(id)
if (res.statusCode === 200) {
metadata = res.body
}
metadata = await (new PackageManager({ context: cwd.get() })).getMetadata()
} catch (e) {
// No connection?
}
Expand All @@ -126,7 +117,7 @@ async function getMetadata (id, context) {
metadataCache.set(id, metadata)
return metadata
} else {
log('Dpendencies', chalk.yellow(`Can't load metadata`), id)
log('Dependencies', chalk.yellow(`Can't load metadata`), id)
}
}

Expand Down Expand Up @@ -211,7 +202,8 @@ function install ({ id, type, range }, context) {
arg = id
}

await installPackage(cwd.get(), getCommand(cwd.get()), arg, type === 'devDependencies')
const pm = new PackageManager({ context: cwd.get() })
await pm.add(arg, type === 'devDependencies')

logs.add({
message: `Dependency ${id} installed`,
Expand Down Expand Up @@ -239,7 +231,8 @@ function uninstall ({ id }, context) {

const dep = findOne(id, context)

await uninstallPackage(cwd.get(), getCommand(cwd.get()), id)
const pm = new PackageManager({ context: cwd.get() })
await pm.remove(id)

logs.add({
message: `Dependency ${id} uninstalled`,
Expand All @@ -265,7 +258,9 @@ function update ({ id }, context) {

const dep = findOne(id, context)
const { current, wanted } = await getVersion(dep, context)
await updatePackage(cwd.get(), getCommand(cwd.get()), id)

const pm = new PackageManager({ context: cwd.get() })
await pm.upgrade(id)

logs.add({
message: `Dependency ${id} updated from ${current} to ${wanted}`,
Expand Down Expand Up @@ -310,9 +305,8 @@ function updateAll (context) {
args: [updatedDeps.length]
})

await updatePackage(cwd.get(), getCommand(cwd.get()), updatedDeps.map(
p => p.id
).join(' '))
const pm = new PackageManager({ context: cwd.get() })
await pm.upgrade(updatedDeps.map(p => p.id).join(' '))

notify({
title: `Dependencies updated`,
Expand Down
24 changes: 11 additions & 13 deletions packages/@vue/cli-ui/apollo-server/connectors/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ const {
clearModule,
execa
} = require('@vue/cli-shared-utils')
const {
progress: installProgress,
installPackage,
uninstallPackage,
updatePackage
} = require('@vue/cli/lib/util/installDeps')
const { getCommand } = require('../util/command')
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
const PackageManager = require('@vue/cli/lib/util/ProjectPackageManager')

const ipc = require('../util/ipc')
const { log } = require('../util/logger')
const { notify } = require('../util/notification')
Expand Down Expand Up @@ -334,7 +330,8 @@ function install (id, context) {
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
mockInstall(id, context)
} else {
await installPackage(cwd.get(), getCommand(cwd.get()), id)
const pm = new PackageManager({ context: cwd.get() })
await pm.add(id)
}
await initPrompts(id, context)
installationStep = 'config'
Expand Down Expand Up @@ -412,7 +409,8 @@ function uninstall (id, context) {
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
mockUninstall(id, context)
} else {
await uninstallPackage(cwd.get(), getCommand(cwd.get()), id)
const pm = new PackageManager({ context: cwd.get() })
await pm.remove(id)
}
currentPluginId = null
installationStep = null
Expand Down Expand Up @@ -520,7 +518,8 @@ function update ({ id, full }, context) {
if (localPath) {
await updateLocalPackage({ cwd: cwd.get(), id, localPath, full }, context)
} else {
await updatePackage(cwd.get(), getCommand(cwd.get()), id)
const pm = new PackageManager({ context: cwd.get() })
await pm.upgrade(id)
}

logs.add({
Expand Down Expand Up @@ -583,9 +582,8 @@ async function updateAll (context) {
args: [updatedPlugins.length]
})

await updatePackage(cwd.get(), getCommand(cwd.get()), updatedPlugins.map(
p => p.id
).join(' '))
const pm = new PackageManager({ context: cwd.get() })
await pm.upgrade(updatedPlugins.map(p => p.id).join(' '))

notify({
title: `Plugins updated`,
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-ui/apollo-server/connectors/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { getPromptModules } = require('@vue/cli/lib/util/createTools')
const { getFeatures } = require('@vue/cli/lib/util/features')
const { defaults } = require('@vue/cli/lib/options')
const { toShortPluginId, execa } = require('@vue/cli-shared-utils')
const { progress: installProgress } = require('@vue/cli/lib/util/installDeps')
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
const parseGitConfig = require('parse-git-config')
// Connectors
const progress = require('./progress')
Expand Down
7 changes: 4 additions & 3 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Generator = require('./Generator')
const cloneDeep = require('lodash.clonedeep')
const sortObject = require('./util/sortObject')
const getVersions = require('./util/getVersions')
const { installDeps } = require('./util/installDeps')
const PackageManager = require('./util/ProjectPackageManager')
const { clearConsole } = require('./util/clearConsole')
const PromptModuleAPI = require('./PromptModuleAPI')
const writeFileTree = require('./util/writeFileTree')
Expand Down Expand Up @@ -117,6 +117,7 @@ module.exports = class Creator extends EventEmitter {
(hasYarn() ? 'yarn' : null) ||
(hasPnpm3OrLater() ? 'pnpm' : 'npm')
)
const pm = new PackageManager({ context, forcePackageManager: packageManager })

await clearConsole()
logWithSpinner(`✨`, `Creating project in ${chalk.yellow(context)}.`)
Expand Down Expand Up @@ -176,7 +177,7 @@ module.exports = class Creator extends EventEmitter {
// in development, avoid installation process
await require('./util/setupDevProject')(context)
} else {
await installDeps(context, packageManager)
await pm.install()
}

// run generator
Expand All @@ -197,7 +198,7 @@ module.exports = class Creator extends EventEmitter {
this.emit('creation', { event: 'deps-install' })
log()
if (!isTestOrDebug) {
await installDeps(context, packageManager)
await pm.install()
}

// run complete cbs if any (injected by generators)
Expand Down
10 changes: 4 additions & 6 deletions packages/@vue/cli/lib/add.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const chalk = require('chalk')
const invoke = require('./invoke')
const { loadOptions } = require('./options')
const { installPackage } = require('./util/installDeps')

const PackageManager = require('./util/ProjectPackageManager')
const {
log,
error,
hasProjectYarn,
hasProjectPnpm,
resolvePluginId,
resolveModule
} = require('@vue/cli-shared-utils')
Expand All @@ -23,8 +21,8 @@ async function add (pluginName, options = {}, context = process.cwd()) {
log(`📦 Installing ${chalk.cyan(packageName)}...`)
log()

const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm')
await installPackage(context, packageManager, packageName)
const pm = new PackageManager({ context })
await pm.add(packageName)

log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
log()
Expand Down
11 changes: 4 additions & 7 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ const inquirer = require('inquirer')
const {
log,
error,
hasProjectYarn,
hasProjectGit,
hasProjectPnpm,
logWithSpinner,
stopSpinner,
resolvePluginId,
loadModule
} = require('@vue/cli-shared-utils')

const Generator = require('./Generator')
const { loadOptions } = require('./options')
const { installDeps } = require('./util/installDeps')

const confirmIfGitDirty = require('./util/confirmIfGitDirty')
const readFiles = require('./util/readFiles')
const PackageManager = require('./util/ProjectPackageManager')

function getPkg (context) {
const pkgPath = path.resolve(context, 'package.json')
Expand Down Expand Up @@ -130,9 +128,8 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
if (!isTestOrDebug && depsChanged) {
log(`📦 Installing additional dependencies...`)
log()
const packageManager =
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm')
await installDeps(context, packageManager)
const pm = new PackageManager({ context })
await pm.install()
}

if (createCompleteCbs.length) {
Expand Down
Loading