59 lines
2.5 KiB
JavaScript
59 lines
2.5 KiB
JavaScript
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
import { dirname, extname, relative, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const skillRoot = resolve(root, "skills", "seal-use");
|
|
export const embeddedSkillOutput = resolve(root, "src", "core", "embedded-skill.generated.ts");
|
|
const textExtensions = new Set([".md", ".yaml", ".yml", ".json", ".txt"]);
|
|
|
|
function walk(dir) {
|
|
const entries = readdirSync(dir, { withFileTypes: true })
|
|
.filter((entry) => !entry.name.startsWith("."))
|
|
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
const files = [];
|
|
for (const entry of entries) {
|
|
const fullPath = resolve(dir, entry.name);
|
|
if (entry.isDirectory()) files.push(...walk(fullPath));
|
|
else if (entry.isFile()) files.push(fullPath);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
export function renderEmbeddedSkill(sourceDir = skillRoot, coreDir = resolve(root, "src", "core")) {
|
|
const files = walk(sourceDir).map((fullPath) => ({
|
|
relativePath: relative(sourceDir, fullPath).split("\\").join("/"),
|
|
importPath: relative(coreDir, fullPath).split("\\").join("/"),
|
|
kind: textExtensions.has(extname(fullPath).toLowerCase()) ? "text" : "asset"
|
|
}));
|
|
|
|
const lines = [
|
|
"// Generated by scripts/generate-embedded-skill.mjs. Do not edit manually.",
|
|
""
|
|
];
|
|
|
|
for (const [index, file] of files.entries()) {
|
|
const loader = file.kind === "text" ? "text" : "file";
|
|
lines.push(`import file${index} from ${JSON.stringify(file.importPath)} with { type: "${loader}" };`);
|
|
}
|
|
|
|
lines.push("", "export const EMBEDDED_SKILL_TEXT_FILES: Record<string, string> = {");
|
|
for (const [index, file] of files.entries()) {
|
|
if (file.kind === "text") lines.push(` ${JSON.stringify(file.relativePath)}: file${index},`);
|
|
}
|
|
lines.push("};", "", "export const EMBEDDED_SKILL_ASSET_PATHS: Record<string, string> = {");
|
|
for (const [index, file] of files.entries()) {
|
|
if (file.kind === "asset") lines.push(` ${JSON.stringify(file.relativePath)}: file${index},`);
|
|
}
|
|
lines.push("};", "", "export const EMBEDDED_SKILL_ALL_PATHS = [");
|
|
for (const file of files) lines.push(` ${JSON.stringify(file.relativePath)},`);
|
|
lines.push("];", "");
|
|
return lines.join("\n");
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
const content = renderEmbeddedSkill();
|
|
const previous = existsSync(embeddedSkillOutput) ? readFileSync(embeddedSkillOutput, "utf8") : "";
|
|
if (previous !== content) writeFileSync(embeddedSkillOutput, content, "utf8");
|
|
}
|