Skip to content

Commit

Permalink
fix: prefer pnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenQingchuan committed May 3, 2024
1 parent 97e76cd commit 4cb23fd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/create-vue-vine/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getTemplateDirectory, renderTemplate } from './utils'

export interface ProjectOptions {
path: string
name: string // TODO
name: string
templateDir: string

templates: string[]
Expand Down
52 changes: 43 additions & 9 deletions packages/create-vue-vine/src/utils/pm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
import process from 'node:process'
import { execa } from 'execa'
import { execa, execaSync } from 'execa'

const userAgent = process.env.npm_config_user_agent ?? ''
const userAgentEnv = process.env.npm_config_user_agent ?? ''

const packageManager = /pnpm/.test(userAgent)
? 'pnpm'
: /yarn/.test(userAgent)
? /bun/.test(userAgent)
? 'bun'
: 'yarn'
: 'npm'
function detectPackageManager() {
const fromUserAgent = /pnpm/.test(userAgentEnv)
? 'pnpm'
: /yarn/.test(userAgentEnv)
? /bun/.test(userAgentEnv)
? 'bun'
: 'yarn'
: undefined

if (fromUserAgent)
return fromUserAgent

let pnpmVersionExitCode: number,
yarnVersionExitCode: number

// Run 'pnpm --version' to check if pnpm is installed
try {
pnpmVersionExitCode = execaSync('pnpm', ['--version'], { stdio: 'ignore' }).exitCode
}
catch (error) {
pnpmVersionExitCode = -1
}
// Run 'yarn --version' to check if yarn is installed
try {
yarnVersionExitCode = execaSync('yarn', ['--version'], { stdio: 'ignore' }).exitCode
}
catch (error) {
yarnVersionExitCode = -1
}

if (pnpmVersionExitCode === 0) {
return 'pnpm'
}
else if (yarnVersionExitCode === 0) {
return 'yarn'
}

return 'npm'
}

const packageManager = detectPackageManager()

type Command = 'install' | 'dev'

Expand Down

0 comments on commit 4cb23fd

Please sign in to comment.