@join-pantheon/cli 0.38.2 → 0.38.3

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.
@@ -3,6 +3,7 @@ import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
3
3
  import { dirname, join } from "node:path";
4
4
  import { CONFIG_DIR } from "./config.js";
5
5
  import { repoRoot, PKG_NAME } from "./upgrade.js";
6
+ import { readRolledBack } from "./upgrade-probation.js";
6
7
  import { VERSION } from "./version.js";
7
8
  import { npmSpawn } from "./which.js";
8
9
  import { proxyEnvFor } from "./network.js";
@@ -44,9 +45,11 @@ export function updateStatus(deps) {
44
45
  return { behind: cache?.behind ?? 0, refreshed: false, latest: cache?.latest };
45
46
  }
46
47
  }
47
- export function versionLine(version, behind, latest) {
48
+ export function versionLine(version, behind, latest, held) {
48
49
  if (behind <= 0)
49
50
  return `pantheon ${version}`;
51
+ if (latest && held && latest === held)
52
+ return `pantheon ${version}`;
50
53
  if (latest && !semverGt(latest, version))
51
54
  return `pantheon ${version}`;
52
55
  return latest
@@ -94,10 +97,22 @@ export function defaultDeps(home) {
94
97
  }
95
98
  },
96
99
  current: () => VERSION,
100
+ held: () => { try {
101
+ return readRolledBack()?.version ?? null;
102
+ }
103
+ catch {
104
+ return null;
105
+ } },
97
106
  };
98
107
  }
99
108
  export function updateBanner(version, deps = defaultDeps()) {
100
109
  const { behind, latest } = updateStatus(deps);
101
- return versionLine(version, behind, latest);
110
+ const held = behind > 0 && latest ? (() => { try {
111
+ return deps.held?.() ?? null;
112
+ }
113
+ catch {
114
+ return null;
115
+ } })() : null;
116
+ return versionLine(version, behind, latest, held);
102
117
  }
103
118
  //# sourceMappingURL=update-check.js.map
