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

fix(create-vite): distinguish pnpm pkgManager #4220

Merged
merged 9 commits into from
Jul 30, 2021
23 changes: 20 additions & 3 deletions packages/create-vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,17 @@ async function init() {

write('package.json', JSON.stringify(pkg, null, 2))

const pkgManager = /yarn/.test(process.env.npm_execpath) ? 'yarn' : 'npm'
const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent)
const pkgManager = pkgInfo ? pkgInfo.name : 'npm'
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved

console.log(`\nDone. Now run:\n`)
if (root !== cwd) {
console.log(` cd ${path.relative(cwd, root)}`)
}
console.log(` ${pkgManager === 'yarn' ? `yarn` : `npm install`}`)
console.log(` ${pkgManager === 'yarn' ? `yarn dev` : `npm run dev`}`)
console.log(` ${pkgManager === 'yarn' ? `yarn` : `${pkgManager} install`}`)
console.log(
` ${pkgManager === 'yarn' ? `yarn dev` : `${pkgManager} run dev`}`
)
1wkk marked this conversation as resolved.
Show resolved Hide resolved
console.log()
}

Expand Down Expand Up @@ -318,6 +321,20 @@ function emptyDir(dir) {
}
}

/**
* @param {string | undefined} userAgent process.env.npm_config_user_agent
* @returns object | undefined
*/
function pkgFromUserAgent(userAgent) {
if (!userAgent) return undefined
const pkgSpec = userAgent.split(' ')[0]
const pkgSpecArr = pkgSpec.split('/')
return {
name: pkgSpecArr[0],
version: pkgSpecArr[1]
}
}

init().catch((e) => {
console.error(e)
})