Files
anidev-design-system/scripts/build-package.mjs
T

164 lines
5.0 KiB
JavaScript

import {
cp,
mkdir,
readFile,
writeFile,
readdir,
lstat,
rm,
} from "node:fs/promises";
import { fileURLToPath } from "node:url";
import path from "node:path";
import { createHash } from "node:crypto";
const root = path.resolve(fileURLToPath(new URL("../", import.meta.url)));
const dist = path.join(root, "dist");
// Only clear named generated children of this repository's dist. Never follow links.
await mkdir(dist, { recursive: true });
if ((await lstat(dist)).isSymbolicLink())
throw Error("dist must not be a symlink");
async function reset(name) {
const target = path.resolve(dist, name);
if (path.dirname(target) !== dist) throw Error("Invalid build target");
const stat = await lstat(target).catch((e) => {
if (e.code !== "ENOENT") throw e;
});
if (stat?.isSymbolicLink()) throw Error("Build target must not be a symlink");
await rm(target, { recursive: true, force: true });
await mkdir(target, { recursive: true });
return target;
}
const pkg = await reset("package"),
skills = await reset("skills"),
starter = await reset("starter");
const { version } = JSON.parse(
await readFile(path.join(root, "package.json"), "utf8"),
);
async function copy(from, to) {
await mkdir(path.dirname(to), { recursive: true });
await cp(path.join(root, from), to, { recursive: true });
}
for (const file of [
"runtime/index.js",
"components/system.css",
"components/workspaces.css",
"components/toast.js",
"components/search-select.js",
"components/copy-field.js",
"components/dock.js",
"components/navigation.js",
"components/account-menu.js",
"components/theme-control.js",
"components/account-submenu.js",
"components/error-page.js",
"components/ssh-console.js",
"components/log-reader.js",
"patterns/demo-pty.js",
"concepts/registry.js",
"tokens/dock",
"tokens/schema.json",
"vendor/xterm",
"icons/icons.js",
"icons/manifest.json",
"icons/sprite.svg",
"icons/svg",
])
await copy(file, path.join(pkg, file));
for (const file of [
"DESIGN.md",
"ANTI-SLOP.md",
"components/CONTRACT.md",
"patterns/UX.md",
"patterns/WORKSPACES.md",
"patterns/IDENTITY.md",
"prompts/SSO.md",
])
await copy(file, path.join(pkg, "docs", file));
await copy("docs/INTEGRATION.md", path.join(pkg, "docs/INTEGRATION.md"));
const integration = await readFile(
path.join(root, "docs/INTEGRATION.md"),
"utf8",
);
await writeFile(
path.join(pkg, "README.md"),
integration
.replaceAll("(PUBLIC-API.md)", "(docs/PUBLIC-API.md)")
.replaceAll("(BACKEND-INTEGRATION.md)", "(docs/BACKEND-INTEGRATION.md)")
.replaceAll("(../NOTICE.md)", "(NOTICE.md)"),
);
const publicApi = await readFile(path.join(root, "docs/PUBLIC-API.md"), "utf8");
await writeFile(
path.join(pkg, "docs/PUBLIC-API.md"),
publicApi
.replaceAll("(../components/", "(components/")
.replaceAll("(../patterns/", "(patterns/"),
);
await copy("docs/PANEL-API.md", path.join(pkg, "docs/PANEL-API.md"));
await copy(
"docs/BACKEND-INTEGRATION.md",
path.join(pkg, "docs/BACKEND-INTEGRATION.md"),
);
await copy("NOTICE.md", path.join(pkg, "NOTICE.md"));
await writeFile(
path.join(pkg, "package.json"),
JSON.stringify(
{
name: "@anidev/design-system",
version,
private: true,
type: "module",
license: "UNLICENSED",
description: "Anidev Dock HTML/CSS/ESM integration preview",
exports: {
".": "./runtime/index.js",
"./styles.css": "./components/system.css",
"./tokens/light.css": "./tokens/dock/light.css",
"./tokens/dark.css": "./tokens/dock/dark.css",
"./tokens/light.json": "./tokens/dock/light.json",
"./tokens/dark.json": "./tokens/dock/dark.json",
"./ssh": "./components/ssh-console.js",
"./logs": "./components/log-reader.js",
},
},
null,
2,
) + "\n",
);
async function hashes(dir, prefix = "") {
const result = {};
for (const entry of (await readdir(dir, { withFileTypes: true })).sort(
(a, b) => a.name.localeCompare(b.name),
)) {
const relative = prefix + entry.name;
if (entry.isDirectory())
Object.assign(
result,
await hashes(path.join(dir, entry.name), relative + "/"),
);
else
result[relative] = createHash("sha256")
.update(await readFile(path.join(dir, entry.name)))
.digest("hex");
}
return result;
}
await writeFile(
path.join(pkg, "manifest.json"),
JSON.stringify(
{ version, channel: "integration-preview", files: await hashes(pkg) },
null,
2,
) + "\n",
);
const skill = path.join(skills, "anidev-design-system");
await copy("skills/anidev-design-system", skill);
await mkdir(path.join(skill, "assets"), { recursive: true });
await cp(pkg, path.join(skill, "assets/anidev"), { recursive: true });
await copy("examples/starter", starter);
await cp(pkg, path.join(starter, "anidev"), { recursive: true });
await mkdir(path.join(skill, "assets/starter"), { recursive: true });
await copy("examples/starter", path.join(skill, "assets/starter"));
console.log(
`Built Anidev ${version}: dist/package, dist/starter, dist/skills/anidev-design-system`,
);