Skip to content

Commit

Permalink
feat: support detect the range version of packageManager (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Nov 9, 2023
1 parent c200dc2 commit b616eb3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
14 changes: 4 additions & 10 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import ini from 'ini'
import { findUp } from 'find-up'
import type { Agent } from './agents'
import { LOCKS } from './agents'
import { detect } from './detect'

const customRcPath = process.env.NI_CONFIG_FILE

Expand All @@ -30,18 +29,13 @@ let config: Config | undefined

export async function getConfig(): Promise<Config> {
if (!config) {
const result = await findUp('package.json') || ''
let packageManager = ''
if (result)
packageManager = JSON.parse(fs.readFileSync(result, 'utf8')).packageManager ?? ''
const [, agent, version] = packageManager.match(new RegExp(`^(${Object.values(LOCKS).join('|')})@(\\d).*?$`)) || []
const agent = await detect({ programmatic: true })
if (agent)
config = Object.assign({}, defaultConfig, { defaultAgent: (agent === 'yarn' && Number.parseInt(version) > 1) ? 'yarn@berry' : agent })
else if (!fs.existsSync(rcPath))
config = defaultConfig
config = { ...defaultConfig, defaultAgent: agent }
else
config = Object.assign({}, defaultConfig, ini.parse(fs.readFileSync(rcPath, 'utf-8')))
}

return config
}

Expand Down
2 changes: 1 addition & 1 deletion src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function detect({ autoInstall, programmatic, cwd }: DetectOptions =
try {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
if (typeof pkg.packageManager === 'string') {
const [name, ver] = pkg.packageManager.split('@')
const [name, ver] = pkg.packageManager.replace(/^\^/, '').split('@')
version = ver
if (name === 'yarn' && Number.parseInt(ver) > 1)
agent = 'yarn@berry'
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/packager/pnpm-version-range/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"packageManager": "^pnpm@8.0.0"
}

0 comments on commit b616eb3

Please sign in to comment.