From 7530e75ee91ed01cb43c39c7171781a500b6c770 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Sat, 5 Aug 2023 08:57:09 +0900 Subject: [PATCH 1/5] allow checkPlatform to override the execution environment --- README.md | 6 +++++- lib/index.js | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 25ed222..c8f057f 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,14 @@ 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. + +```js + Error code: 'EBADPLATFORM' diff --git a/lib/index.js b/lib/index.js index fa5f593..394fbb5 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 && environment.os ? environment.os : process.platform + const arch = environment && environment.cpu ? environment.cpu : process.arch const osOk = target.os ? checkList(platform, target.os) : true const cpuOk = target.cpu ? checkList(arch, target.cpu) : true From ff74213930e38cbbec6454a9e044de480cdbec8e Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Sat, 5 Aug 2023 09:00:19 +0900 Subject: [PATCH 2/5] remove needless comment --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index c8f057f..828b007 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,4 @@ Check if a package's `os`, `cpu` and `libc` match the running system. `environment` overrides the execution environment which comes from `process.platform` and `process.arch` by default. `environment.os` and `environment.cpu` are available. -```js - Error code: 'EBADPLATFORM' From d9b4234687c6971b2c8d0f6d79460d31a2b49608 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Tue, 8 Aug 2023 00:16:52 +0900 Subject: [PATCH 3/5] add test --- test/check-platform.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/check-platform.js b/test/check-platform.js index 48af678..231cca6 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', + }), { code: 'EBADPLATFORM' }) + +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 = '' From 76b0df674da9499660e1b72eb41a05dbfefaf1e3 Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Tue, 8 Aug 2023 00:38:38 +0900 Subject: [PATCH 4/5] remove needless typeguard --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 394fbb5..f0ba2c0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -27,8 +27,8 @@ const checkPlatform = (target, force = false, environment = {}) => { return } - const platform = environment && environment.os ? environment.os : process.platform - const arch = environment && environment.cpu ? environment.cpu : 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 From c5734ed45344a9b99bdaf847e6703529d407f6ff Mon Sep 17 00:00:00 2001 From: Yuku Kotani Date: Tue, 8 Aug 2023 01:07:02 +0900 Subject: [PATCH 5/5] Update test/check-platform.js Co-authored-by: Gar --- test/check-platform.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/check-platform.js b/test/check-platform.js index 231cca6..a84b965 100644 --- a/test/check-platform.js +++ b/test/check-platform.js @@ -57,7 +57,7 @@ t.test('nothing wrong with overridden cpu', async t => os: 'any', }, false, { cpu: 'enten-cpu', - }), { code: 'EBADPLATFORM' }) + })) t.test('wrong os with overridden os', async t => t.throws(() => checkPlatform({