Repository navigation
chore: bootstrap development Node with Chomp #45
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
Open
Open
Changes from all commits
Commits
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
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
This file was deleted.
Oops, something went wrong.
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
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
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,83 @@ | ||
| #!/bin/sh | ||
| set -eu | ||
|
|
||
| has_pinned_node() { | ||
| [ -x .bin/node ] || return 1 | ||
| current_version=$(.bin/node --version) | ||
| [ "$current_version" = "v$NODE_VERSION" ] && [ -x .bin/npm ] | ||
| } | ||
|
|
||
| detect_system() { | ||
| system=$(uname -s) | ||
| case "$system" in | ||
| Darwin) system=darwin ;; | ||
| Linux) system=linux ;; | ||
| *) | ||
| printf 'Unsupported development platform: %s\n' "$system" >&2 | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| detect_machine() { | ||
| machine=$(uname -m) | ||
| case "$machine" in | ||
| arm64 | aarch64) machine=arm64 ;; | ||
| x86_64 | amd64) machine=x64 ;; | ||
| *) | ||
| printf 'Unsupported development architecture: %s\n' "$machine" >&2 | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| needs_download() { | ||
| [ ! -d "$runtime_dir" ] | ||
| } | ||
|
|
||
| verify_archive() { | ||
| awk -v file="$archive" '$2 == file { print }' "$work_dir/SHASUMS256.txt" >"$work_dir/checksum.txt" | ||
| [ -s "$work_dir/checksum.txt" ] | ||
| if command -v sha256sum >/dev/null 2>&1; then | ||
| (cd "$work_dir" && sha256sum -c checksum.txt) | ||
| else | ||
| (cd "$work_dir" && shasum -a 256 -c checksum.txt) | ||
| fi | ||
| } | ||
|
|
||
| download_node() { | ||
| mkdir -p .cache/dev-node | ||
| work_dir=$(mktemp -d .cache/dev-node/download.XXXXXX) | ||
| trap 'rm -rf "$work_dir"' EXIT | ||
| archive="$distribution.tar.gz" | ||
| release_url="https://nodejs.org/dist/v$NODE_VERSION" | ||
| curl --fail --location --silent --show-error --retry 3 "$release_url/$archive" -o "$work_dir/$archive" | ||
| curl --fail --location --silent --show-error --retry 3 "$release_url/SHASUMS256.txt" -o "$work_dir/SHASUMS256.txt" | ||
| verify_archive | ||
| tar -xzf "$work_dir/$archive" -C "$work_dir" | ||
| mv "$work_dir/$distribution" "$runtime_dir" | ||
| } | ||
|
|
||
| link_node() { | ||
| mkdir -p .bin | ||
| for tool in node npm npx; do | ||
| ln -sfn "../$runtime_dir/bin/$tool" ".bin/$tool" | ||
| done | ||
| } | ||
|
|
||
| main() { | ||
| : "${NODE_VERSION:?Run this script through Chomp.}" | ||
| printf '%s\n' "$NODE_VERSION" | grep -Eq '^26\.[0-9]+\.[0-9]+$' | ||
| if has_pinned_node; then return; fi | ||
| detect_system | ||
| detect_machine | ||
| platform="$system-$machine" | ||
| distribution="node-v$NODE_VERSION-$platform" | ||
| runtime_dir=".cache/dev-node/$distribution" | ||
| if needs_download; then download_node; fi | ||
| link_node | ||
| has_pinned_node | ||
| .bin/node --version | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { test } from "node:test"; | ||
| import type { TestContext } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { join, resolve } from "node:path"; | ||
| import { env } from "node:process"; | ||
|
|
||
| const setupScript = resolve("scripts/setup-node.sh"); | ||
| const nodeVersion = "26.13.0"; | ||
|
|
||
| const createSetupDirectory = (t: TestContext): string => { | ||
| mkdirSync(".cache", { recursive: true }); | ||
| const directory = mkdtempSync(resolve(".cache/node-setup-test-")); | ||
| t.after(() => rmSync(directory, { recursive: true, force: true })); | ||
| return directory; | ||
| }; | ||
|
|
||
| const writeExecutable = (directory: string, name: string, source: string): void => { | ||
| const path = join(directory, name); | ||
| writeFileSync(path, source, { mode: 0o755 }); | ||
| }; | ||
|
|
||
| const runSetup = (directory: string, version = nodeVersion) => { | ||
| const executablePath = `${directory}/commands:/usr/bin:/bin`; | ||
| const environment = Object.assign({}, env, { NODE_VERSION: version, PATH: executablePath }); | ||
| const result = spawnSync("/bin/sh", [setupScript], { cwd: directory, env: environment, encoding: "utf8" }); | ||
| return result; | ||
| }; | ||
|
|
||
| test("Node setup rejects an invalid version before downloading", (t) => { | ||
| const directory = createSetupDirectory(t); | ||
| const result = runSetup(directory, "26/../../unexpected"); | ||
| assert.equal(result.status, 1); | ||
| assert.equal(existsSync(join(directory, ".cache")), false); | ||
| }); | ||
|
|
||
| test("Node setup reuses its pinned runtime without network access", (t) => { | ||
| const directory = createSetupDirectory(t); | ||
| const binDirectory = join(directory, ".bin"); | ||
| mkdirSync(binDirectory); | ||
| writeExecutable(binDirectory, "node", `#!/bin/sh\nprintf 'v${nodeVersion}\\n'\n`); | ||
| writeExecutable(binDirectory, "npm", "#!/bin/sh\nexit 0\n"); | ||
| const commands = join(directory, "commands"); | ||
| mkdirSync(commands); | ||
| writeExecutable(commands, "curl", "#!/bin/sh\nexit 99\n"); | ||
| writeExecutable(commands, "uname", "#!/bin/sh\nexit 99\n"); | ||
| const result = runSetup(directory); | ||
| assert.equal(result.status, 0, result.stderr); | ||
| }); | ||
|
|
||
| test("Node setup rejects a bad checksum before extracting the archive", (t) => { | ||
| const directory = createSetupDirectory(t); | ||
| const commands = join(directory, "commands"); | ||
| mkdirSync(commands); | ||
| writeExecutable(commands, "uname", '#!/bin/sh\ncase "$1" in -s) echo Linux;; -m) echo x86_64;; esac\n'); | ||
| const manifest = `${"0".repeat(64)} node-v${nodeVersion}-linux-x64.tar.gz`; | ||
| const curlScript = `#!/bin/sh | ||
| for output do :; done | ||
| case "$*" in | ||
| *SHASUMS256.txt*) printf '%s\\n' '${manifest}' > "$output";; | ||
| *) printf 'unverified archive' > "$output";; | ||
| esac | ||
| `; | ||
| writeExecutable(commands, "curl", curlScript); | ||
| writeExecutable(commands, "tar", "#!/bin/sh\ntouch extracted\n"); | ||
| const result = runSetup(directory); | ||
| assert.notEqual(result.status, 0); | ||
| assert.match(`${result.stdout}${result.stderr}`, /FAILED|did NOT match/); | ||
| assert.equal(existsSync(join(directory, "extracted")), false); | ||
| assert.equal(existsSync(join(directory, ".bin")), false); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, if we need all this related to downloading and managing node stuff. Can we just revert to
.nvmrc. Its simple and we don't need to manage all this downloading and moving between versions.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's a good request.
I wanted to put this (node management) on chomp which means one less dep; aka 1 less security item to think about.
Thoughts before I remove this...? 🙂