@@ -1,4 +1,4 @@
1
- import { readFileSync, rmSync } from "node:fs";
1
+ import { closeSync, constants, fstatSync, linkSync, lstatSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
2
2
  import { posix, win32 } from "node:path";
3
3
  import { managedLayout } from "./managed.js";
4
4
  import { atomicWrite } from "./worker-policy.js";
@@ -6,21 +6,134 @@ export const PROBATION_FILE = "upgrade-probation.json";
6
6
  export const HELD_FILE = "upgrade-held.json";
7
7
  export const PROBATION_MS = 2 * 3600_000;
8
8
  export const FAILS_TO_ROLL_BACK = 3;
9
+ export const STEADY_RUN_MS = 60_000;
10
+ export const CLOCK_SLACK_MS = 60_000;
11
+ export const MARK_LOCK = `${PROBATION_FILE}.lock`;
12
+ export const LOCK_STALE_MS = 10_000;
13
+ export const LOCK_WAIT_MS = 400;
9
14
  export const NEVER_ONLINE_MS = 15 * 60_000;
15
+ export const NEVER_ONLINE_CHECK_MS = 60_000;
16
+ export const NEVER_ONLINE_CHECKS = 3;
10
17
  const isTime = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
18
+ const pidAlive = (pid) => {
19
+ if (!(pid > 0))
20
+ return false;
21
+ try {
22
+ process.kill(pid, 0);
23
+ return true;
24
+ }
25
+ catch (e) {
26
+ return e.code === "EPERM";
27
+ }
28
+ };
29
+ const pause = (ms) => { try {
30
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
31
+ }
32
+ catch { } };
33
+ const rand = () => Math.random().toString(16).slice(2, 10);
11
34
  export function runtimeDir(m) {
12
35
  return (/\.exe$/i.test(m.node) ? win32 : posix).dirname(m.prevDir);
13
36
  }
14
37
  const fileIn = (m, name) => (/\.exe$/i.test(m.node) ? win32 : posix).join(runtimeDir(m), name);
38
+ function readSmall(f) {
39
+ let fd;
40
+ try {
41
+ if (!statSync(f).isFile())
42
+ return null;
43
+ fd = openSync(f, constants.O_RDONLY | (constants.O_NONBLOCK || 0));
44
+ const st = fstatSync(fd);
45
+ if (!st.isFile() || st.size > 1_000_000)
46
+ return null;
47
+ return readFileSync(fd, "utf8").replace(/^/, "");
48
+ }
49
+ catch {
50
+ return null;
51
+ }
52
+ finally {
53
+ if (fd !== undefined) {
54
+ try {
55
+ closeSync(fd);
56
+ }
57
+ catch { }
58
+ }
59
+ }
60
+ }
15
61
  const readJson = (f) => {
16
62
  try {
17
- const v = JSON.parse(readFileSync(f, "utf8"));
18
- return v && typeof v === "object" ? v : null;
63
+ const t = readSmall(f);
64
+ const v = t === null ? null : JSON.parse(t);
65
+ return v && typeof v === "object" && !Array.isArray(v) ? v : null;
19
66
  }
20
67
  catch {
21
68
  return null;
22
69
  }
23
70
  };
71
+ export function withMarkLock(m, fn) {
72
+ const lock = fileIn(m, MARK_LOCK);
73
+ const token = `${process.pid} ${rand()}${rand()}`;
74
+ const deadline = Date.now() + LOCK_WAIT_MS;
75
+ let mine = false;
76
+ for (;;) {
77
+ try {
78
+ writeFileSync(lock, token, { flag: "wx", mode: 0o600 });
79
+ mine = true;
80
+ break;
81
+ }
82
+ catch (e) {
83
+ const code = e.code;
84
+ if (code !== "EEXIST" && !(process.platform === "win32" && (code === "EPERM" || code === "EACCES" || code === "EBUSY")))
85
+ break;
86
+ }
87
+ breakStaleLock(lock);
88
+ if (Date.now() >= deadline)
89
+ break;
90
+ pause(2 + Math.floor(Math.random() * 8));
91
+ }
92
+ try {
93
+ return fn();
94
+ }
95
+ finally {
96
+ if (mine) {
97
+ try {
98
+ if (readSmall(lock) === token)
99
+ rmSync(lock, { force: true });
100
+ }
101
+ catch { }
102
+ }
103
+ }
104
+ }
105
+ function breakStaleLock(lock) {
106
+ let st;
107
+ try {
108
+ st = lstatSync(lock);
109
+ }
110
+ catch {
111
+ return;
112
+ }
113
+ const age = Math.abs(Date.now() - st.mtimeMs);
114
+ const seen = st.isFile() ? readSmall(lock) : null;
115
+ const holder = /^(\d+) /.exec(seen ?? "");
116
+ const stale = age > LOCK_STALE_MS || (holder ? !pidAlive(Number(holder[1])) && age > 50 : age > 1_000);
117
+ if (!stale)
118
+ return;
119
+ const aside = `${lock}.stale-${process.pid}-${rand()}`;
120
+ try {
121
+ renameSync(lock, aside);
122
+ }
123
+ catch {
124
+ return;
125
+ }
126
+ if (st.isFile() && readSmall(aside) !== seen) {
127
+ try {
128
+ linkSync(aside, lock);
129
+ }
130
+ catch { }
131
+ }
132
+ try {
133
+ rmSync(aside, { recursive: true, force: true });
134
+ }
135
+ catch { }
136
+ }
24
137
  export function readProbation(m = managedLayout()) {
25
138
  if (!m)
26
139
  return null;
@@ -32,47 +145,57 @@ export function readProbation(m = managedLayout()) {
32
145
  export function probationText(from, to, now) {
33
146
  return JSON.stringify({ v: 1, ...(from ? { from } : {}), to, at: now, runs: [], fails: [] }, null, 2) + "\n";
34
147
  }
148
+ const expired = (p, now) => now - p.at > PROBATION_MS || p.at - now > CLOCK_SLACK_MS;
35
149
  export function onProbation(version, o = {}) {
36
150
  const p = readProbation(o.m === undefined ? managedLayout() : o.m);
37
- return !!p && p.to === version && !p.stuck && (o.now ?? Date.now()) - p.at <= PROBATION_MS;
151
+ return !!p && p.to === version && !p.stuck && !expired(p, o.now ?? Date.now());
38
152
  }
39
153
  export function probationPassed(version, o = {}) {
40
154
  const m = o.m === undefined ? managedLayout() : o.m;
41
- const p = readProbation(m);
42
- if (!m || !p || p.to !== version)
43
- return false;
44
- try {
45
- rmSync(fileIn(m, PROBATION_FILE), { force: true });
46
- return true;
47
- }
48
- catch {
155
+ if (!m)
49
156
  return false;
50
- }
157
+ return withMarkLock(m, () => {
158
+ const p = readProbation(m);
159
+ if (!p || p.to !== version)
160
+ return false;
161
+ try {
162
+ rmSync(fileIn(m, PROBATION_FILE), { force: true });
163
+ return true;
164
+ }
165
+ catch {
166
+ return false;
167
+ }
168
+ });
51
169
  }
52
- function changeProbation(m, fn) {
53
- const p = readProbation(m);
54
- if (!p)
55
- return false;
56
- fn(p);
57
- try {
58
- atomicWrite(fileIn(m, PROBATION_FILE), JSON.stringify(p, null, 2) + "\n");
59
- return true;
60
- }
61
- catch {
62
- return false;
63
- }
170
+ function changeProbation(m, version, fn) {
171
+ return withMarkLock(m, () => {
172
+ const p = readProbation(m);
173
+ if (!p || (version !== null && p.to !== version))
174
+ return false;
175
+ fn(p);
176
+ try {
177
+ atomicWrite(fileIn(m, PROBATION_FILE), JSON.stringify(p, null, 2) + "\n");
178
+ return true;
179
+ }
180
+ catch {
181
+ return false;
182
+ }
183
+ });
64
184
  }
65
185
  export function markNeverOnline(version, o = {}) {
66
186
  const m = o.m === undefined ? managedLayout() : o.m;
67
- if (!m || readProbation(m)?.to !== version)
187
+ if (!m)
68
188
  return false;
69
- return changeProbation(m, (p) => { p.never_online = o.now ?? Date.now(); });
189
+ return changeProbation(m, version, (p) => { p.never_online = o.now ?? Date.now(); });
70
190
  }
71
191
  export function probationStopExpected(o = {}) {
72
192
  const m = o.m === undefined ? managedLayout() : o.m;
73
193
  if (!m || !readProbation(m))
74
194
  return;
75
- changeProbation(m, (p) => { p.stop_expected_at = o.now ?? Date.now(); });
195
+ changeProbation(m, null, (p) => { p.stop_expected_at = o.now ?? Date.now(); });
196
+ }
197
+ export function replaceProbation(m, write) {
198
+ withMarkLock(m, write);
76
199
  }
77
200
  export function readRolledBack(m = managedLayout()) {
78
201
  if (!m)
@@ -84,13 +207,20 @@ export function readRolledBack(m = managedLayout()) {
84
207
  }
85
208
  export function markRolledBackTold(o = {}) {
86
209
  const m = o.m === undefined ? managedLayout() : o.m;
87
- const h = m ? readRolledBack(m) : null;
88
- if (!m || !h)
89
- return;
90
- try {
91
- atomicWrite(fileIn(m, HELD_FILE), JSON.stringify({ v: 1, ...h, told_at: o.now ?? Date.now() }, null, 2) + "\n");
92
- }
93
- catch { }
210
+ if (!m)
211
+ return false;
212
+ return withMarkLock(m, () => {
213
+ const h = readRolledBack(m);
214
+ if (!h || h.told_at)
215
+ return false;
216
+ try {
217
+ atomicWrite(fileIn(m, HELD_FILE), JSON.stringify({ v: 1, ...h, told_at: o.now ?? Date.now() }, null, 2) + "\n");
218
+ return true;
219
+ }
220
+ catch {
221
+ return false;
222
+ }
223
+ });
94
224
  }
95
225
  export function heldBack(pkg, o = {}) {
96
226
  if (!pkg)
@@ -98,17 +228,53 @@ export function heldBack(pkg, o = {}) {
98
228
  const h = readRolledBack(o.m === undefined ? managedLayout() : o.m);
99
229
  return h && h.version === pkg ? h : null;
100
230
  }
231
+ const whyWords = (h) => (h.why === "never_online" ? "could not reach your team" : "kept stopping");
232
+ const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
233
+ export function heldWhen(h) {
234
+ if (!isTime(h.at))
235
+ return "";
236
+ const d = new Date(h.at);
237
+ return `${d.getDate()} ${MONTHS[d.getMonth()]} at ${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
238
+ }
101
239
  export function rolledBackSentence(h) {
102
- const what = h.why === "never_online" ? "could not reach your team" : "kept stopping";
103
- return `Pantheon ${h.version} ${what} on this computer, so it went back to ${h.back_to} by itself. Nothing to do: it updates again when a newer release is out.`;
240
+ return `Pantheon ${h.version} ${whyWords(h)} on this computer, so it went back to ${h.back_to} by itself. Nothing to do: it updates again when a newer release is out.`;
241
+ }
242
+ export function heldUpgradeSentence(h) {
243
+ const when = heldWhen(h);
244
+ return `Pantheon ${h.version} ${whyWords(h)} on this computer and was put back to ${h.back_to}${when ? ` on ${when}` : ""}, so it is not installed again by itself. To try it again on purpose: pantheon upgrade ${h.version}`;
104
245
  }
105
246
  export function watchNeverOnline(o) {
106
- if (!onProbation(o.version, o.m === undefined ? {} : { m: o.m }))
247
+ const mo = o.m === undefined ? {} : { m: o.m };
248
+ const now = o.now ?? Date.now;
249
+ if (!onProbation(o.version, { ...mo, now: now() }))
107
250
  return null;
108
- const t = setTimeout(() => {
109
- void (async () => {
110
- if (o.connected() || !onProbation(o.version, o.m === undefined ? {} : { m: o.m }))
111
- return;
251
+ const after = o.afterMs ?? NEVER_ONLINE_MS, every = o.everyMs ?? NEVER_ONLINE_CHECK_MS, checks = o.checks ?? NEVER_ONLINE_CHECKS;
252
+ let last = now(), trying = 0, answered = 0, over = false, busy = false;
253
+ let timer = null;
254
+ const stop = () => { over = true; if (timer)
255
+ clearInterval(timer); timer = null; };
256
+ const tick = async () => {
257
+ if (over)
258
+ return "over";
259
+ if (busy)
260
+ return "watching";
261
+ busy = true;
262
+ try {
263
+ const t = now(), step = t - last;
264
+ last = t;
265
+ const awake = step > 0 && step <= every * 3;
266
+ if (!awake)
267
+ answered = 0;
268
+ if (o.reached() || !onProbation(o.version, { ...mo, now: t })) {
269
+ stop();
270
+ return "over";
271
+ }
272
+ if (!o.tried())
273
+ return "watching";
274
+ if (awake)
275
+ trying += step;
276
+ if (trying < after)
277
+ return "watching";
112
278
  let up = false;
113
279
  try {
114
280
  up = await o.relayAnswers();
@@ -116,15 +282,33 @@ export function watchNeverOnline(o) {
116
282
  catch {
117
283
  up = false;
118
284
  }
119
- if (!up || o.connected())
120
- return;
121
- if (!markNeverOnline(o.version, o.m === undefined ? {} : { m: o.m }))
122
- return;
123
- o.log?.(`! pantheon ${o.version} has not reached the team's relay in ${Math.round((o.afterMs ?? NEVER_ONLINE_MS) / 60_000)} minutes though the relay answers — going back to the previous version`);
285
+ if (over)
286
+ return "over";
287
+ if (o.reached()) {
288
+ stop();
289
+ return "over";
290
+ }
291
+ if (!up) {
292
+ answered = 0;
293
+ return "watching";
294
+ }
295
+ if (++answered < checks)
296
+ return "watching";
297
+ stop();
298
+ if (!markNeverOnline(o.version, { ...mo, now: now() }))
299
+ return "over";
300
+ o.log?.(`! pantheon ${o.version} has had no reply from the team's relay in ${Math.round(trying / 60_000)} minutes of trying, though the relay answers — going back to the previous version`);
124
301
  o.exit();
125
- })();
126
- }, o.afterMs ?? NEVER_ONLINE_MS);
127
- t.unref?.();
128
- return t;
302
+ return "marked";
303
+ }
304
+ finally {
305
+ busy = false;
306
+ }
307
+ };
308
+ if (!o.manual) {
309
+ timer = setInterval(() => { void tick().catch(() => { }); }, every);
310
+ timer.unref?.();
311
+ }
312
+ return { tick, stop };
129
313
  }
