Skip to content

Commit b616eb3

Browse files
authoredNov 9, 2023
feat: support detect the range version of packageManager (#179)
1 parent c200dc2 commit b616eb3

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed
 

‎src/config.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import process from 'node:process'
44
import ini from 'ini'
5-
import { findUp } from 'find-up'
65
import type { Agent } from './agents'
7-
import { LOCKS } from './agents'
6+
import { detect } from './detect'
87

98
const customRcPath = process.env.NI_CONFIG_FILE
109

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

3130
export async function getConfig(): Promise<Config> {
3231
if (!config) {
33-
const result = await findUp('package.json') || ''
34-
let packageManager = ''
35-
if (result)
36-
packageManager = JSON.parse(fs.readFileSync(result, 'utf8')).packageManager ?? ''
37-
const [, agent, version] = packageManager.match(new RegExp(`^(${Object.values(LOCKS).join('|')})@(\\d).*?$`)) || []
32+
const agent = await detect({ programmatic: true })
3833
if (agent)
39-
config = Object.assign({}, defaultConfig, { defaultAgent: (agent === 'yarn' && Number.parseInt(version) > 1) ? 'yarn@berry' : agent })
40-
else if (!fs.existsSync(rcPath))
41-
config = defaultConfig
34+
config = { ...defaultConfig, defaultAgent: agent }
4235
else
4336
config = Object.assign({}, defaultConfig, ini.parse(fs.readFileSync(rcPath, 'utf-8')))
4437
}
38+
4539
return config
4640
}
4741

‎src/detect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function detect({ autoInstall, programmatic, cwd }: DetectOptions =
3232
try {
3333
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
3434
if (typeof pkg.packageManager === 'string') {
35-
const [name, ver] = pkg.packageManager.split('@')
35+
const [name, ver] = pkg.packageManager.replace(/^\^/, '').split('@')
3636
version = ver
3737
if (name === 'yarn' && Number.parseInt(ver) > 1)
3838
agent = 'yarn@berry'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"packageManager": "^pnpm@8.0.0"
3+
}

0 commit comments

Comments
 (0)
Please sign in to comment.