Skip to content

Commit

Permalink
feat: Add --cpu and --os option to override platform specific ins…
Browse files Browse the repository at this point in the history
…tall (#6755)
  • Loading branch information
yukukotani authored Sep 8, 2023
1 parent 7bf2374 commit 1c93c44
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Install extends ArboristWorkspaceCmd {
'bin-links',
'fund',
'dry-run',
'cpu',
'os',
...super.params,
]

Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/lib/commands/config.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
"cidr": null,
"color": true,
"commit-hooks": true,
"cpu": null,
"os": null,
"depth": null,
"description": true,
"dev": false,
Expand Down Expand Up @@ -187,6 +189,7 @@ cert = null
cidr = null
color = true
commit-hooks = true
cpu = null
depth = null
description = true
dev = false
Expand Down Expand Up @@ -258,6 +261,7 @@ omit = []
omit-lockfile-registry-resolved = false
only = null
optional = null
os = null
otp = null
pack-destination = "."
package = []
Expand Down
34 changes: 32 additions & 2 deletions tap-snapshots/test/lib/docs.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ Run git commit hooks when using the \`npm version\` command.
#### \`cpu\`
* Default: null
* Type: null or String
Override CPU architecture of native modules to install. Acceptable values
are same as \`cpu\` field of package.json, which comes from \`process.arch\`.
#### \`depth\`
* Default: \`Infinity\` if \`--all\` is set, otherwise \`1\`
Expand Down Expand Up @@ -1085,6 +1095,16 @@ time.
#### \`os\`
* Default: null
* Type: null or String
Override OS of native modules to install. Acceptable values are same as \`os\`
field of package.json, which comes from \`process.platform\`.
#### \`otp\`
* Default: null
Expand Down Expand Up @@ -2006,6 +2026,8 @@ Array [
"cidr",
"color",
"commit-hooks",
"cpu",
"os",
"depth",
"description",
"dev",
Expand Down Expand Up @@ -2159,6 +2181,8 @@ Array [
"cidr",
"color",
"commit-hooks",
"cpu",
"os",
"depth",
"description",
"dev",
Expand Down Expand Up @@ -2313,6 +2337,7 @@ Object {
"cidr": null,
"color": false,
"commitHooks": true,
"cpu": null,
"defaultTag": "latest",
"depth": null,
"diff": Array [],
Expand Down Expand Up @@ -2361,6 +2386,7 @@ Object {
"offline": false,
"omit": Array [],
"omitLockfileRegistryResolved": false,
"os": null,
"otp": null,
"package": Array [],
"packageLock": true,
Expand Down Expand Up @@ -3170,7 +3196,7 @@ Options:
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
[--no-fund] [--dry-run]
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces] [--include-workspace-root] [--install-links]
Expand Down Expand Up @@ -3201,6 +3227,8 @@ aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall
#### \`bin-links\`
#### \`fund\`
#### \`dry-run\`
#### \`cpu\`
#### \`os\`
#### \`workspace\`
#### \`workspaces\`
#### \`include-workspace-root\`
Expand Down Expand Up @@ -3261,7 +3289,7 @@ Options:
[--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
[--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
[--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
[--no-fund] [--dry-run]
[--no-fund] [--dry-run] [--cpu <cpu>] [--os <os>]
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces] [--include-workspace-root] [--install-links]
Expand Down Expand Up @@ -3292,6 +3320,8 @@ alias: it
#### \`bin-links\`
#### \`fund\`
#### \`dry-run\`
#### \`cpu\`
#### \`os\`
#### \`workspace\`
#### \`workspaces\`
#### \`include-workspace-root\`
Expand Down
4 changes: 2 additions & 2 deletions workspaces/arborist/lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ module.exports = cls => class Reifier extends cls {
process.emit('time', timer)
this.addTracker('reify', node.name, node.location)

const { npmVersion, nodeVersion } = this.options
const { npmVersion, nodeVersion, cpu, os } = this.options
const p = Promise.resolve().then(async () => {
// when we reify an optional node, check the engine and platform
// first. be sure to ignore the --force and --engine-strict flags,
Expand All @@ -638,7 +638,7 @@ module.exports = cls => class Reifier extends cls {
// eslint-disable-next-line promise/always-return
if (node.optional) {
checkEngine(node.package, npmVersion, nodeVersion, false)
checkPlatform(node.package, false)
checkPlatform(node.package, false, { cpu, os })
}
await this[_checkBins](node)
await this[_extractOrLink](node)
Expand Down
75 changes: 75 additions & 0 deletions workspaces/arborist/tap-snapshots/test/arborist/reify.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3161,6 +3161,44 @@ ArboristNode {
}
`

exports[`test/arborist/reify.js TAP fail to install optional deps with matched os and mismatched cpu with os and cpu options > expect resolving Promise 1`] = `
ArboristNode {
"edgesOut": Map {
"platform-specifying-test-package" => EdgeOut {
"name": "platform-specifying-test-package",
"spec": "1.0.0",
"to": null,
"type": "optional",
},
},
"isProjectRoot": true,
"location": "",
"name": "tap-testdir-reify-fail-to-install-optional-deps-with-matched-os-and-mismatched-cpu-with-os-and-cpu-options",
"packageName": "platform-test",
"path": "{CWD}/test/arborist/tap-testdir-reify-fail-to-install-optional-deps-with-matched-os-and-mismatched-cpu-with-os-and-cpu-options",
"version": "1.0.0",
}
`

exports[`test/arborist/reify.js TAP fail to install optional deps with mismatched os and matched cpu with os and cpu options > expect resolving Promise 1`] = `
ArboristNode {
"edgesOut": Map {
"platform-specifying-test-package" => EdgeOut {
"name": "platform-specifying-test-package",
"spec": "1.0.0",
"to": null,
"type": "optional",
},
},
"isProjectRoot": true,
"location": "",
"name": "tap-testdir-reify-fail-to-install-optional-deps-with-mismatched-os-and-matched-cpu-with-os-and-cpu-options",
"packageName": "platform-test",
"path": "{CWD}/test/arborist/tap-testdir-reify-fail-to-install-optional-deps-with-mismatched-os-and-matched-cpu-with-os-and-cpu-options",
"version": "1.0.0",
}
`

exports[`test/arborist/reify.js TAP failing script means install failure, unless ignoreScripts prod-dep-allinstall-fail --ignore-scripts > expect resolving Promise 1`] = `
ArboristNode {
"children": Map {
Expand Down Expand Up @@ -32993,6 +33031,43 @@ exports[`test/arborist/reify.js TAP store files with a custom indenting > must m

`

exports[`test/arborist/reify.js TAP success to install optional deps with matched platform specifications with os and cpu options > expect resolving Promise 1`] = `
ArboristNode {
"children": Map {
"platform-specifying-test-package" => ArboristNode {
"edgesIn": Set {
EdgeIn {
"from": "",
"name": "platform-specifying-test-package",
"spec": "1.0.0",
"type": "optional",
},
},
"location": "node_modules/platform-specifying-test-package",
"name": "platform-specifying-test-package",
"optional": true,
"path": "{CWD}/test/arborist/tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options/node_modules/platform-specifying-test-package",
"resolved": "https://registry.npmjs.org/platform-specifying-test-package/-/platform-specifying-test-package-1.0.0.tgz",
"version": "1.0.0",
},
},
"edgesOut": Map {
"platform-specifying-test-package" => EdgeOut {
"name": "platform-specifying-test-package",
"spec": "1.0.0",
"to": "node_modules/platform-specifying-test-package",
"type": "optional",
},
},
"isProjectRoot": true,
"location": "",
"name": "tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options",
"packageName": "platform-test",
"path": "{CWD}/test/arborist/tap-testdir-reify-success-to-install-optional-deps-with-matched-platform-specifications-with-os-and-cpu-options",
"version": "1.0.0",
}
`

exports[`test/arborist/reify.js TAP tarball deps with transitive tarball deps > expect resolving Promise 1`] = `
ArboristNode {
"children": Map {
Expand Down
12 changes: 12 additions & 0 deletions workspaces/arborist/test/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,18 @@ t.test('still do not install optional deps with mismatched platform specificatio
t.test('fail to install deps with mismatched platform specifications', t =>
t.rejects(printReified(fixture(t, 'platform-specification')), { code: 'EBADPLATFORM' }))

t.test('success to install optional deps with matched platform specifications with os and cpu options', t =>
t.resolveMatchSnapshot(printReified(
fixture(t, 'optional-platform-specification'), { os: 'not-your-os', cpu: 'not-your-cpu' })))

t.test('fail to install optional deps with matched os and mismatched cpu with os and cpu options', t =>
t.resolveMatchSnapshot(printReified(
fixture(t, 'optional-platform-specification'), { os: 'not-your-os', cpu: 'another-cpu' })))

t.test('fail to install optional deps with mismatched os and matched cpu with os and cpu options', t =>
t.resolveMatchSnapshot(printReified(
fixture(t, 'optional-platform-specification'), { os: 'another-os', cpu: 'not-your-cpu' })))

t.test('dry run, do not get anything wet', async t => {
const cases = [
'shrinkwrapped-dep-with-lock-empty',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"os": [
"not-your-os"
],
"cpu": [
"not-your-cpu"
],
"_id": "[email protected]",
"_nodeVersion": "12.18.4",
"_npmVersion": "6.14.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
"os": [
"not-your-os"
],
"cpu": [
"not-your-cpu"
]
}
},
Expand Down
22 changes: 22 additions & 0 deletions workspaces/config/lib/definitions/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,28 @@ define('commit-hooks', {
flatten,
})

define('cpu', {
default: null,
type: [null, String],
description: `
Override CPU architecture of native modules to install.
Acceptable values are same as \`cpu\` field of package.json,
which comes from \`process.arch\`.
`,
flatten,
})

define('os', {
default: null,
type: [null, String],
description: `
Override OS of native modules to install.
Acceptable values are same as \`os\` field of package.json,
which comes from \`process.platform\`.
`,
flatten,
})

define('depth', {
default: null,
defaultDescription: `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Object {
"commit-hooks": Array [
"boolean value (true or false)",
],
"cpu": Array [
null,
Function String(),
],
"depth": Array [
null,
"numeric value",
Expand Down Expand Up @@ -335,6 +339,10 @@ Object {
null,
"boolean value (true or false)",
],
"os": Array [
null,
Function String(),
],
"otp": Array [
null,
Function String(),
Expand Down

0 comments on commit 1c93c44

Please sign in to comment.