-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[create-cloudflare] Add Node.js version check before loading CLI #13243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+134
−6
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "create-cloudflare": patch | ||
| --- | ||
|
|
||
| Show a clear error message when running on an unsupported Node.js version | ||
|
|
||
| Previously, running `create-cloudflare` on an older Node.js version (e.g. v18) would fail with a confusing syntax error. Now, a dedicated version check runs before loading the CLI and displays a helpful message explaining the minimum required Node.js version and suggesting version managers like Volta or nvm. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const MIN_NODE_VERSION = "20.0.0"; | ||
|
|
||
| // semiver implementation via https://github.com/lukeed/semiver/blob/ae7eebe6053c96be63032b14fb0b68e2553fcac4/src/index.js | ||
|
|
||
| /** | ||
| MIT License | ||
|
|
||
| Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com) | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
|
||
| */ | ||
|
|
||
| var fn = new Intl.Collator(0, { numeric: 1 }).compare; | ||
|
|
||
| function semiver(a, b, bool) { | ||
| a = a.split("."); | ||
| b = b.split("."); | ||
|
|
||
| return ( | ||
| fn(a[0], b[0]) || | ||
| fn(a[1], b[1]) || | ||
| ((b[2] = b.slice(2).join(".")), | ||
| (bool = /[.-]/.test((a[2] = a.slice(2).join(".")))), | ||
| bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1) | ||
| ); | ||
| } | ||
|
|
||
| // end semiver implementation | ||
|
|
||
| function main() { | ||
| if (semiver(process.versions.node, MIN_NODE_VERSION) < 0) { | ||
| console.error( | ||
| `create-cloudflare requires at least Node.js v${MIN_NODE_VERSION}. You are using v${process.versions.node}. Please update your version of Node.js. | ||
|
|
||
| Consider using a Node.js version manager such as https://volta.sh/ or https://github.com/nvm-sh/nvm.` | ||
| ); | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
|
||
| require("../dist/cli.js"); | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/create-cloudflare/src/__tests__/check-node.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { execFileSync } from "node:child_process"; | ||
| import { resolve } from "node:path"; | ||
| import { describe, test } from "vitest"; | ||
|
|
||
| const binPath = resolve(__dirname, "../../bin/c3.js"); | ||
|
|
||
| describe("check-node version gate", () => { | ||
| test("outputs a useful error message when Node.js version is too old", ({ | ||
| expect, | ||
| }) => { | ||
| // We can't actually run on an old Node, but we can verify the bin file | ||
| // contains the expected version gate by executing it with a mock that | ||
| // overrides process.versions.node. We do this via --eval. | ||
| const script = ` | ||
| // Override the node version to simulate an old version | ||
| Object.defineProperty(process, "versions", { | ||
| value: { ...process.versions, node: "18.0.0" }, | ||
| }); | ||
| require(${JSON.stringify(binPath)}); | ||
| `; | ||
|
|
||
| try { | ||
| execFileSync(process.execPath, ["--eval", script], { | ||
| encoding: "utf-8", | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| }); | ||
| // If it doesn't throw, that's unexpected | ||
| expect.unreachable("Expected the process to exit with code 1"); | ||
| } catch (e: unknown) { | ||
| const error = e as { status: number; stderr: string }; | ||
| expect(error.status).toBe(1); | ||
| expect(error.stderr).toContain( | ||
| "create-cloudflare requires at least Node.js v20.0.0" | ||
| ); | ||
| expect(error.stderr).toContain("You are using v18.0.0"); | ||
| expect(error.stderr).toContain("https://volta.sh/"); | ||
| } | ||
| }); | ||
|
|
||
| test("does not error when Node.js version meets the minimum", ({ | ||
| expect, | ||
| }) => { | ||
| // Running the bin file with the current Node.js (which is >= 20) | ||
| // should not produce the version error. It will fail because | ||
| // dist/cli.js may not exist, but that's a different error. | ||
| try { | ||
| execFileSync(process.execPath, [binPath], { | ||
| encoding: "utf-8", | ||
| stdio: ["pipe", "pipe", "pipe"], | ||
| timeout: 5000, | ||
| }); | ||
| } catch (e: unknown) { | ||
| const error = e as { stderr: string }; | ||
| // Should NOT contain the version error | ||
| expect(error.stderr).not.toContain( | ||
| "create-cloudflare requires at least Node.js" | ||
| ); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.