Skip to content

Commit 073831d

Browse files
committed
feat(scripts): update for pnpm 7
1 parent 3686394 commit 073831d

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

packages/scripts/src/utils/args.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
import { read, argv } from 'argue-cli'
22

3+
const pnpm7 = 7
4+
35
/**
46
* Detect package manager from environment.
57
* @returns Package manager name. 'npm` by default.
68
*/
79
export function detectPackageManager() {
10+
const parsedUa = /(^\w+)(?:\/(\d+)|)/.exec(process.env.npm_config_user_agent)
11+
12+
if (parsedUa) {
13+
const [
14+
,
15+
pm,
16+
ver
17+
] = parsedUa
18+
19+
if (pm) {
20+
if (pm === 'pnpm' && ver && Number(ver) >= pnpm7) {
21+
return 'pnpm >=7'
22+
}
23+
24+
return pm
25+
}
26+
}
27+
828
return (
9-
/^\w+/.exec(process.env.npm_config_user_agent)?.[0]
10-
|| /node_modules[/\\](\w+)[/\\]/.exec(process.env.npm_execpath)?.[1]
29+
/node_modules[/\\](\w+)[/\\]/.exec(process.env.npm_execpath)?.[1]
1130
|| 'npm'
1231
)
1332
}
@@ -19,7 +38,7 @@ export function detectPackageManager() {
1938
* @returns Args to run script with package manager.
2039
*/
2140
export function getRunArgs(pm, args) {
22-
if (pm === 'yarn' || args.length < 2) {
41+
if (pm === 'yarn' || pm === 'pnpm >=7' || args.length < 2) {
2342
return ['run', ...args]
2443
}
2544

@@ -42,7 +61,7 @@ export function getRunArgs(pm, args) {
4261
*/
4362
export function getArgs(pm, args, pkg) {
4463
if (pkg.scripts && (args[0] in pkg.scripts)) {
45-
return [pm, getRunArgs(pm, args)]
64+
return [pm.split(' ')[0], getRunArgs(pm, args)]
4665
}
4766

4867
const [bin, ...restArgs] = args

packages/scripts/src/utils/args.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ describe('scripts', () => {
2222
process.env.npm_execpath = 'node_modules/pnpm/cli.js'
2323
expect(detectPackageManager()).toBe('pnpm')
2424
})
25+
26+
it('should detect pnpm v6', () => {
27+
process.env.npm_config_user_agent = 'pnpm/6.0.0'
28+
process.env.npm_execpath = ''
29+
expect(detectPackageManager()).toBe('pnpm')
30+
})
31+
32+
it('should detect pnpm v7', () => {
33+
process.env.npm_config_user_agent = 'pnpm/7.0.0'
34+
process.env.npm_execpath = ''
35+
expect(detectPackageManager()).toBe('pnpm >=7')
36+
})
2537
})
2638

2739
describe('getRunArgs', () => {
@@ -54,6 +66,16 @@ describe('scripts', () => {
5466
'--fix'
5567
])
5668
})
69+
70+
it('should not add dashes for pnpm >= 7', () => {
71+
expect(
72+
getRunArgs('pnpm >=7', ['lint', '--fix'])
73+
).toEqual([
74+
'run',
75+
'lint',
76+
'--fix'
77+
])
78+
})
5779
})
5880

5981
describe('readScripts', () => {

0 commit comments

Comments
 (0)