@acryldev/launcher-desktop 0.1.7 → 0.1.9
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 +3 -0
- package/lib/client.js +66 -38
- package/lib/client.js.map +1 -1
- package/lib/index.js +5 -996
- package/lib/index.js.map +1 -1
- package/lib/node-DxDbk2F0.js +1062 -0
- package/lib/node-DxDbk2F0.js.map +1 -0
- package/lib/node.js +2 -0
- package/lib/page.js +9 -9
- package/lib/types/contract.d.ts +9 -0
- package/lib/types/index.d.ts +5 -0
- package/lib/types/node.d.ts +7 -0
- package/lib/types/runner.d.ts +3 -0
- package/lib/types/service.d.ts +5 -1
- package/package.json +7 -3
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
import { APP_MARKER, APP_NAME, STARTERS, addApp, appNameProblem, blendOf, blendVersionOf, clearGithub, githubClientId, githubEnv, githubOf, inspectApp, maskKey, modelEnv, pollGithubSignIn, prepareTools, readApps, readGithub, readModelKey, removeApp, setBlendVisibility, startGithubSignIn, toolsEnv, whereProblem } from "@acryldev/launcher";
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
import { StringDecoder } from "node:string_decoder";
|
|
6
|
+
//#region src/contract.ts
|
|
7
|
+
/** The wire contract of the ACRYL screen's routes (the desktop face of the launcher, spec 050 T012). The screen is the client of these routes. */
|
|
8
|
+
/** Every launcher route lives under this prefix, the one the Host's Web adapter mounts for this plugin. */
|
|
9
|
+
const LAUNCHER_API_PREFIX = "/api/acryl-launcher";
|
|
10
|
+
const LAUNCHER_STATE_PATH = `${LAUNCHER_API_PREFIX}/state`;
|
|
11
|
+
const LAUNCHER_HEALTH_PATH = `${LAUNCHER_API_PREFIX}/health`;
|
|
12
|
+
const LAUNCHER_ADD_PATH = `${LAUNCHER_API_PREFIX}/add`;
|
|
13
|
+
const LAUNCHER_REMOVE_PATH = `${LAUNCHER_API_PREFIX}/remove`;
|
|
14
|
+
const LAUNCHER_NEW_PATH = `${LAUNCHER_API_PREFIX}/new`;
|
|
15
|
+
const LAUNCHER_START_PATH = `${LAUNCHER_API_PREFIX}/start`;
|
|
16
|
+
const LAUNCHER_STOP_PATH = `${LAUNCHER_API_PREFIX}/stop`;
|
|
17
|
+
const LAUNCHER_UPDATE_PATH = `${LAUNCHER_API_PREFIX}/update`;
|
|
18
|
+
/** Open ACRYL ADE (the Blend `acryl.ade` in `<home>/ade`, the folder `acryl ade` uses), made on first use, in a window or the browser. */
|
|
19
|
+
const LAUNCHER_ADE_PATH = `${LAUNCHER_API_PREFIX}/ade`;
|
|
20
|
+
const LAUNCHER_BLENDS_PATH = `${LAUNCHER_API_PREFIX}/blends`;
|
|
21
|
+
const LAUNCHER_ASSISTANT_FOLDER_PATH = `${LAUNCHER_API_PREFIX}/assistant-folder`;
|
|
22
|
+
const LAUNCHER_PLUGINS_PATH = `${LAUNCHER_API_PREFIX}/plugins`;
|
|
23
|
+
const LAUNCHER_PLUGIN_PULL_PATH = `${LAUNCHER_API_PREFIX}/plugin-pull`;
|
|
24
|
+
const LAUNCHER_PUBLISH_PLAN_PATH = `${LAUNCHER_API_PREFIX}/publish-plan`;
|
|
25
|
+
const LAUNCHER_PUBLISH_PATH = `${LAUNCHER_API_PREFIX}/publish`;
|
|
26
|
+
const LAUNCHER_SHARE_PLAN_PATH = `${LAUNCHER_API_PREFIX}/share-plan`;
|
|
27
|
+
const LAUNCHER_SHARE_PATH = `${LAUNCHER_API_PREFIX}/share`;
|
|
28
|
+
const LAUNCHER_GITHUB_PATH = `${LAUNCHER_API_PREFIX}/github`;
|
|
29
|
+
const LAUNCHER_GITHUB_START_PATH = `${LAUNCHER_API_PREFIX}/github-start`;
|
|
30
|
+
const LAUNCHER_GITHUB_POLL_PATH = `${LAUNCHER_API_PREFIX}/github-poll`;
|
|
31
|
+
const LAUNCHER_GITHUB_DISCONNECT_PATH = `${LAUNCHER_API_PREFIX}/github-disconnect`;
|
|
32
|
+
const SURFACES = [
|
|
33
|
+
"web",
|
|
34
|
+
"desktop",
|
|
35
|
+
"tui"
|
|
36
|
+
];
|
|
37
|
+
/** Surfaces a person can choose for a new app today. */
|
|
38
|
+
const MAKEABLE_SURFACES = ["web", "desktop"];
|
|
39
|
+
/** WebBoxes.ai's New WebBox form, opened with the app's repository filled in (the person signs in and presses Create there). */
|
|
40
|
+
function webboxesDeployUrl(github, name) {
|
|
41
|
+
const url = new URL(WEBBOXES_NEW_BOX);
|
|
42
|
+
url.searchParams.set("github", github);
|
|
43
|
+
url.searchParams.set("name", name.toLowerCase().replace(/[^a-z0-9-]+/gu, "-").replace(/^-+|-+$/gu, "").slice(0, 40));
|
|
44
|
+
return url.href;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* WebBoxes.ai's New WebBox form for a store Blend: with `?blend=<id>` the form opens on that registry Blend (the person signs in and presses
|
|
48
|
+
* Create there). A community Blend that lives on GitHub rather than in the registry (`source` is `github:owner/repo#tag`) deploys from its
|
|
49
|
+
* repository, as a shared app does.
|
|
50
|
+
*/
|
|
51
|
+
function webboxesDeployBlendUrl(blend) {
|
|
52
|
+
const name = (blend.id.split(".").pop() ?? blend.id).toLowerCase().replace(/[^a-z0-9-]+/gu, "-").replace(/^-+|-+$/gu, "").slice(0, 40);
|
|
53
|
+
const repository = /^github:([A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9_.-]+)(?:#.*)?$/u.exec(blend.source ?? "");
|
|
54
|
+
if (repository?.[1] !== void 0) return webboxesDeployUrl(`https://github.com/${repository[1]}`, name);
|
|
55
|
+
const url = new URL(WEBBOXES_NEW_BOX);
|
|
56
|
+
url.searchParams.set("blend", blend.id);
|
|
57
|
+
url.searchParams.set("name", name);
|
|
58
|
+
return url.href;
|
|
59
|
+
}
|
|
60
|
+
/** A Blend id as `acryl new --from` takes it: it starts with a letter or digit, so it can never read as an option. */
|
|
61
|
+
/** WebBoxes.ai's WebBoxes page: with `github` (a public https://github.com/owner/repo) and `name` it opens the New WebBox form filled in. */
|
|
62
|
+
const WEBBOXES_NEW_BOX = "https://webboxes.ai/dashboard/webboxes";
|
|
63
|
+
const BLEND_ID = /^[a-z0-9][a-z0-9._-]{0,127}$/u;
|
|
64
|
+
/** A Blend released on GitHub, as `--from` takes it (create-acrylblends `github:owner/repo#ref`): what a community Blend is made from. */
|
|
65
|
+
const BLEND_SOURCE = /^github:[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})\/[A-Za-z0-9._-]{1,100}(?:#[A-Za-z0-9._/-]{1,100})?$/u;
|
|
66
|
+
/** A plugin name, a GitHub account and a license id as the door takes them: nothing that could read as an option or a path. */
|
|
67
|
+
const PLUGIN_NAME = /^[a-z0-9][a-z0-9._-]{0,127}$/u;
|
|
68
|
+
const ACCOUNT_NAME = /^[A-Za-z0-9][A-Za-z0-9-]{0,38}$/u;
|
|
69
|
+
const LICENSE_ID = /^[A-Za-z0-9][A-Za-z0-9.+-]{0,39}$/u;
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/door.ts
|
|
72
|
+
/**
|
|
73
|
+
* The running app's door, reached the way the app's own `acryl` command reaches it: `<app>/.acryl/door.json` names the loopback address and the secret of the
|
|
74
|
+
* app that is running now. The screen is a person's surface, so it may do what the terminal's prompt does: send a publish the person confirmed. Only
|
|
75
|
+
* loopback addresses are used, and a missing or stale file means the app is not running.
|
|
76
|
+
*/
|
|
77
|
+
var DoorUnavailable = class extends Error {};
|
|
78
|
+
const TIMEOUT_MS$1 = 6e4;
|
|
79
|
+
const LOOPBACK = /* @__PURE__ */ new Set([
|
|
80
|
+
"127.0.0.1",
|
|
81
|
+
"localhost",
|
|
82
|
+
"[::1]"
|
|
83
|
+
]);
|
|
84
|
+
const NOT_RUNNING = "the app is not running: start it from this screen, then try again";
|
|
85
|
+
function readDoorFile(folder, read) {
|
|
86
|
+
let parsed;
|
|
87
|
+
try {
|
|
88
|
+
parsed = JSON.parse(read(join(folder, ".acryl", "door.json")));
|
|
89
|
+
} catch {
|
|
90
|
+
throw new DoorUnavailable(NOT_RUNNING);
|
|
91
|
+
}
|
|
92
|
+
const file = parsed;
|
|
93
|
+
if (typeof file?.url !== "string" || typeof file.secret !== "string") throw new DoorUnavailable(NOT_RUNNING);
|
|
94
|
+
let url;
|
|
95
|
+
try {
|
|
96
|
+
url = new URL(file.url);
|
|
97
|
+
} catch {
|
|
98
|
+
throw new DoorUnavailable(NOT_RUNNING);
|
|
99
|
+
}
|
|
100
|
+
if (url.protocol !== "http:" || !LOOPBACK.has(url.hostname)) throw new DoorUnavailable(NOT_RUNNING);
|
|
101
|
+
return {
|
|
102
|
+
url: url.origin,
|
|
103
|
+
secret: file.secret
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function fileDoor(fetchImpl = (input, init) => fetch(input, init), read = (path) => readFileSync(path, "utf8")) {
|
|
107
|
+
const post = async (folder, path, body) => {
|
|
108
|
+
const file = readDoorFile(folder, read);
|
|
109
|
+
try {
|
|
110
|
+
const answer = await (await fetchImpl(`${file.url}${path}`, {
|
|
111
|
+
method: "POST",
|
|
112
|
+
headers: {
|
|
113
|
+
authorization: `Bearer ${file.secret}`,
|
|
114
|
+
"content-type": "application/json"
|
|
115
|
+
},
|
|
116
|
+
body: JSON.stringify(body),
|
|
117
|
+
signal: AbortSignal.timeout(TIMEOUT_MS$1)
|
|
118
|
+
})).json();
|
|
119
|
+
if (answer === null || typeof answer !== "object") throw new DoorUnavailable("the app answered with nothing readable");
|
|
120
|
+
return answer;
|
|
121
|
+
} catch (cause) {
|
|
122
|
+
if (cause instanceof DoorUnavailable) throw cause;
|
|
123
|
+
throw new DoorUnavailable(NOT_RUNNING);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
call: (folder, verb, args) => post(folder, "/api/acryl-door/call", {
|
|
128
|
+
verb,
|
|
129
|
+
args
|
|
130
|
+
}),
|
|
131
|
+
confirmPublish: (folder, body) => post(folder, "/api/acryl-door/publish-confirmed", body)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/registry.ts
|
|
136
|
+
/** The public Blend registry as the screen's store lists it: only reads (the same index `acryl new --from` and the terminal's "Install a Blend" use). */
|
|
137
|
+
const REGISTRY_INDEX = "https://acrylblends.github.io/registry/index.json";
|
|
138
|
+
/** Blends released by people outside the ACRYL project and found on GitHub by the registry's hourly sync (origin `community`, each with its `source`). */
|
|
139
|
+
const REGISTRY_DISCOVERED = "https://acrylblends.github.io/registry/discovered.json";
|
|
140
|
+
const TIMEOUT_MS = 1e4;
|
|
141
|
+
var RegistryUnavailable = class extends Error {};
|
|
142
|
+
const record = (value) => value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
143
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
144
|
+
/** The `entries` of one registry list; `RegistryUnavailable` in words a person can act on. */
|
|
145
|
+
async function entriesOf(fetchImpl, url) {
|
|
146
|
+
let body;
|
|
147
|
+
try {
|
|
148
|
+
const response = await fetchImpl(url, { signal: AbortSignal.timeout(TIMEOUT_MS) });
|
|
149
|
+
if (!response.ok) throw new RegistryUnavailable(`The registry answered HTTP ${String(response.status)}.`);
|
|
150
|
+
body = await response.json();
|
|
151
|
+
} catch (cause) {
|
|
152
|
+
if (cause instanceof RegistryUnavailable) throw cause;
|
|
153
|
+
throw new RegistryUnavailable("The registry could not be read. Check the network and try again.");
|
|
154
|
+
}
|
|
155
|
+
const entries = record(body)?.entries;
|
|
156
|
+
if (!Array.isArray(entries)) throw new RegistryUnavailable("The registry answered, but its list could not be read.");
|
|
157
|
+
return entries;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The Blends an ACRYL app can be made from (registry entries whose runtime is `acrylblends`), by name: the public registry's, and the community ones
|
|
161
|
+
* the registry found on GitHub (made from their `source`). The community list is optional: when it cannot be read, the store still lists the rest.
|
|
162
|
+
*/
|
|
163
|
+
async function readBlends(fetchImpl = (input, init) => fetch(input, init), url = REGISTRY_INDEX, discovered = REGISTRY_DISCOVERED) {
|
|
164
|
+
const blends = [];
|
|
165
|
+
const ids = /* @__PURE__ */ new Set();
|
|
166
|
+
for (const raw of await entriesOf(fetchImpl, url)) {
|
|
167
|
+
const entry = record(raw);
|
|
168
|
+
if (entry === void 0 || entry.runtime !== "acrylblends" || typeof entry.id !== "string" || entry.id === "") continue;
|
|
169
|
+
ids.add(entry.id);
|
|
170
|
+
blends.push({
|
|
171
|
+
id: entry.id,
|
|
172
|
+
name: text(entry.name) || entry.id,
|
|
173
|
+
version: text(entry.version),
|
|
174
|
+
category: text(entry.category),
|
|
175
|
+
description: text(entry.description),
|
|
176
|
+
origin: entry.origin === "official" ? "official" : "community"
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const community = discovered === void 0 ? [] : await entriesOf(fetchImpl, discovered).catch(() => []);
|
|
180
|
+
for (const raw of community) {
|
|
181
|
+
const entry = record(raw);
|
|
182
|
+
if (entry === void 0 || entry.runtime !== "acrylblends" || typeof entry.id !== "string" || entry.id === "" || ids.has(entry.id)) continue;
|
|
183
|
+
const source = text(entry.source);
|
|
184
|
+
if (!BLEND_SOURCE.test(source)) continue;
|
|
185
|
+
ids.add(entry.id);
|
|
186
|
+
const placement = record(entry.placement);
|
|
187
|
+
const category = text(placement?.category) === "" ? text(entry.category) : [text(placement?.category), text(placement?.subcategory)].filter(Boolean).join(" › ");
|
|
188
|
+
const publisher = text(entry.publisher);
|
|
189
|
+
blends.push({
|
|
190
|
+
id: entry.id,
|
|
191
|
+
name: text(entry.name) || entry.id,
|
|
192
|
+
version: text(entry.version),
|
|
193
|
+
category,
|
|
194
|
+
description: text(entry.description),
|
|
195
|
+
origin: "community",
|
|
196
|
+
source,
|
|
197
|
+
...publisher === "" ? {} : { publisher }
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return blends.sort((a, b) => a.name.localeCompare(b.name));
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/runner.ts
|
|
204
|
+
/**
|
|
205
|
+
* The one place this plugin starts processes. A `Runner` hands back a `Run` the service watches; the real one spawns the command through the
|
|
206
|
+
* machine's own Node and npm (the desktop app carries no Node of its own yet, spec 050 T005) and stops a whole tree, not only its first process
|
|
207
|
+
* (`npm run dev` starts a server below npm).
|
|
208
|
+
*/
|
|
209
|
+
/** Keys the desktop app set for itself; a started app must choose its own (a shared one would put the new app in this app's home and port). */
|
|
210
|
+
const OWN_KEYS = /^(ACRYL_|ACRYLBLENDS_|DSH_|ELECTRON_|npm_|PORT$|WATCH_REPORT_DEPENDENCIES$)/u;
|
|
211
|
+
/** The environment a started app gets: this process's, without the keys above, plus the places Node usually lives (a Dock launch has a short PATH). */
|
|
212
|
+
function appEnvironment(base, platform = process.platform) {
|
|
213
|
+
const env = {};
|
|
214
|
+
for (const [key, value] of Object.entries(base)) if (!OWN_KEYS.test(key)) env[key] = value;
|
|
215
|
+
if (platform !== "win32") {
|
|
216
|
+
const extra = ["/opt/homebrew/bin", "/usr/local/bin"];
|
|
217
|
+
const have = (env.PATH ?? "").split(":");
|
|
218
|
+
env.PATH = [...have, ...extra.filter((dir) => !have.includes(dir))].filter((dir) => dir !== "").join(":");
|
|
219
|
+
}
|
|
220
|
+
return env;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* What a child of the Electron main process inherits. On Linux Chromium opens its DevTools port (`--remote-debugging-port`), its sockets and its files without
|
|
224
|
+
* close-on-exec and Node's spawn passes every one on, so an app started from here kept the port bound after this app quit. macOS closes all but the standard streams
|
|
225
|
+
* when it starts a process, and Windows does not inherit a socket, so only Linux needs it. bash's `exec 12>&-` closes a descriptor of any number (dash takes one
|
|
226
|
+
* digit); the command then replaces bash, so the process id and group stay the child's. The same text is in plugins/desktop-shell/inherited.ts; keep them equal.
|
|
227
|
+
*/
|
|
228
|
+
const CLOSE_AND_RUN = "for f in /proc/self/fd/*; do n=${f##*/}; if [ \"$n\" -gt 2 ]; then eval \"exec $n>&-\"; fi; done; exec \"$@\"";
|
|
229
|
+
const BASH = "/bin/bash";
|
|
230
|
+
/** The program and arguments to spawn so the child holds no descriptor above 2: the command itself where that is already so, or where bash is not there. */
|
|
231
|
+
function withoutInheritedDescriptors(program, args, platform = process.platform, hasBash = existsSync(BASH)) {
|
|
232
|
+
if (platform !== "linux" || !hasBash) return {
|
|
233
|
+
program,
|
|
234
|
+
args
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
program: BASH,
|
|
238
|
+
args: [
|
|
239
|
+
"-c",
|
|
240
|
+
CLOSE_AND_RUN,
|
|
241
|
+
"acryl-spawn",
|
|
242
|
+
program,
|
|
243
|
+
...args
|
|
244
|
+
]
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/** Stop a command and everything it started: a process group on Unix, `taskkill /t` on Windows. */
|
|
248
|
+
function stopTree(pid, platform) {
|
|
249
|
+
try {
|
|
250
|
+
if (platform === "win32") spawnSync("taskkill", [
|
|
251
|
+
"/pid",
|
|
252
|
+
String(pid),
|
|
253
|
+
"/t",
|
|
254
|
+
"/f"
|
|
255
|
+
], { windowsHide: true });
|
|
256
|
+
else process.kill(-pid, "SIGTERM");
|
|
257
|
+
} catch {}
|
|
258
|
+
}
|
|
259
|
+
/** Read a file as it grows, until the returned function is called (it reads what is left first). */
|
|
260
|
+
function follow(file, emit) {
|
|
261
|
+
const fd = openSync(file, "r");
|
|
262
|
+
const decoder = new StringDecoder("utf8");
|
|
263
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
264
|
+
let at = 0;
|
|
265
|
+
const read = () => {
|
|
266
|
+
for (let n = readSync(fd, buffer, 0, buffer.length, at); n > 0; n = readSync(fd, buffer, 0, buffer.length, at)) {
|
|
267
|
+
at += n;
|
|
268
|
+
emit(decoder.write(buffer.subarray(0, n)));
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
const timer = setInterval(read, 250);
|
|
272
|
+
timer.unref();
|
|
273
|
+
return () => {
|
|
274
|
+
clearInterval(timer);
|
|
275
|
+
read();
|
|
276
|
+
closeSync(fd);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
const spawnRunner = (command) => {
|
|
280
|
+
const win = process.platform === "win32";
|
|
281
|
+
const start = win ? {
|
|
282
|
+
program: command.program,
|
|
283
|
+
args: command.args
|
|
284
|
+
} : withoutInheritedDescriptors(command.program, command.args);
|
|
285
|
+
const log = command.log === void 0 ? void 0 : openSync(command.log, "w");
|
|
286
|
+
const output = log ?? "pipe";
|
|
287
|
+
const child = spawn(start.program, [...start.args], {
|
|
288
|
+
cwd: command.cwd,
|
|
289
|
+
env: command.env,
|
|
290
|
+
shell: win,
|
|
291
|
+
detached: !win,
|
|
292
|
+
windowsHide: true,
|
|
293
|
+
stdio: [
|
|
294
|
+
"ignore",
|
|
295
|
+
output,
|
|
296
|
+
output
|
|
297
|
+
]
|
|
298
|
+
});
|
|
299
|
+
if (log !== void 0) closeSync(log);
|
|
300
|
+
const outputs = [];
|
|
301
|
+
const emit = (text) => {
|
|
302
|
+
for (const listener of outputs) listener(text);
|
|
303
|
+
};
|
|
304
|
+
const unfollow = command.log === void 0 ? () => {} : follow(command.log, emit);
|
|
305
|
+
const ends = [];
|
|
306
|
+
let ended = false;
|
|
307
|
+
const finish = (end) => {
|
|
308
|
+
if (ended) return;
|
|
309
|
+
ended = true;
|
|
310
|
+
unfollow();
|
|
311
|
+
for (const listener of ends) listener(end);
|
|
312
|
+
};
|
|
313
|
+
for (const stream of [child.stdout, child.stderr]) stream?.on("data", (chunk) => {
|
|
314
|
+
emit(chunk.toString("utf8"));
|
|
315
|
+
});
|
|
316
|
+
child.on("error", (cause) => {
|
|
317
|
+
finish({
|
|
318
|
+
code: null,
|
|
319
|
+
notFound: cause.code === "ENOENT",
|
|
320
|
+
error: cause.message
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
child.on("close", (code) => {
|
|
324
|
+
finish({
|
|
325
|
+
code,
|
|
326
|
+
notFound: code === 127
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
return {
|
|
330
|
+
onOutput: (listener) => {
|
|
331
|
+
outputs.push(listener);
|
|
332
|
+
},
|
|
333
|
+
onEnd: (listener) => {
|
|
334
|
+
ends.push(listener);
|
|
335
|
+
},
|
|
336
|
+
stop: () => {
|
|
337
|
+
if (!ended && child.pid !== void 0) stopTree(child.pid, process.platform);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
};
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/service.ts
|
|
343
|
+
/**
|
|
344
|
+
* What the ACRYL screen does, as one service with ports (Clean Architecture: the routes and the Cordis plugin are the edge; this holds the rules).
|
|
345
|
+
* The list is the registry `<home>/apps.json` (`acryl-launcher`, the same file `acryl apps` and the terminal read); what runs is held here, in
|
|
346
|
+
* memory, because the launcher records no port: an app is "running" when this service started it and it has not ended.
|
|
347
|
+
*/
|
|
348
|
+
/** A request the service cannot honour, in a sentence a person can act on. The route answers it as 409. */
|
|
349
|
+
var LauncherRefusal = class extends Error {};
|
|
350
|
+
/** The address an app prints, with the token it needs: `http://127.0.0.1:3999/?token=...`. */
|
|
351
|
+
const ADDRESS = /https?:\/\/(?:127\.0\.0\.1|localhost|\[::1\]):\d+[^\s'")\]]*/u;
|
|
352
|
+
const BLENDS_FRESH_MS = 6e4;
|
|
353
|
+
/** The package `acryl new` runs (`distribution/acryl-npm-launcher/forward.js` CREATE_SPEC). */
|
|
354
|
+
const CREATE_SPEC = "create-acrylblends@latest";
|
|
355
|
+
/** The command that updates an app from its Blend's newer release (`acrylblends update`, in the acrylblends package). */
|
|
356
|
+
const UPDATE_SPEC = "acrylblends@latest";
|
|
357
|
+
/** The app's own share command, from its folder (the `save` plugin of acrylblends). */
|
|
358
|
+
const SHARE_COMMAND = join("plugins", "save", "share-cli.ts");
|
|
359
|
+
/** ACRYL ADE: the Blend and the folder in the home that `acryl ade` makes and opens (`distribution/acryl-npm-launcher/managed.js` MANAGED.ade). */
|
|
360
|
+
const ADE_BLEND = "acryl.ade";
|
|
361
|
+
const ADE_FOLDER = "ade";
|
|
362
|
+
const ANSI = /\u001b\[[0-9;?]*[ -/]*[@-~]/gu;
|
|
363
|
+
const tail = (text) => {
|
|
364
|
+
const lines = text.replace(ANSI, "").split(/\r?\n/u).map((line) => line.trim()).filter((line) => line !== "").slice(-12).join("\n");
|
|
365
|
+
return lines !== "" && /\n\s*$/u.test(text) ? `${lines}\n` : lines;
|
|
366
|
+
};
|
|
367
|
+
var LauncherService = class {
|
|
368
|
+
#ports;
|
|
369
|
+
#started = /* @__PURE__ */ new Map();
|
|
370
|
+
/** Why an app last ended without being stopped, until it is started again. */
|
|
371
|
+
#failures = /* @__PURE__ */ new Map();
|
|
372
|
+
#job;
|
|
373
|
+
#blends;
|
|
374
|
+
#signIn;
|
|
375
|
+
/** Apps just made whose `npm install` is running (`#install`), by id. */
|
|
376
|
+
#installing = /* @__PURE__ */ new Map();
|
|
377
|
+
/** The tools apps need: being fetched (the first start on a computer without them), ready, or not had (apps then try the computer's own). */
|
|
378
|
+
#tools = { state: "ready" };
|
|
379
|
+
constructor(ports) {
|
|
380
|
+
this.#ports = ports;
|
|
381
|
+
}
|
|
382
|
+
/** Start fetching what this computer lacks of Node.js and gh; apps wait for it (`#toolsReady`). Called once, when the launcher starts. */
|
|
383
|
+
prepareTools() {
|
|
384
|
+
const prepare = this.#ports.prepareTools;
|
|
385
|
+
if (prepare === void 0) return Promise.resolve();
|
|
386
|
+
this.#tools = { state: "preparing" };
|
|
387
|
+
return prepare().then(() => {
|
|
388
|
+
this.#tools = { state: "ready" };
|
|
389
|
+
}, (cause) => {
|
|
390
|
+
this.#tools = {
|
|
391
|
+
state: "failed",
|
|
392
|
+
reason: cause instanceof Error ? cause.message : String(cause)
|
|
393
|
+
};
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
/** Refuse, in words, to start anything while the tools are still being fetched; a failed fetch lets apps try the computer's own tools. */
|
|
397
|
+
#toolsReady() {
|
|
398
|
+
if (this.#tools.state === "preparing") throw new LauncherRefusal("Getting Node.js and GitHub's tool ready for your apps (only on the first start on this computer, about a minute). Try again in a moment.");
|
|
399
|
+
}
|
|
400
|
+
state() {
|
|
401
|
+
if (this.#blends === void 0 || Date.now() - this.#blends.at >= BLENDS_FRESH_MS) this.blends().catch(() => void 0);
|
|
402
|
+
const apps = readApps(this.#ports.home).map((entry) => this.#view(entry));
|
|
403
|
+
const at = this.#job;
|
|
404
|
+
const job = at === void 0 ? null : {
|
|
405
|
+
name: at.name,
|
|
406
|
+
kind: at.kind,
|
|
407
|
+
line: at.line,
|
|
408
|
+
status: at.status,
|
|
409
|
+
step: at.step,
|
|
410
|
+
since: at.since,
|
|
411
|
+
output: at.output.trimEnd(),
|
|
412
|
+
...at.message === void 0 ? {} : { message: at.message }
|
|
413
|
+
};
|
|
414
|
+
return {
|
|
415
|
+
apps,
|
|
416
|
+
appsFolder: this.#ports.appsFolder,
|
|
417
|
+
starters: STARTERS,
|
|
418
|
+
job
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
#view(entry) {
|
|
422
|
+
const started = this.#started.get(entry.id);
|
|
423
|
+
const missing = inspectApp(entry).state === "missing";
|
|
424
|
+
const installing = this.#installing.get(entry.id);
|
|
425
|
+
const state = missing ? "missing" : started === void 0 ? installing !== void 0 ? "starting" : "stopped" : started.address === void 0 ? "starting" : "running";
|
|
426
|
+
const failure = this.#failures.get(entry.id);
|
|
427
|
+
const blend = entry.blend === "" ? blendOf(entry.path) : entry.blend;
|
|
428
|
+
const version = missing ? "" : blendVersionOf(entry.path);
|
|
429
|
+
const latest = this.#blends?.list.find((listed) => listed.id === blend)?.version;
|
|
430
|
+
const github = missing ? void 0 : githubOf(entry.path);
|
|
431
|
+
return {
|
|
432
|
+
id: entry.id,
|
|
433
|
+
name: basename(entry.path),
|
|
434
|
+
path: entry.path,
|
|
435
|
+
blend,
|
|
436
|
+
madeBy: entry.madeBy,
|
|
437
|
+
surface: entry.surface,
|
|
438
|
+
state,
|
|
439
|
+
...version === "" ? {} : { version },
|
|
440
|
+
...version !== "" && latest !== void 0 && newerVersion(latest, version) ? { update: latest } : {},
|
|
441
|
+
...started?.address === void 0 ? {} : { address: started.address },
|
|
442
|
+
...started === void 0 || started.address !== void 0 || started.output === "" ? {} : { output: started.output.trimEnd() },
|
|
443
|
+
...started === void 0 && installing !== void 0 && installing.output !== "" ? { output: installing.output.trimEnd() } : {},
|
|
444
|
+
...failure === void 0 || state !== "stopped" ? {} : { failure },
|
|
445
|
+
...github === void 0 ? {} : { github }
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
add(path) {
|
|
449
|
+
try {
|
|
450
|
+
return this.#view(addApp(this.#ports.home, { path }).entry);
|
|
451
|
+
} catch (cause) {
|
|
452
|
+
throw new LauncherRefusal(cause instanceof Error ? cause.message : String(cause));
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
remove(id) {
|
|
456
|
+
this.stop(id);
|
|
457
|
+
if (!removeApp(this.#ports.home, id)) throw new LauncherRefusal(`there is no app "${id}" in the list`);
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Start an app the way the terminal's "Open an app" does, with the saved model key: a Web app with `npm run dev` in its folder (its address opens in the
|
|
461
|
+
* browser), a Desktop app with `npm run desktop`, which starts the same app and opens it in its own window (Electron is fetched the first time).
|
|
462
|
+
*/
|
|
463
|
+
start(id, surface) {
|
|
464
|
+
this.#toolsReady();
|
|
465
|
+
const entry = readApps(this.#ports.home).find((app) => app.id === id);
|
|
466
|
+
if (entry === void 0) throw new LauncherRefusal(`there is no app "${id}" in the list`);
|
|
467
|
+
if (inspectApp(entry).state === "missing") throw new LauncherRefusal(`the folder ${entry.path} is gone: remove it from the list, or put it back`);
|
|
468
|
+
if (this.#started.has(id)) return;
|
|
469
|
+
this.#failures.delete(id);
|
|
470
|
+
if (!this.#ports.exists(join(entry.path, "node_modules"))) {
|
|
471
|
+
this.#installThenStart(id, entry.path, surface);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const desktop = (surface ?? entry.surface) === "desktop";
|
|
475
|
+
const run = this.#ports.runner({
|
|
476
|
+
program: "npm",
|
|
477
|
+
args: ["run", desktop ? "desktop" : "dev"],
|
|
478
|
+
cwd: entry.path,
|
|
479
|
+
env: this.#environment(),
|
|
480
|
+
...desktop ? { log: this.#appLog(id) } : {}
|
|
481
|
+
});
|
|
482
|
+
this.#watch(id, run, void 0, desktop);
|
|
483
|
+
}
|
|
484
|
+
#installThenStart(id, folder, surface) {
|
|
485
|
+
const run = this.#ports.runner({
|
|
486
|
+
program: "npm",
|
|
487
|
+
args: [
|
|
488
|
+
"install",
|
|
489
|
+
"--loglevel",
|
|
490
|
+
"http"
|
|
491
|
+
],
|
|
492
|
+
cwd: folder,
|
|
493
|
+
env: this.#environment()
|
|
494
|
+
});
|
|
495
|
+
const started = {
|
|
496
|
+
run,
|
|
497
|
+
desktop: false,
|
|
498
|
+
address: void 0,
|
|
499
|
+
output: ""
|
|
500
|
+
};
|
|
501
|
+
this.#started.set(id, started);
|
|
502
|
+
run.onOutput((text) => {
|
|
503
|
+
started.output = tail(`${started.output}${text}`);
|
|
504
|
+
});
|
|
505
|
+
run.onEnd((end) => {
|
|
506
|
+
if (this.#started.get(id) !== started) return;
|
|
507
|
+
this.#started.delete(id);
|
|
508
|
+
if (end.notFound) this.#failures.set(id, "Node.js and npm were not found on this machine. Install Node.js 22.19 or later from nodejs.org, then try again.");
|
|
509
|
+
else if (end.code === 0) try {
|
|
510
|
+
this.start(id, surface);
|
|
511
|
+
} catch (cause) {
|
|
512
|
+
this.#failures.set(id, cause instanceof Error ? cause.message : String(cause));
|
|
513
|
+
}
|
|
514
|
+
else if (end.code !== null) this.#failures.set(id, started.output === "" ? end.error ?? "npm install ended before the app could run." : started.output.trimEnd());
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* ACRYL ADE, the way `acryl ade` (`--gui`) opens it: the Blend `acryl.ade` in `<home>/ade`, made from the registry the first time (listed, installed and
|
|
519
|
+
* started as any app made here is), then started in a window or the browser. Already running, it is left as it is.
|
|
520
|
+
*/
|
|
521
|
+
openAde(surface) {
|
|
522
|
+
const folder = join(this.#ports.home, ADE_FOLDER);
|
|
523
|
+
if (!this.#ports.exists(join(folder, APP_MARKER))) {
|
|
524
|
+
this.create({
|
|
525
|
+
name: ADE_FOLDER,
|
|
526
|
+
where: this.#ports.home,
|
|
527
|
+
blend: ADE_BLEND,
|
|
528
|
+
surface
|
|
529
|
+
});
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
this.start(addApp(this.#ports.home, {
|
|
533
|
+
path: folder,
|
|
534
|
+
surface
|
|
535
|
+
}).entry.id, surface);
|
|
536
|
+
}
|
|
537
|
+
stop(id) {
|
|
538
|
+
this.#started.get(id)?.run.stop();
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Bring the newer registry release of the app's Blend into it: `acrylblends update` in its folder (a git merge that keeps the person's changes, then npm
|
|
542
|
+
* install). A running app is stopped first and started again after.
|
|
543
|
+
*/
|
|
544
|
+
update(id) {
|
|
545
|
+
this.#toolsReady();
|
|
546
|
+
if (this.#job?.status === "running") throw new LauncherRefusal(`${this.#job.name} is still being worked on: wait for it to finish`);
|
|
547
|
+
const entry = readApps(this.#ports.home).find((app) => app.id === id);
|
|
548
|
+
if (entry === void 0) throw new LauncherRefusal(`there is no app "${id}" in the list`);
|
|
549
|
+
const view = this.#view(entry);
|
|
550
|
+
if (view.update === void 0) throw new LauncherRefusal(`${view.name} is on the latest release of ${view.blend}`);
|
|
551
|
+
const restart = this.#started.has(id);
|
|
552
|
+
this.stop(id);
|
|
553
|
+
const run = this.#ports.runner({
|
|
554
|
+
program: "npm",
|
|
555
|
+
args: [
|
|
556
|
+
"exec",
|
|
557
|
+
"--yes",
|
|
558
|
+
UPDATE_SPEC,
|
|
559
|
+
"--",
|
|
560
|
+
"update"
|
|
561
|
+
],
|
|
562
|
+
cwd: entry.path,
|
|
563
|
+
env: this.#environment()
|
|
564
|
+
});
|
|
565
|
+
const job = {
|
|
566
|
+
name: view.name,
|
|
567
|
+
kind: "update",
|
|
568
|
+
line: `cd ${entry.path} && npx ${UPDATE_SPEC} update`,
|
|
569
|
+
status: "running",
|
|
570
|
+
run,
|
|
571
|
+
since: Date.now(),
|
|
572
|
+
output: "",
|
|
573
|
+
step: `Updating ${view.name} from ${view.blend} ${view.version ?? ""} to ${view.update}: getting the release and merging it in, your changes are kept, then npm install. Next: ${restart ? "starting it again" : "done"}.`
|
|
574
|
+
};
|
|
575
|
+
this.#job = job;
|
|
576
|
+
let output = "";
|
|
577
|
+
run.onOutput((text) => {
|
|
578
|
+
output = tail(`${output}${text}`);
|
|
579
|
+
job.output = output;
|
|
580
|
+
});
|
|
581
|
+
run.onEnd((end) => {
|
|
582
|
+
if (job.run !== run) return;
|
|
583
|
+
if (end.notFound) {
|
|
584
|
+
job.status = "failed";
|
|
585
|
+
job.message = "Node.js and npm were not found on this machine. Install Node.js 22.19 or later from nodejs.org, then try again.";
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
if (end.code !== 0) {
|
|
589
|
+
job.status = "failed";
|
|
590
|
+
job.message = output === "" ? end.error ?? "The update ended before it was done; the app is unchanged." : output.trimEnd();
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
job.status = "done";
|
|
594
|
+
job.step = `Updated ${view.name} to ${view.blend} ${view.update}.${restart ? " Starting it again: its progress shows on its card below." : ""}`;
|
|
595
|
+
job.output = "";
|
|
596
|
+
if (restart) try {
|
|
597
|
+
this.start(id);
|
|
598
|
+
} catch (cause) {
|
|
599
|
+
this.#failures.set(id, cause instanceof Error ? cause.message : String(cause));
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* When this app quits (or the plugin unloads): stop everything it started, except a Desktop app whose window is open. That window is the person's
|
|
605
|
+
* own, and closing it stops the app; a Web app has no window, so it would run on unseen. Returns the names of the apps left open.
|
|
606
|
+
*/
|
|
607
|
+
stopAll() {
|
|
608
|
+
const kept = [];
|
|
609
|
+
for (const [id, started] of this.#started) if (started.desktop && started.address !== void 0) kept.push(id);
|
|
610
|
+
else started.run.stop();
|
|
611
|
+
this.#job?.run.stop();
|
|
612
|
+
for (const job of this.#installing.values()) job.run.stop();
|
|
613
|
+
return kept;
|
|
614
|
+
}
|
|
615
|
+
/** Where a Desktop app's output goes (`<home>/logs/apps/<id>.log`): a file, so the app does not depend on this process to write it. */
|
|
616
|
+
#appLog(id) {
|
|
617
|
+
const folder = join(this.#ports.home, "logs", "apps");
|
|
618
|
+
this.#ports.makeFolder(folder);
|
|
619
|
+
return join(folder, `${id}.log`);
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Make an app: the same `acryl new <name> <starter>` the terminal runs, in the folder the person chose. `acryl new` goes on to install and start the app,
|
|
623
|
+
* so the process it began is the app's running process: when its blend.yaml appears the app is listed and that process is adopted as the app's.
|
|
624
|
+
*/
|
|
625
|
+
create(request) {
|
|
626
|
+
this.#toolsReady();
|
|
627
|
+
if (this.#job?.status === "running") throw new LauncherRefusal(`${this.#job.name} is still being made: wait for it to finish`);
|
|
628
|
+
const problem = whereProblem(request.where) ?? (APP_NAME.test(request.name) ? void 0 : appNameProblem(request.name, this.#ports));
|
|
629
|
+
if (problem !== void 0) throw new LauncherRefusal(problem);
|
|
630
|
+
if (request.starter === void 0 === (request.blend === void 0)) throw new LauncherRefusal("choose a starter or a Blend, not both");
|
|
631
|
+
if (request.starter !== void 0 && !STARTERS.some((starter) => starter.value === request.starter)) throw new LauncherRefusal(`there is no starter "${request.starter}"`);
|
|
632
|
+
if (request.blend !== void 0 && !BLEND_ID.test(request.blend) && !BLEND_SOURCE.test(request.blend)) throw new LauncherRefusal(`"${request.blend}" is not a Blend id (for example acrylblends.notes) or a Blend on GitHub (github:owner/repo#tag)`);
|
|
633
|
+
if (!MAKEABLE_SURFACES.includes(request.surface)) throw new LauncherRefusal("CLI (TUI) apps are coming: choose Web or Desktop");
|
|
634
|
+
const folder = join(request.where, request.name);
|
|
635
|
+
if (this.#ports.exists(folder)) throw new LauncherRefusal(`there is already a folder named ${request.name} in ${request.where}`);
|
|
636
|
+
this.#ports.makeFolder(request.where);
|
|
637
|
+
const source = request.blend === void 0 ? [request.starter ?? ""] : ["--from", request.blend];
|
|
638
|
+
const args = [
|
|
639
|
+
"exec",
|
|
640
|
+
"--yes",
|
|
641
|
+
CREATE_SPEC,
|
|
642
|
+
"--",
|
|
643
|
+
request.name,
|
|
644
|
+
...source
|
|
645
|
+
];
|
|
646
|
+
const line = `cd ${request.where} && acryl new ${request.name} ${source.join(" ")}`;
|
|
647
|
+
const run = this.#ports.runner({
|
|
648
|
+
program: "npm",
|
|
649
|
+
args,
|
|
650
|
+
cwd: request.where,
|
|
651
|
+
env: {
|
|
652
|
+
...this.#environment(),
|
|
653
|
+
npm_config_loglevel: "http"
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
const step = request.blend === void 0 ? `Step 1 of 2: getting ${request.name} and installing its dependencies (${CREATE_SPEC}, about a minute). Next: starting it.` : `Step 1 of 3: getting ${request.name} (${CREATE_SPEC}). Next: installing its dependencies, then starting it.`;
|
|
657
|
+
const job = {
|
|
658
|
+
name: request.name,
|
|
659
|
+
kind: "make",
|
|
660
|
+
line,
|
|
661
|
+
status: "running",
|
|
662
|
+
run,
|
|
663
|
+
step,
|
|
664
|
+
since: Date.now(),
|
|
665
|
+
output: ""
|
|
666
|
+
};
|
|
667
|
+
this.#job = job;
|
|
668
|
+
this.#watch(void 0, run, {
|
|
669
|
+
folder,
|
|
670
|
+
surface: request.surface,
|
|
671
|
+
job
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
/** The registry's Blends, read again at most once a minute: the store is opened and closed often, and the registry is a public file. */
|
|
675
|
+
async blends() {
|
|
676
|
+
if (this.#blends !== void 0 && Date.now() - this.#blends.at < BLENDS_FRESH_MS) return this.#blends.list;
|
|
677
|
+
const list = await this.#ports.blends();
|
|
678
|
+
this.#blends = {
|
|
679
|
+
at: Date.now(),
|
|
680
|
+
list
|
|
681
|
+
};
|
|
682
|
+
return list;
|
|
683
|
+
}
|
|
684
|
+
/** The apps folder, made when it is not there yet: the assistant's agent works in it, and a folder that does not exist cannot be worked in. */
|
|
685
|
+
assistantFolder() {
|
|
686
|
+
this.#ports.makeFolder(this.#ports.appsFolder);
|
|
687
|
+
return this.#ports.appsFolder;
|
|
688
|
+
}
|
|
689
|
+
#entry(id) {
|
|
690
|
+
const entry = readApps(this.#ports.home).find((app) => app.id === id);
|
|
691
|
+
if (entry === void 0) throw new LauncherRefusal(`there is no app "${id}" in the list`);
|
|
692
|
+
return entry;
|
|
693
|
+
}
|
|
694
|
+
/** A door call, with "the app is not running" and a refusal as the sentence a person can act on. */
|
|
695
|
+
async #door(id, run) {
|
|
696
|
+
const entry = this.#entry(id);
|
|
697
|
+
let answer;
|
|
698
|
+
try {
|
|
699
|
+
answer = await run(this.#ports.door, entry.path);
|
|
700
|
+
} catch (cause) {
|
|
701
|
+
if (cause instanceof DoorUnavailable) throw new LauncherRefusal(cause.message);
|
|
702
|
+
throw cause;
|
|
703
|
+
}
|
|
704
|
+
if (!answer.ok) throw new LauncherRefusal(answer.message ?? "the app refused");
|
|
705
|
+
return answer;
|
|
706
|
+
}
|
|
707
|
+
/** The plugins of a running app: the ones its people made (shareable) and the ones pulled from a registry. */
|
|
708
|
+
async plugins(id) {
|
|
709
|
+
let answer;
|
|
710
|
+
try {
|
|
711
|
+
answer = await this.#door(id, (door, folder) => door.call(folder, "plugin-list", {}));
|
|
712
|
+
} catch (cause) {
|
|
713
|
+
if (cause instanceof LauncherRefusal && /no extensions plugin to drive/u.test(cause.message)) throw new LauncherRefusal("This app keeps its plugins in its own page: open it, then Settings > Plugins. Pulling a plugin from here needs an app made with a newer Blueprint.");
|
|
714
|
+
throw cause;
|
|
715
|
+
}
|
|
716
|
+
return (Array.isArray(answer.value) ? answer.value : []).filter((plugin) => typeof plugin.name === "string").map((plugin) => ({
|
|
717
|
+
name: String(plugin.name),
|
|
718
|
+
version: typeof plugin.version === "string" ? plugin.version : "",
|
|
719
|
+
state: typeof plugin.state === "string" ? plugin.state : "",
|
|
720
|
+
...typeof plugin.pulledFrom === "string" ? { pulledFrom: plugin.pulledFrom } : {},
|
|
721
|
+
...typeof plugin.author === "string" ? { author: plugin.author } : {}
|
|
722
|
+
}));
|
|
723
|
+
}
|
|
724
|
+
/** Pull a plugin into a running app from a registry source (`github:owner/repo@version`), the way `acryl plugin pull` does. */
|
|
725
|
+
async pullPlugin(id, source) {
|
|
726
|
+
if (source.trim() === "" || source.startsWith("-") || source.length > 1024) throw new LauncherRefusal("say where the plugin is, for example github:someone/pomodoro@0.1.0");
|
|
727
|
+
return {
|
|
728
|
+
text: (await this.#door(id, (door, folder) => door.call(folder, "plugin-pull", { source: source.trim() }))).text ?? "Pulled.",
|
|
729
|
+
plugins: await this.plugins(id)
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
#checkPublish(request) {
|
|
733
|
+
if (!PLUGIN_NAME.test(request.plugin)) throw new LauncherRefusal(`"${request.plugin}" is not a plugin name`);
|
|
734
|
+
if (request.owner !== void 0 && !ACCOUNT_NAME.test(request.owner)) throw new LauncherRefusal(`"${request.owner}" is not a GitHub account name`);
|
|
735
|
+
if (request.license !== void 0 && !LICENSE_ID.test(request.license)) throw new LauncherRefusal(`"${request.license}" is not a license id`);
|
|
736
|
+
}
|
|
737
|
+
#options(request) {
|
|
738
|
+
return {
|
|
739
|
+
...request.owner === void 0 ? {} : { owner: request.owner },
|
|
740
|
+
...request.license === void 0 ? {} : { license: request.license }
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
/** What publishing a plugin would send (the account, repository, version, license and files). Sends nothing. */
|
|
744
|
+
async publishPlan(request) {
|
|
745
|
+
this.#checkPublish(request);
|
|
746
|
+
const plan = (await this.#door(request.id, (door, folder) => door.call(folder, "plugin-publish", {
|
|
747
|
+
name: request.plugin,
|
|
748
|
+
...this.#options(request)
|
|
749
|
+
}))).value;
|
|
750
|
+
if (plan === null || typeof plan !== "object" || typeof plan.owner !== "string" || typeof plan.repo !== "string" || typeof plan.version !== "string") throw new LauncherRefusal("the app did not give a publish plan");
|
|
751
|
+
return {
|
|
752
|
+
owner: plan.owner,
|
|
753
|
+
repo: plan.repo,
|
|
754
|
+
version: plan.version,
|
|
755
|
+
visibility: String(plan.visibility ?? ""),
|
|
756
|
+
license: String(plan.license ?? ""),
|
|
757
|
+
createsRepository: plan.createsRepository === true,
|
|
758
|
+
files: Array.isArray(plan.files) ? plan.files.map(String) : [],
|
|
759
|
+
integrity: String(plan.integrity ?? ""),
|
|
760
|
+
digest: String(plan.digest ?? ""),
|
|
761
|
+
pullLine: String(plan.pullLine ?? "")
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
/** The person's yes: publish exactly the plan they read. The app sends it only if the plugin still matches that plan. */
|
|
765
|
+
async publish(request) {
|
|
766
|
+
this.#checkPublish(request);
|
|
767
|
+
const result = (await this.#door(request.id, (door, folder) => door.confirmPublish(folder, {
|
|
768
|
+
name: request.plugin,
|
|
769
|
+
...this.#options(request),
|
|
770
|
+
confirm: request.plan
|
|
771
|
+
}))).value;
|
|
772
|
+
if (result?.ok !== true) throw new LauncherRefusal(`Not published: ${result?.reason ?? "the app did not answer"}${result?.next === void 0 ? "" : `. ${result.next}`}`);
|
|
773
|
+
return {
|
|
774
|
+
releaseUrl: result.releaseUrl ?? "",
|
|
775
|
+
pullLine: result.pullLine ?? ""
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* The app's own share command (`plugins/save/share-cli.ts`, acrylblends 1.5.5), run in its folder: it prints one JSON line, the plan or the result.
|
|
780
|
+
* The app need not be running. An app made before that command has none, and is told how to get it.
|
|
781
|
+
*/
|
|
782
|
+
async #shareCommand(request, confirm) {
|
|
783
|
+
this.#toolsReady();
|
|
784
|
+
const entry = this.#entry(request.id);
|
|
785
|
+
if (request.owner !== void 0 && !ACCOUNT_NAME.test(request.owner)) throw new LauncherRefusal(`"${request.owner}" is not a GitHub account name`);
|
|
786
|
+
if (!this.#ports.exists(join(entry.path, SHARE_COMMAND))) throw new LauncherRefusal(`This app was made before apps could share themselves. In ${entry.path} run "npx acrylblends@latest upgrade", then try again.`);
|
|
787
|
+
if (request.visibility !== void 0 && confirm === void 0) try {
|
|
788
|
+
setBlendVisibility(entry.path, request.visibility);
|
|
789
|
+
} catch (cause) {
|
|
790
|
+
throw new LauncherRefusal(`Could not set who may see it: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
791
|
+
}
|
|
792
|
+
const args = [
|
|
793
|
+
SHARE_COMMAND,
|
|
794
|
+
"--json",
|
|
795
|
+
...request.owner === void 0 ? [] : ["--owner", request.owner],
|
|
796
|
+
...confirm === void 0 ? [] : ["--confirm", Buffer.from(JSON.stringify(confirm), "utf8").toString("base64url")]
|
|
797
|
+
];
|
|
798
|
+
const run = this.#ports.runner({
|
|
799
|
+
program: "node",
|
|
800
|
+
args,
|
|
801
|
+
cwd: entry.path,
|
|
802
|
+
env: this.#environment()
|
|
803
|
+
});
|
|
804
|
+
let output = "";
|
|
805
|
+
run.onOutput((text) => {
|
|
806
|
+
output += text;
|
|
807
|
+
});
|
|
808
|
+
if ((await new Promise((resolve) => {
|
|
809
|
+
run.onEnd(resolve);
|
|
810
|
+
})).notFound) throw new LauncherRefusal("Node.js was not found on this machine. Install Node.js 22.19 or later from nodejs.org, then try again.");
|
|
811
|
+
const line = output.split(/\r?\n/u).map((text) => text.trim()).findLast((text) => text.startsWith("{"));
|
|
812
|
+
let answer;
|
|
813
|
+
try {
|
|
814
|
+
answer = line === void 0 ? void 0 : JSON.parse(line);
|
|
815
|
+
} catch {
|
|
816
|
+
answer = void 0;
|
|
817
|
+
}
|
|
818
|
+
if (answer === null || typeof answer !== "object") throw new LauncherRefusal(tail(output) === "" ? "the app did not answer" : tail(output));
|
|
819
|
+
const value = answer;
|
|
820
|
+
if (value.ok === false) throw new LauncherRefusal(`Not shared: ${typeof value.reason === "string" ? value.reason : "the app refused"}`);
|
|
821
|
+
return value;
|
|
822
|
+
}
|
|
823
|
+
/** What sharing the app would send: the repository (a new one, or the remote it has), its visibility and how many changed files are saved first. Sends nothing. */
|
|
824
|
+
async sharePlan(request) {
|
|
825
|
+
const plan = await this.#shareCommand(request, void 0);
|
|
826
|
+
if (typeof plan.repository !== "string" || typeof plan.create !== "boolean" || plan.visibility !== "private" && plan.visibility !== "public" || typeof plan.changes !== "number") throw new LauncherRefusal("the app did not give a share plan");
|
|
827
|
+
return {
|
|
828
|
+
repository: plan.repository,
|
|
829
|
+
create: plan.create,
|
|
830
|
+
visibility: plan.visibility,
|
|
831
|
+
changes: plan.changes
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
/** The person's yes: share exactly the plan they read. The app sends it only if it still matches that plan. */
|
|
835
|
+
async share(request) {
|
|
836
|
+
const result = await this.#shareCommand(request, request.plan);
|
|
837
|
+
if (result.ok !== true || typeof result.remote !== "string") throw new LauncherRefusal("Not shared: the app did not answer");
|
|
838
|
+
return {
|
|
839
|
+
remote: result.remote,
|
|
840
|
+
created: result.created === true
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
async health(probe = this.#ports.runner) {
|
|
844
|
+
const version = (program) => new Promise((resolve) => {
|
|
845
|
+
const run = probe({
|
|
846
|
+
program,
|
|
847
|
+
args: ["--version"],
|
|
848
|
+
cwd: this.#ports.home,
|
|
849
|
+
env: this.#environment()
|
|
850
|
+
});
|
|
851
|
+
let text = "";
|
|
852
|
+
run.onOutput((chunk) => {
|
|
853
|
+
text += chunk;
|
|
854
|
+
});
|
|
855
|
+
run.onEnd((end) => {
|
|
856
|
+
resolve(end.code === 0 && !end.notFound ? text.trim().split(/\s+/u)[0]?.replace(/^v/u, "") : void 0);
|
|
857
|
+
});
|
|
858
|
+
});
|
|
859
|
+
const [node, npm] = await Promise.all([version("node"), version("npm")]);
|
|
860
|
+
return {
|
|
861
|
+
...node === void 0 ? {} : { node },
|
|
862
|
+
...npm === void 0 ? {} : { npm },
|
|
863
|
+
modelKey: readModelKey(this.#ports.home) !== void 0,
|
|
864
|
+
home: this.#ports.home
|
|
865
|
+
};
|
|
866
|
+
}
|
|
867
|
+
/** The saved key, masked, for the screen's Settings and Health; never the key itself. */
|
|
868
|
+
maskedModelKey() {
|
|
869
|
+
const key = readModelKey(this.#ports.home);
|
|
870
|
+
return key === void 0 ? void 0 : maskKey(key);
|
|
871
|
+
}
|
|
872
|
+
#environment() {
|
|
873
|
+
const inherited = this.#ports.environment();
|
|
874
|
+
return appEnvironment(toolsEnv(this.#ports.home, {
|
|
875
|
+
...inherited,
|
|
876
|
+
...modelEnv(this.#ports.home, inherited),
|
|
877
|
+
...githubEnv(this.#ports.home, inherited)
|
|
878
|
+
}));
|
|
879
|
+
}
|
|
880
|
+
/** Who is signed in to GitHub here (the token apps get as GH_TOKEN), never the token. */
|
|
881
|
+
github() {
|
|
882
|
+
return {
|
|
883
|
+
available: githubClientId(this.#ports.environment()) !== void 0,
|
|
884
|
+
login: readGithub(this.#ports.home)?.login ?? null
|
|
885
|
+
};
|
|
886
|
+
}
|
|
887
|
+
/** Start GitHub's device sign-in: the code for the person; the device code stays here until it is approved, refused or expires. */
|
|
888
|
+
async githubStart() {
|
|
889
|
+
const clientId = githubClientId(this.#ports.environment());
|
|
890
|
+
if (clientId === void 0) throw new LauncherRefusal("this build of Control Center has no GitHub app to sign in with");
|
|
891
|
+
try {
|
|
892
|
+
const started = await startGithubSignIn(clientId);
|
|
893
|
+
this.#signIn = {
|
|
894
|
+
clientId,
|
|
895
|
+
deviceCode: started.deviceCode,
|
|
896
|
+
until: Date.now() + started.expiresIn * 1e3
|
|
897
|
+
};
|
|
898
|
+
return {
|
|
899
|
+
userCode: started.userCode,
|
|
900
|
+
verificationUri: started.verificationUri,
|
|
901
|
+
interval: started.interval
|
|
902
|
+
};
|
|
903
|
+
} catch (cause) {
|
|
904
|
+
throw new LauncherRefusal(`GitHub sign-in could not start: ${cause instanceof Error ? cause.message : String(cause)}. Check the network and try again.`);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
/** One poll of the sign-in under way: pending, connected (the token is saved and every app started from now on gets it), or failed. */
|
|
908
|
+
async githubPoll() {
|
|
909
|
+
const signIn = this.#signIn;
|
|
910
|
+
if (signIn === void 0) return {
|
|
911
|
+
state: "failed",
|
|
912
|
+
reason: "no GitHub sign-in is under way: start it again"
|
|
913
|
+
};
|
|
914
|
+
if (Date.now() > signIn.until) {
|
|
915
|
+
this.#signIn = void 0;
|
|
916
|
+
return {
|
|
917
|
+
state: "failed",
|
|
918
|
+
reason: "the code expired: start the sign-in again"
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
try {
|
|
922
|
+
const answer = await pollGithubSignIn(this.#ports.home, signIn.clientId, signIn.deviceCode);
|
|
923
|
+
if (answer.state !== "pending") this.#signIn = void 0;
|
|
924
|
+
return answer;
|
|
925
|
+
} catch {
|
|
926
|
+
return { state: "pending" };
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
/** Forget the GitHub connection on this machine. Apps started from now on no longer get the token. */
|
|
930
|
+
githubDisconnect() {
|
|
931
|
+
this.#signIn = void 0;
|
|
932
|
+
clearGithub(this.#ports.home);
|
|
933
|
+
return this.github();
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* Follow one process. For an app being started, `id` is known; for one being made it is not until the folder has its blend.yaml, which is when the app
|
|
937
|
+
* is listed and the process becomes that app's.
|
|
938
|
+
*/
|
|
939
|
+
#watch(id, run, making, desktop = false) {
|
|
940
|
+
const started = {
|
|
941
|
+
run,
|
|
942
|
+
desktop,
|
|
943
|
+
address: void 0,
|
|
944
|
+
output: ""
|
|
945
|
+
};
|
|
946
|
+
let appId = id;
|
|
947
|
+
if (appId !== void 0) this.#started.set(appId, started);
|
|
948
|
+
run.onOutput((text) => {
|
|
949
|
+
started.output = tail(`${started.output}${text}`);
|
|
950
|
+
if (making !== void 0 && making.job.run === run) making.job.output = started.output;
|
|
951
|
+
started.address ??= ADDRESS.exec(text)?.[0];
|
|
952
|
+
if (making !== void 0 && appId === void 0 && this.#ports.exists(join(making.folder, APP_MARKER))) {
|
|
953
|
+
appId = addApp(this.#ports.home, {
|
|
954
|
+
path: making.folder,
|
|
955
|
+
surface: making.surface
|
|
956
|
+
}).entry.id;
|
|
957
|
+
this.#started.set(appId, started);
|
|
958
|
+
}
|
|
959
|
+
if (making !== void 0 && started.address !== void 0 && making.job.status === "running") making.job.status = "done";
|
|
960
|
+
});
|
|
961
|
+
run.onEnd((end) => {
|
|
962
|
+
if (appId !== void 0 && this.#started.get(appId) === started) {
|
|
963
|
+
this.#started.delete(appId);
|
|
964
|
+
const closed = started.address !== void 0 && readApps(this.#ports.home).find((entry) => entry.id === appId)?.surface === "desktop";
|
|
965
|
+
if (end.code !== 0 && end.code !== null && !end.notFound && !closed) this.#failures.set(appId, started.output.trimEnd());
|
|
966
|
+
}
|
|
967
|
+
if (end.notFound) this.#note(making, "failed", "Node.js and npm were not found on this machine. Install Node.js 22.19 or later from nodejs.org, then try again.", id);
|
|
968
|
+
else if (making !== void 0 && making.job.status === "running") if (end.code === 0 && appId !== void 0) if (this.#ports.exists(join(making.folder, "node_modules"))) this.#finish(appId, making.job);
|
|
969
|
+
else this.#install(appId, making.folder, making.job);
|
|
970
|
+
else making.job.status = "failed";
|
|
971
|
+
if (making !== void 0 && making.job.status === "failed" && making.job.message === void 0) making.job.message = started.output === "" ? end.error ?? "The command ended before the app was made." : started.output.trimEnd();
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
/** The job's last step: the app is made, so it is started (a Desktop app opens its window). */
|
|
975
|
+
#finish(appId, job) {
|
|
976
|
+
job.status = "done";
|
|
977
|
+
job.step = `Made. Starting ${job.name} now: its progress shows on its card below.`;
|
|
978
|
+
job.output = "";
|
|
979
|
+
delete job.message;
|
|
980
|
+
try {
|
|
981
|
+
this.start(appId);
|
|
982
|
+
} catch (cause) {
|
|
983
|
+
this.#failures.set(appId, cause instanceof Error ? cause.message : String(cause));
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
/** `npm install` in the new app's folder, as the job's second step; the job is still running while it does, and says so. */
|
|
987
|
+
#install(appId, folder, job) {
|
|
988
|
+
this.#installing.set(appId, job);
|
|
989
|
+
job.step = "Step 2 of 3: installing its dependencies (npm install, about a minute). Next: starting it.";
|
|
990
|
+
job.output = "";
|
|
991
|
+
const run = this.#ports.runner({
|
|
992
|
+
program: "npm",
|
|
993
|
+
args: [
|
|
994
|
+
"install",
|
|
995
|
+
"--loglevel",
|
|
996
|
+
"http"
|
|
997
|
+
],
|
|
998
|
+
cwd: folder,
|
|
999
|
+
env: this.#environment()
|
|
1000
|
+
});
|
|
1001
|
+
job.run = run;
|
|
1002
|
+
let output = "";
|
|
1003
|
+
run.onOutput((text) => {
|
|
1004
|
+
output = tail(`${output}${text}`);
|
|
1005
|
+
job.output = output;
|
|
1006
|
+
});
|
|
1007
|
+
run.onEnd((end) => {
|
|
1008
|
+
this.#installing.delete(appId);
|
|
1009
|
+
if (job.run !== run) return;
|
|
1010
|
+
if (end.notFound) {
|
|
1011
|
+
job.status = "failed";
|
|
1012
|
+
job.message = "Node.js and npm were not found on this machine. Install Node.js 22.19 or later from nodejs.org, then try again.";
|
|
1013
|
+
} else if (end.code === 0) this.#finish(appId, job);
|
|
1014
|
+
else {
|
|
1015
|
+
job.status = "failed";
|
|
1016
|
+
job.message = output === "" ? end.error ?? "npm install ended before the app could run." : output.trimEnd();
|
|
1017
|
+
}
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
#note(making, status, message, id) {
|
|
1021
|
+
if (making !== void 0) {
|
|
1022
|
+
making.job.status = status;
|
|
1023
|
+
making.job.message = message;
|
|
1024
|
+
} else if (id !== void 0) this.#failures.set(id, message);
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
/** `0.2.0` is newer than `0.1.9`; anything that is not three numbers is never newer. */
|
|
1028
|
+
function newerVersion(candidate, current) {
|
|
1029
|
+
const parse = (text) => /^(\d+)\.(\d+)\.(\d+)$/u.exec(text)?.slice(1).map(Number);
|
|
1030
|
+
const a = parse(candidate);
|
|
1031
|
+
const b = parse(current);
|
|
1032
|
+
if (a === void 0 || b === void 0) return false;
|
|
1033
|
+
for (let index = 0; index < 3; index += 1) if (a[index] !== b[index]) return (a[index] ?? 0) > (b[index] ?? 0);
|
|
1034
|
+
return false;
|
|
1035
|
+
}
|
|
1036
|
+
//#endregion
|
|
1037
|
+
//#region src/node.ts
|
|
1038
|
+
/**
|
|
1039
|
+
* The Control Center's service wired to this machine: real processes, the running apps' doors, the public registry, and the tools fetch. One wiring for
|
|
1040
|
+
* every face that runs the Control Center in a Node process: the Cordis Host plugin (index.ts) and the terminal Control Center (`acryl`, bundled from
|
|
1041
|
+
* distribution/acryl-npm-launcher/tui), so the window, the browser and the terminal do the same thing with the same code.
|
|
1042
|
+
*/
|
|
1043
|
+
/** `home` is ACRYL's home (`~/.acryl`); new apps are suggested in the visible folder beside it (`~/ACRYL`): the home's parent, never a looked-up user folder. */
|
|
1044
|
+
function createNodeLauncher(home, environment = () => process.env) {
|
|
1045
|
+
return new LauncherService({
|
|
1046
|
+
home,
|
|
1047
|
+
appsFolder: join(dirname(home), "ACRYL"),
|
|
1048
|
+
runner: spawnRunner,
|
|
1049
|
+
environment,
|
|
1050
|
+
exists: (path) => existsSync(path),
|
|
1051
|
+
makeFolder: (path) => {
|
|
1052
|
+
mkdirSync(path, { recursive: true });
|
|
1053
|
+
},
|
|
1054
|
+
blends: () => readBlends(),
|
|
1055
|
+
door: fileDoor(),
|
|
1056
|
+
prepareTools: () => prepareTools(home, process.env)
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
//#endregion
|
|
1060
|
+
export { LAUNCHER_UPDATE_PATH as A, LAUNCHER_PUBLISH_PLAN_PATH as C, LAUNCHER_START_PATH as D, LAUNCHER_SHARE_PLAN_PATH as E, WEBBOXES_NEW_BOX as F, webboxesDeployBlendUrl as I, webboxesDeployUrl as L, MAKEABLE_SURFACES as M, PLUGIN_NAME as N, LAUNCHER_STATE_PATH as O, SURFACES as P, LAUNCHER_PUBLISH_PATH as S, LAUNCHER_SHARE_PATH as T, LAUNCHER_GITHUB_START_PATH as _, readBlends as a, LAUNCHER_PLUGINS_PATH as b, BLEND_SOURCE as c, LAUNCHER_API_PREFIX as d, LAUNCHER_ASSISTANT_FOLDER_PATH as f, LAUNCHER_GITHUB_POLL_PATH as g, LAUNCHER_GITHUB_PATH as h, RegistryUnavailable as i, LICENSE_ID as j, LAUNCHER_STOP_PATH as k, LAUNCHER_ADD_PATH as l, LAUNCHER_GITHUB_DISCONNECT_PATH as m, LauncherRefusal as n, ACCOUNT_NAME as o, LAUNCHER_BLENDS_PATH as p, LauncherService as r, BLEND_ID as s, createNodeLauncher as t, LAUNCHER_ADE_PATH as u, LAUNCHER_HEALTH_PATH as v, LAUNCHER_REMOVE_PATH as w, LAUNCHER_PLUGIN_PULL_PATH as x, LAUNCHER_NEW_PATH as y };
|
|
1061
|
+
|
|
1062
|
+
//# sourceMappingURL=node-DxDbk2F0.js.map
|