diff --git a/README.md b/README.md index 25ed222..828b007 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,12 @@ npm is unable to install the package properly for some reason. Error code: 'EBADENGINE' -### .checkPlatform(pkg, force) +### .checkPlatform(pkg, force, environment) Check if a package's `os`, `cpu` and `libc` match the running system. `force` argument skips all checks. +`environment` overrides the execution environment which comes from `process.platform` and `process.arch` by default. `environment.os` and `environment.cpu` are available. + Error code: 'EBADPLATFORM' diff --git a/lib/index.js b/lib/index.js index fa5f593..f0ba2c0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,13 +22,13 @@ const checkEngine = (target, npmVer, nodeVer, force = false) => { const isMusl = (file) => file.includes('libc.musl-') || file.includes('ld-musl-') -const checkPlatform = (target, force = false) => { +const checkPlatform = (target, force = false, environment = {}) => { if (force) { return } - const platform = process.platform - const arch = process.arch + const platform = environment.os || process.platform + const arch = environment.cpu || process.arch const osOk = target.os ? checkList(platform, target.os) : true const cpuOk = target.cpu ? checkList(arch, target.cpu) : true diff --git a/test/check-platform.js b/test/check-platform.js index 48af678..a84b965 100644 --- a/test/check-platform.js +++ b/test/check-platform.js @@ -43,6 +43,38 @@ t.test('os wrong (negation)', async t => t.test('nothing wrong (negation)', async t => checkPlatform({ cpu: '!enten-cpu', os: '!enten-os' })) +t.test('nothing wrong with overridden os', async t => + checkPlatform({ + cpu: 'any', + os: 'enten-os', + }, false, { + os: 'enten-os', + })) + +t.test('nothing wrong with overridden cpu', async t => + checkPlatform({ + cpu: 'enten-cpu', + os: 'any', + }, false, { + cpu: 'enten-cpu', + })) + +t.test('wrong os with overridden os', async t => + t.throws(() => checkPlatform({ + cpu: 'any', + os: 'enten-os', + }, false, { + os: 'another-os', + }), { code: 'EBADPLATFORM' })) + +t.test('wrong cpu with overridden cpu', async t => + t.throws(() => checkPlatform({ + cpu: 'enten-cpu', + os: 'any', + }, false, { + cpu: 'another-cpu', + }), { code: 'EBADPLATFORM' })) + t.test('libc', (t) => { let PLATFORM = ''