@zuke-build/cli 2.6.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/mod.d.ts +3 -0
- package/mod.js +39 -1
- package/package.json +1 -1
- package/src/binary_launcher.js +1 -3
- package/src/build_file.js +3 -10
- package/src/cache.d.ts +4 -0
- package/src/cache.js +18 -2
- package/src/deno_isolated.js +1 -1
- package/src/deno_path.d.ts +1 -1
- package/src/deno_path.js +7 -2
- package/src/dependencies.d.ts +17 -0
- package/src/dependencies.js +60 -11
- package/src/dispatch.d.ts +10 -1
- package/src/dispatch.js +13 -2
- package/src/launcher.js +3 -62
- package/src/launcher_shared.d.ts +25 -0
- package/src/launcher_shared.js +91 -0
- package/src/mcp_config.d.ts +8 -0
- package/src/mcp_config.js +10 -0
- package/src/node_launcher.d.ts +19 -0
- package/src/node_launcher.js +416 -0
- package/src/node_pin.d.ts +25 -0
- package/src/node_pin.js +30 -0
- package/src/npm_path.d.ts +16 -0
- package/src/npm_path.js +78 -0
- package/src/project.d.ts +14 -1
- package/src/project.js +21 -4
- package/src/records.d.ts +12 -3
- package/src/records.js +21 -3
- package/src/relock.js +116 -59
- package/src/runtime_field.d.ts +15 -0
- package/src/runtime_field.js +29 -0
- package/src/setup.d.ts +8 -0
- package/src/setup.js +126 -29
- package/src/starter.d.ts +11 -2
- package/src/starter.js +15 -3
- package/src/version.js +1 -1
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* The pieces the bootstrapping launchers share: the license header, and the
|
|
5
|
+
* download-with-retry and SHA-256 verification of a pinned archive. One
|
|
6
|
+
* implementation, so the Deno and Node launchers cannot drift on how a
|
|
7
|
+
* download is retried or verified.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
/** The MIT notice every generated launcher opens with (after its shebang). */
|
|
12
|
+
export const LICENSE_HEADER = `# Copyright (c) 2026 the Zuke contributors
|
|
13
|
+
# SPDX-License-Identifier: MIT`;
|
|
14
|
+
/* cspell:disable */
|
|
15
|
+
/**
|
|
16
|
+
* The bash steps that fetch `$download_url` into `$archive` (inside a fresh
|
|
17
|
+
* `$work_dir`, removed on exit) with retries, then check its SHA-256 against
|
|
18
|
+
* `$expected_sha256`, exiting 1 on a mismatch. Expects `asset`,
|
|
19
|
+
* `download_url` and `expected_sha256` to be set, and is indented for use
|
|
20
|
+
* inside an `if` block. `name` is the tool, for messages.
|
|
21
|
+
*/
|
|
22
|
+
export function bashFetchVerified(name) {
|
|
23
|
+
return ` work_dir="$(mktemp -d)"
|
|
24
|
+
trap 'rm -rf "$work_dir"' EXIT
|
|
25
|
+
archive="$work_dir/$asset"
|
|
26
|
+
|
|
27
|
+
# The download fetches over the network, which is occasionally flaky (e.g. a
|
|
28
|
+
# transient 5xx from the CDN). Retry a few times with backoff so a blip
|
|
29
|
+
# doesn't fail the whole run.
|
|
30
|
+
download_${name.toLowerCase()}() {
|
|
31
|
+
curl -fsSL -o "$archive" "$download_url"
|
|
32
|
+
}
|
|
33
|
+
attempt=1
|
|
34
|
+
max_attempts=4
|
|
35
|
+
until download_${name.toLowerCase()}; do
|
|
36
|
+
if [ "$attempt" -ge "$max_attempts" ]; then
|
|
37
|
+
echo "zuke: failed to download ${name} after $max_attempts attempts." >&2
|
|
38
|
+
exit 1
|
|
39
|
+
fi
|
|
40
|
+
delay=$((attempt * 3))
|
|
41
|
+
echo "zuke: ${name} download failed (attempt $attempt/$max_attempts); retrying in \${delay}s..." >&2
|
|
42
|
+
sleep "$delay"
|
|
43
|
+
attempt=$((attempt + 1))
|
|
44
|
+
done
|
|
45
|
+
|
|
46
|
+
if command -v sha256sum >/dev/null 2>&1; then
|
|
47
|
+
actual_sha256="$(sha256sum "$archive" | awk '{print $1}')"
|
|
48
|
+
elif command -v shasum >/dev/null 2>&1; then
|
|
49
|
+
actual_sha256="$(shasum -a 256 "$archive" | awk '{print $1}')"
|
|
50
|
+
else
|
|
51
|
+
echo "zuke: neither sha256sum nor shasum is available to verify the download." >&2
|
|
52
|
+
exit 1
|
|
53
|
+
fi
|
|
54
|
+
if [ "$actual_sha256" != "$expected_sha256" ]; then
|
|
55
|
+
echo "zuke: checksum mismatch for $asset:" >&2
|
|
56
|
+
echo " expected $expected_sha256" >&2
|
|
57
|
+
echo " got $actual_sha256" >&2
|
|
58
|
+
exit 1
|
|
59
|
+
fi`;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The PowerShell steps that fetch `$downloadUrl` into `$archive` with
|
|
63
|
+
* retries, then check its SHA-256 against `$expectedSha256`, throwing on a
|
|
64
|
+
* mismatch. Indented for use inside a `try` block; `name` is the tool, for
|
|
65
|
+
* messages.
|
|
66
|
+
*/
|
|
67
|
+
export function pwshFetchVerified(name) {
|
|
68
|
+
return ` # The download fetches over the network, which is occasionally flaky (e.g.
|
|
69
|
+
# a transient 5xx from the CDN). Retry a few times with backoff so a blip
|
|
70
|
+
# doesn't fail the whole run.
|
|
71
|
+
$maxAttempts = 4
|
|
72
|
+
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
|
|
73
|
+
try {
|
|
74
|
+
Invoke-WebRequest -Uri $downloadUrl -OutFile $archive
|
|
75
|
+
break
|
|
76
|
+
} catch {
|
|
77
|
+
if ($attempt -ge $maxAttempts) {
|
|
78
|
+
throw "zuke: failed to download ${name} after $maxAttempts attempts: $_"
|
|
79
|
+
}
|
|
80
|
+
$delay = $attempt * 3
|
|
81
|
+
Write-Host "zuke: ${name} download failed (attempt $attempt/$maxAttempts); retrying in \${delay}s..."
|
|
82
|
+
Start-Sleep -Seconds $delay
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
$actualSha256 = (Get-FileHash -Path $archive -Algorithm SHA256).Hash.ToLowerInvariant()
|
|
87
|
+
if ($actualSha256 -ne $expectedSha256.ToLowerInvariant()) {
|
|
88
|
+
throw "zuke: checksum mismatch for \${asset}: expected $expectedSha256, got $actualSha256"
|
|
89
|
+
}`;
|
|
90
|
+
}
|
|
91
|
+
/* cspell:enable */
|
package/src/mcp_config.d.ts
CHANGED
|
@@ -28,6 +28,14 @@ export declare function mcpServerEntry(allowRun: boolean, buildFile: string): Mc
|
|
|
28
28
|
* standalone binary runs on the engine it carries, with no Deno.
|
|
29
29
|
*/
|
|
30
30
|
export declare function binaryMcpEntry(allowRun: boolean): McpServerEntry;
|
|
31
|
+
/**
|
|
32
|
+
* The entry a Node project is registered with: `node zuke.ts mcp`, the Node
|
|
33
|
+
* counterpart of {@link mcpServerEntry}. `.mcp.json` is committed and shared,
|
|
34
|
+
* so it names the runtime rather than a launcher script, which a Windows MCP
|
|
35
|
+
* client could not start. It needs Node 26 or newer on `PATH` and the
|
|
36
|
+
* project's dependencies installed, which a first `./zuke` run does.
|
|
37
|
+
*/
|
|
38
|
+
export declare function nodeMcpEntry(allowRun: boolean, buildFile: string): McpServerEntry;
|
|
31
39
|
/**
|
|
32
40
|
* A parsed `.mcp.json`, ready to merge. `present` means a well-formed
|
|
33
41
|
* {@link McpServerEntry} is registered as `zuke` (whatever its arguments — a
|
package/src/mcp_config.js
CHANGED
|
@@ -44,6 +44,16 @@ export function mcpServerEntry(allowRun, buildFile) {
|
|
|
44
44
|
export function binaryMcpEntry(allowRun) {
|
|
45
45
|
return { command: "zuke", args: mcpArgs(allowRun) };
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* The entry a Node project is registered with: `node zuke.ts mcp`, the Node
|
|
49
|
+
* counterpart of {@link mcpServerEntry}. `.mcp.json` is committed and shared,
|
|
50
|
+
* so it names the runtime rather than a launcher script, which a Windows MCP
|
|
51
|
+
* client could not start. It needs Node 26 or newer on `PATH` and the
|
|
52
|
+
* project's dependencies installed, which a first `./zuke` run does.
|
|
53
|
+
*/
|
|
54
|
+
export function nodeMcpEntry(allowRun, buildFile) {
|
|
55
|
+
return { command: "node", args: [buildFile, ...mcpArgs(allowRun)] };
|
|
56
|
+
}
|
|
47
57
|
/** Whether `value` is a well-formed server entry: a command and its arguments. */
|
|
48
58
|
function isServerEntry(value) {
|
|
49
59
|
return isRecord(value) && typeof value.command === "string" &&
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type NodePin } from "./node_pin.ts";
|
|
2
|
+
/** What a Node launcher is rendered for. */
|
|
3
|
+
export interface NodeLauncherOptions {
|
|
4
|
+
/** The build file the launcher runs. Omitted: `zuke.ts`. */
|
|
5
|
+
buildFile?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The bash launcher (`./zuke`) of a Node project.
|
|
9
|
+
*
|
|
10
|
+
* @throws {Error} when the build file name is not a plain file name.
|
|
11
|
+
*/
|
|
12
|
+
export declare function nodeLauncherBash(options?: NodeLauncherOptions, pin?: NodePin): string;
|
|
13
|
+
/**
|
|
14
|
+
* The PowerShell launcher (`.\zuke.ps1`) of a Node project, mirroring
|
|
15
|
+
* {@link nodeLauncherBash}.
|
|
16
|
+
*
|
|
17
|
+
* @throws {Error} when the build file name is not a plain file name.
|
|
18
|
+
*/
|
|
19
|
+
export declare function nodeLauncherPowerShell(options?: NodeLauncherOptions, pin?: NodePin): string;
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* The `./zuke` (bash) and `zuke.ps1` (PowerShell) launchers of a project whose
|
|
5
|
+
* build runs on Node, so a checkout with no Node installed can run
|
|
6
|
+
* `./zuke <target>`. Each uses, in order: a `node` on `PATH` whose major
|
|
7
|
+
* version is at least the pin's ({@link "./node_pin.ts"}); the copy cached
|
|
8
|
+
* under `ZUKE_NODE_HOME` (default `~/.zuke/node`), one directory per release;
|
|
9
|
+
* or the pinned release downloaded from nodejs.org, verified against its
|
|
10
|
+
* SHA-256 and unpacked there. `NODE_VERSION` overrides the release and must
|
|
11
|
+
* come with `NODE_SHA256`: nothing unverified is ever installed.
|
|
12
|
+
*
|
|
13
|
+
* The chosen node's directory goes first on `PATH`, so the `npm` the build runs
|
|
14
|
+
* is that node's. Dependencies are installed with `--ignore-scripts` before the
|
|
15
|
+
* build starts: `npm ci` when `package-lock.json` is newer than the last
|
|
16
|
+
* successful install (or there is none), `npm install` when there is a
|
|
17
|
+
* `package.json` and no lockfile. The launcher is a Node project's own; it names no other stack.
|
|
18
|
+
*
|
|
19
|
+
* @module
|
|
20
|
+
*/
|
|
21
|
+
import { assertBuildFileName, BUILD_FILE } from "./build_file.js";
|
|
22
|
+
import { bashFetchVerified, LICENSE_HEADER, pwshFetchVerified, } from "./launcher_shared.js";
|
|
23
|
+
import { NODE_DIST_URL, NODE_PIN, NODE_POSIX_PLATFORMS, NODE_WINDOWS_PLATFORMS, } from "./node_pin.js";
|
|
24
|
+
/** The pin's major version: the oldest `node` on `PATH` a launcher accepts. */
|
|
25
|
+
function majorOf(pin) {
|
|
26
|
+
return pin.version.split(".")[0];
|
|
27
|
+
}
|
|
28
|
+
/** The `NODE_VERSION` / `NODE_SHA256` / `ZUKE_NODE_HOME` contract, as the comment block both scripts carry. */
|
|
29
|
+
function envDocs(pin) {
|
|
30
|
+
return `# Honoured environment variables:
|
|
31
|
+
# ZUKE_NODE_HOME where Node is kept (default: ~/.zuke/node), one directory
|
|
32
|
+
# per release
|
|
33
|
+
# NODE_VERSION which Node to download, an exact release (e.g. ${pin.version}).
|
|
34
|
+
# Defaults to the pinned, verified ${pin.version}.
|
|
35
|
+
# NODE_SHA256 required alongside a NODE_VERSION override: the SHA-256 of
|
|
36
|
+
# the release asset for the current platform. This launcher
|
|
37
|
+
# never downloads an unverified binary.`;
|
|
38
|
+
}
|
|
39
|
+
/* cspell:disable */
|
|
40
|
+
/**
|
|
41
|
+
* The bash launcher (`./zuke`) of a Node project.
|
|
42
|
+
*
|
|
43
|
+
* @throws {Error} when the build file name is not a plain file name.
|
|
44
|
+
*/
|
|
45
|
+
export function nodeLauncherBash(options = {}, pin = NODE_PIN) {
|
|
46
|
+
const build = assertBuildFileName(options.buildFile ?? BUILD_FILE);
|
|
47
|
+
const arms = NODE_POSIX_PLATFORMS.map((platform) => ` ${platform})\n echo "${pin.checksums[platform]}" ;;`).join("\n");
|
|
48
|
+
return `#!/usr/bin/env bash
|
|
49
|
+
${LICENSE_HEADER}
|
|
50
|
+
#
|
|
51
|
+
# Zuke launcher for a build that runs on Node.
|
|
52
|
+
#
|
|
53
|
+
# ./zuke # run the default target
|
|
54
|
+
# ./zuke <target> # run one target and its prerequisites
|
|
55
|
+
# ./zuke --list # list every target
|
|
56
|
+
#
|
|
57
|
+
# Uses the node on PATH when it is Node ${majorOf(pin)} or newer, else a pinned,
|
|
58
|
+
# checksum-verified Node it downloads once, then installs the project's
|
|
59
|
+
# dependencies and runs ${build}.
|
|
60
|
+
#
|
|
61
|
+
${envDocs(pin)}
|
|
62
|
+
set -euo pipefail
|
|
63
|
+
|
|
64
|
+
DEFAULT_NODE_VERSION="${pin.version}"
|
|
65
|
+
MIN_NODE_MAJOR=${majorOf(pin)}
|
|
66
|
+
|
|
67
|
+
# SHA-256 of each \`node-v<version>-<platform>.tar.gz\` asset, from
|
|
68
|
+
# ${NODE_DIST_URL}/v${pin.version}/SHASUMS256.txt. Generated from @zuke/cli's
|
|
69
|
+
# node_pin.ts: change the pin there.
|
|
70
|
+
node_checksum_for() {
|
|
71
|
+
case "$1" in
|
|
72
|
+
${arms}
|
|
73
|
+
*)
|
|
74
|
+
return 1 ;;
|
|
75
|
+
esac
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
dir="$(cd "$(dirname "\${BASH_SOURCE[0]}")" && pwd)"
|
|
79
|
+
cd "$dir"
|
|
80
|
+
|
|
81
|
+
node_version="\${NODE_VERSION:-$DEFAULT_NODE_VERSION}"
|
|
82
|
+
node_version="\${node_version#v}"
|
|
83
|
+
node_home="\${ZUKE_NODE_HOME:-$HOME/.zuke/node}/v$node_version"
|
|
84
|
+
|
|
85
|
+
# The major version of the node on PATH, empty when there is none or it cannot
|
|
86
|
+
# say (a version-manager shim with no runtime installed, say).
|
|
87
|
+
path_major=""
|
|
88
|
+
if command -v node >/dev/null 2>&1; then
|
|
89
|
+
path_major="$(node --version 2>/dev/null | sed -n 's/^v\\([0-9][0-9]*\\)\\..*/\\1/p')" || path_major=""
|
|
90
|
+
fi
|
|
91
|
+
|
|
92
|
+
if [ -n "$path_major" ] && [ "$path_major" -ge "$MIN_NODE_MAJOR" ]; then
|
|
93
|
+
node_dir="$(dirname "$(command -v node)")"
|
|
94
|
+
elif [ -x "$node_home/bin/node" ]; then
|
|
95
|
+
node_dir="$node_home/bin"
|
|
96
|
+
else
|
|
97
|
+
echo "zuke: Node $MIN_NODE_MAJOR or newer not found — installing $node_version now..." >&2
|
|
98
|
+
for cmd in curl tar; do
|
|
99
|
+
if ! command -v "$cmd" >/dev/null 2>&1; then
|
|
100
|
+
echo "zuke: $cmd is required to download Node; install Node $MIN_NODE_MAJOR or newer instead." >&2
|
|
101
|
+
exit 1
|
|
102
|
+
fi
|
|
103
|
+
done
|
|
104
|
+
|
|
105
|
+
case "$(uname -s)" in
|
|
106
|
+
Linux) os_part="linux" ;;
|
|
107
|
+
Darwin) os_part="darwin" ;;
|
|
108
|
+
*)
|
|
109
|
+
echo "zuke: unsupported OS for the checksum-verified Node download: $(uname -s)." >&2
|
|
110
|
+
exit 1
|
|
111
|
+
;;
|
|
112
|
+
esac
|
|
113
|
+
case "$(uname -m)" in
|
|
114
|
+
x86_64 | amd64) arch_part="x64" ;;
|
|
115
|
+
arm64 | aarch64) arch_part="arm64" ;;
|
|
116
|
+
*)
|
|
117
|
+
echo "zuke: unsupported architecture for the checksum-verified Node download: $(uname -m)." >&2
|
|
118
|
+
exit 1
|
|
119
|
+
;;
|
|
120
|
+
esac
|
|
121
|
+
platform="\${os_part}-\${arch_part}"
|
|
122
|
+
|
|
123
|
+
if [ "$node_version" = "$DEFAULT_NODE_VERSION" ]; then
|
|
124
|
+
expected_sha256="$(node_checksum_for "$platform")" || {
|
|
125
|
+
echo "zuke: no pinned checksum for $platform." >&2
|
|
126
|
+
exit 1
|
|
127
|
+
}
|
|
128
|
+
elif [ -n "\${NODE_SHA256:-}" ]; then
|
|
129
|
+
expected_sha256="$NODE_SHA256"
|
|
130
|
+
else
|
|
131
|
+
echo "zuke: NODE_VERSION=$node_version overrides the pinned $DEFAULT_NODE_VERSION," >&2
|
|
132
|
+
echo " but no checksum is pinned for it. Set NODE_SHA256 to the expected" >&2
|
|
133
|
+
echo " SHA-256 of node-v$node_version-$platform.tar.gz, or unset NODE_VERSION." >&2
|
|
134
|
+
exit 1
|
|
135
|
+
fi
|
|
136
|
+
|
|
137
|
+
asset="node-v\${node_version}-\${platform}.tar.gz"
|
|
138
|
+
download_url="${NODE_DIST_URL}/v\${node_version}/\${asset}"
|
|
139
|
+
${bashFetchVerified("Node")}
|
|
140
|
+
|
|
141
|
+
# Unpack into a sibling of the final directory, on the same filesystem, then
|
|
142
|
+
# rename it into place: the rename is atomic, so an interrupted run never
|
|
143
|
+
# leaves a half-extracted Node that looks installed.
|
|
144
|
+
node_root="$(dirname "$node_home")"
|
|
145
|
+
mkdir -p "$node_root"
|
|
146
|
+
staging="$(mktemp -d "$node_root/.v$node_version.XXXXXX")"
|
|
147
|
+
trap 'rm -rf "$work_dir" "$staging"' EXIT
|
|
148
|
+
tar -xzf "$archive" -C "$staging" --strip-components=1
|
|
149
|
+
# The rename publishes ours. If a concurrent launcher published first, mv
|
|
150
|
+
# either fails (their rename won the race) or nests ours inside theirs; we
|
|
151
|
+
# drop ours and use theirs. Nothing already at $node_home is ever deleted: it
|
|
152
|
+
# may be another run's Node.
|
|
153
|
+
mv "$staging" "$node_home" 2>/dev/null || true
|
|
154
|
+
rm -rf "$node_home/$(basename "$staging")"
|
|
155
|
+
# Here, not only in the trap: the exec below replaces this shell, so the
|
|
156
|
+
# EXIT trap never runs on success.
|
|
157
|
+
rm -rf "$staging" "$work_dir"
|
|
158
|
+
if [ ! -x "$node_home/bin/node" ]; then
|
|
159
|
+
echo "zuke: $node_home exists but holds no bin/node; remove it and run again." >&2
|
|
160
|
+
exit 1
|
|
161
|
+
fi
|
|
162
|
+
node_dir="$node_home/bin"
|
|
163
|
+
fi
|
|
164
|
+
|
|
165
|
+
# This node first on PATH, so the npm and npx the build runs are its own.
|
|
166
|
+
export PATH="$node_dir:$PATH"
|
|
167
|
+
|
|
168
|
+
# Written only after npm succeeds, so a failed install is retried next run.
|
|
169
|
+
installed=node_modules/.zuke-installed
|
|
170
|
+
|
|
171
|
+
# The npm command this checkout needs, or nothing when its install is current.
|
|
172
|
+
install_needed() {
|
|
173
|
+
if [ -f package-lock.json ]; then
|
|
174
|
+
if [ ! -f "$installed" ] || [ package-lock.json -nt "$installed" ]; then
|
|
175
|
+
echo ci
|
|
176
|
+
fi
|
|
177
|
+
elif [ -f package.json ] && [ ! -f "$installed" ]; then
|
|
178
|
+
echo install
|
|
179
|
+
fi
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if [ -n "$(install_needed)" ]; then
|
|
183
|
+
# One install at a time per checkout: a second launcher waits for the lock,
|
|
184
|
+
# then finds the first one's install current. The lock is created with
|
|
185
|
+
# noclobber, which fails when it exists, and holds its owner's process id. A
|
|
186
|
+
# lock whose owner is gone is reported, never removed: deciding that for
|
|
187
|
+
# another run is a guess two waiting runs could both act on.
|
|
188
|
+
lock=.zuke/install.lock
|
|
189
|
+
mkdir -p .zuke
|
|
190
|
+
waited=0
|
|
191
|
+
until (set -C; echo "$$" > "$lock") 2>/dev/null; do
|
|
192
|
+
holder="$(cat "$lock" 2>/dev/null || true)"
|
|
193
|
+
if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null; then
|
|
194
|
+
echo "zuke: $lock was left by an install that did not finish (process $holder); remove it and run again." >&2
|
|
195
|
+
exit 1
|
|
196
|
+
fi
|
|
197
|
+
if [ "$waited" -ge 600 ]; then
|
|
198
|
+
echo "zuke: another launcher has held $lock for 10 minutes; if none is running, remove it and run again." >&2
|
|
199
|
+
exit 1
|
|
200
|
+
fi
|
|
201
|
+
sleep 1
|
|
202
|
+
waited=$((waited + 1))
|
|
203
|
+
done
|
|
204
|
+
trap 'rm -f "$lock"' EXIT
|
|
205
|
+
case "$(install_needed)" in
|
|
206
|
+
ci)
|
|
207
|
+
rm -f "$installed"
|
|
208
|
+
npm ci --ignore-scripts
|
|
209
|
+
mkdir -p node_modules && : > "$installed"
|
|
210
|
+
;;
|
|
211
|
+
install)
|
|
212
|
+
echo "zuke: no package-lock.json here yet — running npm install so one can be written." >&2
|
|
213
|
+
npm install --ignore-scripts
|
|
214
|
+
mkdir -p node_modules && : > "$installed"
|
|
215
|
+
;;
|
|
216
|
+
esac
|
|
217
|
+
# Released here, because the exec below skips the EXIT trap.
|
|
218
|
+
rm -f "$lock"
|
|
219
|
+
trap - EXIT
|
|
220
|
+
fi
|
|
221
|
+
|
|
222
|
+
exec node ${build} "$@"
|
|
223
|
+
`;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The PowerShell launcher (`.\zuke.ps1`) of a Node project, mirroring
|
|
227
|
+
* {@link nodeLauncherBash}.
|
|
228
|
+
*
|
|
229
|
+
* @throws {Error} when the build file name is not a plain file name.
|
|
230
|
+
*/
|
|
231
|
+
export function nodeLauncherPowerShell(options = {}, pin = NODE_PIN) {
|
|
232
|
+
const build = assertBuildFileName(options.buildFile ?? BUILD_FILE);
|
|
233
|
+
const width = Math.max(...NODE_WINDOWS_PLATFORMS.map((p) => p.length)) + 2;
|
|
234
|
+
const entries = NODE_WINDOWS_PLATFORMS.map((platform) => ` ${`"${platform}"`.padEnd(width)} = "${pin.checksums[platform]}"`).join("\n");
|
|
235
|
+
return `#!/usr/bin/env pwsh
|
|
236
|
+
${LICENSE_HEADER}
|
|
237
|
+
#
|
|
238
|
+
# Zuke launcher (PowerShell) for a build that runs on Node.
|
|
239
|
+
#
|
|
240
|
+
# .\\zuke.ps1 # run the default target
|
|
241
|
+
# .\\zuke.ps1 <target> # run one target and its prerequisites
|
|
242
|
+
# .\\zuke.ps1 --list # list every target
|
|
243
|
+
#
|
|
244
|
+
# Uses the node on PATH when it is Node ${majorOf(pin)} or newer, else a pinned,
|
|
245
|
+
# checksum-verified Node it downloads once, then installs the project's
|
|
246
|
+
# dependencies and runs ${build}.
|
|
247
|
+
#
|
|
248
|
+
${envDocs(pin)}
|
|
249
|
+
|
|
250
|
+
$ErrorActionPreference = "Stop"
|
|
251
|
+
|
|
252
|
+
$DefaultNodeVersion = "${pin.version}"
|
|
253
|
+
$MinNodeMajor = ${majorOf(pin)}
|
|
254
|
+
|
|
255
|
+
# SHA-256 of each \`node-v<version>-<platform>.zip\` asset, from
|
|
256
|
+
# ${NODE_DIST_URL}/v${pin.version}/SHASUMS256.txt. Generated from @zuke/cli's
|
|
257
|
+
# node_pin.ts: change the pin there.
|
|
258
|
+
$NodeChecksums = @{
|
|
259
|
+
${entries}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
$dir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
263
|
+
Set-Location $dir
|
|
264
|
+
|
|
265
|
+
$nodeVersion = if ($env:NODE_VERSION) { $env:NODE_VERSION } else { $DefaultNodeVersion }
|
|
266
|
+
$nodeVersion = $nodeVersion.TrimStart("v")
|
|
267
|
+
$nodeRoot = if ($env:ZUKE_NODE_HOME) { $env:ZUKE_NODE_HOME } else { Join-Path $HOME ".zuke\\node" }
|
|
268
|
+
$nodeHome = Join-Path $nodeRoot "v$nodeVersion"
|
|
269
|
+
|
|
270
|
+
function Resolve-Node {
|
|
271
|
+
$onPath = Get-Command node -CommandType Application -ErrorAction SilentlyContinue
|
|
272
|
+
if ($onPath) {
|
|
273
|
+
# A shim that cannot report a version is treated as no node at all.
|
|
274
|
+
$reported = ""
|
|
275
|
+
try { $reported = & $onPath.Source --version 2>$null } catch { }
|
|
276
|
+
if ($reported -match '^v(\\d+)\\.' -and [int]$Matches[1] -ge $MinNodeMajor) {
|
|
277
|
+
return $onPath.Source
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
$local = Join-Path $nodeHome "node.exe"
|
|
281
|
+
if (Test-Path $local) { return $local }
|
|
282
|
+
return $null
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
$node = Resolve-Node
|
|
286
|
+
if (-not $node) {
|
|
287
|
+
Write-Host "zuke: Node $MinNodeMajor or newer not found - installing $nodeVersion now..."
|
|
288
|
+
|
|
289
|
+
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
|
|
290
|
+
$platform = switch ($arch) {
|
|
291
|
+
"X64" { "win-x64" }
|
|
292
|
+
"Arm64" { "win-arm64" }
|
|
293
|
+
default {
|
|
294
|
+
throw "zuke: unsupported architecture for the checksum-verified Node download: $arch."
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if ($nodeVersion -eq $DefaultNodeVersion -and $NodeChecksums.ContainsKey($platform)) {
|
|
299
|
+
$expectedSha256 = $NodeChecksums[$platform]
|
|
300
|
+
} elseif ($env:NODE_SHA256) {
|
|
301
|
+
$expectedSha256 = $env:NODE_SHA256
|
|
302
|
+
} else {
|
|
303
|
+
throw "zuke: NODE_VERSION=$nodeVersion overrides the pinned $DefaultNodeVersion, " +
|
|
304
|
+
"but no checksum is pinned for it. Set NODE_SHA256 to the expected SHA-256 " +
|
|
305
|
+
"of node-v$nodeVersion-$platform.zip, or unset NODE_VERSION."
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
$asset = "node-v$nodeVersion-$platform.zip"
|
|
309
|
+
$downloadUrl = "${NODE_DIST_URL}/v$nodeVersion/$asset"
|
|
310
|
+
$workDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid())
|
|
311
|
+
New-Item -ItemType Directory -Path $workDir | Out-Null
|
|
312
|
+
$archive = Join-Path $workDir $asset
|
|
313
|
+
$staging = $null
|
|
314
|
+
try {
|
|
315
|
+
${pwshFetchVerified("Node")}
|
|
316
|
+
|
|
317
|
+
# Unpack into a sibling of the final directory, on the same volume, then
|
|
318
|
+
# move the release folder into place: the move is a rename there, so an
|
|
319
|
+
# interrupted run never leaves a half-extracted Node that looks installed.
|
|
320
|
+
New-Item -ItemType Directory -Force -Path $nodeRoot | Out-Null
|
|
321
|
+
$staging = Join-Path $nodeRoot ".v$nodeVersion.$([System.Guid]::NewGuid())"
|
|
322
|
+
Expand-Archive -Path $archive -DestinationPath $staging -Force
|
|
323
|
+
$release = Get-ChildItem -Path $staging -Directory | Select-Object -First 1
|
|
324
|
+
# A name of its own, so a nested copy below is only ever this run's.
|
|
325
|
+
$release = Rename-Item -Path $release.FullName -NewName ([System.Guid]::NewGuid()) -PassThru
|
|
326
|
+
# The move publishes ours; if a concurrent launcher published first, ours
|
|
327
|
+
# is refused or nested inside theirs, and we drop it and use theirs.
|
|
328
|
+
# Nothing already at $nodeHome is ever deleted: it may be another run's Node.
|
|
329
|
+
try {
|
|
330
|
+
Move-Item -Path $release.FullName -Destination $nodeHome
|
|
331
|
+
} catch {
|
|
332
|
+
if (-not (Test-Path $nodeHome)) { throw }
|
|
333
|
+
}
|
|
334
|
+
$nested = Join-Path $nodeHome $release.Name
|
|
335
|
+
Remove-Item -Recurse -Force $nested -ErrorAction SilentlyContinue
|
|
336
|
+
if (-not (Test-Path (Join-Path $nodeHome "node.exe"))) {
|
|
337
|
+
throw "zuke: $nodeHome exists but holds no node.exe; remove it and run again."
|
|
338
|
+
}
|
|
339
|
+
} finally {
|
|
340
|
+
Remove-Item -Recurse -Force $workDir -ErrorAction SilentlyContinue
|
|
341
|
+
if ($staging) { Remove-Item -Recurse -Force $staging -ErrorAction SilentlyContinue }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
$node = Join-Path $nodeHome "node.exe"
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
# This node first on PATH, so the npm and npx the build runs are its own.
|
|
348
|
+
$env:PATH = (Split-Path -Parent $node) + [IO.Path]::PathSeparator + $env:PATH
|
|
349
|
+
|
|
350
|
+
# Written only after npm succeeds, so a failed install is retried next run.
|
|
351
|
+
$installed = Join-Path "node_modules" ".zuke-installed"
|
|
352
|
+
|
|
353
|
+
# The npm command this checkout needs, or $null when its install is current.
|
|
354
|
+
function Get-InstallNeeded {
|
|
355
|
+
if (Test-Path "package-lock.json") {
|
|
356
|
+
if (-not (Test-Path $installed) -or
|
|
357
|
+
(Get-Item "package-lock.json").LastWriteTime -gt (Get-Item $installed).LastWriteTime) {
|
|
358
|
+
return "ci"
|
|
359
|
+
}
|
|
360
|
+
} elseif ((Test-Path "package.json") -and -not (Test-Path $installed)) {
|
|
361
|
+
return "install"
|
|
362
|
+
}
|
|
363
|
+
return $null
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (Get-InstallNeeded) {
|
|
367
|
+
# One install at a time per checkout: a second launcher waits for the lock,
|
|
368
|
+
# then finds the first one's install current. The lock is created with
|
|
369
|
+
# CreateNew, which fails when it exists, and holds its owner's process id. A
|
|
370
|
+
# lock whose owner is gone is reported, never removed: deciding that for
|
|
371
|
+
# another run is a guess two waiting runs could both act on.
|
|
372
|
+
New-Item -ItemType Directory -Force -Path (Join-Path $dir ".zuke") | Out-Null
|
|
373
|
+
$lock = Join-Path $dir ".zuke/install.lock"
|
|
374
|
+
$waited = 0
|
|
375
|
+
while ($true) {
|
|
376
|
+
try {
|
|
377
|
+
$stream = [System.IO.File]::Open($lock, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write)
|
|
378
|
+
$bytes = [System.Text.Encoding]::ASCII.GetBytes("$PID")
|
|
379
|
+
$stream.Write($bytes, 0, $bytes.Length)
|
|
380
|
+
$stream.Close()
|
|
381
|
+
break
|
|
382
|
+
} catch {
|
|
383
|
+
$holder = 0
|
|
384
|
+
$text = Get-Content -Path $lock -Raw -ErrorAction SilentlyContinue
|
|
385
|
+
if ([int]::TryParse("$text".Trim(), [ref]$holder) -and
|
|
386
|
+
-not (Get-Process -Id $holder -ErrorAction SilentlyContinue)) {
|
|
387
|
+
throw "zuke: $lock was left by an install that did not finish (process $holder); remove it and run again."
|
|
388
|
+
}
|
|
389
|
+
if ($waited -ge 600) {
|
|
390
|
+
throw "zuke: another launcher has held $lock for 10 minutes; if none is running, remove it and run again."
|
|
391
|
+
}
|
|
392
|
+
Start-Sleep -Seconds 1
|
|
393
|
+
$waited++
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
$install = Get-InstallNeeded
|
|
398
|
+
if ($install -eq "install") {
|
|
399
|
+
Write-Warning "zuke: no package-lock.json here yet - running npm install so one can be written."
|
|
400
|
+
}
|
|
401
|
+
if ($install) {
|
|
402
|
+
if (Test-Path $installed) { Remove-Item -Force $installed }
|
|
403
|
+
& npm $install --ignore-scripts
|
|
404
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
405
|
+
New-Item -ItemType File -Force -Path $installed | Out-Null
|
|
406
|
+
}
|
|
407
|
+
} finally {
|
|
408
|
+
Remove-Item -Force $lock -ErrorAction SilentlyContinue
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
& $node (Join-Path $dir "${build}") @args
|
|
413
|
+
exit $LASTEXITCODE
|
|
414
|
+
`;
|
|
415
|
+
}
|
|
416
|
+
/* cspell:enable */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Node.js release the Node launchers bootstrap, and the SHA-256 of each
|
|
3
|
+
* platform's official `nodejs.org` dist asset they verify it against. The one
|
|
4
|
+
* place the pin lives, as {@link "./deno_pin.ts"} is for Deno: bump the version
|
|
5
|
+
* and the checksums together, from `https://nodejs.org/dist/v<version>/SHASUMS256.txt`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** A Node dist platform, as it appears in the asset name `node-v<version>-<platform>.<ext>`. */
|
|
10
|
+
export type NodePlatform = "darwin-arm64" | "darwin-x64" | "linux-arm64" | "linux-x64" | "win-arm64" | "win-x64";
|
|
11
|
+
/** The pinned Node release and the checksum of each platform's asset. */
|
|
12
|
+
export interface NodePin {
|
|
13
|
+
/** The release version without its `v` prefix, e.g. `"26.13.1"`. */
|
|
14
|
+
readonly version: string;
|
|
15
|
+
/** Lowercase hex SHA-256 of the dist asset for each supported platform. */
|
|
16
|
+
readonly checksums: Readonly<Record<NodePlatform, string>>;
|
|
17
|
+
}
|
|
18
|
+
/** The Node release the launchers bootstrap (Node 26, Active LTS), with its verified checksums. */
|
|
19
|
+
export declare const NODE_PIN: NodePin;
|
|
20
|
+
/** The platforms the POSIX launcher can bootstrap (`.tar.gz` assets). */
|
|
21
|
+
export declare const NODE_POSIX_PLATFORMS: readonly NodePlatform[];
|
|
22
|
+
/** The platforms the PowerShell launcher can bootstrap (`.zip` assets). */
|
|
23
|
+
export declare const NODE_WINDOWS_PLATFORMS: readonly NodePlatform[];
|
|
24
|
+
/** Where the dist assets are downloaded from; the launchers append `v<version>/<asset>`. */
|
|
25
|
+
export declare const NODE_DIST_URL = "https://nodejs.org/dist";
|
package/src/node_pin.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright (c) 2026 the Zuke contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/* cspell:disable */
|
|
4
|
+
/** The Node release the launchers bootstrap (Node 26, Active LTS), with its verified checksums. */
|
|
5
|
+
export const NODE_PIN = {
|
|
6
|
+
version: "26.13.1",
|
|
7
|
+
checksums: {
|
|
8
|
+
"darwin-arm64": "aec2514ea1ceb2ba52455e9ec65986f6749a06dd759e5db2a72e6889668c9735",
|
|
9
|
+
"darwin-x64": "82bb96078a24ca50b9c9a38f2379e6be6d87171fcd400fc14c8934b3bce96e2f",
|
|
10
|
+
"linux-arm64": "80a330b35415382878df18d41afbd106ea89bfb3be0087d6608e289a14c5cc7d",
|
|
11
|
+
"linux-x64": "518674de2b621728a5e4486f5fb3b7d2900bb3cc4aa97969edaa463db99f9778",
|
|
12
|
+
"win-arm64": "181ebe431b90787113a9e4fc80d38ce86d37888a353a0386de44d2872e48cb97",
|
|
13
|
+
"win-x64": "e2edda75bfb089654705cf65a13b58933bfd5f57148e8ebda916f58a2474f1f7",
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
/* cspell:enable */
|
|
17
|
+
/** The platforms the POSIX launcher can bootstrap (`.tar.gz` assets). */
|
|
18
|
+
export const NODE_POSIX_PLATFORMS = [
|
|
19
|
+
"darwin-arm64",
|
|
20
|
+
"darwin-x64",
|
|
21
|
+
"linux-arm64",
|
|
22
|
+
"linux-x64",
|
|
23
|
+
];
|
|
24
|
+
/** The platforms the PowerShell launcher can bootstrap (`.zip` assets). */
|
|
25
|
+
export const NODE_WINDOWS_PLATFORMS = [
|
|
26
|
+
"win-arm64",
|
|
27
|
+
"win-x64",
|
|
28
|
+
];
|
|
29
|
+
/** Where the dist assets are downloaded from; the launchers append `v<version>/<asset>`. */
|
|
30
|
+
export const NODE_DIST_URL = "https://nodejs.org/dist";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type DenoHost } from "./deno_path.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The npm executables to try, in order: the bare name for `PATH`, then the
|
|
4
|
+
* one beside the pinned Node the launchers cache. That one is left out when
|
|
5
|
+
* the environment names no absolute directory to find it in.
|
|
6
|
+
*/
|
|
7
|
+
export declare function npmCandidates(host?: DenoHost): string[];
|
|
8
|
+
/**
|
|
9
|
+
* Run `npm <args>` in `cwd` with the terminal's output, trying each candidate
|
|
10
|
+
* in order and skipping the ones that are not installed, and return its exit
|
|
11
|
+
* code. The cached npm runs with its Node first on `PATH`, which its script
|
|
12
|
+
* needs to start.
|
|
13
|
+
*
|
|
14
|
+
* @throws {Error} naming every candidate tried when none is installed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function runNpm(args: string[], cwd: string, candidates?: readonly string[]): Promise<number>;
|