From c0a3dbfa66266378f6f329f1981d5827b781fdbb Mon Sep 17 00:00:00 2001 From: Ryan Moyo Date: Fri, 30 Jul 2021 21:08:45 +0800 Subject: [PATCH] fix(create-vite): distinguish pnpm pkgManager (#4220) --- packages/create-vite/index.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/create-vite/index.js b/packages/create-vite/index.js index aeadcc0a8bd4e5..4c5879bdd0bd12 100755 --- a/packages/create-vite/index.js +++ b/packages/create-vite/index.js @@ -254,14 +254,23 @@ 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' 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`}`) + switch (pkgManager) { + case 'yarn': + console.log(' yarn') + console.log(' yarn dev') + break + default: + console.log(` ${pkgManager} install`) + console.log(` ${pkgManager} run dev`) + break + } console.log() } @@ -318,6 +327,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) })