topotopo 0.2.0 → 0.4.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/NOTICE +30 -0
- package/README.md +54 -16
- package/dist/cli.js +95 -14
- package/dist/core/analysis.d.ts +5 -1
- package/dist/core/analysis.js +8 -3
- package/dist/core/bake.d.ts +2 -5
- package/dist/core/bake.js +12 -25
- package/dist/core/bevel.js +165 -97
- package/dist/core/geometry.js +1 -1
- package/dist/core/imageops.d.ts +6 -0
- package/dist/core/imageops.js +51 -0
- package/dist/core/materials.d.ts +4 -0
- package/dist/core/materials.js +11 -0
- package/dist/core/packed.js +11 -0
- package/dist/core/particles.d.ts +1 -1
- package/dist/core/particles.js +1 -1
- package/dist/core/rbd.js +13 -1
- package/dist/core/scatter.d.ts +1 -1
- package/dist/core/scatter.js +1 -1
- package/dist/core/scene.d.ts +20 -0
- package/dist/core/scene.js +71 -0
- package/dist/core/snippet-lib.d.ts +1 -1
- package/dist/core/snippet-lib.js +1 -1
- package/dist/core/solidify.d.ts +38 -0
- package/dist/core/solidify.js +356 -0
- package/dist/core/tangents.d.ts +52 -0
- package/dist/core/tangents.js +214 -0
- package/dist/core/texcoord.d.ts +4 -0
- package/dist/core/texcoord.js +14 -0
- package/dist/core/topology.d.ts +22 -0
- package/dist/core/topology.js +70 -7
- package/dist/core/traceimage.d.ts +7 -0
- package/dist/core/traceimage.js +221 -0
- package/dist/core/tritri.d.ts +5 -0
- package/dist/core/tritri.js +120 -0
- package/dist/core/uv.d.ts +13 -0
- package/dist/core/uv.js +3 -3
- package/dist/core/uvreport.d.ts +66 -0
- package/dist/core/uvreport.js +308 -0
- package/dist/core/xatlas.js +32 -2
- package/dist/gallery/app/gallery.css +100 -0
- package/dist/gallery/app/gallery.js +80 -0
- package/dist/gallery/bake.d.ts +34 -0
- package/dist/gallery/bake.js +144 -0
- package/dist/gallery/cli.d.ts +4 -0
- package/dist/gallery/cli.js +157 -0
- package/dist/gallery/library.d.ts +124 -0
- package/dist/gallery/library.js +366 -0
- package/dist/gallery/pages.d.ts +94 -0
- package/dist/gallery/pages.js +148 -0
- package/dist/gallery/server.d.ts +21 -0
- package/dist/gallery/server.js +391 -0
- package/dist/graph/engine.js +14 -0
- package/dist/graph/types.d.ts +17 -0
- package/dist/host/artifacts.d.ts +21 -0
- package/dist/host/artifacts.js +58 -0
- package/dist/host/export-targets.d.ts +33 -0
- package/dist/host/export-targets.js +84 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/inspect.d.ts +6 -1
- package/dist/inspect.js +13 -1
- package/dist/io/common.d.ts +8 -0
- package/dist/io/gltf.js +236 -19
- package/dist/io/targets.d.ts +31 -0
- package/dist/io/targets.js +75 -0
- package/dist/mcp/docs.d.ts +1 -1
- package/dist/mcp/docs.js +73 -6
- package/dist/mcp/library.d.ts +12 -0
- package/dist/mcp/library.js +159 -0
- package/dist/mcp/server.js +29 -12
- package/dist/mcp/tools.d.ts +1 -1
- package/dist/mcp/tools.js +102 -43
- package/dist/nodes/bake.js +56 -36
- package/dist/nodes/bevel.js +4 -3
- package/dist/nodes/code.js +33 -7
- package/dist/nodes/curves.js +7 -0
- package/dist/nodes/generate.js +1 -1
- package/dist/nodes/imageops.d.ts +1 -0
- package/dist/nodes/imageops.js +206 -0
- package/dist/nodes/index.d.ts +3 -0
- package/dist/nodes/index.js +3 -0
- package/dist/nodes/material.js +3 -0
- package/dist/nodes/measure.js +3 -2
- package/dist/nodes/rbd.js +1 -1
- package/dist/nodes/scene.d.ts +1 -0
- package/dist/nodes/scene.js +53 -0
- package/dist/nodes/solidify.d.ts +1 -0
- package/dist/nodes/solidify.js +52 -0
- package/dist/nodes/transform.js +5 -1
- package/dist/nodes/util.d.ts +1 -1
- package/dist/nodes/wasmcall.js +1 -1
- package/dist/render/options.js +1 -1
- package/dist/render/pbr.d.ts +28 -0
- package/dist/render/pbr.js +378 -0
- package/dist/render/preview.d.ts +58 -0
- package/dist/render/preview.js +24 -0
- package/dist/render/raster.d.ts +12 -2
- package/dist/render/raster.js +98 -11
- package/dist/render/texview.d.ts +1 -1
- package/dist/render/texview.js +7 -2
- package/dist/viewer/app/camera.js +27 -7
- package/dist/viewer/app/embed.js +80 -0
- package/dist/viewer/app/export-popover.js +68 -0
- package/dist/viewer/app/grid.js +1 -1
- package/dist/viewer/app/guides.js +1 -1
- package/dist/viewer/app/image-view.js +114 -0
- package/dist/viewer/app/keymap.js +15 -3
- package/dist/viewer/app/layers.js +24 -5
- package/dist/viewer/app/main.js +71 -15
- package/dist/viewer/app/markers.js +1 -1
- package/dist/viewer/app/outliner.js +56 -15
- package/dist/viewer/app/outputs.css +73 -0
- package/dist/viewer/app/outputs.js +240 -0
- package/dist/viewer/app/props.js +1 -1
- package/dist/viewer/app/scene.js +6 -2
- package/dist/viewer/app/select.js +14 -3
- package/dist/viewer/app/selection.js +1 -1
- package/dist/viewer/app/status.js +20 -0
- package/dist/viewer/app/style.css +2 -1
- package/dist/viewer/app/units.js +1 -1
- package/dist/viewer/app/vars.js +1 -1
- package/dist/viewer/index.html +20 -6
- package/dist/viewer/outputs.d.ts +54 -0
- package/dist/viewer/outputs.js +436 -0
- package/dist/viewer/server.d.ts +19 -2
- package/dist/viewer/server.js +77 -9
- package/dist/workspace.d.ts +21 -2
- package/dist/workspace.js +124 -4
- package/kernels/pkg/README.md +21 -28
- package/kernels/pkg-simd/README.md +21 -28
- package/package.json +11 -4
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// Baking a project's previews: .gallery/thumb.png (512 px matcap) and .gallery/preview.glb (meshopt), plus
|
|
2
|
+
// .gallery/bake.json recording the bake key they were made from. The gallery never cooks in its own process (cooks
|
|
3
|
+
// block the event loop and cannot be cancelled): BakeQueue runs `topotopo library bake <dir>` children one at a time,
|
|
4
|
+
// with a shorter simulation limit, a hard time limit and a low priority. A failed bake keeps the previous files.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import os from 'node:os';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import zlib from 'node:zlib';
|
|
9
|
+
import { spawn } from 'node:child_process';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import '../nodes/index.js';
|
|
12
|
+
import { Engine } from '../graph/engine.js';
|
|
13
|
+
import { createNodeHost } from '../host/node.js';
|
|
14
|
+
import { Workspace } from '../workspace.js';
|
|
15
|
+
import { thumbnailPNG, previewGLB } from '../render/preview.js';
|
|
16
|
+
import { bakeKey, galleryDir, readBakeState, readManifest, writeAtomic } from './library.js';
|
|
17
|
+
export const THUMB_SIZE = 512;
|
|
18
|
+
/** a preview.glb above this is not written (the page shows the thumbnail instead) */
|
|
19
|
+
export const MAX_PREVIEW_GLB = 50 * 1024 * 1024;
|
|
20
|
+
/** a bake child is stopped after this */
|
|
21
|
+
export const BAKE_TIMEOUT_MS = 300_000;
|
|
22
|
+
/** the simulation limit inside a bake child (the engine's default is 600 s per node) */
|
|
23
|
+
export const BAKE_SIM_TIMEOUT_MS = 120_000;
|
|
24
|
+
/** Bake one project in this process. Never throws for a graph problem: the result (also in bake.json) says. */
|
|
25
|
+
export async function bakeProject(dir) {
|
|
26
|
+
const m = readManifest(dir);
|
|
27
|
+
const key = bakeKey(dir, m);
|
|
28
|
+
const out = galleryDir(dir);
|
|
29
|
+
fs.mkdirSync(out, { recursive: true });
|
|
30
|
+
const t0 = performance.now();
|
|
31
|
+
const finish = (s) => {
|
|
32
|
+
const state = { key, at: Date.now(), ms: Math.round(performance.now() - t0), ...s };
|
|
33
|
+
writeAtomic(path.join(out, 'bake.json'), JSON.stringify(state, null, 1) + '\n');
|
|
34
|
+
return state;
|
|
35
|
+
};
|
|
36
|
+
try {
|
|
37
|
+
const engine = new Engine(createNodeHost({ baseDir: dir }));
|
|
38
|
+
if (process.env.TOPOTOPO_SIM_TIMEOUT_MS)
|
|
39
|
+
engine.simTimeoutMs = Number(process.env.TOPOTOPO_SIM_TIMEOUT_MS);
|
|
40
|
+
if (process.env.TOPOTOPO_SCRIPT_TIMEOUT_MS)
|
|
41
|
+
engine.scriptTimeoutMs = Number(process.env.TOPOTOPO_SCRIPT_TIMEOUT_MS);
|
|
42
|
+
await engine.init();
|
|
43
|
+
const ws = new Workspace(dir, engine);
|
|
44
|
+
ws.load();
|
|
45
|
+
const name = m.bake.graph ?? m.graph;
|
|
46
|
+
const g = ws.graphs.get(name);
|
|
47
|
+
if (!g)
|
|
48
|
+
return finish({ ok: false, error: `no graph "${name}" in graphs/ (project.json "graph")` });
|
|
49
|
+
const r = await engine.cook(g, { target: m.bake.node, frame: m.bake.frame });
|
|
50
|
+
if (!r.ok || !r.geo)
|
|
51
|
+
return finish({ ok: false, error: r.errors.map((e) => `[${e.node}] ${e.message}`).join('\n') || 'the cook gave no geometry' });
|
|
52
|
+
writeAtomic(path.join(out, 'thumb.png'), thumbnailPNG(r.geo, g, (d) => zlib.deflateSync(d, { level: 9 }), THUMB_SIZE));
|
|
53
|
+
const glb = await previewGLB(r.geo, { compress: 'meshopt' });
|
|
54
|
+
const small = glb.length <= MAX_PREVIEW_GLB;
|
|
55
|
+
if (small)
|
|
56
|
+
writeAtomic(path.join(out, 'preview.glb'), glb);
|
|
57
|
+
else
|
|
58
|
+
fs.rmSync(path.join(out, 'preview.glb'), { force: true });
|
|
59
|
+
const warnings = r.warnings.map((w) => `[${w.node}] ${w.message}`);
|
|
60
|
+
if (!small)
|
|
61
|
+
warnings.push(`preview.glb would be ${(glb.length / 2 ** 20).toFixed(0)} MB (over ${MAX_PREVIEW_GLB / 2 ** 20} MB): not written`);
|
|
62
|
+
return finish({ ok: true, glb: small, points: r.geo.numPoints, prims: r.geo.numPrims, warnings });
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
return finish({ ok: false, error: String(e?.message ?? e) });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// ------------------------------------------------------------------------------------------------------ BakeQueue
|
|
69
|
+
const cli = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', import.meta.url.endsWith('.ts') ? 'cli.ts' : 'cli.js');
|
|
70
|
+
/** Bakes in child processes, one at a time, each project once however often it is asked for meanwhile. */
|
|
71
|
+
export class BakeQueue {
|
|
72
|
+
queue = [];
|
|
73
|
+
running = null;
|
|
74
|
+
child = null;
|
|
75
|
+
closed = false;
|
|
76
|
+
opts;
|
|
77
|
+
constructor(opts = {}) { this.opts = opts; }
|
|
78
|
+
/** Ask for a bake; true when it was queued now (false: queued or running already). */
|
|
79
|
+
add(dir) {
|
|
80
|
+
if (this.closed || this.running === dir || this.queue.includes(dir))
|
|
81
|
+
return false;
|
|
82
|
+
this.queue.push(dir);
|
|
83
|
+
this.next();
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
pending(dir) { return this.running === dir || this.queue.includes(dir); }
|
|
87
|
+
get size() { return this.queue.length + (this.running ? 1 : 0); }
|
|
88
|
+
/** Resolves when nothing is queued or running. */
|
|
89
|
+
async idle() {
|
|
90
|
+
while (this.size)
|
|
91
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
92
|
+
}
|
|
93
|
+
close() {
|
|
94
|
+
this.closed = true;
|
|
95
|
+
this.queue = [];
|
|
96
|
+
this.child?.kill();
|
|
97
|
+
}
|
|
98
|
+
next() {
|
|
99
|
+
if (this.running || this.closed)
|
|
100
|
+
return;
|
|
101
|
+
const dir = this.queue.shift();
|
|
102
|
+
if (!dir)
|
|
103
|
+
return;
|
|
104
|
+
this.running = dir;
|
|
105
|
+
const t0 = Date.now();
|
|
106
|
+
const child = spawn(process.execPath, [cli, 'library', 'bake', dir], {
|
|
107
|
+
env: { ...process.env, TOPOTOPO_SIM_TIMEOUT_MS: process.env.TOPOTOPO_SIM_TIMEOUT_MS ?? String(BAKE_SIM_TIMEOUT_MS) },
|
|
108
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
109
|
+
windowsHide: true,
|
|
110
|
+
});
|
|
111
|
+
this.child = child;
|
|
112
|
+
try {
|
|
113
|
+
if (child.pid)
|
|
114
|
+
os.setPriority(child.pid, os.constants.priority.PRIORITY_BELOW_NORMAL);
|
|
115
|
+
}
|
|
116
|
+
catch { /* not allowed: normal priority */ }
|
|
117
|
+
let stderr = '';
|
|
118
|
+
child.stderr?.on('data', (d) => { if (stderr.length < 4000)
|
|
119
|
+
stderr += d; });
|
|
120
|
+
const timer = setTimeout(() => child.kill(), this.opts.timeoutMs ?? BAKE_TIMEOUT_MS);
|
|
121
|
+
timer.unref();
|
|
122
|
+
child.on('exit', (code, signal) => {
|
|
123
|
+
clearTimeout(timer);
|
|
124
|
+
this.child = null;
|
|
125
|
+
this.running = null;
|
|
126
|
+
let state = readBakeState(dir);
|
|
127
|
+
// killed (time limit) or crashed: record it, so the page says why and the same key is not retried in a loop
|
|
128
|
+
if (!state || state.at < t0) {
|
|
129
|
+
const why = signal ? `stopped after ${((Date.now() - t0) / 1000).toFixed(0)} s (the bake time limit is ${(this.opts.timeoutMs ?? BAKE_TIMEOUT_MS) / 1000} s)` : `the bake process failed (exit ${code}): ${stderr.trim().split('\n').slice(-3).join(' ')}`;
|
|
130
|
+
try {
|
|
131
|
+
state = { key: bakeKey(dir, readManifest(dir)), at: Date.now(), ok: false, error: why };
|
|
132
|
+
fs.mkdirSync(galleryDir(dir), { recursive: true });
|
|
133
|
+
writeAtomic(path.join(galleryDir(dir), 'bake.json'), JSON.stringify(state, null, 1) + '\n');
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
state = null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
this.opts.log?.(`bake ${dir}: ${state?.ok ? `ok in ${state.ms} ms` : `failed: ${state?.error ?? 'no result'}`}`);
|
|
140
|
+
this.opts.onDone?.(dir, state);
|
|
141
|
+
this.next();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function galleryCommand(argv: string[], prog: string): Promise<undefined>;
|
|
2
|
+
export declare function libraryCommand(argv: string[], prog: string): Promise<undefined>;
|
|
3
|
+
/** `edit <project>`: the live viewer on a project's workspace, following changes other processes make to it. */
|
|
4
|
+
export declare function editCommand(argv: string[], prog: string): Promise<void>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// The gallery's commands (src/cli.ts dispatches here):
|
|
2
|
+
// topotopo gallery [--port 7340] [--library <folder>] [--no-bake] the gallery website on 127.0.0.1
|
|
3
|
+
// topotopo library add <name> <folder> list a folder as a library (~/.topotopo/config.json)
|
|
4
|
+
// topotopo library list libraries, projects and problems
|
|
5
|
+
// topotopo library new <library> <folder/name> [--title t] [--tags a,b] [--description d]
|
|
6
|
+
// topotopo library bake <project folder> | --stale | --all bake previews in this process
|
|
7
|
+
// topotopo edit <project folder> [--port 7341] [--json] the live viewer on a project (the gallery's editor)
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { addLibrary, createProject, isStale, loadConfig, projectDirOf, readBakeState, scanAll, slugify } from './library.js';
|
|
11
|
+
const fail = (msg) => { console.error(msg); process.exit(1); };
|
|
12
|
+
export async function galleryCommand(argv, prog) {
|
|
13
|
+
const opt = (f) => { const i = argv.indexOf(f); return i >= 0 ? argv[i + 1] : undefined; };
|
|
14
|
+
let cfg;
|
|
15
|
+
try {
|
|
16
|
+
cfg = loadConfig();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return fail(e.message);
|
|
20
|
+
}
|
|
21
|
+
const extra = opt('--library');
|
|
22
|
+
if (extra) {
|
|
23
|
+
const name = slugify(path.basename(path.resolve(extra))) || 'library';
|
|
24
|
+
cfg = { ...cfg, libraries: [...cfg.libraries.filter((l) => l.name !== name), { name, path: path.resolve(extra) }] };
|
|
25
|
+
}
|
|
26
|
+
if (!cfg.libraries.length)
|
|
27
|
+
console.log(`no libraries yet: add a folder with \`${prog} library add <name> <folder>\` (or pass --library <folder>)`);
|
|
28
|
+
const port = opt('--port') !== undefined ? Number(opt('--port')) : cfg.gallery.port;
|
|
29
|
+
const { startGallery, runningGallery } = await import('./server.js');
|
|
30
|
+
try {
|
|
31
|
+
const g = await startGallery({ config: cfg, port, bake: !argv.includes('--no-bake'), log: (m) => console.log(m) });
|
|
32
|
+
const n = cfg.libraries.length;
|
|
33
|
+
console.log(`gallery: ${g.url} (${n} librar${n === 1 ? 'y' : 'ies'}: ${cfg.libraries.map((l) => `${l.name} → ${l.path}`).join(', ') || 'none'}; Ctrl+C to stop)`);
|
|
34
|
+
if (g.queue.size)
|
|
35
|
+
console.log(`baking ${g.queue.size} project(s) in the background`);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
if (e?.code !== 'EADDRINUSE')
|
|
39
|
+
throw e;
|
|
40
|
+
const url = await runningGallery(port);
|
|
41
|
+
if (url) {
|
|
42
|
+
console.log(`gallery: ${url} (already running)`);
|
|
43
|
+
process.exit(0);
|
|
44
|
+
}
|
|
45
|
+
fail(`port ${port} is taken by another program: pass --port, or set "gallery": {"port": N} in ${cfg.file}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function libraryCommand(argv, prog) {
|
|
49
|
+
const opt = (f) => { const i = argv.indexOf(f); return i >= 0 ? argv[i + 1] : undefined; };
|
|
50
|
+
const [sub, a, b] = argv;
|
|
51
|
+
const usage = `usage: ${prog} library add <name> <folder> | list | new <library> <folder/name> [--title t] [--tags a,b] [--description d] | bake <project folder> | bake --stale | bake --all`;
|
|
52
|
+
let cfg;
|
|
53
|
+
try {
|
|
54
|
+
cfg = loadConfig();
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return fail(e.message);
|
|
58
|
+
}
|
|
59
|
+
if (sub === 'add') {
|
|
60
|
+
if (!a || !b)
|
|
61
|
+
fail(usage);
|
|
62
|
+
try {
|
|
63
|
+
cfg = addLibrary(a, b);
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
fail(e.message);
|
|
67
|
+
}
|
|
68
|
+
console.log(`library ${a} → ${path.resolve(b)} (in ${cfg.file})`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (sub === 'list') {
|
|
72
|
+
if (!cfg.libraries.length)
|
|
73
|
+
console.log(`no libraries (config ${cfg.file}): ${prog} library add <name> <folder>`);
|
|
74
|
+
for (const s of scanAll(cfg)) {
|
|
75
|
+
console.log(`${s.name} ${s.root} (${s.projects.length} projects)`);
|
|
76
|
+
for (const p of s.projects) {
|
|
77
|
+
const st = readBakeState(p.dir);
|
|
78
|
+
const status = !st ? 'not baked' : !st.ok ? 'bake failed' : isStale(p) ? 'stale' : 'baked';
|
|
79
|
+
console.log(` ${p.id.padEnd(24)} ${p.rel.padEnd(32)} ${status}${p.manifest.tags.length ? ' [' + p.manifest.tags.join(', ') + ']' : ''}`);
|
|
80
|
+
}
|
|
81
|
+
for (const pr of s.problems)
|
|
82
|
+
console.log(` ! ${pr}`);
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (sub === 'new') {
|
|
87
|
+
if (!a || !b)
|
|
88
|
+
fail(usage);
|
|
89
|
+
const lib = cfg.libraries.find((l) => l.name === a) ?? fail(`no library "${a}" (${cfg.libraries.map((l) => l.name).join(', ') || `none: ${prog} library add <name> <folder>`})`);
|
|
90
|
+
try {
|
|
91
|
+
const p = createProject(lib, b, { title: opt('--title'), description: opt('--description'), tags: opt('--tags')?.split(',').map((t) => t.trim()).filter(Boolean) });
|
|
92
|
+
console.log(`project ${p.lib}/${p.id} → ${p.dir}`);
|
|
93
|
+
console.log(` gallery: http://127.0.0.1:${cfg.gallery.port}/p/${p.lib}/${p.id}`);
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
fail(e.message);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (sub === 'bake') {
|
|
101
|
+
if (!a)
|
|
102
|
+
fail(usage);
|
|
103
|
+
const { bakeProject } = await import('./bake.js');
|
|
104
|
+
let dirs;
|
|
105
|
+
if (a === '--all' || a === '--stale')
|
|
106
|
+
dirs = scanAll(cfg).flatMap((s) => s.projects).filter((p) => a === '--all' || isStale(p)).map((p) => p.dir);
|
|
107
|
+
else {
|
|
108
|
+
const d = projectDirOf(a);
|
|
109
|
+
if (!d)
|
|
110
|
+
fail(`${a}: no project.json here or in a parent folder`);
|
|
111
|
+
dirs = [d];
|
|
112
|
+
}
|
|
113
|
+
let failed = 0;
|
|
114
|
+
for (const d of dirs) {
|
|
115
|
+
const s = await bakeProject(d);
|
|
116
|
+
if (!s.ok)
|
|
117
|
+
failed++;
|
|
118
|
+
console.log(`${s.ok ? 'ok ' : 'FAIL'} ${d} ${s.ok ? `${s.ms} ms, ${s.points} points, ${s.prims} prims${s.glb ? '' : ', no preview.glb'}` : s.error}`);
|
|
119
|
+
}
|
|
120
|
+
if (!dirs.length)
|
|
121
|
+
console.log('nothing to bake');
|
|
122
|
+
process.exit(failed ? 1 : 0);
|
|
123
|
+
}
|
|
124
|
+
fail(usage);
|
|
125
|
+
}
|
|
126
|
+
/** `edit <project>`: the live viewer on a project's workspace, following changes other processes make to it. */
|
|
127
|
+
export async function editCommand(argv, prog) {
|
|
128
|
+
const opt = (f) => { const i = argv.indexOf(f); return i >= 0 ? argv[i + 1] : undefined; };
|
|
129
|
+
const dir = argv[0] && !argv[0].startsWith('-') ? projectDirOf(argv[0]) ?? path.resolve(argv[0]) : fail(`usage: ${prog} edit <project folder> [--port 7341] [--json]`);
|
|
130
|
+
if (!fs.existsSync(dir))
|
|
131
|
+
fail(`no folder ${dir}`);
|
|
132
|
+
const { Engine } = await import('../graph/engine.js');
|
|
133
|
+
const { createNodeHost } = await import('../host/node.js');
|
|
134
|
+
const { Workspace } = await import('../workspace.js');
|
|
135
|
+
const { startViewer } = await import('../viewer/server.js');
|
|
136
|
+
const engine = new Engine(createNodeHost({ baseDir: dir }));
|
|
137
|
+
await engine.init();
|
|
138
|
+
const ws = new Workspace(dir, engine);
|
|
139
|
+
ws.load();
|
|
140
|
+
ws.watch();
|
|
141
|
+
const v = await startViewer(ws, opt('--port') !== undefined ? Number(opt('--port')) : undefined);
|
|
142
|
+
if (argv.includes('--json')) {
|
|
143
|
+
// started by the gallery: report the URL, and stop once no page has been open for a while
|
|
144
|
+
console.log(JSON.stringify({ url: v.url }));
|
|
145
|
+
let idleSince = Date.now();
|
|
146
|
+
setInterval(() => {
|
|
147
|
+
if (v.clients() > 0)
|
|
148
|
+
idleSince = Date.now();
|
|
149
|
+
else if (Date.now() - idleSince > 10 * 60_000) {
|
|
150
|
+
v.close();
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
}, 30_000);
|
|
154
|
+
}
|
|
155
|
+
else
|
|
156
|
+
console.log(`edit: ${v.url} (${dir}; Ctrl+C to stop)`);
|
|
157
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** Bumped when the baked files change shape (render options, GLB options), so every project bakes again. */
|
|
2
|
+
export declare const BAKE_VERSION = 1;
|
|
3
|
+
export declare const DEFAULT_PORT = 7340;
|
|
4
|
+
/** a library name or project id: lower-case letters, digits, "-" and "_", starting with a letter or digit */
|
|
5
|
+
export declare const ID_RE: RegExp;
|
|
6
|
+
export interface LibraryRef {
|
|
7
|
+
name: string;
|
|
8
|
+
path: string;
|
|
9
|
+
}
|
|
10
|
+
export interface GalleryConfig {
|
|
11
|
+
/** the config file read (or the one `library add` writes) */
|
|
12
|
+
file: string;
|
|
13
|
+
libraries: LibraryRef[];
|
|
14
|
+
gallery: {
|
|
15
|
+
port: number;
|
|
16
|
+
cors: string[];
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface ProjectLink {
|
|
20
|
+
label: string;
|
|
21
|
+
url: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ProjectManifest {
|
|
24
|
+
id: string;
|
|
25
|
+
title: string;
|
|
26
|
+
description: string;
|
|
27
|
+
tags: string[];
|
|
28
|
+
/** the graph the gallery shows and bakes */
|
|
29
|
+
graph: string;
|
|
30
|
+
/** what to bake instead of the graph's output: another graph, a node, a frame (simulations settle after frame 1) */
|
|
31
|
+
bake: {
|
|
32
|
+
graph?: string;
|
|
33
|
+
node?: string;
|
|
34
|
+
frame?: number;
|
|
35
|
+
};
|
|
36
|
+
/** links back to the board, concept pages, tickets */
|
|
37
|
+
links: ProjectLink[];
|
|
38
|
+
/** ISO date */
|
|
39
|
+
created: string;
|
|
40
|
+
}
|
|
41
|
+
export interface BakeState {
|
|
42
|
+
key: string;
|
|
43
|
+
/** ms since the epoch */
|
|
44
|
+
at: number;
|
|
45
|
+
ok: boolean;
|
|
46
|
+
error?: string;
|
|
47
|
+
ms?: number;
|
|
48
|
+
/** preview.glb was written (false: too large, or the bake failed before it) */
|
|
49
|
+
glb?: boolean;
|
|
50
|
+
points?: number;
|
|
51
|
+
prims?: number;
|
|
52
|
+
warnings?: string[];
|
|
53
|
+
}
|
|
54
|
+
export interface Project {
|
|
55
|
+
lib: string;
|
|
56
|
+
id: string;
|
|
57
|
+
/** absolute folder */
|
|
58
|
+
dir: string;
|
|
59
|
+
/** posix path of the folder relative to the library root */
|
|
60
|
+
rel: string;
|
|
61
|
+
/** the parent folders' path ("" at the top), how the gallery groups projects */
|
|
62
|
+
collection: string;
|
|
63
|
+
manifest: ProjectManifest;
|
|
64
|
+
/** newest change to project.json, graphs/ or assets/ (ms) */
|
|
65
|
+
updated: number;
|
|
66
|
+
}
|
|
67
|
+
export interface LibraryScan {
|
|
68
|
+
name: string;
|
|
69
|
+
root: string;
|
|
70
|
+
projects: Project[];
|
|
71
|
+
problems: string[];
|
|
72
|
+
}
|
|
73
|
+
export declare const slugify: (s: string) => string;
|
|
74
|
+
export declare function configFile(): string;
|
|
75
|
+
/** The user's config with defaults filled in; libraries from $TOPOTOPO_LIBRARIES come after the file's. */
|
|
76
|
+
export declare function loadConfig(): GalleryConfig;
|
|
77
|
+
/** Add (or re-point) a library in the config file, keeping its other keys. */
|
|
78
|
+
export declare function addLibrary(name: string, dir: string): GalleryConfig;
|
|
79
|
+
/** project.json checked and normalised; throws naming the problem. `fallbackId` is used when it has no id. */
|
|
80
|
+
export declare function parseManifest(raw: any, fallbackId: string): ProjectManifest;
|
|
81
|
+
export declare const manifestFile: (dir: string) => string;
|
|
82
|
+
export declare function readManifest(dir: string): ProjectManifest;
|
|
83
|
+
/** Write a manifest (atomically), keeping keys of the file this version does not know. */
|
|
84
|
+
export declare function writeManifest(dir: string, m: ProjectManifest): void;
|
|
85
|
+
/** Written as a dot-file next to the target and renamed, so readers never see half a file. */
|
|
86
|
+
export declare function writeAtomic(file: string, data: string | Uint8Array): void;
|
|
87
|
+
/** Every project below a library root, sorted by path. Ids used twice: the first path keeps it, the others are problems. */
|
|
88
|
+
export declare function scanLibrary(lib: LibraryRef): LibraryScan;
|
|
89
|
+
export declare function scanAll(cfg?: GalleryConfig): LibraryScan[];
|
|
90
|
+
/** The project a folder belongs to (the folder itself, or the nearest parent with a project.json), or null. */
|
|
91
|
+
export declare function projectDirOf(start: string): string | null;
|
|
92
|
+
export interface CreateOptions {
|
|
93
|
+
title?: string;
|
|
94
|
+
description?: string;
|
|
95
|
+
tags?: string[];
|
|
96
|
+
links?: ProjectLink[];
|
|
97
|
+
graph?: object;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A new project at `<library root>/<rel>` ("props/lantern"): project.json, the workspace folders, attachments/, and a
|
|
101
|
+
* .gitignore for the derived files. `graph` (a graph spec) becomes graphs/main.json. Fails when the folder holds a
|
|
102
|
+
* project already or the id is taken in the library.
|
|
103
|
+
*/
|
|
104
|
+
export declare function createProject(lib: LibraryRef, rel: string, o?: CreateOptions): Project;
|
|
105
|
+
export interface ProjectFile {
|
|
106
|
+
path: string;
|
|
107
|
+
size: number;
|
|
108
|
+
mtime: number;
|
|
109
|
+
image: boolean;
|
|
110
|
+
}
|
|
111
|
+
/** Files under a project subfolder (attachments/, exports/), recursively: posix paths, dot-files skipped. */
|
|
112
|
+
export declare function listFiles(dir: string): ProjectFile[];
|
|
113
|
+
export declare const graphNames: (dir: string) => string[];
|
|
114
|
+
/** A project's graph file, parsed (null when missing or unreadable). */
|
|
115
|
+
export declare function readGraph(dir: string, name: string): any;
|
|
116
|
+
export declare const galleryDir: (dir: string) => string;
|
|
117
|
+
/**
|
|
118
|
+
* What a bake depends on: the bake version, the package version, the manifest's graph and bake choice, every graph
|
|
119
|
+
* and asset file's content, and each input file's path, size and time.
|
|
120
|
+
*/
|
|
121
|
+
export declare function bakeKey(dir: string, m: ProjectManifest): string;
|
|
122
|
+
export declare function readBakeState(dir: string): BakeState | null;
|
|
123
|
+
/** The project needs a bake: never baked, or something it depends on changed since. */
|
|
124
|
+
export declare function isStale(p: Project): boolean;
|