130
314
  //# sourceMappingURL=upgrade-probation.js.map
package/dist/upgrade.js CHANGED
@@ -8,7 +8,7 @@ import { proxyEnvFor } from "./network.js";
8
8
  import { relayProc, workerProc, notifierProc, menubarProc } from "./machine.js";
9
9
  import { load } from "./config.js";
10
10
  import { compareVersions, INSTALLER_MIN_CLI, isVersion } from "./installer.js";
11
- import { HELD_FILE, PROBATION_FILE, probationText } from "./upgrade-probation.js";
11
+ import { HELD_FILE, PROBATION_FILE, heldUpgradeSentence, probationText, readRolledBack, replaceProbation } from "./upgrade-probation.js";
12
12
  export const WINDOWS_PROFILE_ROOT_MIN = "0.33.1-0";
13
13
  import { childNodeFlags, installerLineHere, launcherWithRepair, managedLayout, nodeSatisfies, nodeTooOldSentence, runtimeNodeVersion } from "./managed.js";
14
14
  export function repoRoot() {
@@ -89,7 +89,7 @@ export function defaultDeps() {
89
89
  return up;
90
90
  },
91
91
  npmUpgrade: (version) => npm(npmInstallArgs(version), { stdio: ["ignore", "pipe", "pipe"] }),
92
- stagedUpgrade: (m, spec) => { stagedManagedUpgrade(m, spec); },
92
+ stagedUpgrade: (m, spec, o) => { stagedManagedUpgrade(m, spec, undefined, o); },
93
93
  npmLatest: () => {
94
94
  try {
95
95
  return npm(["view", `${PKG_NAME}`, "version"], { stdio: ["ignore", "pipe", "ignore"], timeout: 4000 }).trim() || null;
@@ -138,14 +138,33 @@ export function upgrade(deps = defaultDeps(), opts = {}) {
138
138
  return { ...stop("node", why), needsPerson: why };
139
139
  }
140
140
  }
141
+ const hold = managed && !opts.exact ? (() => { try {
142
+ return deps.held ? deps.held() : readRolledBack(managed);
143
+ }
144
+ catch {
145
+ return null;
146
+ } })() : null;
147
+ const heldReport = (h) => ({ root, from, to: from, steps: [...steps, { name: "check", ok: true, detail: heldUpgradeSentence(h) }], ok: true, alreadyCurrent: true, restart: [], heldBack: h });
148
+ if (hold) {
149
+ const target = spec === "latest" ? (() => { try {
150
+ return deps.npmLatest();
151
+ }
152
+ catch {
153
+ return null;
154
+ } })() : spec;
155
+ if (target === hold.version)
156
+ return heldReport(hold);
157
+ }
141
158
  try {
142
159
  if (managed && deps.stagedUpgrade)
143
- deps.stagedUpgrade(managed, spec);
160
+ deps.stagedUpgrade(managed, spec, hold ? { refuse: hold.version } : {});
144
161
  else
145
162
  deps.npmUpgrade(spec === "latest" ? undefined : spec);
146
163
  steps.push({ name: "npm", ok: true, detail: `installed ${PKG_NAME}@${spec}` });
147
164
  }
148
165
  catch (e) {
166
+ if (e instanceof UpgradeHeld && hold)
167
+ return heldReport(hold);
149
168
  if (e instanceof UpgradeBusy) {
150
169
  return { root, from, to: from, steps: [...steps, { name: "npm", ok: true, detail: e.message }], ok: true, alreadyCurrent: true, restart: [] };
151
170
  }
@@ -257,6 +276,7 @@ const realSwapIo = (m) => ({
257
276
  catch { }
258
277
  },
259
278
  alive: (pid) => pidAlive(pid),
279
+ markLock: (fn) => replaceProbation(m, fn),
260
280
  holdSignals: () => {
261
281
  const hold = () => { };
262
282
  const sigs = ["SIGINT", "SIGTERM", "SIGHUP", "SIGBREAK"].filter((x) => x !== "SIGBREAK" || process.platform === "win32");
@@ -306,7 +326,14 @@ export const realSwapIoForTest = realSwapIo;
306
326
  export class UpgradeBusy extends Error {
307
327
  constructor() { super("another pantheon upgrade is running on this machine; nothing was changed, and the next check tries again"); }
308
328
  }
309
- export function stagedManagedUpgrade(m, spec, io = realSwapIo(m)) {
329
+ export class UpgradeHeld extends Error {
330
+ version;
331
+ constructor(version) {
332
+ super(`pantheon ${version} was put back on this machine after it failed here; nothing was changed`);
333
+ this.version = version;
334
+ }
335
+ }
336
+ export function stagedManagedUpgrade(m, spec, io = realSwapIo(m), o = {}) {
310
337
  const win = /\.exe$/i.test(m.node);
311
338
  const P = win ? win32 : posix;
312
339
  const runtime = P.dirname(m.nodeDir);
@@ -398,6 +425,8 @@ export function stagedManagedUpgrade(m, spec, io = realSwapIo(m)) {
398
425
  catch { }
399
426
  if (!version || (spec !== "latest" && version !== spec))
400
427
  throw new Error(`npm installed ${version || "nothing readable"} into the staging folder, not ${spec}; nothing was changed`);
428
+ if (o.refuse && version === o.refuse)
429
+ throw new UpgradeHeld(version);
401
430
  const staged = startsAs(P.join(stagedPkg, "bin", "pantheon.js"));
402
431
  if (staged !== version)
403
432
  throw new Error(`the new pantheon ${version} did not start (it answered ${staged ?? "nothing"}); the working version was kept`);
@@ -437,12 +466,17 @@ export function stagedManagedUpgrade(m, spec, io = realSwapIo(m)) {
437
466
  ? `pantheon ${version} did not start once in place (it answered ${placed ?? "nothing"}); the previous version was put back`
438
467
  : `pantheon ${version} did not start once in place, and the previous version could not be put back yet. ${inPlace() ? `It is kept at ${prev}; the next upgrade puts it back, or run the installer again` : repairHint()}`);
439
468
  }
440
- if (from && from !== version && whole(prev)) {
441
- try {
442
- io.write(P.join(runtime, PROBATION_FILE), probationText(from, version, Date.now()));
443
- }
444
- catch { }
469
+ const mark = P.join(runtime, PROBATION_FILE);
470
+ const up = !!from && from !== version && (!isVersion(from) || !isVersion(version) || compareVersions(from, version) < 0);
471
+ try {
472
+ (io.markLock ?? ((fn) => fn()))(() => {
473
+ if (up && whole(prev))
474
+ io.write(mark, probationText(from, version, Date.now()));
475
+ else
476
+ io.remove(mark);
477
+ });
445
478
  }
479
+ catch { }
446
480
  tidy(P.join(runtime, HELD_FILE));
447
481
  return { version };
448
482
  }
package/dist/which.js CHANGED
@@ -190,7 +190,7 @@ catch {
190
190
  return { why: "was read but not recognised as a package manager's shim" };
191
191
  const target = resolve(dir, ...parts);
192
192
  if (!exists(target))
193
- return { why: `was read, but what it runs is not there (${target})` };
193
+ return { why: `was read, but what it runs is not there (${target})`, missing: target };
194
194
  if (/\.exe$/i.test(target))
195
195
  return { command: target, args: [] };
196
196
  if (/\.(?:cmd|bat)$/i.test(target)) {