@0perator/operator 0.1.354 → 0.1.355-next.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.
package/dist/update.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // `operator update` — self-update (bareword, like a CLI self-updater). Two install shapes:
2
- // • npm-global → the published package on the registry; update = reinstall @latest.
2
+ // • npm-global → the published package on the registry; update = reinstall a channel's dist-tag: @latest (main's
3
+ // releases, the default and `--latest`) or @next (`--next`: 0.1.N-next.K prereleases from develop).
3
4
  // • source/dev → a git checkout linked via `npm link`; update = pull + rebuild.
4
5
  // We detect the shape from where THIS module resolves: a checkout still has a .git above it;
5
6
  // a global install lives under a node_modules tree with no repo around it.
@@ -8,6 +9,15 @@ import { existsSync, readFileSync } from "node:fs";
8
9
  import { dirname, join, resolve } from "node:path";
9
10
  import { fileURLToPath } from "node:url";
10
11
  const PKG = "@0perator/operator"; // published (private) scope/name — keep in sync with package.json
12
+ /** `--next` or `--latest` (the default); `explicit` says a flag picked it. Both flags is a usage error. */
13
+ export function updateChannel(args) {
14
+ const next = args.includes("--next"), latest = args.includes("--latest");
15
+ if (next && latest)
16
+ return { error: "operator update: --next and --latest are two channels — pass one.\n" };
17
+ return { channel: next ? "next" : "latest", explicit: next || latest };
18
+ }
19
+ // A next build is a 0.1.N-next.K prerelease; a release is a plain 0.1.N.
20
+ const channelOf = (version) => (/-next\.\d+$/.test(version) ? "next" : "latest");
11
21
  // Walk up from the running file looking for a .git dir (source checkout) before we hit a
12
22
  // node_modules boundary (global install). Returns the repo root, or null for a packaged install.
13
23
  function findRepoRoot() {
@@ -28,8 +38,78 @@ function run(cmd, args, cwd) {
28
38
  const r = spawnSync(cmd, args, { stdio: "inherit", cwd });
29
39
  return r.status ?? 1;
30
40
  }
31
- export async function runUpdate() {
41
+ /** The registry's dist-tags for PKG (`{ latest: "0.1.352", next: "0.1.353-next.0" }`), or why they can't be read. */
42
+ function distTags() {
43
+ const r = spawnSync("npm", ["view", PKG, "dist-tags", "--json"], { encoding: "utf8" });
44
+ let out;
45
+ try {
46
+ const parsed = JSON.parse(r.stdout);
47
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
48
+ out = parsed;
49
+ }
50
+ catch { /* not JSON */ }
51
+ if (r.status === 0 && out && !("error" in out))
52
+ return { tags: out };
53
+ // With --json npm puts its own error on stdout ({ error: { code, summary } }); stderr ends with the log file's path.
54
+ const e = out?.error;
55
+ if (e?.code || e?.summary)
56
+ return { error: [e.code, e.summary].filter(Boolean).join(" ") };
57
+ const lines = `${r.stderr ?? ""}`.split("\n").map((l) => l.trim()).filter((l) => l && !/complete log of this run/i.test(l));
58
+ return { error: lines.slice(0, 2).join(" · ") || `npm exited ${r.status ?? r.signal}` };
59
+ }
60
+ function versionAt(pkgJson) {
61
+ try {
62
+ return JSON.parse(readFileSync(pkgJson, "utf8")).version;
63
+ }
64
+ catch {
65
+ return undefined;
66
+ }
67
+ }
68
+ /** npm-global update: reinstall PKG@<channel>, naming the channel and version before and after. It never falls back to
69
+ * the other channel: a tag the registry doesn't have, or a registry it can't read, installs nothing and exits 1.
70
+ * Before and after are read from THIS install's package.json (npm replaces it in place), not from `npm root -g`: npm
71
+ * redacts UUID-shaped path segments in its output as `***`. */
72
+ export function installChannel(channel, pkgJson = fileURLToPath(new URL("../package.json", import.meta.url))) {
73
+ const current = versionAt(pkgJson) ?? "?";
74
+ const read = distTags();
75
+ if ("error" in read) {
76
+ process.stderr.write(`operator update: can't read ${PKG}'s channels from the registry (${read.error}) — nothing installed. ` +
77
+ `If ${PKG} is private, run \`npm login\` for the 0perator org first.\n`);
78
+ return 1;
79
+ }
80
+ const target = read.tags[channel];
81
+ if (!target) {
82
+ const has = Object.entries(read.tags).map(([tag, v]) => `${tag} ${v}`).join(", ") || "no tags";
83
+ process.stderr.write(`operator update: ${PKG} has no ${channel} channel on the registry yet (it has ${has}) — nothing installed.\n`);
84
+ return 1;
85
+ }
86
+ process.stderr.write(`operator update: v${current} (${channelOf(current)}) → ${PKG}@${channel} (v${target})\n`);
87
+ const code = run("npm", ["install", "-g", `${PKG}@${channel}`]);
88
+ if (code !== 0) {
89
+ process.stderr.write(`operator update: failed. If ${PKG} is private, run \`npm login\` for the 0perator org first.\n`);
90
+ return code;
91
+ }
92
+ const now = versionAt(pkgJson);
93
+ if (now !== target) {
94
+ process.stderr.write(`operator update: npm finished, but this operator (${dirname(pkgJson)}) is v${now ?? "?"}, not v${target} — npm's ` +
95
+ `global prefix may hold another install: check \`which -a operator\` and \`npm ls -g ${PKG}\`.\n`);
96
+ return 1;
97
+ }
98
+ process.stderr.write(`operator update: done — now v${now} (${channel}).\n`);
99
+ return 0;
100
+ }
101
+ export async function runUpdate(args = []) {
102
+ const pick = updateChannel(args);
103
+ if ("error" in pick) {
104
+ process.stderr.write(pick.error);
105
+ return 2;
106
+ }
32
107
  const repo = findRepoRoot();
108
+ if (repo && pick.explicit) {
109
+ process.stderr.write(`operator update: --${pick.channel} picks an npm channel, but this operator runs from the source checkout at ${repo}, ` +
110
+ `which updates by git. To install the ${pick.channel} channel instead: npm install -g ${PKG}@${pick.channel}\n`);
111
+ return 1;
112
+ }
33
113
  if (repo) {
34
114
  // kode-tsi lives in a subdir of the kode repo; build happens where package.json is.
35
115
  const pkgDir = existsSync(join(repo, "kode-tsi", "package.json")) ? join(repo, "kode-tsi") : repo;
@@ -77,11 +157,5 @@ export async function runUpdate() {
77
157
  }
78
158
  return code;
79
159
  }
80
- process.stderr.write(`operator update: reinstalling ${PKG}@latest from the registry\n`);
81
- const code = run("npm", ["install", "-g", `${PKG}@latest`]);
82
- if (code === 0)
83
- process.stderr.write("operator update: done.\n");
84
- else
85
- process.stderr.write(`operator update: failed. If ${PKG} is private, run \`npm login\` for the 0perator org first.\n`);
86
- return code;
160
+ return installChannel(pick.channel);
87
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0perator/operator",
3
- "version": "0.1.354",
3
+ "version": "0.1.355-next.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "operator": "dist/cli.js",