@niscorp/cli 0.9.0 → 0.10.0
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/CHANGELOG.md +72 -0
- package/README.md +176 -14
- package/dist/adopt.js +2 -2
- package/dist/adopt.js.map +1 -1
- package/dist/{chunk-4EDCXCKZ.js → chunk-VGA7OHHG.js} +23 -6
- package/dist/chunk-VGA7OHHG.js.map +1 -0
- package/dist/cli.js +448 -228
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/project-5TBKATKF.js +3 -0
- package/dist/{project-RA4IMQ4V.js.map → project-5TBKATKF.js.map} +1 -1
- package/dist/shape.cjs +294 -0
- package/dist/shape.cjs.map +1 -0
- package/dist/shape.d.cts +22 -0
- package/dist/shape.d.ts +22 -0
- package/dist/shape.js +292 -0
- package/dist/shape.js.map +1 -0
- package/package.json +16 -6
- package/dist/chunk-4EDCXCKZ.js.map +0 -1
- package/dist/project-RA4IMQ4V.js +0 -3
package/dist/cli.js
CHANGED
|
@@ -1,121 +1,17 @@
|
|
|
1
|
-
import { loadProject, isShellProject, configFileOf, isProject } from './chunk-
|
|
2
|
-
import { resolve, join, sep, extname, dirname } from 'path';
|
|
1
|
+
import { loadProject, distOf, say, isShellProject, toolOf, configFileOf, isProject } from './chunk-VGA7OHHG.js';
|
|
2
|
+
import { resolve, join, sep, extname, dirname, basename, relative, isAbsolute } from 'path';
|
|
3
3
|
import { spawn, spawnSync } from 'child_process';
|
|
4
|
-
import { existsSync, statSync, readFileSync, rmSync, cpSync, mkdirSync,
|
|
5
|
-
import { createRequire } from 'module';
|
|
4
|
+
import { existsSync, statSync, readFileSync, writeFileSync, realpathSync, rmSync, cpSync, mkdirSync, mkdtempSync, lstatSync, readdirSync, renameSync, unlinkSync, rmdirSync } from 'fs';
|
|
6
5
|
import { serve } from '@hono/node-server';
|
|
7
|
-
import { pathToFileURL, fileURLToPath } from 'url';
|
|
8
6
|
import { tmpdir } from 'os';
|
|
7
|
+
import { pathToFileURL, fileURLToPath } from 'url';
|
|
9
8
|
import { shellSettled, shellView } from '@niscorp/nova';
|
|
10
9
|
import { placeHead, headOf } from '@niscorp/nova/document';
|
|
11
10
|
import { livenessOf } from '@niscorp/nova/reflect';
|
|
12
11
|
import { createBrotliCompress, constants, createGzip } from 'zlib';
|
|
12
|
+
import { randomUUID } from 'crypto';
|
|
13
|
+
import { createRequire } from 'module';
|
|
13
14
|
|
|
14
|
-
var isViteApi = (value) => value !== null && typeof value === "object" && typeof Reflect.get(value, "createServer") === "function";
|
|
15
|
-
var viteOf = async (root) => {
|
|
16
|
-
let entry;
|
|
17
|
-
try {
|
|
18
|
-
entry = createRequire(join(root, "package.json")).resolve("vite");
|
|
19
|
-
} catch {
|
|
20
|
-
throw new Error(`nisc: this app has no vite installed (looked from ${root}).`);
|
|
21
|
-
}
|
|
22
|
-
const loaded = await import(pathToFileURL(entry).href);
|
|
23
|
-
if (!isViteApi(loaded)) throw new Error(`nisc: ${entry} is not vite.`);
|
|
24
|
-
return loaded;
|
|
25
|
-
};
|
|
26
|
-
var devPluginOptionsOf = (dev2) => ({
|
|
27
|
-
...dev2?.watch !== void 0 ? { watch: dev2.watch } : {},
|
|
28
|
-
...dev2?.owned !== void 0 ? { owned: dev2.owned } : {},
|
|
29
|
-
...dev2?.signInFromNetwork !== void 0 ? { signInFromNetwork: dev2.signInFromNetwork } : {}
|
|
30
|
-
});
|
|
31
|
-
var mossPlugin = async (configFile, dev2) => {
|
|
32
|
-
const { mossDev } = await import('@niscorp/moss/vite');
|
|
33
|
-
return mossDev({
|
|
34
|
-
label: "nisc",
|
|
35
|
-
...devPluginOptionsOf(dev2),
|
|
36
|
-
app: async (load) => {
|
|
37
|
-
const loaded = await load(`/${configFile}`);
|
|
38
|
-
const project = Reflect.get(loaded, "project");
|
|
39
|
-
if (!isProject(project) || isShellProject(project)) throw new Error(`nisc: ${configFile} no longer describes an app behind moss.`);
|
|
40
|
-
const booted = await project.boot();
|
|
41
|
-
try {
|
|
42
|
-
await project.routes?.(booted.server);
|
|
43
|
-
await booted.server.checkFunctions();
|
|
44
|
-
} catch (error) {
|
|
45
|
-
await booted.close?.();
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
const signIn = project.dev?.signIn;
|
|
49
|
-
return {
|
|
50
|
-
server: booted.server,
|
|
51
|
-
...booted.close !== void 0 ? { close: booted.close } : {},
|
|
52
|
-
draw: project.draw,
|
|
53
|
-
...project.htmlAttributes !== void 0 ? { htmlAttributes: project.htmlAttributes } : {},
|
|
54
|
-
...project.site !== void 0 ? { site: project.site } : {},
|
|
55
|
-
...project.tokenKey !== void 0 ? { tokenKey: project.tokenKey } : {},
|
|
56
|
-
...signIn !== void 0 ? { signIn: (who) => signIn(booted.server, who) } : {}
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
var devServer = async (options) => {
|
|
62
|
-
const project = await loadProject(options.root);
|
|
63
|
-
const configFile = configFileOf(options.root);
|
|
64
|
-
const vite = await viteOf(options.root);
|
|
65
|
-
const server = await vite.createServer({
|
|
66
|
-
root: options.root,
|
|
67
|
-
...options.port !== void 0 ? { server: { port: options.port } } : {},
|
|
68
|
-
plugins: isShellProject(project) || configFile === void 0 ? [] : [await mossPlugin(configFile, project.dev)]
|
|
69
|
-
});
|
|
70
|
-
await server.listen();
|
|
71
|
-
server.printUrls();
|
|
72
|
-
const url = server.resolvedUrls?.local[0] ?? `http://localhost:${options.port ?? 5173}/`;
|
|
73
|
-
return { url, close: () => server.close() };
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
// src/routes.ts
|
|
77
|
-
var defaultPaths = (pages) => [
|
|
78
|
-
"/",
|
|
79
|
-
...pages.map((page) => page.path).filter((path) => !path.split("/").some((segment) => segment.startsWith(":"))).sort()
|
|
80
|
-
];
|
|
81
|
-
var fileOf = (path) => {
|
|
82
|
-
const segments = path.split("?")[0]?.split("/").filter((segment) => segment !== "") ?? [];
|
|
83
|
-
if (segments.some((segment) => segment === ".." || segment === ".")) throw new Error(`nisc: "${path}" is not a path a file can be written for.`);
|
|
84
|
-
return [...segments, "index.html"].join("/");
|
|
85
|
-
};
|
|
86
|
-
var pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
87
|
-
var routeTable = (routes) => {
|
|
88
|
-
const says = (route) => (route.head?.elements.length ?? 0) > 0;
|
|
89
|
-
const headed = routes.some(says);
|
|
90
|
-
const rows = routes.map((route) => {
|
|
91
|
-
const mark = !route.drawn ? "\u2717" : route.live ? "\u25CF" : "\u25CB";
|
|
92
|
-
const what = route.page === void 0 ? "the app" : `page ${route.page}`;
|
|
93
|
-
const served = !route.drawn ? "not drawn" : route.live ? "server" : "file";
|
|
94
|
-
const notes = [
|
|
95
|
-
...route.drawn ? route.why : ["it could not be drawn \u2014 it goes out as the undrawn template"],
|
|
96
|
-
...route.drawn && !route.settled ? ["it was still loading when the wait ran out \u2014 drawn with what it had"] : [],
|
|
97
|
-
...route.drawnWith.length > 0 ? [`drawn with ${route.drawnWith.join(", ")} \u2014 as it answered at build`] : []
|
|
98
|
-
];
|
|
99
|
-
const head = headed && route.drawn ? [says(route) ? `head: its own, said by ${route.head?.actions.join(", ") || "the shell\u2019s chrome"}` : "head: the template\u2019s own"] : [];
|
|
100
|
-
return { mark, path: route.path, what, served, notes, head };
|
|
101
|
-
});
|
|
102
|
-
const widths = {
|
|
103
|
-
path: Math.max(5, ...rows.map((row) => row.path.length)),
|
|
104
|
-
what: Math.max(4, ...rows.map((row) => row.what.length)),
|
|
105
|
-
served: Math.max(9, ...rows.map((row) => row.served.length))
|
|
106
|
-
};
|
|
107
|
-
const lines = [` ${pad("Route", widths.path + 2)}${pad("What", widths.what + 2)}${pad("Served as", widths.served + 2)}Because`];
|
|
108
|
-
for (const row of rows) {
|
|
109
|
-
const head = `${row.mark} ${pad(row.path, widths.path + 2)}${pad(row.what, widths.what + 2)}${pad(row.served, widths.served + 2)}`;
|
|
110
|
-
const [first, ...rest] = [...row.notes.length > 0 ? row.notes : ["nothing on it can still happen"], ...row.head];
|
|
111
|
-
lines.push(`${head}${first ?? ""}`);
|
|
112
|
-
for (const note of rest) lines.push(`${" ".repeat(head.length)}${note}`);
|
|
113
|
-
}
|
|
114
|
-
lines.push("");
|
|
115
|
-
lines.push("\u25CB file drawn once, for nobody in particular; nothing on it can still happen");
|
|
116
|
-
lines.push("\u25CF server something on it can still happen; it wants moss behind it");
|
|
117
|
-
return lines.join("\n");
|
|
118
|
-
};
|
|
119
15
|
var DEFAULT_BUILD_WAIT_MS = 5e3;
|
|
120
16
|
var DEFAULT_ROOT = '<div id="root"></div>';
|
|
121
17
|
var escapeAttribute = (value) => value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
@@ -158,7 +54,7 @@ var isAdoption = (value) => value !== null && typeof value === "object" && typeo
|
|
|
158
54
|
var adoptAll = (root, pages) => {
|
|
159
55
|
if (pages.length === 0) return [];
|
|
160
56
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
161
|
-
const entry = [join(here, "adopt.js"), join(here, "..", "dist", "adopt.js")].find((candidate) => existsSync(candidate));
|
|
57
|
+
const entry = [join(here, "adopt.js"), join(here, "..", "..", "dist", "adopt.js")].find((candidate) => existsSync(candidate));
|
|
162
58
|
if (entry === void 0) throw new Error("nisc: the adoption check is not built (packages/cli/dist/adopt.js).");
|
|
163
59
|
const scratch = mkdtempSync(join(tmpdir(), "nisc-adopt-"));
|
|
164
60
|
try {
|
|
@@ -220,10 +116,10 @@ var differenceOf = (a, b) => {
|
|
|
220
116
|
const around = (text) => JSON.stringify(text.slice(Math.max(0, at - 30), at + 50));
|
|
221
117
|
return ` \u2014 at character ${at}: ${around(a)} then ${around(b)}`;
|
|
222
118
|
};
|
|
223
|
-
var
|
|
119
|
+
var pad = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
224
120
|
var shellRouteTable = (routes) => {
|
|
225
121
|
const width = Math.max(5, ...routes.map((route) => route.path.length)) + 2;
|
|
226
|
-
const lines = [` ${
|
|
122
|
+
const lines = [` ${pad("Route", width)}First screen`];
|
|
227
123
|
const indent = " ".repeat(width + 2);
|
|
228
124
|
const says = (route) => (route.head?.elements.length ?? 0) > 0;
|
|
229
125
|
const headed = routes.some(says);
|
|
@@ -238,12 +134,12 @@ var shellRouteTable = (routes) => {
|
|
|
238
134
|
...headed ? [headNote(route)] : []
|
|
239
135
|
] : route.problems;
|
|
240
136
|
const [head, ...rest] = notes;
|
|
241
|
-
lines.push(`${ok ? "\u25CB" : "\u2717"} ${
|
|
137
|
+
lines.push(`${ok ? "\u25CB" : "\u2717"} ${pad(route.path, width)}${head ?? ""}`);
|
|
242
138
|
for (const note of rest) lines.push(`${indent}${note}`);
|
|
243
139
|
}
|
|
244
140
|
lines.push("");
|
|
245
141
|
lines.push("\u25CB a file: the first screen, drawn for nobody in particular; the page\u2019s own shell picks it up");
|
|
246
|
-
lines.push("\u2717 not written: a check
|
|
142
|
+
lines.push("\u2717 not written: a check failed");
|
|
247
143
|
return lines.join("\n");
|
|
248
144
|
};
|
|
249
145
|
var TYPES = {
|
|
@@ -265,10 +161,19 @@ var TYPES = {
|
|
|
265
161
|
".map": "application/json; charset=utf-8",
|
|
266
162
|
".txt": "text/plain; charset=utf-8"
|
|
267
163
|
};
|
|
164
|
+
var decodedPath = (pathname) => {
|
|
165
|
+
try {
|
|
166
|
+
const path = decodeURIComponent(pathname);
|
|
167
|
+
return path.includes("\0") ? void 0 : path;
|
|
168
|
+
} catch {
|
|
169
|
+
return void 0;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
268
172
|
var shellSite = (project, dist) => {
|
|
269
173
|
const base = resolve(dist);
|
|
270
174
|
return async (request) => {
|
|
271
|
-
const path =
|
|
175
|
+
const path = decodedPath(new URL(request.url).pathname);
|
|
176
|
+
if (path === void 0) return new Response("not a path", { status: 400 });
|
|
272
177
|
if (path !== "/" && path !== "/index.html") {
|
|
273
178
|
const file = resolve(base, `.${path}`);
|
|
274
179
|
const stats = file === base || file.startsWith(base + sep) ? statSync(file, { throwIfNoEntry: false }) : void 0;
|
|
@@ -327,10 +232,9 @@ var encodingFor = (header) => {
|
|
|
327
232
|
return void 0;
|
|
328
233
|
};
|
|
329
234
|
var compressorFor = (encoding) => encoding === "br" ? createBrotliCompress({ flush: constants.BROTLI_OPERATION_FLUSH, params: { [constants.BROTLI_PARAM_QUALITY]: BROTLI_QUALITY } }) : createGzip({ flush: constants.Z_SYNC_FLUSH });
|
|
330
|
-
var compressedBody = (source, encoding
|
|
235
|
+
var compressedBody = (source, encoding) => {
|
|
331
236
|
const reader = source.getReader();
|
|
332
237
|
const compressor = compressorFor(encoding);
|
|
333
|
-
const pieces = [];
|
|
334
238
|
let isLetGo = false;
|
|
335
239
|
let finished = () => void 0;
|
|
336
240
|
const ended = new Promise((done) => {
|
|
@@ -339,15 +243,10 @@ var compressedBody = (source, encoding, whole) => {
|
|
|
339
243
|
return new ReadableStream({
|
|
340
244
|
start: (controller) => {
|
|
341
245
|
compressor.on("data", (piece) => {
|
|
342
|
-
if (isLetGo)
|
|
343
|
-
if (whole !== void 0) pieces.push(piece);
|
|
344
|
-
controller.enqueue(piece);
|
|
246
|
+
if (!isLetGo) controller.enqueue(piece);
|
|
345
247
|
});
|
|
346
248
|
compressor.on("end", () => {
|
|
347
|
-
if (!isLetGo)
|
|
348
|
-
whole?.(Buffer.concat(pieces));
|
|
349
|
-
controller.close();
|
|
350
|
-
}
|
|
249
|
+
if (!isLetGo) controller.close();
|
|
351
250
|
finished();
|
|
352
251
|
});
|
|
353
252
|
compressor.on("error", (error) => {
|
|
@@ -375,35 +274,126 @@ var compressedBody = (source, encoding, whole) => {
|
|
|
375
274
|
}
|
|
376
275
|
});
|
|
377
276
|
};
|
|
378
|
-
var withCompression = (handler, options = {}) => {
|
|
379
|
-
const
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
if (etag !== null && !etag.startsWith("W/")) headers.set("etag", `W/${etag}`);
|
|
397
|
-
if (!/\bimmutable\b/i.test(headers.get("cache-control") ?? "")) return new Response(compressedBody(response.body, encoding), { status: 200, headers });
|
|
398
|
-
const key = `${encoding} ${path} ${headers.get("last-modified") ?? ""}`;
|
|
399
|
-
const known = remembered.get(key);
|
|
400
|
-
if (known === void 0) return new Response(compressedBody(response.body, encoding, (bytes) => remembered.set(key, bytes)), { status: 200, headers });
|
|
401
|
-
await response.body.cancel();
|
|
402
|
-
headers.set("content-length", String(known.length));
|
|
403
|
-
return new Response(known, { status: 200, headers });
|
|
404
|
-
};
|
|
277
|
+
var withCompression = (handler, options = {}) => async (request, ...rest) => {
|
|
278
|
+
const response = await handler(request, ...rest);
|
|
279
|
+
if (request.method !== "GET" || response.status !== 200 || response.body === null) return response;
|
|
280
|
+
const path = new URL(request.url).pathname;
|
|
281
|
+
if (options.except?.test(path) === true) return response;
|
|
282
|
+
if (!COMPRESSIBLE.test(response.headers.get("content-type") ?? "")) return response;
|
|
283
|
+
if (response.headers.has("content-encoding") || /no-transform/i.test(response.headers.get("cache-control") ?? "")) return response;
|
|
284
|
+
const headers = new Headers(response.headers);
|
|
285
|
+
const varies = (headers.get("vary") ?? "").toLowerCase().split(",").map((name) => name.trim());
|
|
286
|
+
if (!varies.includes("accept-encoding")) headers.append("vary", "Accept-Encoding");
|
|
287
|
+
const encoding = encodingFor(request.headers.get("accept-encoding"));
|
|
288
|
+
const length = headers.get("content-length");
|
|
289
|
+
if (encoding === void 0 || length !== null && Number(length) < SMALL_BYTES) return new Response(response.body, { status: 200, headers });
|
|
290
|
+
headers.set("content-encoding", encoding);
|
|
291
|
+
headers.delete("content-length");
|
|
292
|
+
const etag = headers.get("etag");
|
|
293
|
+
if (etag !== null && !etag.startsWith("W/")) headers.set("etag", `W/${etag}`);
|
|
294
|
+
return new Response(compressedBody(response.body, encoding), { status: 200, headers });
|
|
405
295
|
};
|
|
406
296
|
var siteHandler = (handler, options = {}) => withCompression(withCaching(handler, options), options);
|
|
297
|
+
|
|
298
|
+
// src/serve/start.ts
|
|
299
|
+
var portOf = (text, from) => {
|
|
300
|
+
const port = Number(text);
|
|
301
|
+
if (Number.isInteger(port) && port >= 0 && port <= 65535) return port;
|
|
302
|
+
throw new Error(`nisc: ${from} ${JSON.stringify(text)} is not a port. Give a whole number from 0 to 65535.`);
|
|
303
|
+
};
|
|
304
|
+
var listening = (httpServer, port) => new Promise((up, fail) => {
|
|
305
|
+
const refused = (error) => fail(new Error(`nisc: cannot listen on port ${port} (${error.message}). Stop what is using it, or give another port: \`--port <n>\`.`));
|
|
306
|
+
httpServer.once("error", refused);
|
|
307
|
+
httpServer.once("listening", () => {
|
|
308
|
+
httpServer.off("error", refused);
|
|
309
|
+
const address = httpServer.address();
|
|
310
|
+
up(`http://localhost:${address !== null && typeof address === "object" ? address.port : port}`);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
var start = async (options) => {
|
|
314
|
+
const project = await loadProject(options.root);
|
|
315
|
+
const dist = distOf(options.root, project);
|
|
316
|
+
if (!existsSync(join(dist, "index.html"))) throw new Error(`nisc: no built terminal in ${dist} \u2014 run \`nisc build\`.`);
|
|
317
|
+
if (options.port === void 0 && process.env["PORT"] === "") say(options)("nisc: PORT is set but empty, read as 0: any free port. Unset it for 8787.");
|
|
318
|
+
const port = options.port ?? portOf(process.env["PORT"] ?? "8787", "PORT");
|
|
319
|
+
if (isShellProject(project)) {
|
|
320
|
+
const httpServer = serve({ fetch: siteHandler(shellSite(project, dist)), port });
|
|
321
|
+
const url = await listening(httpServer, port);
|
|
322
|
+
say(options)(`nisc: serving ${url} \u2014 the built app, each path\u2019s first screen drawn; the shell is the page\u2019s own`);
|
|
323
|
+
return { url, close: async () => void httpServer.close() };
|
|
324
|
+
}
|
|
325
|
+
const { attachSocket, mountSite, MOSS_PATHS } = await import('@niscorp/moss/node');
|
|
326
|
+
const booted = await project.boot();
|
|
327
|
+
try {
|
|
328
|
+
await project.routes?.(booted.server);
|
|
329
|
+
mountSite(booted.server, {
|
|
330
|
+
dist,
|
|
331
|
+
draw: project.draw,
|
|
332
|
+
...project.htmlAttributes !== void 0 ? { htmlAttributes: project.htmlAttributes } : {},
|
|
333
|
+
...project.site !== void 0 ? { site: project.site } : {},
|
|
334
|
+
...project.tokenKey !== void 0 ? { tokenKey: project.tokenKey } : {}
|
|
335
|
+
});
|
|
336
|
+
await booted.server.checkFunctions();
|
|
337
|
+
const httpServer = serve({ fetch: siteHandler(booted.server.fetch, { except: MOSS_PATHS }), port });
|
|
338
|
+
attachSocket(httpServer, booted.server.socket);
|
|
339
|
+
const url = await listening(httpServer, port);
|
|
340
|
+
say(options)(`nisc: serving ${url} \u2014 the built terminal, its pages drawn; the socket at /socket`);
|
|
341
|
+
return {
|
|
342
|
+
url,
|
|
343
|
+
close: async () => {
|
|
344
|
+
httpServer.close();
|
|
345
|
+
await booted.close?.();
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
} catch (error) {
|
|
349
|
+
await booted.close?.();
|
|
350
|
+
throw error;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// src/build/routes.ts
|
|
355
|
+
var defaultPaths = (pages) => [
|
|
356
|
+
"/",
|
|
357
|
+
...pages.map((page) => page.path).filter((path) => !path.split("/").some((segment) => segment.startsWith(":"))).sort()
|
|
358
|
+
];
|
|
359
|
+
var fileOf = (path) => {
|
|
360
|
+
const segments = path.split("?")[0]?.split("/").filter((segment) => segment !== "") ?? [];
|
|
361
|
+
if (segments.some((segment) => segment === ".." || segment === ".")) throw new Error(`nisc: "${path}" is not a path a file can be written for.`);
|
|
362
|
+
return [...segments, "index.html"].join("/");
|
|
363
|
+
};
|
|
364
|
+
var pad2 = (text, width) => text + " ".repeat(Math.max(0, width - text.length));
|
|
365
|
+
var routeTable = (routes) => {
|
|
366
|
+
const says = (route) => (route.head?.elements.length ?? 0) > 0;
|
|
367
|
+
const headed = routes.some(says);
|
|
368
|
+
const rows = routes.map((route) => {
|
|
369
|
+
const mark = !route.drawn ? "\u2717" : route.live ? "\u25CF" : "\u25CB";
|
|
370
|
+
const what = route.page === void 0 ? "the app" : `page ${route.page}`;
|
|
371
|
+
const served = !route.drawn ? "not drawn" : route.live ? "server" : "file";
|
|
372
|
+
const notes = [
|
|
373
|
+
...route.drawn ? route.why : ["it could not be drawn \u2014 it goes out as the undrawn template"],
|
|
374
|
+
...route.drawn && !route.settled ? ["it was still loading when the wait ran out \u2014 drawn with what it had"] : [],
|
|
375
|
+
...route.drawnWith.length > 0 ? [`drawn with ${route.drawnWith.join(", ")} \u2014 as it answered at build`] : []
|
|
376
|
+
];
|
|
377
|
+
const head = headed && route.drawn ? [says(route) ? `head: its own, said by ${route.head?.actions.join(", ") || "the shell\u2019s chrome"}` : "head: the template\u2019s own"] : [];
|
|
378
|
+
return { mark, path: route.path, what, served, notes, head };
|
|
379
|
+
});
|
|
380
|
+
const widths = {
|
|
381
|
+
path: Math.max(5, ...rows.map((row) => row.path.length)),
|
|
382
|
+
what: Math.max(4, ...rows.map((row) => row.what.length)),
|
|
383
|
+
served: Math.max(9, ...rows.map((row) => row.served.length))
|
|
384
|
+
};
|
|
385
|
+
const lines = [` ${pad2("Route", widths.path + 2)}${pad2("What", widths.what + 2)}${pad2("Served as", widths.served + 2)}Because`];
|
|
386
|
+
for (const row of rows) {
|
|
387
|
+
const head = `${row.mark} ${pad2(row.path, widths.path + 2)}${pad2(row.what, widths.what + 2)}${pad2(row.served, widths.served + 2)}`;
|
|
388
|
+
const [first, ...rest] = [...row.notes.length > 0 ? row.notes : ["nothing on it can still happen"], ...row.head];
|
|
389
|
+
lines.push(`${head}${first ?? ""}`);
|
|
390
|
+
for (const note of rest) lines.push(`${" ".repeat(head.length)}${note}`);
|
|
391
|
+
}
|
|
392
|
+
lines.push("");
|
|
393
|
+
lines.push("\u25CB file drawn once, for nobody in particular; nothing on it can still happen");
|
|
394
|
+
lines.push("\u25CF server something on it can still happen; it wants moss behind it");
|
|
395
|
+
return lines.join("\n");
|
|
396
|
+
};
|
|
407
397
|
var attributesOf = (tag) => {
|
|
408
398
|
const found = {};
|
|
409
399
|
for (const match of tag.slice(5).matchAll(/([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g)) {
|
|
@@ -459,23 +449,7 @@ var writeStylesheetIntoPage = (dist) => {
|
|
|
459
449
|
return { inPage: rewrite.inPage, left: rewrite.left };
|
|
460
450
|
};
|
|
461
451
|
|
|
462
|
-
// src/
|
|
463
|
-
var say = (options) => options.print ?? ((line) => console.log(line));
|
|
464
|
-
var toolOf = (root, name, bin) => {
|
|
465
|
-
const require2 = createRequire(join(root, "package.json"));
|
|
466
|
-
let manifestPath;
|
|
467
|
-
try {
|
|
468
|
-
manifestPath = require2.resolve(`${name}/package.json`);
|
|
469
|
-
} catch {
|
|
470
|
-
throw new Error(`nisc: this app has no ${name} installed (looked from ${root}).`);
|
|
471
|
-
}
|
|
472
|
-
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
473
|
-
const bins = manifest !== null && typeof manifest === "object" ? Reflect.get(manifest, "bin") : void 0;
|
|
474
|
-
const relative = typeof bins === "string" ? bins : bins !== null && typeof bins === "object" ? Reflect.get(bins, bin) : void 0;
|
|
475
|
-
if (typeof relative !== "string") throw new Error(`nisc: ${name} ships no "${bin}" command.`);
|
|
476
|
-
return join(dirname(manifestPath), relative);
|
|
477
|
-
};
|
|
478
|
-
var distOf = (root, project) => resolve(root, project.dist ?? "dist");
|
|
452
|
+
// src/build/build.ts
|
|
479
453
|
var bundle = (options, project) => {
|
|
480
454
|
const result = spawnSync(process.execPath, [toolOf(options.root, "vite", "vite"), "build"], { cwd: options.root, stdio: "inherit" });
|
|
481
455
|
if (result.status !== 0) throw new Error("nisc: the bundler failed \u2014 nothing was drawn.");
|
|
@@ -489,7 +463,7 @@ var bundle = (options, project) => {
|
|
|
489
463
|
};
|
|
490
464
|
var warnOfOwnAssets = (options) => {
|
|
491
465
|
if (!existsSync(join(options.root, "public", "assets"))) return;
|
|
492
|
-
say(options)("nisc: public/assets/
|
|
466
|
+
say(options)("nisc: public/assets/ has files of your own. `nisc start` has browsers keep everything under /assets/ for a year, so a change to one of them will not be seen under the same name \u2014 keep them elsewhere in public/.");
|
|
493
467
|
};
|
|
494
468
|
var warnOfOneAddress = (options, project, template, paths) => {
|
|
495
469
|
if (project.site !== void 0 || paths < 2 || !/<link\b[^>]*\brel\s*=\s*["']?canonical\b/i.test(template)) return;
|
|
@@ -497,6 +471,12 @@ var warnOfOneAddress = (options, project, template, paths) => {
|
|
|
497
471
|
say(options)(`nisc: index.html names one canonical address and this app has ${paths} paths \u2014 every file would say it lives there.`);
|
|
498
472
|
say(options)(' `site: "https://\u2026"` in nisc.config.ts gives each path its own.');
|
|
499
473
|
};
|
|
474
|
+
var warnOfUnreadCharter = (options, unread) => {
|
|
475
|
+
if (unread.length === 0) return;
|
|
476
|
+
say(options)("");
|
|
477
|
+
say(options)("nisc: the charter has what it does not read as written \u2014 the app runs, and what it lists does not apply:");
|
|
478
|
+
for (const detail of unread) say(options)(` ${detail}`);
|
|
479
|
+
};
|
|
500
480
|
var templateOf = (options, project) => {
|
|
501
481
|
const template = join(distOf(options.root, project), "index.html");
|
|
502
482
|
if (!existsSync(template)) throw new Error(`nisc: no built terminal at ${template} \u2014 run \`nisc build\`.`);
|
|
@@ -509,7 +489,7 @@ var survey = async (options, project) => {
|
|
|
509
489
|
try {
|
|
510
490
|
await booted.server.checkFunctions();
|
|
511
491
|
const paths = project.paths === void 0 ? defaultPaths(booted.server.pages) : await project.paths(booted.server);
|
|
512
|
-
|
|
492
|
+
const routes = await exportDocuments({
|
|
513
493
|
server: booted.server,
|
|
514
494
|
template,
|
|
515
495
|
draw: project.draw,
|
|
@@ -518,6 +498,8 @@ var survey = async (options, project) => {
|
|
|
518
498
|
...project.tokenKey !== void 0 ? { tokenKey: project.tokenKey } : {},
|
|
519
499
|
paths
|
|
520
500
|
});
|
|
501
|
+
const unread = booted.server.charterReport().warnings.flatMap((warning) => warning.rule === "shape" ? [warning.detail] : []);
|
|
502
|
+
return { routes, unread };
|
|
521
503
|
} finally {
|
|
522
504
|
await booted.close?.();
|
|
523
505
|
}
|
|
@@ -535,93 +517,325 @@ var build = async (options) => {
|
|
|
535
517
|
if (options.skipBundle !== true) bundle(options, project);
|
|
536
518
|
warnOfOwnAssets(options);
|
|
537
519
|
if (isShellProject(project)) return buildShell(options, project);
|
|
538
|
-
const routes = await survey(options, project);
|
|
520
|
+
const { routes, unread } = await survey(options, project);
|
|
539
521
|
say(options)("");
|
|
540
522
|
say(options)(routeTable(routes));
|
|
523
|
+
warnOfUnreadCharter(options, unread);
|
|
541
524
|
warnOfOneAddress(options, project, templateOf(options, project), routes.length);
|
|
542
525
|
return { kind: "moss", ok: routes.every((route) => route.drawn), routes };
|
|
543
526
|
};
|
|
544
|
-
var
|
|
545
|
-
|
|
546
|
-
|
|
527
|
+
var STAGE = ".nisc-export-";
|
|
528
|
+
var IS_CASE_BLIND = process.platform === "win32" || process.platform === "darwin";
|
|
529
|
+
var codeOf = (error) => error instanceof Error ? Reflect.get(error, "code") : void 0;
|
|
530
|
+
var isOwn = (error) => error instanceof Error && error.message.startsWith("nisc: ");
|
|
531
|
+
var describe = (error) => {
|
|
532
|
+
const [code, path] = error instanceof Error ? [Reflect.get(error, "code"), Reflect.get(error, "dest") ?? Reflect.get(error, "path")] : [];
|
|
533
|
+
if (typeof code !== "string") return error instanceof Error ? error.message : String(error);
|
|
534
|
+
return typeof path === "string" ? `${code} on ${path}` : code;
|
|
535
|
+
};
|
|
536
|
+
var isWithin = (folder, path) => {
|
|
537
|
+
const way = relative(folder, path);
|
|
538
|
+
return way === "" || way !== ".." && !way.startsWith(`..${sep}`) && !isAbsolute(way);
|
|
539
|
+
};
|
|
540
|
+
var realOf = (path) => {
|
|
541
|
+
try {
|
|
542
|
+
return realpathSync.native(path);
|
|
543
|
+
} catch (error) {
|
|
544
|
+
if (codeOf(error) !== "ENOENT" && codeOf(error) !== "ENOTDIR") throw error;
|
|
545
|
+
const parent = dirname(path);
|
|
546
|
+
return parent === path ? path : join(realOf(parent), basename(path));
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
var absentThroughAFile = (read) => {
|
|
550
|
+
try {
|
|
551
|
+
return read();
|
|
552
|
+
} catch (error) {
|
|
553
|
+
if (codeOf(error) === "ENOTDIR") return void 0;
|
|
554
|
+
throw error;
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
var lstatOf = (path) => absentThroughAFile(() => lstatSync(path, { throwIfNoEntry: false }));
|
|
558
|
+
var isDefaultOut = (out, root) => {
|
|
559
|
+
const defaultOut = join(root, "out");
|
|
560
|
+
const [given, own] = [out, defaultOut].map((path) => absentThroughAFile(() => lstatSync(path, { bigint: true, throwIfNoEntry: false })));
|
|
561
|
+
if (given !== void 0 && own !== void 0 && given.ino !== 0n) return given.dev === own.dev && given.ino === own.ino;
|
|
562
|
+
const locationOf = (path) => join(realOf(dirname(path)), basename(path));
|
|
563
|
+
const [location, defaultLocation] = [locationOf(out), locationOf(defaultOut)];
|
|
564
|
+
if (location === defaultLocation) return true;
|
|
565
|
+
return given === void 0 && own === void 0 && IS_CASE_BLIND && location.toLowerCase() === defaultLocation.toLowerCase();
|
|
566
|
+
};
|
|
567
|
+
var writeRoutes = (folder, routes) => {
|
|
547
568
|
for (const route of routes) {
|
|
548
|
-
const file = join(
|
|
569
|
+
const file = join(folder, fileOf(route.path));
|
|
549
570
|
mkdirSync(dirname(file), { recursive: true });
|
|
550
571
|
writeFileSync(file, route.html);
|
|
551
572
|
}
|
|
573
|
+
};
|
|
574
|
+
var entriesOf = (folder, enters = () => true, under = "") => readdirSync(join(folder, under), { withFileTypes: true }).flatMap((found) => {
|
|
575
|
+
const entry = under === "" ? found.name : `${under}/${found.name}`;
|
|
576
|
+
if (!found.isDirectory()) return [entry];
|
|
577
|
+
const inside = enters(entry) ? entriesOf(folder, enters, entry) : [];
|
|
578
|
+
return inside.length > 0 ? inside : [`${entry}/`];
|
|
579
|
+
});
|
|
580
|
+
var recordIn = (file) => {
|
|
581
|
+
try {
|
|
582
|
+
const record = JSON.parse(readFileSync(file, "utf8"));
|
|
583
|
+
return record !== null && typeof record === "object" && !Array.isArray(record) ? record : {};
|
|
584
|
+
} catch {
|
|
585
|
+
return {};
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
var writeRecord = (file, record) => {
|
|
589
|
+
const next = `${file}.${randomUUID()}`;
|
|
590
|
+
try {
|
|
591
|
+
if (realOf(dirname(file)) !== dirname(file)) return "a link is on the way to it";
|
|
592
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
593
|
+
writeFileSync(next, `${JSON.stringify(record, null, 2)}
|
|
594
|
+
`);
|
|
595
|
+
renameSync(next, file);
|
|
596
|
+
return void 0;
|
|
597
|
+
} catch (error) {
|
|
598
|
+
if (existsSync(next)) rmSync(next);
|
|
599
|
+
return describe(error);
|
|
600
|
+
}
|
|
601
|
+
};
|
|
602
|
+
var isRunning = (pid) => {
|
|
603
|
+
try {
|
|
604
|
+
process.kill(pid, 0);
|
|
605
|
+
return true;
|
|
606
|
+
} catch (error) {
|
|
607
|
+
return codeOf(error) === "EPERM";
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
var removeEndedStages = (folder) => {
|
|
611
|
+
for (const name of readdirSync(folder)) {
|
|
612
|
+
const pid = Number(name.slice(STAGE.length).split("-")[0]);
|
|
613
|
+
if (name.startsWith(STAGE) && Number.isInteger(pid) && pid > 0 && !isRunning(pid) && lstatOf(join(folder, name))?.isDirectory() === true) rmSync(join(folder, name), { recursive: true });
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
var removeEmpty = (folder, path) => {
|
|
617
|
+
if (path === folder || !isWithin(folder, path) || lstatOf(path)?.isDirectory() !== true || readdirSync(path).length > 0) return;
|
|
618
|
+
rmdirSync(path);
|
|
619
|
+
removeEmpty(folder, dirname(path));
|
|
620
|
+
};
|
|
621
|
+
var hasNoLinkOnTheWay = (path) => realOf(dirname(path)) === dirname(path);
|
|
622
|
+
var removeListed = (folder, listed) => listed.filter((entry) => {
|
|
623
|
+
const path = join(folder, entry);
|
|
624
|
+
if (relative(folder, path) === "" || !isWithin(folder, path) || !hasNoLinkOnTheWay(path)) return true;
|
|
625
|
+
const there = lstatOf(path);
|
|
626
|
+
if (there?.isDirectory() === true) removeEmpty(folder, path);
|
|
627
|
+
else if (there !== void 0) unlinkSync(path);
|
|
628
|
+
removeEmpty(folder, dirname(path));
|
|
629
|
+
return false;
|
|
630
|
+
});
|
|
631
|
+
var place = (from, to) => {
|
|
632
|
+
const isFolder = lstatSync(from).isDirectory();
|
|
633
|
+
const there = lstatOf(to);
|
|
634
|
+
if (isFolder && there?.isDirectory() === true) {
|
|
635
|
+
for (const name of readdirSync(from)) place(join(from, name), join(to, name));
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
if (there !== void 0 && (isFolder || !there.isFile())) rmSync(to, { recursive: true });
|
|
639
|
+
renameSync(from, to);
|
|
640
|
+
};
|
|
641
|
+
var cacheOf = (root) => join(realOf(root), "node_modules", ".cache", "nisc");
|
|
642
|
+
var replaceWritten = (root, out, folder, stage) => {
|
|
643
|
+
const file = join(cacheOf(root), "exports.json");
|
|
644
|
+
const record = recordIn(file);
|
|
645
|
+
const recorded = Reflect.get(record, folder);
|
|
646
|
+
const listed = (Array.isArray(recorded) ? recorded : []).flatMap((entry) => typeof entry === "string" ? [entry] : []);
|
|
647
|
+
const written = entriesOf(stage);
|
|
648
|
+
const listedSet = new Set(listed);
|
|
649
|
+
const blocked = written.filter((entry) => {
|
|
650
|
+
const path = join(folder, entry);
|
|
651
|
+
return !entry.endsWith("/") && hasNoLinkOnTheWay(path) && lstatOf(path)?.isDirectory() === true && entriesOf(path).some((inside) => !listedSet.has(`${entry}/${inside}`));
|
|
652
|
+
});
|
|
653
|
+
if (blocked.length > 0) throw new Error(`nisc: a folder with files of its own stands where the export writes a file, in ${out}: ${blocked.join(", ")}. Nothing was written.`);
|
|
654
|
+
const kept = Object.entries(record).filter(([other]) => other !== folder && existsSync(other));
|
|
655
|
+
const unrecorded = writeRecord(file, Object.fromEntries([...kept, [folder, written]]));
|
|
656
|
+
const notRemoved = removeListed(folder, listed);
|
|
657
|
+
const files = written.filter((entry) => !entry.endsWith("/")).length;
|
|
658
|
+
try {
|
|
659
|
+
place(stage, folder);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
const missing = entriesOf(stage).filter((entry) => !entry.endsWith("/")).length;
|
|
662
|
+
throw new Error(
|
|
663
|
+
`nisc: the export could not be moved into ${out} (${describe(error)}). ${files - missing} of its ${files} files are in place and ${missing} are not; the previous export's files are removed. Export again to finish.`
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
rmSync(stage, { recursive: true });
|
|
667
|
+
const writtenSet = new Set(written);
|
|
668
|
+
const writtenFolders = new Set(written.flatMap((entry) => entry.split("/").slice(0, -1).map((_name, at, names) => names.slice(0, at + 1).join("/"))));
|
|
669
|
+
const left = entriesOf(folder, (entry) => writtenFolders.has(entry)).filter((entry) => !writtenSet.has(entry)).sort();
|
|
670
|
+
return [
|
|
671
|
+
...unrecorded === void 0 ? [] : [`nisc: what this export wrote could not be recorded (${unrecorded}) in ${file}, so the next export into ${out} removes none of it.`],
|
|
672
|
+
...notRemoved.length > 0 ? [`nisc: not removed, as its real location is not inside ${out}: ${notRemoved.join(", ")}`] : [],
|
|
673
|
+
...left.length > 0 ? [`nisc: left as they were in ${out}, not written by this export (${left.length}): ${left.slice(0, 5).join(", ")}${left.length > 5 ? ` and ${left.length - 5} more` : ""}`] : []
|
|
674
|
+
];
|
|
675
|
+
};
|
|
676
|
+
var PATH_REFUSALS = /* @__PURE__ */ new Set(["ENOENT", "EINVAL", "ENAMETOOLONG", "EEXIST", "ENOTDIR", "EISDIR"]);
|
|
677
|
+
var cannotWrite = (path, why) => new Error(`nisc: the path "${path}" cannot be written here (${why}). Nothing was written.`);
|
|
678
|
+
var notWritten = (out, error) => new Error(`nisc: the export could not be written into ${out} (${describe(error)}). Nothing already in it was changed.`);
|
|
679
|
+
var stageExport = (parent, out, dist, routes) => {
|
|
680
|
+
const stage = join(parent, `${STAGE}${process.pid}-${randomUUID()}`);
|
|
681
|
+
try {
|
|
682
|
+
try {
|
|
683
|
+
mkdirSync(parent, { recursive: true });
|
|
684
|
+
removeEndedStages(parent);
|
|
685
|
+
mkdirSync(stage);
|
|
686
|
+
cpSync(dist, stage, { recursive: true });
|
|
687
|
+
} catch (error) {
|
|
688
|
+
throw notWritten(out, error);
|
|
689
|
+
}
|
|
690
|
+
for (const route of routes) {
|
|
691
|
+
const file = join(stage, fileOf(route.path));
|
|
692
|
+
if (!isWithin(stage, file)) throw cannotWrite(route.path, `its file would be outside ${out}`);
|
|
693
|
+
try {
|
|
694
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
695
|
+
writeFileSync(file, route.html);
|
|
696
|
+
} catch (error) {
|
|
697
|
+
const code = codeOf(error);
|
|
698
|
+
throw typeof code === "string" && PATH_REFUSALS.has(code) ? cannotWrite(route.path, code) : notWritten(out, error);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return stage;
|
|
702
|
+
} catch (error) {
|
|
703
|
+
rmSync(stage, { recursive: true, force: true, maxRetries: 3 });
|
|
704
|
+
throw error;
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
var writeDefaultFolder = (root, out, dist, routes) => {
|
|
708
|
+
rmSync(stageExport(cacheOf(root), out, dist, routes), { recursive: true, force: true, maxRetries: 3 });
|
|
709
|
+
rmSync(out, { recursive: true, force: true });
|
|
710
|
+
cpSync(dist, out, { recursive: true });
|
|
711
|
+
writeRoutes(out, routes);
|
|
712
|
+
};
|
|
713
|
+
var writeNamedFolder = (root, out, dist, routes) => {
|
|
714
|
+
const folder = realOf(out);
|
|
715
|
+
if (lstatOf(folder)?.isDirectory() === false) throw new Error(`nisc: --out ${out} is a file, not a folder. Nothing was written.`);
|
|
716
|
+
mkdirSync(folder, { recursive: true });
|
|
717
|
+
const stage = stageExport(folder, out, dist, routes);
|
|
718
|
+
try {
|
|
719
|
+
return replaceWritten(root, out, folder, stage);
|
|
720
|
+
} catch (error) {
|
|
721
|
+
throw isOwn(error) ? error : new Error(`nisc: the export could not be put into ${out} (${describe(error)}); part of it may be in place. Export again to finish.`);
|
|
722
|
+
} finally {
|
|
723
|
+
rmSync(stage, { recursive: true, force: true });
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
// src/build/export.ts
|
|
728
|
+
var write = (options, project, out, routes) => {
|
|
729
|
+
const dist = distOf(options.root, project);
|
|
730
|
+
const isDefault = isDefaultOut(out, options.root);
|
|
731
|
+
if (isDefault) writeDefaultFolder(options.root, out, dist, routes);
|
|
732
|
+
const said2 = isDefault ? [] : writeNamedFolder(options.root, out, dist, routes);
|
|
552
733
|
say(options)("");
|
|
553
734
|
say(options)(`nisc: ${routes.length} paths written to ${out}`);
|
|
735
|
+
for (const line of said2) say(options)(line);
|
|
736
|
+
};
|
|
737
|
+
var refuseOut = (out, root, dist) => {
|
|
738
|
+
const refusalOf = (folder, app, built) => isWithin(folder, app) ? `the app's folder (${root}) or a folder above it` : isWithin(folder, built) || isWithin(built, folder) ? `the built app (${dist}), a folder inside it or one above it` : void 0;
|
|
739
|
+
const what = refusalOf(out, root, dist) ?? (isDefaultOut(out, root) ? void 0 : refusalOf(realOf(out), realOf(root), realOf(dist)));
|
|
740
|
+
if (what === void 0) return;
|
|
741
|
+
throw new Error(`nisc: --out ${out} is ${what}, and an export writes over what is there. Nothing was written. Give the export a folder of its own: \`--out out\`.`);
|
|
554
742
|
};
|
|
555
743
|
var exportSite = async (options) => {
|
|
556
744
|
const project = await loadProject(options.root);
|
|
557
|
-
const result = await build(options);
|
|
558
745
|
const out = resolve(options.root, options.out ?? "out");
|
|
746
|
+
refuseOut(out, options.root, distOf(options.root, project));
|
|
747
|
+
const result = await build(options);
|
|
559
748
|
if (result.kind === "shell") {
|
|
560
749
|
if (!result.ok) {
|
|
561
750
|
const failed = result.routes.filter((route) => route.problems.length > 0);
|
|
562
751
|
say(options)("");
|
|
563
|
-
say(options)(`nisc: ${failed.length} of ${result.routes.length} paths
|
|
752
|
+
say(options)(`nisc: ${failed.length} of ${result.routes.length} paths failed a check (${failed.map((route) => route.path).join(", ")}). Nothing was written.`);
|
|
564
753
|
return { written: false, result, out };
|
|
565
754
|
}
|
|
566
755
|
write(options, project, out, result.routes);
|
|
567
756
|
return { written: true, result, out };
|
|
568
757
|
}
|
|
569
758
|
const routes = result.routes;
|
|
570
|
-
const
|
|
571
|
-
|
|
759
|
+
const pathsOf = (some) => some.map((route) => route.path).join(", ");
|
|
760
|
+
const live = routes.filter((route) => route.drawn && route.live);
|
|
761
|
+
const undrawn = routes.filter((route) => !route.drawn);
|
|
762
|
+
if (live.length + undrawn.length > 0 && options.allowLive !== true) {
|
|
572
763
|
say(options)("");
|
|
573
|
-
say(options)(`nisc: ${live.length} of ${routes.length} paths cannot be a file alone (${live
|
|
574
|
-
|
|
764
|
+
if (live.length > 0) say(options)(`nisc: ${live.length} of ${routes.length} paths cannot be a file alone (${pathsOf(live)}).`);
|
|
765
|
+
if (undrawn.length > 0) {
|
|
766
|
+
say(options)(`nisc: ${undrawn.length} of ${routes.length} paths could not be drawn (see the table): it would go out as the template, empty until its terminal connects (${pathsOf(undrawn)}).`);
|
|
767
|
+
}
|
|
768
|
+
say(options)(live.length > 0 ? " Nothing was written. With a server beside the files they still work: `nisc export --allow-live`." : " Nothing was written.");
|
|
575
769
|
return { written: false, result, out };
|
|
576
770
|
}
|
|
577
771
|
write(options, project, out, routes);
|
|
772
|
+
for (const route of undrawn) say(options)(`nisc: ${route.path} could not be drawn \u2014 written as the template; it shows nothing until its terminal connects.`);
|
|
578
773
|
return { written: true, result, out };
|
|
579
774
|
};
|
|
580
|
-
var
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
const port = address !== null && typeof address === "object" ? address.port : options.port;
|
|
584
|
-
return `http://localhost:${port}`;
|
|
585
|
-
};
|
|
586
|
-
var start = async (options) => {
|
|
587
|
-
const project = await loadProject(options.root);
|
|
588
|
-
const dist = distOf(options.root, project);
|
|
589
|
-
if (!existsSync(join(dist, "index.html"))) throw new Error(`nisc: no built terminal in ${dist} \u2014 run \`nisc build\`.`);
|
|
590
|
-
const port = options.port ?? Number(process.env["PORT"] ?? 8787);
|
|
591
|
-
if (isShellProject(project)) {
|
|
592
|
-
const httpServer2 = serve({ fetch: siteHandler(shellSite(project, dist)), port });
|
|
593
|
-
const url2 = await listening(httpServer2, options);
|
|
594
|
-
say(options)(`nisc: serving ${url2} \u2014 the built app, each path\u2019s first screen drawn; the shell is the page\u2019s own`);
|
|
595
|
-
return { url: url2, close: async () => void httpServer2.close() };
|
|
596
|
-
}
|
|
597
|
-
const { attachSocket, mountSite, MOSS_PATHS } = await import('@niscorp/moss/node');
|
|
598
|
-
const booted = await project.boot();
|
|
775
|
+
var isViteApi = (value) => value !== null && typeof value === "object" && typeof Reflect.get(value, "createServer") === "function";
|
|
776
|
+
var viteOf = async (root) => {
|
|
777
|
+
let entry;
|
|
599
778
|
try {
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
draw: project.draw,
|
|
604
|
-
...project.htmlAttributes !== void 0 ? { htmlAttributes: project.htmlAttributes } : {},
|
|
605
|
-
...project.site !== void 0 ? { site: project.site } : {},
|
|
606
|
-
...project.tokenKey !== void 0 ? { tokenKey: project.tokenKey } : {}
|
|
607
|
-
});
|
|
608
|
-
await booted.server.checkFunctions();
|
|
609
|
-
} catch (error) {
|
|
610
|
-
await booted.close?.();
|
|
611
|
-
throw error;
|
|
779
|
+
entry = createRequire(join(root, "package.json")).resolve("vite");
|
|
780
|
+
} catch {
|
|
781
|
+
throw new Error(`nisc: this app has no vite installed (looked from ${root}).`);
|
|
612
782
|
}
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
783
|
+
const loaded = await import(pathToFileURL(entry).href);
|
|
784
|
+
if (!isViteApi(loaded)) throw new Error(`nisc: ${entry} is not vite.`);
|
|
785
|
+
return loaded;
|
|
786
|
+
};
|
|
787
|
+
var devPluginOptionsOf = (dev2) => ({
|
|
788
|
+
...dev2?.watch !== void 0 ? { watch: dev2.watch } : {},
|
|
789
|
+
...dev2?.owned !== void 0 ? { owned: dev2.owned } : {},
|
|
790
|
+
...dev2?.signInFromNetwork !== void 0 ? { signInFromNetwork: dev2.signInFromNetwork } : {}
|
|
791
|
+
});
|
|
792
|
+
var mossPlugin = async (configFile, dev2) => {
|
|
793
|
+
const { mossDev } = await import('@niscorp/moss/vite');
|
|
794
|
+
return mossDev({
|
|
795
|
+
label: "nisc",
|
|
796
|
+
...devPluginOptionsOf(dev2),
|
|
797
|
+
app: async (load) => {
|
|
798
|
+
const loaded = await load(`/${configFile}`);
|
|
799
|
+
const project = Reflect.get(loaded, "project");
|
|
800
|
+
if (!isProject(project) || isShellProject(project)) throw new Error(`nisc: ${configFile} no longer describes an app behind moss.`);
|
|
801
|
+
const booted = await project.boot();
|
|
802
|
+
try {
|
|
803
|
+
await project.routes?.(booted.server);
|
|
804
|
+
await booted.server.checkFunctions();
|
|
805
|
+
} catch (error) {
|
|
806
|
+
await booted.close?.();
|
|
807
|
+
throw error;
|
|
808
|
+
}
|
|
809
|
+
const signIn = project.dev?.signIn;
|
|
810
|
+
return {
|
|
811
|
+
server: booted.server,
|
|
812
|
+
...booted.close !== void 0 ? { close: booted.close } : {},
|
|
813
|
+
draw: project.draw,
|
|
814
|
+
...project.htmlAttributes !== void 0 ? { htmlAttributes: project.htmlAttributes } : {},
|
|
815
|
+
...project.site !== void 0 ? { site: project.site } : {},
|
|
816
|
+
...project.tokenKey !== void 0 ? { tokenKey: project.tokenKey } : {},
|
|
817
|
+
...signIn !== void 0 ? { signIn: (who) => signIn(booted.server, who) } : {}
|
|
818
|
+
};
|
|
622
819
|
}
|
|
623
|
-
};
|
|
820
|
+
});
|
|
624
821
|
};
|
|
822
|
+
var devServer = async (options) => {
|
|
823
|
+
const project = await loadProject(options.root);
|
|
824
|
+
const configFile = configFileOf(options.root);
|
|
825
|
+
const vite = await viteOf(options.root);
|
|
826
|
+
const server = await vite.createServer({
|
|
827
|
+
root: options.root,
|
|
828
|
+
...options.port !== void 0 ? { server: { port: options.port } } : {},
|
|
829
|
+
plugins: isShellProject(project) || configFile === void 0 ? [] : [await mossPlugin(configFile, project.dev)]
|
|
830
|
+
});
|
|
831
|
+
await server.listen();
|
|
832
|
+
server.printUrls();
|
|
833
|
+
const url = server.resolvedUrls?.local[0] ?? `http://localhost:${options.port ?? 5173}/`;
|
|
834
|
+
return { url, close: () => server.close() };
|
|
835
|
+
};
|
|
836
|
+
var dev = (options) => devServer({ root: options.root, ...options.port !== void 0 ? { port: options.port } : {} });
|
|
837
|
+
|
|
838
|
+
// src/migrate/migrate.ts
|
|
625
839
|
var migrate = async (options) => {
|
|
626
840
|
const project = await loadProject(options.root);
|
|
627
841
|
if (isShellProject(project) || project.app === void 0 || project.runtime === void 0) {
|
|
@@ -632,6 +846,9 @@ var migrate = async (options) => {
|
|
|
632
846
|
const isDryRun = options.dryRun === true;
|
|
633
847
|
const runtime = await project.runtime();
|
|
634
848
|
try {
|
|
849
|
+
if (runtime.migrations === "apply") {
|
|
850
|
+
print("nisc: this runtime's database starts empty every time (migrations: 'apply') \u2014 what is applied here is gone when the command ends. A real database is migrated by pointing runtime at it.");
|
|
851
|
+
}
|
|
635
852
|
if (options.status === true) {
|
|
636
853
|
const plan = await tableStatus(runtime, project.app);
|
|
637
854
|
print(`nisc: applied ${Object.entries(plan.applied).map(([sequence, n]) => `${sequence} ${n}`).join(", ")}`);
|
|
@@ -659,13 +876,12 @@ var handOver = (options, tool, args) => new Promise((done) => {
|
|
|
659
876
|
const child = spawn(process.execPath, [tool, ...args], { cwd: options.root, stdio: "inherit" });
|
|
660
877
|
child.once("exit", (code) => done(code ?? 1));
|
|
661
878
|
});
|
|
662
|
-
var dev = (options) => devServer({ root: options.root, ...options.port !== void 0 ? { port: options.port } : {} });
|
|
663
879
|
var check = async (options) => {
|
|
664
880
|
const project = await loadProject(options.root);
|
|
665
881
|
return handOver(options, toolOf(options.root, "tsx", "tsx"), [project.checks ?? "src/dev/all-checks.ts"]);
|
|
666
882
|
};
|
|
667
883
|
|
|
668
|
-
// src/
|
|
884
|
+
// src/command/index.ts
|
|
669
885
|
var USAGE = `nisc \u2014 the command for a nisc application
|
|
670
886
|
|
|
671
887
|
nisc dev the app's dev server \u2014 its vite, with the app server inside
|
|
@@ -700,12 +916,12 @@ var main = async () => {
|
|
|
700
916
|
const options = {
|
|
701
917
|
root: resolve(valueAfter(args, "--root") ?? process.cwd()),
|
|
702
918
|
...out !== void 0 ? { out } : {},
|
|
703
|
-
...port !== void 0 ? { port: Number(port) } : {},
|
|
704
919
|
allowLive: args.includes("--allow-live"),
|
|
705
920
|
skipBundle: args.includes("--skip-bundle")
|
|
706
921
|
};
|
|
707
922
|
if (command === "dev" || command === "start") {
|
|
708
|
-
const
|
|
923
|
+
const given = { ...options, ...port !== void 0 ? { port: portOf(port, "--port") } : {} };
|
|
924
|
+
const served = command === "dev" ? await dev(given) : await start(given);
|
|
709
925
|
const stop = () => {
|
|
710
926
|
void served.close().finally(() => process.exit(0));
|
|
711
927
|
};
|
|
@@ -725,12 +941,16 @@ var main = async () => {
|
|
|
725
941
|
console.log(USAGE);
|
|
726
942
|
return command === void 0 || command === "help" || command === "--help" ? 0 : 1;
|
|
727
943
|
};
|
|
944
|
+
var reportOf = (error) => {
|
|
945
|
+
if (!(error instanceof Error)) return error;
|
|
946
|
+
return error.message.startsWith("nisc: ") ? error.message : error.stack ?? error.message;
|
|
947
|
+
};
|
|
728
948
|
main().then(
|
|
729
949
|
// An explicit exit: an app's own handles (a database, a timer) would keep a
|
|
730
950
|
// finished command open.
|
|
731
951
|
(code) => process.exit(code),
|
|
732
952
|
(error) => {
|
|
733
|
-
console.error(error
|
|
953
|
+
console.error(reportOf(error));
|
|
734
954
|
process.exit(1);
|
|
735
955
|
}
|
|
736
956
|
);
|