create-zfb 0.1.0-next.8 → 0.1.0-next.80

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 CHANGED
@@ -11,3 +11,5 @@ npm create zfb@latest my-site
11
11
  ```
12
12
 
13
13
  This creates a new project directory `my-site/` bootstrapped from the built-in `basic-blog` template. For full documentation, configuration options, and CLI reference, see the [`@takazudo/zfb` docs](https://takazudomodular.com/pj/zudo-front-builder/).
14
+
15
+ > **pnpm 11 note:** pnpm 11's `minimumReleaseAge` may install a previous release of `create-zfb` within ~48h of any new release. If `create-zfb` warns about a stale install at startup, re-run with `pnpm create zfb@latest --config.minimumReleaseAge=0` or `npm create zfb@latest`.
@@ -1,13 +1,52 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawnSync } from "node:child_process";
3
3
  import { createRequire } from "node:module";
4
+ import { constants as osConstants } from "node:os";
4
5
  import { dirname, join } from "node:path";
6
+ import { checkForNewerVersion } from "./version-check.mjs";
7
+ import { formatSpawnErrorMessage } from "./spawn-error-message.mjs";
5
8
 
6
9
  const require = createRequire(import.meta.url);
7
10
 
11
+ try {
12
+ const ownPkg = require("../package.json");
13
+ const staleWarning = await checkForNewerVersion({ version: ownPkg.version });
14
+ if (staleWarning) {
15
+ process.stderr.write(staleWarning);
16
+ }
17
+ } catch {
18
+ // silently skip on any error: network failure, timeout (AbortError), parse error, etc.
19
+ }
20
+
8
21
  const zfbPkgJson = require.resolve("@takazudo/zfb/package.json");
9
22
  const zfbBin = join(dirname(zfbPkgJson), "bin", "zfb.mjs");
10
23
 
11
24
  const args = ["new", ...process.argv.slice(2)];
12
25
  const result = spawnSync(process.execPath, [zfbBin, ...args], { stdio: "inherit" });
26
+
27
+ // Surface spawn failures clearly. Without this check, an ENOENT/EACCES
28
+ // failure to even launch the resolved @takazudo/zfb bin/zfb.mjs (e.g. a
29
+ // broken resolution, a wiped node_modules, an unreadable file) silently
30
+ // exits 1 with no message — a broken @takazudo/zfb resolution looks like a
31
+ // mystery exit. Mirrors packages/zfb/bin/zfb.mjs's child.on("error")
32
+ // handling (issues #447/#441).
33
+ if (result.error) {
34
+ process.stderr.write(formatSpawnErrorMessage(result.error, zfbBin));
35
+ process.exit(1);
36
+ }
37
+
38
+ if (result.signal) {
39
+ // Re-raise the child's termination signal on ourselves so the caller sees
40
+ // the real cause of death (e.g. WIFSIGNALED), not a plain exit code 1.
41
+ // Mirrors the wrapper behaviour in packages/zfb/bin/zfb.mjs.
42
+ try {
43
+ process.kill(process.pid, result.signal);
44
+ } catch {
45
+ // Signal cannot be re-raised on this platform (Windows emulation).
46
+ }
47
+ // Reached only if the re-raised signal did not terminate us — fall back
48
+ // to the shell's 128+n convention for death-by-signal.
49
+ const signum = osConstants.signals[result.signal];
50
+ process.exit(signum ? 128 + signum : 1);
51
+ }
13
52
  process.exit(result.status ?? 1);
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Build an actionable stderr message for a `spawnSync()` launch failure
3
+ * (`result.error` set — e.g. ENOENT/EACCES trying to invoke the resolved
4
+ * `@takazudo/zfb` launcher). Extracted as a pure function (no top-level
5
+ * side effects, unlike create-zfb.mjs) so it can be unit tested directly,
6
+ * mirroring the version-check.mjs pattern in this package.
7
+ *
8
+ * Mirrors packages/zfb/bin/zfb.mjs's `child.on("error", ...)` messaging
9
+ * (issues #447/#441) so both wrappers give the same reinstall guidance.
10
+ *
11
+ * @param {NodeJS.ErrnoException} error
12
+ * @param {string} zfbBin - absolute path to the resolved bin/zfb.mjs launcher
13
+ * @returns {string}
14
+ */
15
+ export function formatSpawnErrorMessage(error, zfbBin) {
16
+ if (error.code === "EACCES") {
17
+ return (
18
+ "[create-zfb] not executable; was the install corrupt?\n" +
19
+ ` ${zfbBin}\n` +
20
+ " Try reinstalling: npm install --include=optional\n"
21
+ );
22
+ }
23
+ return `[create-zfb] failed to spawn @takazudo/zfb: ${error.message}\n` + ` ${zfbBin}\n`;
24
+ }
@@ -0,0 +1,92 @@
1
+ // Extracted from create-zfb.mjs so compareSemver and checkForNewerVersion can
2
+ // be unit-tested directly. create-zfb.mjs has side-effecting top-level code
3
+ // (fetch, spawnSync), so importing it in a test would run the whole CLI —
4
+ // this module has none, and is safe to import.
5
+
6
+ // Returns -1 if a < b, 0 if a === b, 1 if a > b.
7
+ // Handles prerelease suffixes: 0.1.0-next.6 < 0.1.0-next.8 < 0.1.0
8
+ export function compareSemver(a, b) {
9
+ const splitRelease = (v) => {
10
+ // Drop SemVer build metadata (`+...`) before splitting — it is ignored
11
+ // for precedence and would otherwise yield NaN numeric parts.
12
+ const [main, pre] = v.split("+")[0].split(/-(.+)/, 2);
13
+ return { parts: main.split(".").map(Number), pre: pre ?? null };
14
+ };
15
+ const ra = splitRelease(a);
16
+ const rb = splitRelease(b);
17
+ for (let i = 0; i < Math.max(ra.parts.length, rb.parts.length); i++) {
18
+ const pa = ra.parts[i] ?? 0;
19
+ const pb = rb.parts[i] ?? 0;
20
+ if (pa !== pb) return pa < pb ? -1 : 1;
21
+ }
22
+ // same numeric parts — prerelease is less than no prerelease (semver rule)
23
+ if (ra.pre !== null && rb.pre === null) return -1;
24
+ if (ra.pre === null && rb.pre !== null) return 1;
25
+ if (ra.pre !== null && rb.pre !== null) {
26
+ // Compare dot-separated prerelease identifiers per semver: numeric
27
+ // identifiers compare numerically (so next.9 < next.10), numeric ranks
28
+ // below alphanumeric, and a shorter prefix ranks lower. Plain string
29
+ // `<`/`>` would order next.10 before next.9 lexicographically.
30
+ const sa = ra.pre.split(".");
31
+ const sb = rb.pre.split(".");
32
+ for (let i = 0; i < Math.max(sa.length, sb.length); i++) {
33
+ if (sa[i] === undefined) return -1;
34
+ if (sb[i] === undefined) return 1;
35
+ const na = /^\d+$/.test(sa[i]);
36
+ const nb = /^\d+$/.test(sb[i]);
37
+ if (na && nb) {
38
+ const da = Number(sa[i]);
39
+ const db = Number(sb[i]);
40
+ if (da !== db) return da < db ? -1 : 1;
41
+ } else if (na !== nb) {
42
+ return na ? -1 : 1;
43
+ } else if (sa[i] !== sb[i]) {
44
+ return sa[i] < sb[i] ? -1 : 1;
45
+ }
46
+ }
47
+ }
48
+ return 0;
49
+ }
50
+
51
+ /**
52
+ * Checks the npm registry for a newer create-zfb release than `version` and
53
+ * returns a warning message if the installed version is stale, or null if
54
+ * up to date. Never throws — network failure, timeout (AbortError), and
55
+ * parse errors are all treated as "no warning" so a registry hiccup can
56
+ * never block scaffolding. `fetchFn` and `timeoutMs` are injectable so tests
57
+ * don't need to hit the real network or wait out the real timeout.
58
+ */
59
+ export async function checkForNewerVersion({ version, fetchFn = fetch, timeoutMs = 1500 } = {}) {
60
+ try {
61
+ const controller = new AbortController();
62
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
63
+ try {
64
+ const res = await fetchFn("https://registry.npmjs.org/create-zfb/latest", {
65
+ signal: controller.signal,
66
+ headers: {
67
+ "User-Agent": `create-zfb/${version}`,
68
+ Accept: "application/json",
69
+ },
70
+ });
71
+ if (!res.ok) return null;
72
+ const data = await res.json();
73
+ const latest = data?.version;
74
+ if (typeof latest !== "string" || typeof version !== "string") return null;
75
+ if (compareSemver(version, latest) >= 0) return null;
76
+ return (
77
+ `[create-zfb] You are running create-zfb@${version} but @latest is ${latest}.\n` +
78
+ `[create-zfb] pnpm 11's minimumReleaseAge may have downgraded the install.\n` +
79
+ `[create-zfb] Re-run with: pnpm create zfb@latest --config.minimumReleaseAge=0\n` +
80
+ `[create-zfb] Or use: npm create zfb@latest\n`
81
+ );
82
+ } finally {
83
+ // Keep the abort signal armed across both the header and body reads.
84
+ // Only cancel the timer once the body has been fully consumed (or on
85
+ // any error path), so a proxy that stalls after headers cannot hang
86
+ // the scaffolder indefinitely.
87
+ clearTimeout(timer);
88
+ }
89
+ } catch {
90
+ return null;
91
+ }
92
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-zfb",
3
- "version": "0.1.0-next.8",
3
+ "version": "0.1.0-next.80",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Scaffold a new zfb static-site project: `npm create zfb@latest my-site`",
@@ -27,11 +27,22 @@
27
27
  },
28
28
  "files": [
29
29
  "bin",
30
+ "!bin/__tests__",
30
31
  "README.md",
31
32
  "CHANGELOG.md",
32
33
  "LICENSE"
33
34
  ],
35
+ "engines": {
36
+ "node": ">=18"
37
+ },
34
38
  "dependencies": {
35
- "@takazudo/zfb": "0.1.0-next.8"
39
+ "@takazudo/zfb": "0.1.0-next.80"
40
+ },
41
+ "devDependencies": {
42
+ "vitest": "^2.1.9"
43
+ },
44
+ "scripts": {
45
+ "test": "vitest run",
46
+ "test:watch": "vitest"
36
47
  }
37
48
  }