omp-config-crypt 0.8.1 → 0.9.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/README.md +8 -0
- package/bin/omp-sync.js +23 -0
- package/index.js +166 -4
- package/package.json +1 -1
- package/src/preflight.js +231 -0
package/README.md
CHANGED
|
@@ -99,8 +99,16 @@ Configuration exports include the installed `omp-config-crypt` source (excluding
|
|
|
99
99
|
/config-pull https://… [--dry-run] [--force]
|
|
100
100
|
/config-rollback
|
|
101
101
|
/config-upgrade [--check]
|
|
102
|
+
/config-doctor [gist:<id> | https://…]
|
|
102
103
|
```
|
|
103
104
|
|
|
105
|
+
`/config-pull` (and `omp-sync pull`) runs a preflight before downloading: corrupt sync
|
|
106
|
+
metadata is healed in place, an outdated self version is upgraded via npm (restart omp to
|
|
107
|
+
activate), and plugin dependencies missing after the plan are auto-installed and the plan
|
|
108
|
+
recomputed once. Unfixable findings (missing npm, unreachable/wrong target) fail with exact
|
|
109
|
+
next-step commands. `/config-doctor` (or `omp-sync doctor [gist:<id>|https://url]`) runs the
|
|
110
|
+
same checks standalone; exit code 1 when any check fails.
|
|
111
|
+
|
|
104
112
|
### Provider control menu
|
|
105
113
|
|
|
106
114
|
Run `/provider` in an interactive OMP session for the native keyboard menu (↑↓ / Enter / Esc).
|
package/bin/omp-sync.js
CHANGED
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
inventoryResources,
|
|
41
41
|
readSyncState,
|
|
42
42
|
formatSyncReport,
|
|
43
|
+
runPreflight,
|
|
43
44
|
} from "../index.js";
|
|
44
45
|
function printHelp() {
|
|
45
46
|
console.log(`
|
|
@@ -62,6 +63,8 @@ Commands:
|
|
|
62
63
|
conflicts by taking remote or local side; default:
|
|
63
64
|
report conflicts and abort)
|
|
64
65
|
rollback Revert the most recent config restore from its backup
|
|
66
|
+
doctor [target] Preflight env/config checks with self-healing
|
|
67
|
+
(optional gist:<id> or https://url target)
|
|
65
68
|
rehydrate Rehydrate and repair locked/linked plugins in ~/.omp/plugins
|
|
66
69
|
maint / gc [--force] Run automatic storage maintenance, WAL checkpoint & GC
|
|
67
70
|
cron [install|status|rm] Manage OS-level scheduled crontab maintenance job
|
|
@@ -376,6 +379,11 @@ async function main() {
|
|
|
376
379
|
if (res.adoptedNamespace) {
|
|
377
380
|
console.log(`[pull] Adopted remote namespace ${res.adoptedNamespace} (--force); prior policy/state saved as .bak`);
|
|
378
381
|
}
|
|
382
|
+
if (Array.isArray(res.preflight) && res.preflight.length > 0) {
|
|
383
|
+
const tally = { pass: 0, fixed: 0, warn: 0 };
|
|
384
|
+
for (const c of res.preflight) tally[c.status] = (tally[c.status] || 0) + 1;
|
|
385
|
+
console.log(`[pull] preflight: ${tally.pass} pass, ${tally.fixed} fixed, ${tally.warn} warn`);
|
|
386
|
+
}
|
|
379
387
|
if (Array.isArray(res.healed) && res.healed.length > 0) {
|
|
380
388
|
console.log(`[pull] Self-healed: ${res.healed.join("; ")}`);
|
|
381
389
|
}
|
|
@@ -424,6 +432,16 @@ async function main() {
|
|
|
424
432
|
break;
|
|
425
433
|
}
|
|
426
434
|
|
|
435
|
+
case "doctor": {
|
|
436
|
+
const target = options.target ? parseSpec(options.target) : null;
|
|
437
|
+
const pf = await runPreflight({ root, target, token: options.token ?? null });
|
|
438
|
+
for (const c of pf.checks) {
|
|
439
|
+
console.log(`[${c.status.toUpperCase()}] ${c.id}: ${c.detail}`);
|
|
440
|
+
}
|
|
441
|
+
if (!pf.ok) process.exitCode = 1;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
|
|
427
445
|
case "rehydrate": {
|
|
428
446
|
const res = rehydratePlugins(root, { dryRun: options.dryRun });
|
|
429
447
|
console.log(
|
|
@@ -742,6 +760,11 @@ async function main() {
|
|
|
742
760
|
process.exit(1);
|
|
743
761
|
}
|
|
744
762
|
} catch (err) {
|
|
763
|
+
if (Array.isArray(err?.checks)) {
|
|
764
|
+
for (const c of err.checks) {
|
|
765
|
+
console.error(` [${c.status.toUpperCase()}] ${c.id}: ${c.detail}`);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
745
768
|
console.error(`[error] ${err.stack || err.message || err}`);
|
|
746
769
|
process.exit(1);
|
|
747
770
|
}
|
package/index.js
CHANGED
|
@@ -8,11 +8,13 @@ import { gzipSync, gunzipSync } from "node:zlib";
|
|
|
8
8
|
import fs from "node:fs";
|
|
9
9
|
import os from "node:os";
|
|
10
10
|
import path from "node:path";
|
|
11
|
+
import { execFileSync } from "node:child_process";
|
|
11
12
|
import {
|
|
12
13
|
rehydratePlugins,
|
|
13
14
|
getPluginLinks,
|
|
14
15
|
savePluginLinks,
|
|
15
16
|
detectMissingPlugins,
|
|
17
|
+
resolveNpmCommand,
|
|
16
18
|
} from "./src/rehydrator.js";
|
|
17
19
|
|
|
18
20
|
export {
|
|
@@ -20,6 +22,18 @@ export {
|
|
|
20
22
|
getPluginLinks,
|
|
21
23
|
savePluginLinks,
|
|
22
24
|
detectMissingPlugins,
|
|
25
|
+
resolveNpmCommand,
|
|
26
|
+
};
|
|
27
|
+
import {
|
|
28
|
+
runPreflight,
|
|
29
|
+
probeGistAccess,
|
|
30
|
+
probeHttpAccess,
|
|
31
|
+
} from "./src/preflight.js";
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
runPreflight,
|
|
35
|
+
probeGistAccess,
|
|
36
|
+
probeHttpAccess,
|
|
23
37
|
};
|
|
24
38
|
import {
|
|
25
39
|
probeProvider,
|
|
@@ -97,6 +111,7 @@ import {
|
|
|
97
111
|
import {
|
|
98
112
|
inventoryResources,
|
|
99
113
|
readResource,
|
|
114
|
+
canonicalResourceValue,
|
|
100
115
|
sanitizeResourceForExport,
|
|
101
116
|
restoreResourceForImport,
|
|
102
117
|
HOME_PLACEHOLDER,
|
|
@@ -341,6 +356,82 @@ export function adaptArchive(blob, pass) {
|
|
|
341
356
|
};
|
|
342
357
|
}
|
|
343
358
|
|
|
359
|
+
/**
|
|
360
|
+
* Installs exact plugin package versions through npm (or an injected installer).
|
|
361
|
+
* Returns the fixed "name@version" entries plus names skipped for missing versions.
|
|
362
|
+
*/
|
|
363
|
+
function installPluginPackages(entries, root, install) {
|
|
364
|
+
const npmCmd = resolveNpmCommand();
|
|
365
|
+
const doInstall = install ?? ((name, version) => {
|
|
366
|
+
execFileSync(
|
|
367
|
+
npmCmd.file,
|
|
368
|
+
["install", "--prefix", path.join(ompHome(root), "plugins"), `${name}@${version}`],
|
|
369
|
+
{ stdio: "pipe", shell: npmCmd.shell },
|
|
370
|
+
);
|
|
371
|
+
});
|
|
372
|
+
const fixed = [];
|
|
373
|
+
const skipped = [];
|
|
374
|
+
for (const { name, version } of entries) {
|
|
375
|
+
if (!version) {
|
|
376
|
+
skipped.push(name);
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
doInstall(name, version);
|
|
381
|
+
} catch (err) {
|
|
382
|
+
throw new Error(`plugin dependency install failed for ${name}@${version}: ${err.message}`);
|
|
383
|
+
}
|
|
384
|
+
fixed.push(`${name}@${version}`);
|
|
385
|
+
}
|
|
386
|
+
return { fixed, skipped };
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Installs remote-declared plugins whose local declaration can no longer be
|
|
391
|
+
* canonicalized — a range dependency (e.g. "^0.1.2") whose node_modules vanished
|
|
392
|
+
* makes planSync throw before the blocked-plugin path can trigger. Restoring the
|
|
393
|
+
* exact remote version first keeps the plan buildable.
|
|
394
|
+
*/
|
|
395
|
+
export function repairUnreadablePlugins(payload, root, { install } = {}) {
|
|
396
|
+
const entries = [];
|
|
397
|
+
for (const resource of payload?.resources || []) {
|
|
398
|
+
const id = String(resource?.id || "");
|
|
399
|
+
if (!id.startsWith("plugin:")) continue;
|
|
400
|
+
const version = resource.value?.version;
|
|
401
|
+
if (!version) continue;
|
|
402
|
+
let local = null;
|
|
403
|
+
try {
|
|
404
|
+
local = readResource(root, id);
|
|
405
|
+
} catch { local = null; }
|
|
406
|
+
if (!local) continue;
|
|
407
|
+
try {
|
|
408
|
+
canonicalResourceValue(id, local.value, root);
|
|
409
|
+
} catch {
|
|
410
|
+
entries.push({ name: id.slice("plugin:".length), version });
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return installPluginPackages(entries, root, install).fixed;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Installs the missing or mismatched plugin dependencies a sync plan blocked on.
|
|
418
|
+
* Versions come from the plan action value when present (internal/hand-built plans);
|
|
419
|
+
* public plans sanitize action values away, so the snapshot payload is consulted as
|
|
420
|
+
* fallback. Skipped entries keep their blocked state and surface as apply errors.
|
|
421
|
+
*/
|
|
422
|
+
export function fixPluginPrerequisites(plan, root, { install, payload = null } = {}) {
|
|
423
|
+
const payloadVersions = new Map((payload?.resources || []).map((resource) => [resource.id, resource.value]));
|
|
424
|
+
const entries = [];
|
|
425
|
+
for (const action of plan.actions || []) {
|
|
426
|
+
if (action.action !== "blocked" || !String(action.id || "").startsWith("plugin:")) continue;
|
|
427
|
+
if (action.reason !== "plugin-not-installed" && action.reason !== "plugin-version-mismatch") continue;
|
|
428
|
+
const name = action.id.slice("plugin:".length);
|
|
429
|
+
const version = action.value?.version ?? payloadVersions.get(action.id)?.version ?? null;
|
|
430
|
+
entries.push({ name, version });
|
|
431
|
+
}
|
|
432
|
+
return installPluginPackages(entries, root, install);
|
|
433
|
+
}
|
|
434
|
+
|
|
344
435
|
/**
|
|
345
436
|
* Restores an encrypted archive buffer into the config tree with 3-way merge planning and journaled transaction.
|
|
346
437
|
*/
|
|
@@ -364,7 +455,22 @@ export function restoreArchive(blob, pass, root = configRoot(), options = {}) {
|
|
|
364
455
|
}
|
|
365
456
|
|
|
366
457
|
const dryRun = Boolean(options.dryRun);
|
|
367
|
-
|
|
458
|
+
|
|
459
|
+
// Self-healing stage 1: repair local plugin declarations that would break planning
|
|
460
|
+
// (range dependency with a vanished install). Must run before planSync.
|
|
461
|
+
if (!dryRun && options.autoFixPlugins !== false) {
|
|
462
|
+
repairUnreadablePlugins(effectivePayload, root, { install: options.installFn });
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
let plan = planSync(effectivePayload, root, { strategy: options.strategy });
|
|
466
|
+
|
|
467
|
+
// Self-healing stage 2: install plugin dependencies the plan blocked on, then replan
|
|
468
|
+
// once (the install mutates the plugins tree, so the original plan is stale). Remaining
|
|
469
|
+
// blocked actions still fail the apply.
|
|
470
|
+
if (!dryRun && plan.prerequisites.length > 0 && options.autoFixPlugins !== false) {
|
|
471
|
+
const fx = fixPluginPrerequisites(plan, root, { install: options.installFn, payload: effectivePayload });
|
|
472
|
+
if (fx.fixed.length > 0) plan = planSync(effectivePayload, root, { strategy: options.strategy });
|
|
473
|
+
}
|
|
368
474
|
|
|
369
475
|
if (dryRun) {
|
|
370
476
|
return {
|
|
@@ -705,14 +811,37 @@ export function restoreRemoteArchive(blob, pass, root = configRoot(), options =
|
|
|
705
811
|
};
|
|
706
812
|
}
|
|
707
813
|
|
|
814
|
+
/**
|
|
815
|
+
* Runs the pull preflight before any download; throws a preflight-failed error
|
|
816
|
+
* carrying the full checks list when a fail-status check blocks the pull.
|
|
817
|
+
*/
|
|
818
|
+
async function gatePullPreflight(root, target, token, options = {}) {
|
|
819
|
+
if (options.preflight === false) return [];
|
|
820
|
+
const pf = await runPreflight({ root, target, token, dryRun: Boolean(options.dryRun) });
|
|
821
|
+
if (!pf.ok) {
|
|
822
|
+
const err = new Error(
|
|
823
|
+
`preflight failed:\n${pf.checks
|
|
824
|
+
.filter((check) => check.status === "fail")
|
|
825
|
+
.map((check) => ` [FAIL] ${check.id}: ${check.detail}`)
|
|
826
|
+
.join("\n")}`,
|
|
827
|
+
);
|
|
828
|
+
err.checks = pf.checks;
|
|
829
|
+
throw err;
|
|
830
|
+
}
|
|
831
|
+
return pf.checks;
|
|
832
|
+
}
|
|
833
|
+
|
|
708
834
|
/** Pull the archive from a secret gist. */
|
|
709
835
|
export async function pullGist(id, pass, root = configRoot(), token = envToken(), options = {}) {
|
|
710
836
|
if (!token) throw new Error("GitHub token required: set OMP_CFG_CLOUD_TOKEN or GITHUB_TOKEN");
|
|
837
|
+
const preflightChecks = await gatePullPreflight(root, { kind: "gist", id }, token, options);
|
|
711
838
|
const data = await gh(token, `/gists/${id}`);
|
|
712
839
|
const content = data?.files?.[GIST_FILE]?.content;
|
|
713
840
|
if (!content) throw new Error(`gist ${id} has no ${GIST_FILE}`);
|
|
714
841
|
const blob = Buffer.from(content, "base64");
|
|
715
|
-
|
|
842
|
+
const result = restoreRemoteArchive(blob, pass, root, options);
|
|
843
|
+
result.preflight = preflightChecks;
|
|
844
|
+
return result;
|
|
716
845
|
}
|
|
717
846
|
|
|
718
847
|
/** Push to any endpoint accepting PUT (presigned S3, WebDAV, custom). */
|
|
@@ -740,13 +869,16 @@ export async function pushHttp(url, pass, root = configRoot(), token = envToken(
|
|
|
740
869
|
|
|
741
870
|
/** Pull from any endpoint serving the archive. */
|
|
742
871
|
export async function pullHttp(url, pass, root = configRoot(), token = envToken(), options = {}) {
|
|
872
|
+
const preflightChecks = await gatePullPreflight(root, { kind: "http", url }, token, options);
|
|
743
873
|
const res = await fetchWithRetry(url, {
|
|
744
874
|
signal: AbortSignal.timeout(30_000),
|
|
745
875
|
headers: token ? { authorization: `Bearer ${token}` } : {},
|
|
746
876
|
});
|
|
747
877
|
if (!res.ok) throw new Error(`GET ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
748
878
|
const blob = Buffer.from(await res.arrayBuffer());
|
|
749
|
-
|
|
879
|
+
const result = restoreRemoteArchive(blob, pass, root, options);
|
|
880
|
+
result.preflight = preflightChecks;
|
|
881
|
+
return result;
|
|
750
882
|
}
|
|
751
883
|
|
|
752
884
|
export function parseSpec(spec) {
|
|
@@ -1106,6 +1238,12 @@ export default function (pi) {
|
|
|
1106
1238
|
const r = target.kind === "gist"
|
|
1107
1239
|
? await pullGist(target.id, pass, configRoot(), token, { dryRun, force, strategy })
|
|
1108
1240
|
: await pullHttp(target.url, pass, configRoot(), token, { dryRun, force, strategy });
|
|
1241
|
+
if (Array.isArray(r.preflight) && r.preflight.length > 0) {
|
|
1242
|
+
ctx.ui.notify(
|
|
1243
|
+
"[pull] preflight:\n" + r.preflight.map((c) => ` [${c.status.toUpperCase()}] ${c.id}: ${c.detail}`).join("\n"),
|
|
1244
|
+
"info",
|
|
1245
|
+
);
|
|
1246
|
+
}
|
|
1109
1247
|
if (r.adoptedNamespace) {
|
|
1110
1248
|
ctx.ui.notify(`[pull] adopted remote namespace ${r.adoptedNamespace}; prior policy/state saved as .bak`, "info");
|
|
1111
1249
|
}
|
|
@@ -1122,7 +1260,10 @@ export default function (pi) {
|
|
|
1122
1260
|
}
|
|
1123
1261
|
} catch (error) {
|
|
1124
1262
|
const report = error?.plan ? `\n${formatSyncReport(error.plan)}` : "";
|
|
1125
|
-
|
|
1263
|
+
const checkLines = Array.isArray(error?.checks)
|
|
1264
|
+
? `\n${error.checks.map((c) => ` [${c.status.toUpperCase()}] ${c.id}: ${c.detail}`).join("\n")}`
|
|
1265
|
+
: "";
|
|
1266
|
+
ctx.ui.notify(`config-pull failed: ${error?.message ?? error}${report}${checkLines}`, "error");
|
|
1126
1267
|
}
|
|
1127
1268
|
},
|
|
1128
1269
|
});
|
|
@@ -1154,6 +1295,27 @@ export default function (pi) {
|
|
|
1154
1295
|
},
|
|
1155
1296
|
});
|
|
1156
1297
|
|
|
1298
|
+
pi.registerCommand("config-doctor", {
|
|
1299
|
+
description: "Preflight environment/config checks with self-healing (optional gist:<id> or https target)",
|
|
1300
|
+
handler: async (args, ctx) => {
|
|
1301
|
+
try {
|
|
1302
|
+
const spec = String(args || "").trim();
|
|
1303
|
+
let target = null;
|
|
1304
|
+
let token = envToken();
|
|
1305
|
+
if (spec) {
|
|
1306
|
+
target = parseSpec(spec);
|
|
1307
|
+
const asked = await askToken(ctx, "Cloud token", target.kind === "gist");
|
|
1308
|
+
if (asked) token = asked;
|
|
1309
|
+
}
|
|
1310
|
+
const pf = await runPreflight({ root: configRoot(), target, token });
|
|
1311
|
+
const lines = pf.checks.map((c) => ` [${c.status.toUpperCase()}] ${c.id}: ${c.detail}`).join("\n");
|
|
1312
|
+
ctx.ui.notify(`[doctor] ${pf.ok ? "all checks passed" : "checks failed"}\n${lines}`, pf.ok ? "info" : "error");
|
|
1313
|
+
} catch (error) {
|
|
1314
|
+
ctx.ui.notify(`config-doctor failed: ${error?.message ?? error}`, "error");
|
|
1315
|
+
}
|
|
1316
|
+
},
|
|
1317
|
+
});
|
|
1318
|
+
|
|
1157
1319
|
pi.registerCommand("provider", {
|
|
1158
1320
|
description: "Provider control menu · ls | add | edit <name> | test <name> | rm <name>",
|
|
1159
1321
|
getArgumentCompletions: prefix => providerCompletions(prefix, configRoot()),
|
package/package.json
CHANGED
package/src/preflight.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pull lifecycle preflight: environment and configuration checks with self-healing.
|
|
3
|
+
* Machine-fixable problems (corrupt sync metadata, outdated self version) are repaired
|
|
4
|
+
* in place; unfixable ones report the exact next command. Every check accepts injected
|
|
5
|
+
* dependencies (fetchImpl / upgradeFn) so the test suite stays fully offline.
|
|
6
|
+
*/
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { readSyncPolicy } from "./sync-policy.js";
|
|
10
|
+
import { readSyncState, healSyncMetadata } from "./sync-plan.js";
|
|
11
|
+
import { resolveNpmCommand } from "./rehydrator.js";
|
|
12
|
+
import { runUpgrade } from "./maintenance.js";
|
|
13
|
+
|
|
14
|
+
const GIST_API = "https://api.github.com/gists/";
|
|
15
|
+
const PROBE_TIMEOUT_MS = 30_000;
|
|
16
|
+
|
|
17
|
+
function selfPackage() {
|
|
18
|
+
return JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function nodeEngineFloor() {
|
|
22
|
+
const match = /^>=\s*(\d+)/.exec(String(selfPackage().engines?.node || ""));
|
|
23
|
+
return match ? Number(match[1]) : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Availability check without spawning a process: npm must exist on PATH because
|
|
28
|
+
* plugin dependency installs and self-upgrades both shell out through it.
|
|
29
|
+
*/
|
|
30
|
+
function npmOnPath(npmCmd) {
|
|
31
|
+
const dirs = String(process.env.PATH || process.env.Path || "").split(path.delimiter).filter(Boolean);
|
|
32
|
+
for (const dir of dirs) {
|
|
33
|
+
try {
|
|
34
|
+
fs.accessSync(path.join(dir, npmCmd.file), fs.constants.X_OK);
|
|
35
|
+
return true;
|
|
36
|
+
} catch { /* keep scanning */ }
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function metadataCorrupt(root) {
|
|
42
|
+
try {
|
|
43
|
+
readSyncPolicy(root);
|
|
44
|
+
} catch {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
readSyncState(root);
|
|
49
|
+
} catch {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Probes gist readability with the same headers the pull transport uses.
|
|
57
|
+
* HEAD is not supported by the gists endpoint, so this is a GET; it does not
|
|
58
|
+
* read or parse the body.
|
|
59
|
+
*/
|
|
60
|
+
export async function probeGistAccess(id, token, fetchImpl = fetch) {
|
|
61
|
+
try {
|
|
62
|
+
const res = await fetchImpl(`${GIST_API}${encodeURIComponent(id)}`, {
|
|
63
|
+
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
|
|
64
|
+
headers: {
|
|
65
|
+
accept: "application/vnd.github+json",
|
|
66
|
+
...(token ? { authorization: `Bearer ${token}` } : {}),
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (res.status === 401 || res.status === 403) {
|
|
70
|
+
return {
|
|
71
|
+
ok: false,
|
|
72
|
+
status: res.status,
|
|
73
|
+
message: `token unauthorized (${res.status}): ensure OMP_CFG_CLOUD_TOKEN has 'gist' scope and belongs to the gist owner`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (res.status === 404) {
|
|
77
|
+
return {
|
|
78
|
+
ok: false,
|
|
79
|
+
status: res.status,
|
|
80
|
+
message: "gist not found (404): wrong id, or this token cannot see the gist",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (res.ok) return { ok: true, status: res.status, message: "gist accessible" };
|
|
84
|
+
return { ok: false, status: res.status, message: `GitHub API ${res.status} while probing the gist` };
|
|
85
|
+
} catch (err) {
|
|
86
|
+
return { ok: false, status: null, message: `api.github.com unreachable: ${err.message}` };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Probes a plain HTTP(S) target serving the archive.
|
|
92
|
+
*/
|
|
93
|
+
export async function probeHttpAccess(url, fetchImpl = fetch) {
|
|
94
|
+
try {
|
|
95
|
+
const res = await fetchImpl(url, { signal: AbortSignal.timeout(PROBE_TIMEOUT_MS) });
|
|
96
|
+
const ok = res.status >= 200 && res.status < 400;
|
|
97
|
+
return {
|
|
98
|
+
ok,
|
|
99
|
+
status: res.status,
|
|
100
|
+
message: ok ? `target reachable (HTTP ${res.status})` : `HTTP ${res.status} from target`,
|
|
101
|
+
};
|
|
102
|
+
} catch (err) {
|
|
103
|
+
return { ok: false, status: null, message: `target unreachable: ${err.message}` };
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Runs the ordered preflight checklist for a pull (or a standalone doctor run).
|
|
109
|
+
* Returns `{ ok, checks }` where ok is true unless any check reports "fail";
|
|
110
|
+
* "fixed" marks issues repaired in place, "warn" marks non-blocking findings.
|
|
111
|
+
*/
|
|
112
|
+
export async function runPreflight({
|
|
113
|
+
root,
|
|
114
|
+
target = null,
|
|
115
|
+
token = null,
|
|
116
|
+
dryRun = false,
|
|
117
|
+
fetchImpl = null,
|
|
118
|
+
upgradeFn = runUpgrade,
|
|
119
|
+
} = {}) {
|
|
120
|
+
const checks = [];
|
|
121
|
+
const doFetch = fetchImpl || fetch;
|
|
122
|
+
|
|
123
|
+
// metadata: sync policy/state must be readable; heal in place unless dry-run.
|
|
124
|
+
try {
|
|
125
|
+
if (metadataCorrupt(root)) {
|
|
126
|
+
if (dryRun) {
|
|
127
|
+
checks.push({ id: "metadata", status: "warn", detail: "corrupt; would heal on apply" });
|
|
128
|
+
} else {
|
|
129
|
+
const healed = healSyncMetadata(root).healed;
|
|
130
|
+
if (healed.length > 0) {
|
|
131
|
+
checks.push({ id: "metadata", status: "fixed", detail: healed.join("; ") });
|
|
132
|
+
} else {
|
|
133
|
+
checks.push({ id: "metadata", status: "pass", detail: "policy and state readable" });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
checks.push({ id: "metadata", status: "pass", detail: "policy and state readable" });
|
|
138
|
+
}
|
|
139
|
+
} catch (err) {
|
|
140
|
+
checks.push({ id: "metadata", status: "fail", detail: `sync metadata not repairable: ${err.message}` });
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// node-engine: informational gate against package.json engines (warn only).
|
|
144
|
+
const nodeMajor = Number(process.versions.node.split(".")[0]);
|
|
145
|
+
const floor = nodeEngineFloor();
|
|
146
|
+
if (floor !== null && nodeMajor < floor) {
|
|
147
|
+
checks.push({
|
|
148
|
+
id: "node-engine",
|
|
149
|
+
status: "warn",
|
|
150
|
+
detail: `node ${process.versions.node} is below engines.node >=${floor}; upgrade when possible`,
|
|
151
|
+
});
|
|
152
|
+
} else {
|
|
153
|
+
checks.push({ id: "node-engine", status: "pass", detail: `node ${process.versions.node}` });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// npm-cli: without npm neither plugin installs nor self-upgrade can run.
|
|
157
|
+
const npmCmd = resolveNpmCommand();
|
|
158
|
+
const npmOk = npmOnPath(npmCmd);
|
|
159
|
+
if (npmOk) {
|
|
160
|
+
checks.push({ id: "npm-cli", status: "pass", detail: npmCmd.file });
|
|
161
|
+
} else {
|
|
162
|
+
checks.push({
|
|
163
|
+
id: "npm-cli",
|
|
164
|
+
status: "fail",
|
|
165
|
+
detail: `${npmCmd.file} not found on PATH; plugin install and self-upgrade unavailable`,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// registry + self-version: compare against npm latest; upgrade in place when possible.
|
|
170
|
+
let upgradePlan = null;
|
|
171
|
+
try {
|
|
172
|
+
upgradePlan = await upgradeFn({ check: true });
|
|
173
|
+
} catch (err) {
|
|
174
|
+
checks.push({ id: "registry", status: "warn", detail: `registry check failed: ${err.message}` });
|
|
175
|
+
}
|
|
176
|
+
if (upgradePlan && upgradePlan.latest != null) {
|
|
177
|
+
checks.push({ id: "registry", status: "pass", detail: `local ${upgradePlan.local} / latest ${upgradePlan.latest}` });
|
|
178
|
+
if (upgradePlan.action === "upgrade" && npmOk) {
|
|
179
|
+
try {
|
|
180
|
+
const done = await upgradeFn({});
|
|
181
|
+
if (done.action === "upgraded") {
|
|
182
|
+
checks.push({
|
|
183
|
+
id: "self-version",
|
|
184
|
+
status: "fixed",
|
|
185
|
+
detail: `upgraded ${upgradePlan.local} -> ${upgradePlan.latest}; restart omp to activate`,
|
|
186
|
+
});
|
|
187
|
+
} else {
|
|
188
|
+
checks.push({
|
|
189
|
+
id: "self-version",
|
|
190
|
+
status: "warn",
|
|
191
|
+
detail: `upgrade not completed (${done.action}${done.reason ? `: ${done.reason}` : ""}); continue with ${upgradePlan.local}`,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
} catch (err) {
|
|
195
|
+
checks.push({
|
|
196
|
+
id: "self-version",
|
|
197
|
+
status: "warn",
|
|
198
|
+
detail: `automatic upgrade failed: ${err.message}; continue with ${upgradePlan.local}`,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
} else if (upgradePlan.action === "upgrade") {
|
|
202
|
+
checks.push({
|
|
203
|
+
id: "self-version",
|
|
204
|
+
status: "warn",
|
|
205
|
+
detail: `upgrade available (${upgradePlan.local} -> ${upgradePlan.latest}) but npm is unavailable`,
|
|
206
|
+
});
|
|
207
|
+
} else if (upgradePlan.action === "manual") {
|
|
208
|
+
checks.push({ id: "self-version", status: "warn", detail: upgradePlan.reason || "manual upgrade required" });
|
|
209
|
+
} else {
|
|
210
|
+
checks.push({ id: "self-version", status: "pass", detail: `local ${upgradePlan.local} is up to date` });
|
|
211
|
+
}
|
|
212
|
+
} else if (upgradePlan) {
|
|
213
|
+
checks.push({ id: "registry", status: "warn", detail: "registry unreachable" });
|
|
214
|
+
checks.push({ id: "self-version", status: "warn", detail: "cannot compare versions while the registry is unreachable" });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// remote-access: probe the pull target before spending the real download.
|
|
218
|
+
if (target?.kind === "gist") {
|
|
219
|
+
const probe = await probeGistAccess(target.id, token, doFetch);
|
|
220
|
+
checks.push(probe.ok
|
|
221
|
+
? { id: "remote-access", status: "pass", detail: probe.message }
|
|
222
|
+
: { id: "remote-access", status: "fail", detail: probe.message });
|
|
223
|
+
} else if (target?.kind === "http") {
|
|
224
|
+
const probe = await probeHttpAccess(target.url, doFetch);
|
|
225
|
+
checks.push(probe.ok
|
|
226
|
+
? { id: "remote-access", status: "pass", detail: probe.message }
|
|
227
|
+
: { id: "remote-access", status: "fail", detail: probe.message });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return { ok: checks.every((check) => check.status !== "fail"), checks };
|
|
231
|
+
}
|