Skip to content
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 pkgManager =
pmFromUserAgent(process.env.npm_config_user_agent)?.name || 'npm'
Comment thread
heydong1 marked this conversation as resolved.
Outdated

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`}`
)
Comment thread
heydong1 marked this conversation as resolved.
Outdated
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 pmFromUserAgent(userAgent) {
if (!userAgent) return undefined
const pmSpec = userAgent.split(' ')[0]
const pmSpecArr = pmSpec.split('/')
return {
name: pmSpecArr[0],
version: pmSpecArr[1]
}
}

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