Skip to content

fix: add pnpm v4 support #4677

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 16 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
29 changes: 15 additions & 14 deletions packages/@vue/cli-shared-utils/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,33 @@ exports.hasProjectGit = (cwd) => {
}

let _hasPnpm
let _hasPnpm3orLater
let _pnpmVersion
const _pnpmProjects = new LRU({
max: 10,
maxAge: 1000
})

exports.hasPnpm3OrLater = () => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_hasPnpm3orLater != null) {
return _hasPnpm3orLater
}
function getPnpmVersion () {
try {
const pnpmVersion = execSync('pnpm --version', {
_pnpmVersion = execSync('pnpm --version', {
stdio: ['pipe', 'pipe', 'ignore']
}).toString()
// there's a critical bug in pnpm 2
// https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972
// so we only support pnpm >= 3.0.0
_hasPnpm = true
_hasPnpm3orLater = semver.gte(pnpmVersion, '3.0.0')
return _hasPnpm3orLater
} catch (e) {
return (_hasPnpm3orLater = false)
} catch (e) {}
return _pnpmVersion || '0.0.0'
}

exports.hasPnpmXOrLater = (version) => {
if (process.env.VUE_CLI_TEST) {
return true
}
if (_pnpmVersion != null) {
return semver.gte(_pnpmVersion, version)
}
return semver.gte(getPnpmVersion(), version)
}

exports.hasProjectPnpm = (cwd) => {
Expand All @@ -119,7 +120,7 @@ exports.hasProjectPnpm = (cwd) => {
}

function checkPnpm (result) {
if (result && !exports.hasPnpm3OrLater()) {
if (result && !exports.hasPnpmXOrLater('3.0.0')) {
throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`)
}
return result
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli-ui/apollo-server/util/command.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const {
hasYarn,
hasProjectYarn,
hasPnpm3OrLater,
hasPnpmXOrLater,
hasProjectPnpm
} = require('@vue/cli-shared-utils')
const { loadOptions } = require('@vue/cli/lib/options')

exports.getCommand = function (cwd = undefined) {
if (!cwd) {
return loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpm3OrLater() ? 'pnpm' : 'npm')
return loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpmXOrLater('3.0.0') ? 'pnpm' : 'npm')
}
return hasProjectYarn(cwd) ? 'yarn' : hasProjectPnpm(cwd) ? 'pnpm' : 'npm'
}
8 changes: 4 additions & 4 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const {
hasGit,
hasProjectGit,
hasYarn,
hasPnpm3OrLater,
hasPnpmXOrLater,
logWithSpinner,
stopSpinner,
exit,
Expand Down Expand Up @@ -116,7 +116,7 @@ module.exports = class Creator extends EventEmitter {
cliOptions.packageManager ||
loadOptions().packageManager ||
(hasYarn() ? 'yarn' : null) ||
(hasPnpm3OrLater() ? 'pnpm' : 'npm')
(hasPnpmXOrLater('3.0.0') ? 'pnpm' : 'npm')
)
const pm = new PackageManager({ context, forcePackageManager: packageManager })

Expand Down Expand Up @@ -448,7 +448,7 @@ module.exports = class Creator extends EventEmitter {

// ask for packageManager once
const savedOptions = loadOptions()
if (!savedOptions.packageManager && (hasYarn() || hasPnpm3OrLater())) {
if (!savedOptions.packageManager && (hasYarn() || hasPnpmXOrLater('3.0.0'))) {
const packageManagerChoices = []

if (hasYarn()) {
Expand All @@ -459,7 +459,7 @@ module.exports = class Creator extends EventEmitter {
})
}

if (hasPnpm3OrLater()) {
if (hasPnpmXOrLater('3.0.0')) {
packageManagerChoices.push({
name: 'Use PNPM',
value: 'pnpm',
Expand Down
23 changes: 15 additions & 8 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const chalk = require('chalk')
const {
hasYarn,
hasProjectYarn,
hasPnpm3OrLater,
hasPnpmXOrLater,
hasProjectPnpm
} = require('@vue/cli-shared-utils/lib/env')
const { isOfficialPlugin, resolvePluginId } = require('@vue/cli-shared-utils/lib/pluginResolution')
Expand All @@ -32,19 +32,26 @@ const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG

const TAOBAO_DIST_URL = 'https://npm.taobao.org/dist'
const SUPPORTED_PACKAGE_MANAGERS = ['yarn', 'pnpm', 'npm']
const PACKAGE_MANAGER_PNPM4_CONFIG = {
install: ['install', '--reporter', 'silent', '--shamefully-hoist'],
add: ['install', '--reporter', 'silent', '--shamefully-hoist'],
upgrade: ['update', '--reporter', 'silent'],
remove: ['uninstall', '--reporter', 'silent']
}
const PACKAGE_MANAGER_PNPM3_CONFIG = {
install: ['install', '--loglevel', 'error', '--shamefully-flatten'],
add: ['install', '--loglevel', 'error', '--shamefully-flatten'],
upgrade: ['update', '--loglevel', 'error'],
remove: ['uninstall', '--loglevel', 'error']
}
const PACKAGE_MANAGER_CONFIG = {
npm: {
install: ['install', '--loglevel', 'error'],
add: ['install', '--loglevel', 'error'],
upgrade: ['update', '--loglevel', 'error'],
remove: ['uninstall', '--loglevel', 'error']
},
pnpm: {
install: ['install', '--loglevel', 'error', '--shamefully-flatten'],
add: ['install', '--loglevel', 'error', '--shamefully-flatten'],
upgrade: ['update', '--loglevel', 'error'],
remove: ['uninstall', '--loglevel', 'error']
},
pnpm: hasPnpmXOrLater('4.0.0') ? PACKAGE_MANAGER_PNPM4_CONFIG : PACKAGE_MANAGER_PNPM3_CONFIG,
yarn: {
install: [],
add: ['add'],
Expand Down Expand Up @@ -74,7 +81,7 @@ class PackageManager {
} else if (context) {
this.bin = hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm'
} else {
this.bin = loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpm3OrLater() ? 'pnpm' : 'npm')
this.bin = loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpmXOrLater('3.0.0') ? 'pnpm' : 'npm')
}

if (!SUPPORTED_PACKAGE_MANAGERS.includes(this.bin)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/util/clearConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const chalk = require('chalk')
const execa = require('execa')
const semver = require('semver')
const getVersions = require('./getVersions')
const { clearConsole, hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
const { clearConsole, hasYarn, hasPnpmXOrLater } = require('@vue/cli-shared-utils')

async function getInstallationCommand () {
if (hasYarn()) {
Expand All @@ -12,7 +12,7 @@ async function getInstallationCommand () {
}
}

if (hasPnpm3OrLater()) {
if (hasPnpmXOrLater('3.0.0')) {
const { stdout: pnpmGlobalPrefix } = await execa('pnpm', ['config', 'get', 'prefix'])
if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
return `pnpm i -g`
Expand Down
4 changes: 2 additions & 2 deletions packages/@vue/cli/lib/util/loadCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ module.exports = function loadCommand (commandName, moduleName) {
} catch (err2) {
if (isNotFoundError(err2)) {
const chalk = require('chalk')
const { hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
const { hasYarn, hasPnpmXOrLater } = require('@vue/cli-shared-utils')
let installCommand = `npm install -g`
if (hasYarn()) {
installCommand = `yarn global add`
} else if (hasPnpm3OrLater()) {
} else if (hasPnpmXOrLater('3.0.0')) {
installCommand = `pnpm install -g`
}
console.log()
Expand Down