@clagentic/console 1.7.0 → 1.7.1-beta.1

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.
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ // install-local-test.js — pack the current tree and install it globally (lr-0d45).
5
+ //
6
+ // Replaces the old `sh -c 'f=$(npm pack) && npm install -g "$f" && rm -f "$f"'`
7
+ // one-liner. That form broke because `$(npm pack)` captures npm's multi-line
8
+ // human-readable notices AND any output written to stdout by the `prepack`
9
+ // lifecycle hook (scripts/write-build-sha.js's own console.log) — not just the
10
+ // tarball filename. The resulting `$f` was a garbage multi-line string, so
11
+ // `npm install -g "$f"` failed with EINVALIDPACKAGENAME.
12
+ //
13
+ // `npm pack --json` also mixes prepack-hook stdout into the same stream, so
14
+ // this reads npm's output and extracts the tarball filename by parsing the
15
+ // trailing JSON array specifically (the only valid JSON substring `npm pack
16
+ // --json` ever emits), rather than trusting the whole stdout to be clean.
17
+
18
+ const { execFileSync } = require('child_process');
19
+
20
+ // Extracts the packed tarball filename from `npm pack --json`'s raw stdout.
21
+ // Exported (not just used internally) so tests can exercise the parsing
22
+ // logic against captured/synthetic npm output without shelling out to a
23
+ // real `npm pack` — see test/install-local-test-lr-0d45.test.js.
24
+ function extractTarballFilename(raw) {
25
+ // npm pack --json emits a JSON array `[ { ... } ]` (pretty-printed,
26
+ // multi-line, in practice), but the prepack lifecycle hook
27
+ // (scripts/write-build-sha.js) writes its own log line to the same stdout
28
+ // stream first, and that line itself starts with a literal '[' (its
29
+ // "[write-build-sha] wrote ..." prefix) — so a naive raw.indexOf('[') finds
30
+ // that '[' instead of the JSON array's. Rather than assume a specific
31
+ // whitespace shape around the array's opening bracket, try every '[' that
32
+ // starts a line as a candidate array start and parse from there to the end
33
+ // of the string, taking the first candidate that parses as a non-empty
34
+ // array of objects with a "filename" field. This tolerates both
35
+ // pretty-printed and single-line npm output across npm versions.
36
+ const lines = raw.split('\n');
37
+ let offset = 0;
38
+ const candidates = [];
39
+ for (const line of lines) {
40
+ if (line.startsWith('[')) {
41
+ candidates.push(offset);
42
+ }
43
+ offset += line.length + 1;
44
+ }
45
+
46
+ for (const start of candidates) {
47
+ let parsed;
48
+ try {
49
+ parsed = JSON.parse(raw.slice(start));
50
+ } catch {
51
+ continue;
52
+ }
53
+ if (Array.isArray(parsed) && parsed.length > 0 && typeof parsed[0].filename === 'string') {
54
+ // npm pack --json's "filename" field uses the @scope/name@version form
55
+ // with a literal '/' in place of the tarball's on-disk '-' separator;
56
+ // the actual file on disk uses '-'. Normalize to get the real path.
57
+ return parsed[0].filename.replace('/', '-');
58
+ }
59
+ }
60
+
61
+ throw new Error(`could not locate a valid \`npm pack --json\` array in output:\n${raw}`);
62
+ }
63
+
64
+ function packAndGetFilename() {
65
+ const raw = execFileSync('npm', ['pack', '--json'], { encoding: 'utf8' });
66
+ return extractTarballFilename(raw);
67
+ }
68
+
69
+ function main() {
70
+ let tarball;
71
+ try {
72
+ tarball = packAndGetFilename();
73
+ } catch (err) {
74
+ console.error(`[install-local-test] ERROR: ${err.message}`);
75
+ process.exit(1);
76
+ }
77
+
78
+ console.log(`[install-local-test] packed ${tarball}`);
79
+
80
+ try {
81
+ execFileSync('npm', ['install', '-g', tarball], { stdio: 'inherit' });
82
+ } finally {
83
+ try {
84
+ execFileSync('rm', ['-f', tarball]);
85
+ } catch (cleanupErr) {
86
+ console.error(`[install-local-test] WARN: could not remove ${tarball}: ${cleanupErr.message}`);
87
+ }
88
+ }
89
+
90
+ console.log('[install-local-test] ok: installed');
91
+ }
92
+
93
+ module.exports = { extractTarballFilename };
94
+
95
+ if (require.main === module) {
96
+ main();
97
+ }
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ // verify-installed-build.js — post-merge install assertion (lr-0d45).
5
+ //
6
+ // Confirms the globally-installed @clagentic/console build (installed by the
7
+ // preceding `npm run install:local-test` post_merge_steps entry) actually
8
+ // reflects the merged HEAD commit, rather than trusting a zero exit code from
9
+ // `npm install -g` alone. Compares the SHA embedded by scripts/write-build-sha.js
10
+ // (baked into the tarball at `prepack` time, so it travels with the package
11
+ // that `npm install -g` unpacked) against this working tree's own HEAD SHA at
12
+ // the moment loadout-merge runs post_merge_steps (i.e. the merged commit).
13
+ //
14
+ // Exit codes:
15
+ // 0 — installed build-sha.json matches this tree's HEAD.
16
+ // 1 — mismatch, or build-sha.json missing/unreadable, or HEAD unresolvable.
17
+ // Reported on stderr with both SHAs so the exact drift is visible in
18
+ // loadout-merge's captured step output.
19
+ //
20
+ // Run as a SEPARATE post_merge_steps entry (on_failure: fail) after
21
+ // `npm run install:local-test` — never combined into one shell string, per
22
+ // loadout-merge's shell-operator-token rejection (merge/post_merge.py).
23
+
24
+ const fs = require('fs');
25
+ const path = require('path');
26
+ const { execFileSync } = require('child_process');
27
+
28
+ function resolveHeadSha() {
29
+ return execFileSync('git', ['rev-parse', 'HEAD'], {
30
+ cwd: path.join(__dirname, '..'),
31
+ stdio: ['ignore', 'pipe', 'pipe'],
32
+ }).toString().trim();
33
+ }
34
+
35
+ function resolveInstalledSha() {
36
+ // `npm install -g` resolves the global node_modules root via `npm root -g`;
37
+ // read the build-sha.json that write-build-sha.js baked into the tarball
38
+ // at pack time (lib/ is in package.json's `files`, so it ships).
39
+ const globalRoot = execFileSync('npm', ['root', '-g'], {
40
+ stdio: ['ignore', 'pipe', 'pipe'],
41
+ }).toString().trim();
42
+ const installedPath = path.join(globalRoot, '@clagentic', 'console', 'lib', 'build-sha.json');
43
+ const raw = fs.readFileSync(installedPath, 'utf8');
44
+ const parsed = JSON.parse(raw);
45
+ if (!parsed || typeof parsed.sha !== 'string') {
46
+ throw new Error(`${installedPath} does not contain a "sha" field`);
47
+ }
48
+ return { sha: parsed.sha, path: installedPath };
49
+ }
50
+
51
+ function main() {
52
+ let headSha;
53
+ try {
54
+ headSha = resolveHeadSha();
55
+ } catch (err) {
56
+ console.error(`[verify-installed-build] ERROR: could not resolve this tree's HEAD SHA: ${err.message}`);
57
+ process.exit(1);
58
+ }
59
+
60
+ let installed;
61
+ try {
62
+ installed = resolveInstalledSha();
63
+ } catch (err) {
64
+ console.error(`[verify-installed-build] ERROR: could not read installed build-sha.json: ${err.message}`);
65
+ process.exit(1);
66
+ }
67
+
68
+ if (installed.sha !== headSha) {
69
+ console.error(
70
+ `[verify-installed-build] MISMATCH: installed build (${installed.path}) is at ` +
71
+ `${installed.sha}, but merged HEAD is ${headSha}. The post-merge install did ` +
72
+ `not land the merged commit — investigate before trusting the running build.`
73
+ );
74
+ process.exit(1);
75
+ }
76
+
77
+ console.log(`[verify-installed-build] ok: installed build matches merged HEAD (${headSha})`);
78
+ }
79
+
80
+ main();
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ // write-build-sha.js — embeds the current git commit SHA into lib/build-sha.json
5
+ // so a later install of this package can be checked against the source tree it
6
+ // was built from (lr-0d45: post-merge install verification).
7
+ //
8
+ // Runs as npm's `prepack` lifecycle hook — fires before `npm pack` builds the
9
+ // tarball, so `lib/build-sha.json` is always baked into what install:local-test
10
+ // ships. Also runs standalone via `node scripts/write-build-sha.js` for local
11
+ // debugging.
12
+ //
13
+ // Fails loudly (non-zero exit) if the SHA cannot be determined — a package
14
+ // packed without a resolvable SHA would make the post-install verification
15
+ // step (scripts/verify-installed-build.js) meaningless, so refuse to produce
16
+ // a stale/empty build-sha.json rather than pack one silently.
17
+
18
+ const fs = require('fs');
19
+ const path = require('path');
20
+ const { execFileSync } = require('child_process');
21
+
22
+ const OUT_PATH = path.join(__dirname, '..', 'lib', 'build-sha.json');
23
+
24
+ function resolveSha() {
25
+ try {
26
+ return execFileSync('git', ['rev-parse', 'HEAD'], {
27
+ cwd: path.join(__dirname, '..'),
28
+ stdio: ['ignore', 'pipe', 'pipe'],
29
+ }).toString().trim();
30
+ } catch (err) {
31
+ const stderr = err.stderr ? err.stderr.toString().trim() : err.message;
32
+ throw new Error(`could not resolve git HEAD SHA: ${stderr}`);
33
+ }
34
+ }
35
+
36
+ function main() {
37
+ let sha;
38
+ try {
39
+ sha = resolveSha();
40
+ } catch (err) {
41
+ console.error(`[write-build-sha] ERROR: ${err.message}`);
42
+ process.exit(1);
43
+ }
44
+ if (!/^[0-9a-f]{40}$/i.test(sha)) {
45
+ console.error(`[write-build-sha] ERROR: resolved SHA does not look like a git commit hash: ${JSON.stringify(sha)}`);
46
+ process.exit(1);
47
+ }
48
+ const payload = { sha, writtenAt: new Date().toISOString() };
49
+ fs.writeFileSync(OUT_PATH, JSON.stringify(payload, null, 2) + '\n');
50
+ console.log(`[write-build-sha] wrote ${OUT_PATH} (sha=${sha})`);
51
+ }
52
+
53
+ main();