@jmanuelcorral/bond-app 0.3.0 → 0.3.2
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/app/mods/bond-agents/api.js +27 -0
- package/app/mods/bond-agents/model.js +3 -0
- package/app/mods/bond-board/api.js +27 -0
- package/app/mods/bond-settings/api.js +27 -0
- package/app/mods/bond-settings/main.js +72 -2
- package/app/mods/bond-status/api.js +27 -0
- package/app/mods/bond-status/main.js +14 -3
- package/app/mods/bond-status/model.js +13 -0
- package/package.json +1 -1
- package/src/cli.ts +110 -2
- package/src/config.ts +6 -0
- package/src/core/diagnose.ts +279 -0
- package/src/core/orchestrator.ts +28 -2
- package/src/core/service-windows.ts +1 -1
- package/src/core/service.ts +36 -0
- package/src/core/setup-install.ts +84 -26
- package/src/core/stuck.ts +12 -0
- package/src/core/updates.ts +155 -0
- package/src/index.ts +2 -0
- package/src/server/app.ts +6 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* «Hay versión nueva»: the orchestrator asks npm, at start and then once a day, for the latest
|
|
3
|
+
* bond-app and bond, and compares them with what is installed. The app shows it in the status
|
|
4
|
+
* bar and Ajustes → Avanzado, whose «Actualizar» runs `bond-app update`. The only network call
|
|
5
|
+
* is to registry.npmjs.org; without network it says nothing (no error to the person).
|
|
6
|
+
*/
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { execFile } from 'node:child_process';
|
|
10
|
+
import { promisify } from 'node:util';
|
|
11
|
+
import { fileURLToPath } from 'node:url';
|
|
12
|
+
|
|
13
|
+
const execFileAsync = promisify(execFile);
|
|
14
|
+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
|
|
15
|
+
|
|
16
|
+
export const UPDATE_PACKAGES = { bond_app: '@jmanuelcorral/bond-app', bond: '@jmanuelcorral/bond' } as const;
|
|
17
|
+
export const UPDATE_CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000;
|
|
18
|
+
|
|
19
|
+
export interface PackageUpdate {
|
|
20
|
+
installed: string | null;
|
|
21
|
+
latest: string | null;
|
|
22
|
+
update: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UpdateStatus {
|
|
26
|
+
bond_app: PackageUpdate;
|
|
27
|
+
bond: PackageUpdate;
|
|
28
|
+
/** Something to update. */
|
|
29
|
+
available: boolean;
|
|
30
|
+
checked_at: string | null;
|
|
31
|
+
/** Why the last check could not ask npm (no network...): for Ajustes, not an alarm. */
|
|
32
|
+
error: string | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** a < b for plain x.y.z versions (pre-releases count as lower). */
|
|
36
|
+
export function isNewer(latest: string | null, installed: string | null): boolean {
|
|
37
|
+
if (!latest || !installed) return false;
|
|
38
|
+
const parse = (v: string) => {
|
|
39
|
+
const [core, pre] = v.replace(/^v/, '').split('-', 2);
|
|
40
|
+
return { nums: core.split('.').map((n) => Number.parseInt(n, 10) || 0), pre: pre ?? null };
|
|
41
|
+
};
|
|
42
|
+
const a = parse(latest);
|
|
43
|
+
const b = parse(installed);
|
|
44
|
+
for (let i = 0; i < 3; i++) {
|
|
45
|
+
if ((a.nums[i] ?? 0) !== (b.nums[i] ?? 0)) return (a.nums[i] ?? 0) > (b.nums[i] ?? 0);
|
|
46
|
+
}
|
|
47
|
+
return a.pre === null && b.pre !== null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UpdateDeps {
|
|
51
|
+
/** The latest version npm has for a package (registry.npmjs.org/<pkg>/latest). */
|
|
52
|
+
latestVersion?: (pkg: string) => Promise<string | null>;
|
|
53
|
+
/** The installed bond (`bond --version`). */
|
|
54
|
+
bondVersion?: () => Promise<string | null>;
|
|
55
|
+
/** This bond-app's version. */
|
|
56
|
+
appVersion?: string;
|
|
57
|
+
now?: () => number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function npmLatest(pkg: string): Promise<string | null> {
|
|
61
|
+
const ctrl = new AbortController();
|
|
62
|
+
const timer = setTimeout(() => ctrl.abort(), 8000);
|
|
63
|
+
try {
|
|
64
|
+
const res = await fetch(`https://registry.npmjs.org/${pkg.replace('/', '%2F')}/latest`, {
|
|
65
|
+
signal: ctrl.signal,
|
|
66
|
+
headers: { Accept: 'application/json' },
|
|
67
|
+
});
|
|
68
|
+
if (!res.ok) throw new Error(`npm respondió ${res.status}`);
|
|
69
|
+
const body = (await res.json()) as { version?: unknown };
|
|
70
|
+
return typeof body.version === 'string' ? body.version : null;
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function installedBond(): Promise<string | null> {
|
|
77
|
+
try {
|
|
78
|
+
const { stdout } = await execFileAsync('bond', ['--version'], { timeout: 10_000, shell: process.platform === 'win32' });
|
|
79
|
+
return /(\d+\.\d+\.\d+(?:-[\w.]+)?)/.exec(stdout)?.[1] ?? null;
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function ownVersion(): string {
|
|
86
|
+
try {
|
|
87
|
+
return (JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8')) as { version: string }).version;
|
|
88
|
+
} catch {
|
|
89
|
+
return '0.0.0';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export class UpdateChecker {
|
|
94
|
+
private status: UpdateStatus;
|
|
95
|
+
private timer: NodeJS.Timeout | null = null;
|
|
96
|
+
private inFlight: Promise<UpdateStatus> | null = null;
|
|
97
|
+
private deps: Required<UpdateDeps>;
|
|
98
|
+
|
|
99
|
+
constructor(deps: UpdateDeps = {}) {
|
|
100
|
+
this.deps = {
|
|
101
|
+
latestVersion: deps.latestVersion ?? npmLatest,
|
|
102
|
+
bondVersion: deps.bondVersion ?? installedBond,
|
|
103
|
+
appVersion: deps.appVersion ?? ownVersion(),
|
|
104
|
+
now: deps.now ?? Date.now,
|
|
105
|
+
};
|
|
106
|
+
const empty = (installed: string | null): PackageUpdate => ({ installed, latest: null, update: false });
|
|
107
|
+
this.status = { bond_app: empty(this.deps.appVersion), bond: empty(null), available: false, checked_at: null, error: null };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get(): UpdateStatus {
|
|
111
|
+
return { ...this.status, bond_app: { ...this.status.bond_app }, bond: { ...this.status.bond } };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Ask npm now (one request at a time). Never throws: a failure is kept in `error`. */
|
|
115
|
+
check(): Promise<UpdateStatus> {
|
|
116
|
+
if (this.inFlight) return this.inFlight;
|
|
117
|
+
this.inFlight = (async () => {
|
|
118
|
+
const bondInstalled = await this.deps.bondVersion().catch(() => null);
|
|
119
|
+
try {
|
|
120
|
+
const [appLatest, bondLatest] = await Promise.all([
|
|
121
|
+
this.deps.latestVersion(UPDATE_PACKAGES.bond_app),
|
|
122
|
+
this.deps.latestVersion(UPDATE_PACKAGES.bond),
|
|
123
|
+
]);
|
|
124
|
+
const app: PackageUpdate = { installed: this.deps.appVersion, latest: appLatest, update: isNewer(appLatest, this.deps.appVersion) };
|
|
125
|
+
const bond: PackageUpdate = { installed: bondInstalled, latest: bondLatest, update: isNewer(bondLatest, bondInstalled) };
|
|
126
|
+
this.status = { bond_app: app, bond, available: app.update || bond.update, checked_at: new Date(this.deps.now()).toISOString(), error: null };
|
|
127
|
+
} catch (err) {
|
|
128
|
+
this.status = {
|
|
129
|
+
...this.status,
|
|
130
|
+
bond: { ...this.status.bond, installed: bondInstalled },
|
|
131
|
+
checked_at: new Date(this.deps.now()).toISOString(),
|
|
132
|
+
error: String((err as Error).message || err).slice(0, 200),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return this.get();
|
|
136
|
+
})().finally(() => {
|
|
137
|
+
this.inFlight = null;
|
|
138
|
+
});
|
|
139
|
+
return this.inFlight;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Check now and then every `intervalMs` (0: only now). */
|
|
143
|
+
start(intervalMs = UPDATE_CHECK_INTERVAL_MS): void {
|
|
144
|
+
void this.check();
|
|
145
|
+
if (intervalMs > 0) {
|
|
146
|
+
this.timer = setInterval(() => void this.check(), intervalMs);
|
|
147
|
+
this.timer.unref?.();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
stop(): void {
|
|
152
|
+
if (this.timer) clearInterval(this.timer);
|
|
153
|
+
this.timer = null;
|
|
154
|
+
}
|
|
155
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -30,6 +30,8 @@ export async function bootstrap(configOverrides = {}, hooks: ServerHooks = {}) {
|
|
|
30
30
|
);
|
|
31
31
|
|
|
32
32
|
await orchestrator.start();
|
|
33
|
+
// «Hay versión nueva»: ask npm now and once a day (BOND_APP_UPDATE_CHECK=0 turns it off).
|
|
34
|
+
if (config.updateCheck) orchestrator.updates.start();
|
|
33
35
|
|
|
34
36
|
const server = createServer(orchestrator, hooks);
|
|
35
37
|
|
package/src/server/app.ts
CHANGED
|
@@ -1358,6 +1358,12 @@ export function createServer(orchestrator: Orchestrator, hooks: ServerHooks = {}
|
|
|
1358
1358
|
return;
|
|
1359
1359
|
}
|
|
1360
1360
|
|
|
1361
|
+
// «Hay versión nueva»: the last check of bond-app and bond against npm; POST asks again now.
|
|
1362
|
+
if (pathname === '/api/v1/system/updates' && (method === 'GET' || method === 'POST')) {
|
|
1363
|
+
sendJson(200, method === 'POST' ? await orchestrator.updates.check() : orchestrator.updates.get());
|
|
1364
|
+
return;
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1361
1367
|
// Progress of the image build in flight, or how the last one ended (Ajustes → Imagen).
|
|
1362
1368
|
if (pathname === '/api/v1/system/rebuild' && method === 'GET') {
|
|
1363
1369
|
sendJson(200, orchestrator.getRebuildStatus());
|