codekingpro/portable-devtools
114k
1const semver = require('semver')
2const currentEnv = require('./current-env')
3const { checkDevEngines } = require('./dev-engines')
4
5const checkEngine = (target, npmVer, nodeVer, force = false) => {
6 const nodev = force ? null : nodeVer
7 const eng = target.engines
8 const opt = { includePrerelease: true }
9 if (!eng) {
10 return
11 }
12
13 const nodeFail = nodev && eng.node && !semver.satisfies(nodev, eng.node, opt)
14 const npmFail = npmVer && eng.npm && !semver.satisfies(npmVer, eng.npm, opt)
15 if (nodeFail || npmFail) {
16 throw Object.assign(new Error('Unsupported engine'), {
17 pkgid: target._id,
18 current: { node: nodeVer, npm: npmVer },
19 required: eng,
20 code: 'EBADENGINE',
21 })
22 }
23}
24
25const checkPlatform = (target, force = false, environment = {}) => {
26 if (force) {
27 return
28 }
29
30 const os = environment.os || currentEnv.os()
31 const cpu = environment.cpu || currentEnv.cpu()
32 const libc = environment.libc || currentEnv.libc(os)
33
34 const osOk = target.os ? checkList(os, target.os) : true
35 const cpuOk = target.cpu ? checkList(cpu, target.cpu) : true
36 let libcOk = target.libc ? checkList(libc, target.libc) : true
37 if (target.libc && !libc) {
38 libcOk = false
39 }
40
41 if (!osOk || !cpuOk || !libcOk) {
42 throw Object.assign(new Error('Unsupported platform'), {
43 pkgid: target._id,
44 current: {
45 os,
46 cpu,
47 libc,
48 },
49 required: {
50 os: target.os,
51 cpu: target.cpu,
52 libc: target.libc,
53 },
54 code: 'EBADPLATFORM',
55 })
56 }
57}
58
59const checkList = (value, list) => {
60 if (typeof list === 'string') {
61 list = [list]
62 }
63 if (list.length === 1 && list[0] === 'any') {
64 return true
65 }
66 // match none of the negated values, and at least one of the
67 // non-negated values, if any are present.
68 let negated = 0
69 let match = false
70 for (const entry of list) {
71 const negate = entry.charAt(0) === '!'
72 const test = negate ? entry.slice(1) : entry
73 if (negate) {
74 negated++
75 if (value === test) {
76 return false
77 }
78 } else {
79 match = match || value === test
80 }
81 }
82 return match || negated === list.length
83}
84
85module.exports = {
86 checkEngine,
87 checkPlatform,
88 checkDevEngines,
89 currentEnv,
90}
91 