Skip to content

Commit 4af2724

Browse files
authored
refactor: unify package manager related logic (#4256)
1 parent 7fc0f84 commit 4af2724

14 files changed

+435
-489
lines changed

packages/@vue/cli-ui/apollo-server/connectors/dependencies.js

+13-19
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ const logs = require('./logs')
1313
const getContext = require('../context')
1414
// Utils
1515
const { isPlugin, hasYarn, resolveModule } = require('@vue/cli-shared-utils')
16-
const getPackageVersion = require('@vue/cli/lib/util/getPackageVersion')
17-
const {
18-
progress: installProgress,
19-
installPackage,
20-
uninstallPackage,
21-
updatePackage
22-
} = require('@vue/cli/lib/util/installDeps')
23-
const { getCommand } = require('../util/command')
16+
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
17+
const PackageManager = require('@vue/cli/lib/util/ProjectPackageManager')
2418
const { resolveModuleRoot } = require('../util/resolve-path')
2519
const { notify } = require('../util/notification')
2620
const { log } = require('../util/logger')
@@ -113,10 +107,7 @@ async function getMetadata (id, context) {
113107

114108
if (!metadata) {
115109
try {
116-
const res = await getPackageVersion(id)
117-
if (res.statusCode === 200) {
118-
metadata = res.body
119-
}
110+
metadata = await (new PackageManager({ context: cwd.get() })).getMetadata()
120111
} catch (e) {
121112
// No connection?
122113
}
@@ -126,7 +117,7 @@ async function getMetadata (id, context) {
126117
metadataCache.set(id, metadata)
127118
return metadata
128119
} else {
129-
log('Dpendencies', chalk.yellow(`Can't load metadata`), id)
120+
log('Dependencies', chalk.yellow(`Can't load metadata`), id)
130121
}
131122
}
132123

@@ -211,7 +202,8 @@ function install ({ id, type, range }, context) {
211202
arg = id
212203
}
213204

214-
await installPackage(cwd.get(), getCommand(cwd.get()), arg, type === 'devDependencies')
205+
const pm = new PackageManager({ context: cwd.get() })
206+
await pm.add(arg, type === 'devDependencies')
215207

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

240232
const dep = findOne(id, context)
241233

242-
await uninstallPackage(cwd.get(), getCommand(cwd.get()), id)
234+
const pm = new PackageManager({ context: cwd.get() })
235+
await pm.remove(id)
243236

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

266259
const dep = findOne(id, context)
267260
const { current, wanted } = await getVersion(dep, context)
268-
await updatePackage(cwd.get(), getCommand(cwd.get()), id)
261+
262+
const pm = new PackageManager({ context: cwd.get() })
263+
await pm.upgrade(id)
269264

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

313-
await updatePackage(cwd.get(), getCommand(cwd.get()), updatedDeps.map(
314-
p => p.id
315-
).join(' '))
308+
const pm = new PackageManager({ context: cwd.get() })
309+
await pm.upgrade(updatedDeps.map(p => p.id).join(' '))
316310

317311
notify({
318312
title: `Dependencies updated`,

packages/@vue/cli-ui/apollo-server/connectors/plugins.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ const {
3030
clearModule,
3131
execa
3232
} = require('@vue/cli-shared-utils')
33-
const {
34-
progress: installProgress,
35-
installPackage,
36-
uninstallPackage,
37-
updatePackage
38-
} = require('@vue/cli/lib/util/installDeps')
39-
const { getCommand } = require('../util/command')
33+
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
34+
const PackageManager = require('@vue/cli/lib/util/ProjectPackageManager')
35+
4036
const ipc = require('../util/ipc')
4137
const { log } = require('../util/logger')
4238
const { notify } = require('../util/notification')
@@ -334,7 +330,8 @@ function install (id, context) {
334330
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
335331
mockInstall(id, context)
336332
} else {
337-
await installPackage(cwd.get(), getCommand(cwd.get()), id)
333+
const pm = new PackageManager({ context: cwd.get() })
334+
await pm.add(id)
338335
}
339336
await initPrompts(id, context)
340337
installationStep = 'config'
@@ -412,7 +409,8 @@ function uninstall (id, context) {
412409
if (process.env.VUE_CLI_DEBUG && isOfficialPlugin(id)) {
413410
mockUninstall(id, context)
414411
} else {
415-
await uninstallPackage(cwd.get(), getCommand(cwd.get()), id)
412+
const pm = new PackageManager({ context: cwd.get() })
413+
await pm.remove(id)
416414
}
417415
currentPluginId = null
418416
installationStep = null
@@ -520,7 +518,8 @@ function update ({ id, full }, context) {
520518
if (localPath) {
521519
await updateLocalPackage({ cwd: cwd.get(), id, localPath, full }, context)
522520
} else {
523-
await updatePackage(cwd.get(), getCommand(cwd.get()), id)
521+
const pm = new PackageManager({ context: cwd.get() })
522+
await pm.upgrade(id)
524523
}
525524

526525
logs.add({
@@ -583,9 +582,8 @@ async function updateAll (context) {
583582
args: [updatedPlugins.length]
584583
})
585584

586-
await updatePackage(cwd.get(), getCommand(cwd.get()), updatedPlugins.map(
587-
p => p.id
588-
).join(' '))
585+
const pm = new PackageManager({ context: cwd.get() })
586+
await pm.upgrade(updatedPlugins.map(p => p.id).join(' '))
589587

590588
notify({
591589
title: `Plugins updated`,

packages/@vue/cli-ui/apollo-server/connectors/projects.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { getPromptModules } = require('@vue/cli/lib/util/createTools')
66
const { getFeatures } = require('@vue/cli/lib/util/features')
77
const { defaults } = require('@vue/cli/lib/options')
88
const { toShortPluginId, execa } = require('@vue/cli-shared-utils')
9-
const { progress: installProgress } = require('@vue/cli/lib/util/installDeps')
9+
const { progress: installProgress } = require('@vue/cli/lib/util/executeCommand')
1010
const parseGitConfig = require('parse-git-config')
1111
// Connectors
1212
const progress = require('./progress')

packages/@vue/cli/lib/Creator.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Generator = require('./Generator')
99
const cloneDeep = require('lodash.clonedeep')
1010
const sortObject = require('./util/sortObject')
1111
const getVersions = require('./util/getVersions')
12-
const { installDeps } = require('./util/installDeps')
12+
const PackageManager = require('./util/ProjectPackageManager')
1313
const { clearConsole } = require('./util/clearConsole')
1414
const PromptModuleAPI = require('./PromptModuleAPI')
1515
const writeFileTree = require('./util/writeFileTree')
@@ -117,6 +117,7 @@ module.exports = class Creator extends EventEmitter {
117117
(hasYarn() ? 'yarn' : null) ||
118118
(hasPnpm3OrLater() ? 'pnpm' : 'npm')
119119
)
120+
const pm = new PackageManager({ context, forcePackageManager: packageManager })
120121

121122
await clearConsole()
122123
logWithSpinner(`✨`, `Creating project in ${chalk.yellow(context)}.`)
@@ -176,7 +177,7 @@ module.exports = class Creator extends EventEmitter {
176177
// in development, avoid installation process
177178
await require('./util/setupDevProject')(context)
178179
} else {
179-
await installDeps(context, packageManager)
180+
await pm.install()
180181
}
181182

182183
// run generator
@@ -197,7 +198,7 @@ module.exports = class Creator extends EventEmitter {
197198
this.emit('creation', { event: 'deps-install' })
198199
log()
199200
if (!isTestOrDebug) {
200-
await installDeps(context, packageManager)
201+
await pm.install()
201202
}
202203

203204
// run complete cbs if any (injected by generators)

packages/@vue/cli/lib/add.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const chalk = require('chalk')
22
const invoke = require('./invoke')
3-
const { loadOptions } = require('./options')
4-
const { installPackage } = require('./util/installDeps')
3+
4+
const PackageManager = require('./util/ProjectPackageManager')
55
const {
66
log,
77
error,
8-
hasProjectYarn,
9-
hasProjectPnpm,
108
resolvePluginId,
119
resolveModule
1210
} = require('@vue/cli-shared-utils')
@@ -23,8 +21,8 @@ async function add (pluginName, options = {}, context = process.cwd()) {
2321
log(`📦 Installing ${chalk.cyan(packageName)}...`)
2422
log()
2523

26-
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm')
27-
await installPackage(context, packageManager, packageName)
24+
const pm = new PackageManager({ context })
25+
await pm.add(packageName)
2826

2927
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
3028
log()

packages/@vue/cli/lib/invoke.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ const inquirer = require('inquirer')
66
const {
77
log,
88
error,
9-
hasProjectYarn,
109
hasProjectGit,
11-
hasProjectPnpm,
1210
logWithSpinner,
1311
stopSpinner,
1412
resolvePluginId,
1513
loadModule
1614
} = require('@vue/cli-shared-utils')
1715

1816
const Generator = require('./Generator')
19-
const { loadOptions } = require('./options')
20-
const { installDeps } = require('./util/installDeps')
17+
2118
const confirmIfGitDirty = require('./util/confirmIfGitDirty')
2219
const readFiles = require('./util/readFiles')
20+
const PackageManager = require('./util/ProjectPackageManager')
2321

2422
function getPkg (context) {
2523
const pkgPath = path.resolve(context, 'package.json')
@@ -130,9 +128,8 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
130128
if (!isTestOrDebug && depsChanged) {
131129
log(`📦 Installing additional dependencies...`)
132130
log()
133-
const packageManager =
134-
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm')
135-
await installDeps(context, packageManager)
131+
const pm = new PackageManager({ context })
132+
await pm.install()
136133
}
137134

138135
if (createCompleteCbs.length) {

0 commit comments

Comments
 (0)