@pstdio/canvas 0.0.0-stage → 0.45.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/README.md +95 -2
- package/bin/pstdio-canvas.ts +4 -0
- package/package.json +30 -4
- package/src/compose.ts +83 -0
- package/src/index.ts +16 -0
- package/src/indexer.ts +64 -0
- package/src/native-preview-runtime.tsx +30 -0
- package/src/project/archive.ts +126 -0
- package/src/project/build.ts +113 -0
- package/src/project/bundle.ts +140 -0
- package/src/project/cli.ts +58 -0
- package/src/project/discover.ts +64 -0
- package/src/project/index-canvas-file.ts +130 -0
- package/src/project/inputs.ts +76 -0
- package/src/project/lock.ts +42 -0
- package/src/project/manifest.ts +45 -0
- package/src/project/watch.ts +71 -0
- package/src/runtime/frame-runtime.tsx +183 -0
- package/src/runtime/navigation.ts +135 -0
- package/src/runtime/protocol.ts +54 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import type { CanvasEntry } from "./manifest";
|
|
4
|
+
|
|
5
|
+
const RUNTIME = resolve(import.meta.dir, "../runtime/frame-runtime.tsx");
|
|
6
|
+
|
|
7
|
+
// The runtime lives in this package, but every frame must share the project's single React.
|
|
8
|
+
const projectReact = (root: string): Bun.BunPlugin => ({
|
|
9
|
+
name: "pstdio-canvas-project-react",
|
|
10
|
+
setup(build) {
|
|
11
|
+
build.onResolve({ filter: /^react(-dom)?($|\/)/ }, (args) => ({ path: Bun.resolveSync(args.path, root) }));
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const ownFiles = (root: string, canvas: CanvasEntry) =>
|
|
16
|
+
new Set([join(root, canvas.id), ...(canvas.preview ? [join(root, canvas.preview)] : [])]);
|
|
17
|
+
|
|
18
|
+
const entrySource = (root: string, canvases: CanvasEntry[]) => {
|
|
19
|
+
const lines = canvases.map((canvas) => {
|
|
20
|
+
const load = `load: () => import(${JSON.stringify(join(root, canvas.id))})`;
|
|
21
|
+
const preview = canvas.preview ? `, preview: () => import(${JSON.stringify(join(root, canvas.preview))})` : "";
|
|
22
|
+
return ` ${JSON.stringify(canvas.id)}: { ${load}${preview} },`;
|
|
23
|
+
});
|
|
24
|
+
return `import { mountFrameRuntime } from ${JSON.stringify(RUNTIME)};\nmountFrameRuntime({\n${lines.join("\n")}\n});\n`;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Frames load modules from blob URLs, so a relative asset URL has nothing to resolve against.
|
|
28
|
+
// Bun emits images, fonts and media as files; the archive inlines them as data URLs.
|
|
29
|
+
export const ASSET_TYPES: Record<string, string> = {
|
|
30
|
+
png: "image/png",
|
|
31
|
+
jpg: "image/jpeg",
|
|
32
|
+
jpeg: "image/jpeg",
|
|
33
|
+
gif: "image/gif",
|
|
34
|
+
webp: "image/webp",
|
|
35
|
+
avif: "image/avif",
|
|
36
|
+
svg: "image/svg+xml",
|
|
37
|
+
ico: "image/x-icon",
|
|
38
|
+
woff: "font/woff",
|
|
39
|
+
woff2: "font/woff2",
|
|
40
|
+
ttf: "font/ttf",
|
|
41
|
+
otf: "font/otf",
|
|
42
|
+
eot: "application/vnd.ms-fontobject",
|
|
43
|
+
mp4: "video/mp4",
|
|
44
|
+
webm: "video/webm",
|
|
45
|
+
mp3: "audio/mpeg",
|
|
46
|
+
wav: "audio/wav",
|
|
47
|
+
};
|
|
48
|
+
const loader = Object.fromEntries(Object.keys(ASSET_TYPES).map((extension) => [`.${extension}`, "file" as const]));
|
|
49
|
+
|
|
50
|
+
// Canvas and preview files are entry points too, so each gets its own CSS bundle and a frame
|
|
51
|
+
// loads only the styles of the canvas it shows.
|
|
52
|
+
const runBuild = async (root: string, entry: string, canvases: CanvasEntry[], outdir?: string) => {
|
|
53
|
+
// Bun names modules and metafile paths relative to the working folder; keep them project-relative.
|
|
54
|
+
const previous = process.cwd();
|
|
55
|
+
process.chdir(root);
|
|
56
|
+
try {
|
|
57
|
+
return await bunBuild(root, entry, canvases, outdir);
|
|
58
|
+
} finally {
|
|
59
|
+
process.chdir(previous);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const bunBuild = (root: string, entry: string, canvases: CanvasEntry[], outdir?: string) =>
|
|
64
|
+
Bun.build({
|
|
65
|
+
entrypoints: [entry, ...new Set(canvases.flatMap((canvas) => [...ownFiles(root, canvas)]))],
|
|
66
|
+
...(outdir ? { outdir } : {}),
|
|
67
|
+
target: "browser",
|
|
68
|
+
format: "esm",
|
|
69
|
+
splitting: true,
|
|
70
|
+
metafile: true,
|
|
71
|
+
throw: false,
|
|
72
|
+
// Frames load modules from blob URLs, so relative preload links cannot resolve.
|
|
73
|
+
modulePreload: false,
|
|
74
|
+
define: { "process.env.NODE_ENV": '"production"' },
|
|
75
|
+
naming: { entry: "[name]-[hash].[ext]", chunk: "chunk-[hash].[ext]", asset: "[name]-[hash].[ext]" },
|
|
76
|
+
loader,
|
|
77
|
+
plugins: [projectReact(root)],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
type BuildLogs = Bun.BuildOutput["logs"];
|
|
81
|
+
|
|
82
|
+
const errorFile = (root: string, log: BuildLogs[number]) =>
|
|
83
|
+
log.level === "error" && log.position?.file ? resolve(root, log.position.file) : undefined;
|
|
84
|
+
|
|
85
|
+
const describe = (root: string, logs: BuildLogs, files?: Set<string>) =>
|
|
86
|
+
logs
|
|
87
|
+
.filter((log) => log.level === "error" && (!files || files.has(errorFile(root, log) ?? "")))
|
|
88
|
+
.map((log) => String(log))
|
|
89
|
+
.join("\n") || "The canvas could not be built.";
|
|
90
|
+
|
|
91
|
+
const failedFiles = (root: string, logs: BuildLogs) => new Set(logs.flatMap((log) => errorFile(root, log) ?? []));
|
|
92
|
+
|
|
93
|
+
type WriteEntry = (name: string, canvases: CanvasEntry[]) => Promise<string>;
|
|
94
|
+
|
|
95
|
+
// The broken import is shared code, so build each canvas alone to find the ones that use it.
|
|
96
|
+
const isolateFailures = async (root: string, write: WriteEntry, canvases: CanvasEntry[]) => {
|
|
97
|
+
const failures = new Map<CanvasEntry, BuildLogs>();
|
|
98
|
+
for (const canvas of canvases) {
|
|
99
|
+
const alone = await runBuild(root, await write("isolated", [canvas]), [canvas]);
|
|
100
|
+
if (!alone.success) failures.set(canvas, alone.logs);
|
|
101
|
+
}
|
|
102
|
+
return failures;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Builds every canvas into `outdir`. A canvas that cannot be built is left out with its error, so
|
|
107
|
+
* one broken import never hides the other canvases.
|
|
108
|
+
*/
|
|
109
|
+
export const bundleCanvases = async (root: string, workdir: string, outdir: string, canvases: CanvasEntry[]) => {
|
|
110
|
+
const entries = join(workdir, "entries");
|
|
111
|
+
await mkdir(entries, { recursive: true });
|
|
112
|
+
const write: WriteEntry = async (name, included) => {
|
|
113
|
+
const path = join(entries, `${name}.tsx`);
|
|
114
|
+
await writeFile(path, entrySource(root, included));
|
|
115
|
+
return path;
|
|
116
|
+
};
|
|
117
|
+
const errors = new Map<string, string>();
|
|
118
|
+
// Files that broke a build are inputs too: fixing one must trigger a rebuild.
|
|
119
|
+
const failed = new Set<string>();
|
|
120
|
+
let included = canvases;
|
|
121
|
+
for (;;) {
|
|
122
|
+
const result = await runBuild(root, await write("frame", included), included, outdir);
|
|
123
|
+
if (result.success) return { result, errors, failedFiles: failed };
|
|
124
|
+
const files = failedFiles(root, result.logs);
|
|
125
|
+
let failures = new Map(
|
|
126
|
+
included
|
|
127
|
+
.filter((canvas) => [...ownFiles(root, canvas)].some((file) => files.has(file)))
|
|
128
|
+
.map((canvas) => [canvas, result.logs] as const),
|
|
129
|
+
);
|
|
130
|
+
if (!failures.size) failures = await isolateFailures(root, write, included);
|
|
131
|
+
if (!failures.size) throw new Error(describe(root, result.logs));
|
|
132
|
+
for (const [canvas, logs] of failures) {
|
|
133
|
+
for (const file of failedFiles(root, logs)) failed.add(file);
|
|
134
|
+
// A canvas's own broken import names its own file; a shared one is the only error in its build.
|
|
135
|
+
const own = logs === result.logs ? ownFiles(root, canvas) : undefined;
|
|
136
|
+
errors.set(canvas.id, describe(root, logs, own));
|
|
137
|
+
}
|
|
138
|
+
included = included.filter((canvas) => !failures.has(canvas));
|
|
139
|
+
}
|
|
140
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import { buildCanvases, resolveOut, resolveRoot } from "./build";
|
|
3
|
+
import { discoverCanvases } from "./discover";
|
|
4
|
+
import type { CanvasBuildManifest } from "./manifest";
|
|
5
|
+
import { watchCanvases } from "./watch";
|
|
6
|
+
|
|
7
|
+
const USAGE = `Usage: pstdio-canvas <command> [--root <dir>] [--out <dir>]
|
|
8
|
+
|
|
9
|
+
Commands:
|
|
10
|
+
watch Build the project's canvases, then rebuild whenever their files change.
|
|
11
|
+
build Build the project's canvases once. Add --force to build even when nothing changed.
|
|
12
|
+
list Print the project's canvases as JSON.
|
|
13
|
+
|
|
14
|
+
--root defaults to the current folder. --out defaults to .pstdio/extension-storage/pstdio.canvas/build,
|
|
15
|
+
the folder UI Canvas reads.`;
|
|
16
|
+
|
|
17
|
+
const option = (args: string[], name: string) => {
|
|
18
|
+
const index = args.indexOf(`--${name}`);
|
|
19
|
+
return index < 0 ? undefined : args[index + 1];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const report = (manifest: CanvasBuildManifest, built: boolean, out: string) => {
|
|
23
|
+
const failed = manifest.canvases.filter((canvas) => canvas.error);
|
|
24
|
+
for (const canvas of failed) console.error(`✗ ${canvas.id}\n${canvas.error}\n`);
|
|
25
|
+
const summary = `${manifest.canvases.length} canvases, ${failed.length} failed`;
|
|
26
|
+
console.log(built ? `Built ${summary} into ${out}.` : `Canvases are current (${summary}).`);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Runs the `pstdio-canvas` command and returns its exit code. */
|
|
30
|
+
export const runCli = async (args: string[]) => {
|
|
31
|
+
const [command] = args;
|
|
32
|
+
const root = resolveRoot(option(args, "root") ?? process.cwd());
|
|
33
|
+
const out = option(args, "out");
|
|
34
|
+
switch (command) {
|
|
35
|
+
case "list":
|
|
36
|
+
console.log(JSON.stringify(await discoverCanvases(root), null, 2));
|
|
37
|
+
return 0;
|
|
38
|
+
case "build": {
|
|
39
|
+
// Failed canvases are reported in the manifest; the build itself still succeeded.
|
|
40
|
+
const { built, manifest } = await buildCanvases({ root, out, force: args.includes("--force") });
|
|
41
|
+
report(manifest, built, relative(root, resolveOut(root, out)) || ".");
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
case "watch": {
|
|
45
|
+
const watcher = watchCanvases({ root, out });
|
|
46
|
+
console.log(`Watching ${root} for canvas changes. Press Ctrl+C to stop.`);
|
|
47
|
+
await new Promise((done) => {
|
|
48
|
+
process.once("SIGINT", done);
|
|
49
|
+
process.once("SIGTERM", done);
|
|
50
|
+
});
|
|
51
|
+
watcher.close();
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
console.log(USAGE);
|
|
56
|
+
return command && command !== "--help" ? 1 : 0;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { indexCanvasFile } from "./index-canvas-file";
|
|
5
|
+
import type { CanvasEntry } from "./manifest";
|
|
6
|
+
|
|
7
|
+
const CANVAS_FILE = /\.canvas\.[jt]sx$/;
|
|
8
|
+
const SKIPPED_FOLDERS = new Set(["node_modules", "dist", ".pstdio", ".git"]);
|
|
9
|
+
const toPosix = (path: string) => path.split(sep).join("/");
|
|
10
|
+
|
|
11
|
+
const listWithGit = (root: string) => {
|
|
12
|
+
const result = Bun.spawnSync(
|
|
13
|
+
["git", "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "*.canvas.tsx", "*.canvas.jsx"],
|
|
14
|
+
{ cwd: root, stdout: "pipe", stderr: "pipe" },
|
|
15
|
+
);
|
|
16
|
+
if (result.exitCode !== 0) return;
|
|
17
|
+
// `--cached` still lists files deleted from the working tree but not from the index.
|
|
18
|
+
return result.stdout
|
|
19
|
+
.toString()
|
|
20
|
+
.split("\0")
|
|
21
|
+
.filter((path) => path && existsSync(join(root, path)));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Skipped folders are never entered: polling must not walk installed packages or build output.
|
|
25
|
+
const scan = async (root: string, folder = ""): Promise<string[]> => {
|
|
26
|
+
const files: string[] = [];
|
|
27
|
+
for (const entry of await readdir(join(root, folder), { withFileTypes: true })) {
|
|
28
|
+
const path = folder ? `${folder}/${entry.name}` : entry.name;
|
|
29
|
+
if (entry.isDirectory() && !SKIPPED_FOLDERS.has(entry.name)) files.push(...(await scan(root, path)));
|
|
30
|
+
else if (entry.isFile() && CANVAS_FILE.test(entry.name)) files.push(path);
|
|
31
|
+
}
|
|
32
|
+
return files;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Canvas files relative to the project root. Git-ignored files are skipped. */
|
|
36
|
+
export const listCanvasFiles = async (root: string) =>
|
|
37
|
+
(listWithGit(root) ?? (await scan(root))).filter((path) => CANVAS_FILE.test(path)).sort();
|
|
38
|
+
|
|
39
|
+
/** The nearest `.canvas/preview.tsx` at or above the file's folder, relative to the project root. */
|
|
40
|
+
export const createPreviewFinder = (root: string) => {
|
|
41
|
+
const folders = new Map<string, string | undefined>();
|
|
42
|
+
const find = (folder: string): string | undefined => {
|
|
43
|
+
if (folders.has(folder)) return folders.get(folder);
|
|
44
|
+
const candidate = join(folder, ".canvas", "preview.tsx");
|
|
45
|
+
const parent = dirname(folder);
|
|
46
|
+
let found: string | undefined;
|
|
47
|
+
if (existsSync(candidate)) found = toPosix(relative(root, candidate));
|
|
48
|
+
else if (folder !== root && parent !== folder) found = find(parent);
|
|
49
|
+
folders.set(folder, found);
|
|
50
|
+
return found;
|
|
51
|
+
};
|
|
52
|
+
return (file: string) => find(dirname(resolve(root, file)));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const discoverCanvases = async (root: string): Promise<CanvasEntry[]> => {
|
|
56
|
+
const findPreview = createPreviewFinder(root);
|
|
57
|
+
return Promise.all(
|
|
58
|
+
(await listCanvasFiles(root)).map(async (id) => ({
|
|
59
|
+
id,
|
|
60
|
+
...indexCanvasFile(id, await readFile(join(root, id), "utf8")),
|
|
61
|
+
preview: findPreview(id),
|
|
62
|
+
})),
|
|
63
|
+
);
|
|
64
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
// Reads a canvas file without running it, so listing never executes project code.
|
|
4
|
+
|
|
5
|
+
interface SourceIndex {
|
|
6
|
+
file: ts.SourceFile;
|
|
7
|
+
declarations: Map<string, ts.Expression>;
|
|
8
|
+
functions: Set<string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const sentenceCase = (name: string) => {
|
|
12
|
+
const words = name
|
|
13
|
+
.replace(/([a-z\d])([A-Z])/g, "$1 $2")
|
|
14
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
15
|
+
.split(/[\s_]+/)
|
|
16
|
+
.filter(Boolean);
|
|
17
|
+
return words.map((word, index) => (index === 0 ? word : word.toLowerCase())).join(" ");
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const unwrap = (node: ts.Expression): ts.Expression =>
|
|
21
|
+
ts.isSatisfiesExpression(node) || ts.isAsExpression(node) || ts.isParenthesizedExpression(node)
|
|
22
|
+
? unwrap(node.expression)
|
|
23
|
+
: node;
|
|
24
|
+
|
|
25
|
+
const readSource = (path: string, source: string): SourceIndex => {
|
|
26
|
+
const file = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
27
|
+
const declarations = new Map<string, ts.Expression>();
|
|
28
|
+
const functions = new Set<string>();
|
|
29
|
+
for (const statement of file.statements) {
|
|
30
|
+
if (ts.isFunctionDeclaration(statement) && statement.name) functions.add(statement.name.text);
|
|
31
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
32
|
+
for (const declaration of statement.declarationList.declarations)
|
|
33
|
+
if (ts.isIdentifier(declaration.name) && declaration.initializer)
|
|
34
|
+
declarations.set(declaration.name.text, declaration.initializer);
|
|
35
|
+
}
|
|
36
|
+
return { file, declarations, functions };
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const resolveValue = (index: SourceIndex, node?: ts.Expression): ts.Expression | undefined => {
|
|
40
|
+
if (!node) return;
|
|
41
|
+
const value = unwrap(node);
|
|
42
|
+
if (ts.isIdentifier(value) && index.declarations.has(value.text))
|
|
43
|
+
return resolveValue(index, index.declarations.get(value.text));
|
|
44
|
+
return value;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const property = (index: SourceIndex, node: ts.Expression | undefined, key: string) => {
|
|
48
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return;
|
|
49
|
+
const found = node.properties.find(
|
|
50
|
+
(entry): entry is ts.PropertyAssignment => ts.isPropertyAssignment(entry) && entry.name.getText(index.file) === key,
|
|
51
|
+
);
|
|
52
|
+
return found && unwrap(found.initializer);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const text = (node?: ts.Expression) =>
|
|
56
|
+
node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) ? node.text : undefined;
|
|
57
|
+
const flag = (node?: ts.Expression) => node?.kind === ts.SyntaxKind.TrueKeyword || undefined;
|
|
58
|
+
|
|
59
|
+
// Only components and frame objects are frames; other exported values are helpers.
|
|
60
|
+
const isFrameValue = (value?: ts.Expression) =>
|
|
61
|
+
Boolean(
|
|
62
|
+
value && (ts.isArrowFunction(value) || ts.isFunctionExpression(value) || ts.isObjectLiteralExpression(value)),
|
|
63
|
+
);
|
|
64
|
+
const frameObject = (value?: ts.Expression) => (value && ts.isObjectLiteralExpression(value) ? value : undefined);
|
|
65
|
+
|
|
66
|
+
const isExported = (statement: ts.Statement) =>
|
|
67
|
+
ts.canHaveModifiers(statement) &&
|
|
68
|
+
Boolean(ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword));
|
|
69
|
+
const isDefaultExport = (statement: ts.Statement) =>
|
|
70
|
+
ts.canHaveModifiers(statement) &&
|
|
71
|
+
Boolean(ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword));
|
|
72
|
+
|
|
73
|
+
type FrameExport = { exportName: string; value?: ts.ObjectLiteralExpression };
|
|
74
|
+
|
|
75
|
+
// `export { Primary, Loading as Busy }`
|
|
76
|
+
const listedFrames = (index: SourceIndex, clause: ts.NamedExports): FrameExport[] =>
|
|
77
|
+
clause.elements.flatMap((element) => {
|
|
78
|
+
const local = (element.propertyName ?? element.name).getText(index.file);
|
|
79
|
+
const exportName = element.name.getText(index.file);
|
|
80
|
+
const value = resolveValue(index, index.declarations.get(local));
|
|
81
|
+
const isFrame = exportName !== "default" && (index.functions.has(local) || isFrameValue(value));
|
|
82
|
+
return isFrame ? [{ exportName, value: frameObject(value) }] : [];
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// `export const Primary = { ... }` and `export function Loading() { ... }`
|
|
86
|
+
const declaredFrames = (index: SourceIndex, statement: ts.Statement): FrameExport[] => {
|
|
87
|
+
if (!isExported(statement)) return [];
|
|
88
|
+
if (ts.isFunctionDeclaration(statement))
|
|
89
|
+
return statement.name && !isDefaultExport(statement) ? [{ exportName: statement.name.text }] : [];
|
|
90
|
+
if (!ts.isVariableStatement(statement)) return [];
|
|
91
|
+
return statement.declarationList.declarations.flatMap((declaration) => {
|
|
92
|
+
const value = resolveValue(index, declaration.initializer);
|
|
93
|
+
return ts.isIdentifier(declaration.name) && isFrameValue(value)
|
|
94
|
+
? [{ exportName: declaration.name.text, value: frameObject(value) }]
|
|
95
|
+
: [];
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Named exports that are frames, in source order, with their object literal when they have one. */
|
|
100
|
+
const frameExports = (index: SourceIndex) =>
|
|
101
|
+
index.file.statements.flatMap((statement) => {
|
|
102
|
+
if (!ts.isExportDeclaration(statement)) return declaredFrames(index, statement);
|
|
103
|
+
const clause = statement.exportClause;
|
|
104
|
+
return !statement.moduleSpecifier && clause && ts.isNamedExports(clause) ? listedFrames(index, clause) : [];
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
export const indexCanvasFile = (path: string, source: string) => {
|
|
108
|
+
const index = readSource(path, source);
|
|
109
|
+
const meta = resolveValue(index, index.file.statements.find(ts.isExportAssignment)?.expression);
|
|
110
|
+
const metaFullscreen = flag(property(index, meta, "fullscreen"));
|
|
111
|
+
const frames = frameExports(index).map(({ exportName, value }) => ({
|
|
112
|
+
exportName,
|
|
113
|
+
name: text(property(index, value, "name")) ?? sentenceCase(exportName),
|
|
114
|
+
description: text(property(index, value, "description")),
|
|
115
|
+
fullscreen: flag(property(index, value, "fullscreen")) ?? metaFullscreen,
|
|
116
|
+
}));
|
|
117
|
+
const fileName = path
|
|
118
|
+
.split("/")
|
|
119
|
+
.at(-1)!
|
|
120
|
+
.replace(/\.canvas\.[jt]sx$/, "");
|
|
121
|
+
const title = text(property(index, meta, "title")) ?? fileName;
|
|
122
|
+
const separator = title.lastIndexOf("/");
|
|
123
|
+
return {
|
|
124
|
+
title,
|
|
125
|
+
group: separator < 0 ? "" : title.slice(0, separator),
|
|
126
|
+
name: title.slice(separator + 1),
|
|
127
|
+
description: text(property(index, meta, "description")),
|
|
128
|
+
frames,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import type { CanvasEntry } from "./manifest";
|
|
5
|
+
|
|
6
|
+
const PROJECT_FILES = ["package.json", "bun.lock", "bun.lockb", "package-lock.json", "yarn.lock", "pnpm-lock.yaml"];
|
|
7
|
+
const toPosix = (path: string) => path.split(sep).join("/");
|
|
8
|
+
const inNodeModules = (path: string) => toPosix(path).split("/").includes("node_modules");
|
|
9
|
+
|
|
10
|
+
/** Files inside the root are stored relative to it; others, such as sibling workspace packages, absolutely. */
|
|
11
|
+
export const inputKey = (root: string, file: string) => {
|
|
12
|
+
const path = toPosix(relative(root, file));
|
|
13
|
+
return path.startsWith("../") || isAbsolute(path) ? toPosix(file) : path;
|
|
14
|
+
};
|
|
15
|
+
export const inputPath = (root: string, key: string) => (isAbsolute(key) ? key : join(root, key));
|
|
16
|
+
|
|
17
|
+
// `.../node_modules/@scope/name/dist/index.js` belongs to the package at `.../node_modules/@scope/name`.
|
|
18
|
+
const packageFolder = (file: string) => {
|
|
19
|
+
const parts = toPosix(file).split("/");
|
|
20
|
+
const index = parts.lastIndexOf("node_modules");
|
|
21
|
+
return parts.slice(0, index + (parts[index + 1]?.startsWith("@") ? 3 : 2)).join("/");
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Installing a package adds an entry to a `node_modules` folder the project's files resolve from.
|
|
25
|
+
const nodeModulesFolders = (root: string, sources: string[]) => {
|
|
26
|
+
const folders = new Set<string>();
|
|
27
|
+
const visited = new Set<string>();
|
|
28
|
+
for (const source of sources) {
|
|
29
|
+
for (let folder = dirname(source); !visited.has(folder); folder = dirname(folder)) {
|
|
30
|
+
visited.add(folder);
|
|
31
|
+
if (existsSync(join(folder, "node_modules"))) folders.add(join(folder, "node_modules"));
|
|
32
|
+
if (folder === root || dirname(folder) === folder) break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return folders;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Every file whose change can change the build: the sources it read, wherever they live, files that
|
|
40
|
+
* broke it, the project's package manifests and lockfiles, the manifests of the packages it
|
|
41
|
+
* resolved, and the `node_modules` folders new packages are installed into.
|
|
42
|
+
*/
|
|
43
|
+
export const collectInputs = (root: string, read: string[], canvases: CanvasEntry[], failedFiles: Set<string>) => {
|
|
44
|
+
const sources = [
|
|
45
|
+
...read.filter((file) => !inNodeModules(file)),
|
|
46
|
+
...canvases.flatMap((canvas) => [join(root, canvas.id), ...(canvas.preview ? [join(root, canvas.preview)] : [])]),
|
|
47
|
+
...failedFiles,
|
|
48
|
+
];
|
|
49
|
+
const packages = read.filter(inNodeModules).map((file) => join(packageFolder(file), "package.json"));
|
|
50
|
+
const projectFiles = PROJECT_FILES.map((name) => join(root, name)).filter((file) => existsSync(file));
|
|
51
|
+
return new Set(
|
|
52
|
+
[...sources, ...projectFiles, ...packages, ...nodeModulesFolders(root, sources)].map((file) => resolve(file)),
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Modification times keyed by `inputKey`. A file changed at or after `startedAt` may have been read
|
|
58
|
+
* before or after the change, so it is recorded as unknown and the next check rebuilds.
|
|
59
|
+
*/
|
|
60
|
+
export const readInputs = async (root: string, files: Set<string>, startedAt: number) => {
|
|
61
|
+
const inputs: Record<string, number> = {};
|
|
62
|
+
for (const file of [...files].sort()) {
|
|
63
|
+
const info = await stat(file).catch(() => undefined);
|
|
64
|
+
if (info) inputs[inputKey(root, file)] = info.mtimeMs >= startedAt ? -1 : info.mtimeMs;
|
|
65
|
+
}
|
|
66
|
+
return inputs;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** True when every recorded input still has the recorded modification time. */
|
|
70
|
+
export const inputsUnchanged = async (root: string, inputs: Record<string, number>) => {
|
|
71
|
+
for (const [key, modified] of Object.entries(inputs)) {
|
|
72
|
+
const info = await stat(inputPath(root, key)).catch(() => undefined);
|
|
73
|
+
if (info?.mtimeMs !== modified) return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { open, readFile, rm, stat } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
const isAlive = (pid: number) => {
|
|
5
|
+
try {
|
|
6
|
+
process.kill(pid, 0);
|
|
7
|
+
return true;
|
|
8
|
+
} catch (error) {
|
|
9
|
+
// EPERM: the process exists but belongs to another user.
|
|
10
|
+
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Lets one build at a time use an output folder: two builders would delete each other's modules.
|
|
16
|
+
* A lock whose process has exited is stale and taken over. A second builder waits for the first.
|
|
17
|
+
*/
|
|
18
|
+
export const lockOutput = async (out: string, timeoutMs = 120_000) => {
|
|
19
|
+
const path = join(out, "build.lock");
|
|
20
|
+
const deadline = Date.now() + timeoutMs;
|
|
21
|
+
for (;;) {
|
|
22
|
+
try {
|
|
23
|
+
const handle = await open(path, "wx");
|
|
24
|
+
await handle.writeFile(String(process.pid));
|
|
25
|
+
await handle.close();
|
|
26
|
+
return { release: () => rm(path, { force: true }) };
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
29
|
+
}
|
|
30
|
+
const content = await readFile(path, "utf8").catch(() => "");
|
|
31
|
+
const holder = Number(content);
|
|
32
|
+
// An empty lock is still being written, unless its writer died before writing its process id.
|
|
33
|
+
const abandoned = !holder && Date.now() - ((await stat(path).catch(() => undefined))?.mtimeMs ?? 0) > 5_000;
|
|
34
|
+
if ((holder && !isAlive(holder)) || abandoned) {
|
|
35
|
+
if ((await readFile(path, "utf8").catch(() => "")) === content) await rm(path, { force: true });
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (Date.now() > deadline)
|
|
39
|
+
throw new Error(`Another pstdio-canvas build (process ${holder}) is still using ${out}. Try again when it ends.`);
|
|
40
|
+
await Bun.sleep(100);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// The contract between `pstdio-canvas` and the UI Canvas extension. Bump the format when a
|
|
2
|
+
// reader could misread an older or newer manifest.
|
|
3
|
+
export const MANIFEST_FORMAT = 1;
|
|
4
|
+
|
|
5
|
+
// The folder UI Canvas reads, relative to the project root.
|
|
6
|
+
export const DEFAULT_OUT = ".pstdio/extension-storage/pstdio.canvas/build";
|
|
7
|
+
|
|
8
|
+
export interface FrameManifest {
|
|
9
|
+
entry: string;
|
|
10
|
+
catalog: string;
|
|
11
|
+
sources: Record<string, { script: string; project?: string }>;
|
|
12
|
+
modules: Record<string, { file: string; imports: string[]; dynamicImports: string[]; bytes: number; css?: string[] }>;
|
|
13
|
+
styles: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CanvasFrameEntry {
|
|
17
|
+
exportName: string;
|
|
18
|
+
name: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
fullscreen?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CanvasEntry {
|
|
24
|
+
/** The canvas file path relative to the project root. */
|
|
25
|
+
id: string;
|
|
26
|
+
title: string;
|
|
27
|
+
/** The title before its last `/`; empty for a title without one. */
|
|
28
|
+
group: string;
|
|
29
|
+
name: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
/** The nearest `.canvas/preview.tsx`, relative to the project root. */
|
|
32
|
+
preview?: string;
|
|
33
|
+
frames: CanvasFrameEntry[];
|
|
34
|
+
/** Why this canvas could not be built. Its frames are not in the module graph. */
|
|
35
|
+
error?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CanvasBuildManifest {
|
|
39
|
+
format: typeof MANIFEST_FORMAT;
|
|
40
|
+
builtAt: string;
|
|
41
|
+
canvases: CanvasEntry[];
|
|
42
|
+
frames: FrameManifest;
|
|
43
|
+
/** Source files the build read, relative to the project root, with their modification times. */
|
|
44
|
+
inputs: Record<string, number>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { type CanvasBuildOptions, isCurrent, readManifest, resolveOut, resolveRoot } from "./build";
|
|
4
|
+
import { listCanvasFiles } from "./discover";
|
|
5
|
+
import { inputPath } from "./inputs";
|
|
6
|
+
import type { CanvasBuildManifest } from "./manifest";
|
|
7
|
+
|
|
8
|
+
const CLI = resolve(import.meta.dir, "../../bin/pstdio-canvas.ts");
|
|
9
|
+
|
|
10
|
+
export interface CanvasWatchOptions extends Omit<CanvasBuildOptions, "force"> {
|
|
11
|
+
/** How often to check the last build's files for changes. */
|
|
12
|
+
intervalMs?: number;
|
|
13
|
+
/** Called after each build with the build's exit code. */
|
|
14
|
+
onBuild?: (exitCode: number) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The files a build depends on, with their modification times. A failed build is retried only
|
|
18
|
+
// after this changes, so a broken setup never rebuilds in a loop.
|
|
19
|
+
const inputState = async (root: string, manifest: CanvasBuildManifest | undefined) => {
|
|
20
|
+
const files = [
|
|
21
|
+
...(await listCanvasFiles(root)).map((file) => join(root, file)),
|
|
22
|
+
join(root, "package.json"),
|
|
23
|
+
...Object.keys(manifest?.inputs ?? {}).map((key) => inputPath(root, key)),
|
|
24
|
+
];
|
|
25
|
+
const times = await Promise.all(files.map(async (file) => (await stat(file).catch(() => undefined))?.mtimeMs));
|
|
26
|
+
return JSON.stringify(files.map((file, index) => [file, times[index]]));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Rebuilds the canvases whenever a canvas file or an input of the last build changes.
|
|
31
|
+
*
|
|
32
|
+
* It checks the last build's own inputs instead of registering a recursive `fs.watch`: on Linux
|
|
33
|
+
* that walks every folder, including `node_modules` symlinks (lesson 0012). Each build runs in a
|
|
34
|
+
* new Bun process, because a long-running process keeps package resolutions it has already read
|
|
35
|
+
* and would miss newly installed packages.
|
|
36
|
+
*/
|
|
37
|
+
export const watchCanvases = (options: CanvasWatchOptions) => {
|
|
38
|
+
const root = resolveRoot(options.root);
|
|
39
|
+
const out = resolveOut(root, options.out);
|
|
40
|
+
let stopped = false;
|
|
41
|
+
let failedState: string | undefined;
|
|
42
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
43
|
+
let child: ReturnType<typeof Bun.spawn> | undefined;
|
|
44
|
+
const build = async () => {
|
|
45
|
+
child = Bun.spawn([process.execPath, CLI, "build", "--root", root, "--out", out], {
|
|
46
|
+
stdout: "inherit",
|
|
47
|
+
stderr: "inherit",
|
|
48
|
+
});
|
|
49
|
+
const exitCode = await child.exited;
|
|
50
|
+
child = undefined;
|
|
51
|
+
options.onBuild?.(exitCode);
|
|
52
|
+
return exitCode;
|
|
53
|
+
};
|
|
54
|
+
const check = async () => {
|
|
55
|
+
const manifest = await readManifest(out);
|
|
56
|
+
if (!stopped && !(manifest && (await isCurrent(root, manifest)))) {
|
|
57
|
+
const state = await inputState(root, manifest);
|
|
58
|
+
if (state !== failedState) failedState = (await build()) === 0 ? undefined : state;
|
|
59
|
+
}
|
|
60
|
+
if (!stopped) timer = setTimeout(check, options.intervalMs ?? 500);
|
|
61
|
+
};
|
|
62
|
+
const started = check();
|
|
63
|
+
return {
|
|
64
|
+
started,
|
|
65
|
+
close: () => {
|
|
66
|
+
stopped = true;
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
child?.kill();
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
};
|