1
+ import { stripIndents } from 'common-tags'
1
2
import chalk from 'chalk'
2
3
import { getPackageJson , lockFileExists } from '../helpers/files.js'
3
4
import packagesList , { packageExists } from '../packages/list.js'
4
5
5
6
const packageName = 'package.json'
6
7
7
- const searchOnPackageJson = async ( ) => {
8
- const packageJson = await getPackageJson ( )
9
- if ( ! packageJson || ! ( 'swpm' in packageJson ) ) {
8
+ const searchForProperty = async ( packageJson , property ) => {
9
+ if ( ! packageJson || ! ( property in packageJson ) ) {
10
10
return undefined
11
11
}
12
12
13
- const pinned = packageJson ?. swpm
14
- if ( pinned && packageExists ( pinned ) ) {
15
- return packageJson ?. swpm
13
+ const prop = packageJson ?. [ property ]
14
+ if ( prop && packageExists ( prop ) ) {
15
+ return packageJson ?. [ property ]
16
16
}
17
17
18
- console . log ( `${ chalk . red . bold ( 'Error' ) } : Package Manager (${ chalk . bold ( pinned ) } ) pinned on ${ chalk . bold ( packageName ) } file is not valid.` )
19
- console . log ( `Use ${ chalk . blue . bold ( 'npm --pin <npm|yarn|pnpm>' ) } to fix it.` )
18
+ console . log ( stripIndents `
19
+ ${ chalk . red . bold ( 'Error' ) } : the value in (${ chalk . bold ( prop ) } ) property on ${ chalk . bold ( packageName ) } file is not valid.
20
+ Use ${ chalk . blue . bold ( 'npm --pin <npm|yarn|pnpm>' ) } to fix it.
21
+ ` )
20
22
process . exit ( 1 )
21
23
}
22
24
@@ -32,21 +34,29 @@ const searchForLockFiles = async () => {
32
34
}
33
35
34
36
export const getCurrentPackageManager = async ( ) => {
35
- const pinned = await searchOnPackageJson ( )
37
+ const packageJson = await getPackageJson ( )
38
+
39
+ const pinned = await searchForProperty ( packageJson , 'swpm' )
36
40
if ( pinned ) {
37
41
return pinned
38
42
}
39
43
44
+ // https://nodejs.org/api/corepack.html
45
+ const packageManager = await searchForProperty ( packageJson , 'packageManager' )
46
+ if ( packageManager ) {
47
+ return packageManager
48
+ }
49
+
40
50
const lock = await searchForLockFiles ( )
41
51
if ( lock ) {
42
52
return lock
43
53
}
44
54
45
- console . log ( `
46
- ${ chalk . red . bold ( 'Error' ) } : no Package Manager was found.
55
+ console . log ( stripIndents `
56
+ ${ chalk . red . bold ( 'Error' ) } : no Package Manager was found.
47
57
48
- Please review if the current path has a ${ chalk . bold ( 'package.json' ) } or a ${ chalk . bold ( 'lock' ) } file.
49
- Highly recommend pin a Package Manager with ${ chalk . blue . bold ( 'swpm --pin <npm|yarn|pnpm>' ) } command.
50
- ` )
58
+ Please review if the current path has a ${ chalk . bold ( 'package.json' ) } or a ${ chalk . bold ( 'lock' ) } file.
59
+ Highly recommend pin a Package Manager with ${ chalk . blue . bold ( 'swpm --pin <npm|yarn|pnpm>' ) } command.
60
+ `)
51
61
process . exit ( 1 )
52
62
}
0 commit comments