Skip to content

Commit

Permalink
experimental: support package.json#engines for use and install (#…
Browse files Browse the repository at this point in the history
…839)

Co-authored-by: Gal Schlezinger <gal@spitfire.co.il>
  • Loading branch information
amitdahan and Schniz authored Jul 6, 2023
1 parent 30deaf9 commit 97be792
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-parrots-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": minor
---

Support resolving `engines.node` field via experimental `--resolve-engines` flag
91 changes: 91 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
Expand Down Expand Up @@ -112,6 +119,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -162,6 +176,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -222,6 +243,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -282,6 +310,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -347,6 +382,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -402,6 +444,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -459,6 +508,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -513,6 +569,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -569,6 +632,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -619,6 +689,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -681,6 +758,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down Expand Up @@ -737,6 +821,13 @@ Options:
[env: FNM_COREPACK_ENABLED]
--resolve-engines
Resolve `engines.node` field in `package.json` whenever a `.node-version` or `.nvmrc` file is not present.
Experimental: This feature is subject to change.
Note: `engines.node` can be any semver range, with the latest satisfying version being resolved.
[env: FNM_RESOLVE_ENGINES]
-h, --help
Print help (see a summary with '-h')
```
Expand Down
82 changes: 82 additions & 0 deletions e2e/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ if [ "$(node --version)" != "v8.11.3" ]; then
fi"
`;

exports[`Bash package.json engines.node with semver range: Bash 1`] = `
"set -e
eval "$(fnm env --resolve-engines)"
fnm install
fnm use
if [ "$(node --version)" != "v6.17.0" ]; then
echo "Expected node version to be v6.17.0. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash package.json engines.node: Bash 1`] = `
"set -e
eval "$(fnm env --resolve-engines)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Bash resolves partial semver: Bash 1`] = `
"set -e
eval "$(fnm env)"
Expand Down Expand Up @@ -118,6 +140,28 @@ if test "$____test____" != "v8.11.3"
end"
`;

exports[`Fish package.json engines.node with semver range: Fish 1`] = `
"fnm env --resolve-engines | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v6.17.0"
echo "Expected node version to be v6.17.0. Got $____test____"
exit 1
end"
`;

exports[`Fish package.json engines.node: Fish 1`] = `
"fnm env --resolve-engines | source
fnm install
fnm use
set ____test____ (node --version)
if test "$____test____" != "v8.11.3"
echo "Expected node version to be v8.11.3. Got $____test____"
exit 1
end"
`;

exports[`Fish resolves partial semver: Fish 1`] = `
"fnm env | source
fnm install 6
Expand Down Expand Up @@ -182,6 +226,22 @@ fnm use v8.11.3
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell package.json engines.node with semver range: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env --resolve-engines | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v6.17.0" ) { echo "Expected node version to be v6.17.0. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell package.json engines.node: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env --resolve-engines | Out-String | Invoke-Expression
fnm install
fnm use
if ( "$(node --version)" -ne "v8.11.3" ) { echo "Expected node version to be v8.11.3. Got $(node --version)"; exit 1 }"
`;

exports[`PowerShell resolves partial semver: PowerShell 1`] = `
"$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
Expand Down Expand Up @@ -249,6 +309,28 @@ if [ "$(node --version)" != "v8.11.3" ]; then
fi"
`;

exports[`Zsh package.json engines.node with semver range: Zsh 1`] = `
"set -e
eval "$(fnm env --resolve-engines)"
fnm install
fnm use
if [ "$(node --version)" != "v6.17.0" ]; then
echo "Expected node version to be v6.17.0. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh package.json engines.node: Zsh 1`] = `
"set -e
eval "$(fnm env --resolve-engines)"
fnm install
fnm use
if [ "$(node --version)" != "v8.11.3" ]; then
echo "Expected node version to be v8.11.3. Got $(node --version)"
exit 1
fi"
`;

exports[`Zsh resolves partial semver: Zsh 1`] = `
"set -e
eval "$(fnm env)"
Expand Down
28 changes: 28 additions & 0 deletions e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
.execute(shell)
})

test(`package.json engines.node`, async () => {
await writeFile(
join(testCwd(), "package.json"),
JSON.stringify({ engines: { node: "8.11.3" } })
)
await script(shell)
.then(shell.env({ resolveEngines: true }))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v8.11.3"))
.takeSnapshot(shell)
.execute(shell)
})

test(`package.json engines.node with semver range`, async () => {
await writeFile(
join(testCwd(), "package.json"),
JSON.stringify({ engines: { node: "^6 < 6.17.1" } })
)
await script(shell)
.then(shell.env({ resolveEngines: true }))
.then(shell.call("fnm", ["install"]))
.then(shell.call("fnm", ["use"]))
.then(testNodeVersion(shell, "v6.17.0"))
.takeSnapshot(shell)
.execute(shell)
})

test(`use on cd`, async () => {
await mkdir(join(testCwd(), "subdir"), { recursive: true })
await writeFile(join(testCwd(), "subdir", ".node-version"), "v12.22.12")
Expand Down
1 change: 1 addition & 0 deletions e2e/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ for (const shell of [Bash, Zsh, Fish, PowerShell, WinCmd]) {
FNM_LOGLEVEL: "info",
FNM_MULTISHELL_PATH: expect.any(String),
FNM_NODE_DIST_MIRROR: expect.any(String),
FNM_RESOLVE_ENGINES: "false",
FNM_COREPACK_ENABLED: "false",
FNM_VERSION_FILE_STRATEGY: "local",
})
Expand Down
2 changes: 1 addition & 1 deletion e2e/shellcode/shells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const PowerShell = {
...define<Shell>({
binaryName: () => "pwsh",
forceFile: ".ps1",
currentlySupported: () => true,
currentlySupported: () => process.platform === "win32",
name: () => "PowerShell",
launchArgs: () => ["-NoProfile"],
escapeText: (x) => x,
Expand Down
4 changes: 3 additions & 1 deletion e2e/shellcode/shells/cmdEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ type EnvConfig = {
useOnCd: boolean
logLevel: string
corepackEnabled: boolean
resolveEngines: boolean
}
export type HasEnv = { env(cfg: Partial<EnvConfig>): ScriptLine }

function stringify(envConfig: Partial<EnvConfig> = {}) {
const { useOnCd, logLevel, corepackEnabled } = envConfig
const { useOnCd, logLevel, corepackEnabled, resolveEngines } = envConfig
return [
`fnm env`,
useOnCd && "--use-on-cd",
logLevel && `--log-level=${logLevel}`,
corepackEnabled && "--corepack-enabled",
resolveEngines && `--resolve-engines`,
]
.filter(Boolean)
.join(" ")
Expand Down
1 change: 1 addition & 0 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Command for Env {
"FNM_COREPACK_ENABLED",
config.corepack_enabled().to_string(),
),
("FNM_RESOLVE_ENGINES", config.resolve_engines().to_string()),
("FNM_ARCH", config.arch.to_string()),
]);

Expand Down
Loading

0 comments on commit 97be792

Please sign in to comment.