Skip to content

Commit 7a3f5ed

Browse files
authored
feat: allow checkPlatform to override execution environment (#65)
This PR allows `checkPlatform` to override execution environment which comes from `process.platform` and `process.arch` by default. This will be required to achieve npm/rfcs#612
1 parent 5965ee3 commit 7a3f5ed

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ npm is unable to install the package properly for some reason.
1919

2020
Error code: 'EBADENGINE'
2121

22-
### .checkPlatform(pkg, force)
22+
### .checkPlatform(pkg, force, environment)
2323

2424
Check if a package's `os`, `cpu` and `libc` match the running system.
2525

2626
`force` argument skips all checks.
2727

28+
`environment` overrides the execution environment which comes from `process.platform` and `process.arch` by default. `environment.os` and `environment.cpu` are available.
29+
2830
Error code: 'EBADPLATFORM'

lib/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ const checkEngine = (target, npmVer, nodeVer, force = false) => {
2222

2323
const isMusl = (file) => file.includes('libc.musl-') || file.includes('ld-musl-')
2424

25-
const checkPlatform = (target, force = false) => {
25+
const checkPlatform = (target, force = false, environment = {}) => {
2626
if (force) {
2727
return
2828
}
2929

30-
const platform = process.platform
31-
const arch = process.arch
30+
const platform = environment.os || process.platform
31+
const arch = environment.cpu || process.arch
3232
const osOk = target.os ? checkList(platform, target.os) : true
3333
const cpuOk = target.cpu ? checkList(arch, target.cpu) : true
3434

test/check-platform.js

+32
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ t.test('os wrong (negation)', async t =>
4343
t.test('nothing wrong (negation)', async t =>
4444
checkPlatform({ cpu: '!enten-cpu', os: '!enten-os' }))
4545

46+
t.test('nothing wrong with overridden os', async t =>
47+
checkPlatform({
48+
cpu: 'any',
49+
os: 'enten-os',
50+
}, false, {
51+
os: 'enten-os',
52+
}))
53+
54+
t.test('nothing wrong with overridden cpu', async t =>
55+
checkPlatform({
56+
cpu: 'enten-cpu',
57+
os: 'any',
58+
}, false, {
59+
cpu: 'enten-cpu',
60+
}))
61+
62+
t.test('wrong os with overridden os', async t =>
63+
t.throws(() => checkPlatform({
64+
cpu: 'any',
65+
os: 'enten-os',
66+
}, false, {
67+
os: 'another-os',
68+
}), { code: 'EBADPLATFORM' }))
69+
70+
t.test('wrong cpu with overridden cpu', async t =>
71+
t.throws(() => checkPlatform({
72+
cpu: 'enten-cpu',
73+
os: 'any',
74+
}, false, {
75+
cpu: 'another-cpu',
76+
}), { code: 'EBADPLATFORM' }))
77+
4678
t.test('libc', (t) => {
4779
let PLATFORM = ''
4880

0 commit comments

Comments
 (0)