-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
65 lines (64 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const findYarnWorkspaceRoot = require('find-yarn-workspace-root2')
const fs = require('fs')
const path = require('path')
const whichPM = require('which-pm')
module.exports = async function preferredPM (pkgPath) {
if (typeof pkgPath !== 'string') {
throw new TypeError(`pkgPath should be a string, got ${typeof pkgPath}`)
}
if (fs.existsSync(path.join(pkgPath, 'package-lock.json'))) {
return {
name: 'npm',
version: '>=5'
}
}
if (fs.existsSync(path.join(pkgPath, 'yarn.lock'))) {
return {
name: 'yarn',
version: '*'
}
}
if (fs.existsSync(path.join(pkgPath, 'pnpm-lock.yaml'))) {
return {
name: 'pnpm',
version: '>=3'
}
}
if (fs.existsSync(path.join(pkgPath, 'shrinkwrap.yaml'))) {
return {
name: 'pnpm',
version: '1 || 2'
}
}
if (fs.existsSync(path.join(pkgPath, 'bun.lockb'))) {
return {
name: 'bun',
version: '*'
}
}
const { findUp } = await import('find-up-simple')
if (await findUp('pnpm-lock.yaml', { cwd: pkgPath })) {
return {
name: 'pnpm',
version: '>=3'
}
}
try {
const workspaceRoot = findYarnWorkspaceRoot(pkgPath)
if (typeof workspaceRoot === 'string') {
if (fs.existsSync(path.join(workspaceRoot, 'package-lock.json'))) {
return {
name: 'npm',
version: '>=7'
}
}
return {
name: 'yarn',
version: '*'
}
}
} catch (err) {}
const pm = await whichPM(pkgPath)
return pm && { name: pm.name, version: pm.version || '*' }
}