-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathbuild.ts
More file actions
136 lines (125 loc) · 3.89 KB
/
build.ts
File metadata and controls
136 lines (125 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bun
import { execSync } from "node:child_process";
import * as fs from "node:fs";
import * as path from "node:path";
const PACKAGE_DIR = import.meta.dir;
const DIST_DIR = path.join(PACKAGE_DIR, "dist");
const LEGACY_TEMPLATE_SOURCE_DIR = path.resolve(PACKAGE_DIR, "..", "templates");
const TEMPLATE_DIR = path.join(PACKAGE_DIR, "templates");
const MANIFEST_PATH = path.join(PACKAGE_DIR, "templates-manifest.json");
const SKIP_ENTRIES = new Set([
".DS_Store",
".git",
".turbo",
".vite",
"artifacts",
"build",
"coverage",
"dist",
"node_modules",
]);
function copyDir(src: string, dest: string): void {
fs.mkdirSync(dest, { recursive: true });
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
if (SKIP_ENTRIES.has(entry.name)) continue;
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);
if (entry.isDirectory()) {
copyDir(srcPath, destPath);
continue;
}
fs.mkdirSync(path.dirname(destPath), { recursive: true });
fs.copyFileSync(srcPath, destPath);
}
}
function resolveTemplateSourceDir(): string {
if (fs.existsSync(LEGACY_TEMPLATE_SOURCE_DIR)) {
return LEGACY_TEMPLATE_SOURCE_DIR;
}
return TEMPLATE_DIR;
}
function loadTemplateDefinitions(sourceDir: string) {
const templates = [];
for (const entry of fs.readdirSync(sourceDir, {
withFileTypes: true,
})) {
if (!entry.isDirectory()) continue;
const metadataPath = path.join(sourceDir, entry.name, "template.json");
if (!fs.existsSync(metadataPath)) continue;
templates.push(JSON.parse(fs.readFileSync(metadataPath, "utf-8")));
}
templates.sort((a, b) => a.name.localeCompare(b.name));
return templates;
}
function readExistingManifest() {
if (!fs.existsSync(MANIFEST_PATH)) return null;
try {
return JSON.parse(fs.readFileSync(MANIFEST_PATH, "utf-8"));
} catch {
return null;
}
}
function manifestPayloadMatches(
manifest: unknown,
payload: { version: string; repoUrl: string; templates: unknown[] },
): manifest is { generatedAt?: string } {
if (!manifest || typeof manifest !== "object") return false;
const candidate = manifest as {
version?: unknown;
repoUrl?: unknown;
templates?: unknown;
};
return (
candidate.version === payload.version &&
candidate.repoUrl === payload.repoUrl &&
JSON.stringify(candidate.templates) === JSON.stringify(payload.templates)
);
}
function prepareTemplates(): void {
const sourceDir = resolveTemplateSourceDir();
if (path.resolve(sourceDir) !== path.resolve(TEMPLATE_DIR)) {
fs.rmSync(TEMPLATE_DIR, { force: true, recursive: true });
copyDir(sourceDir, TEMPLATE_DIR);
}
const payload = {
version: "1.0.0",
repoUrl: "https://github.com/elizaos/eliza",
templates: loadTemplateDefinitions(TEMPLATE_DIR),
};
const existingManifest = readExistingManifest();
const manifest = {
...payload,
generatedAt:
manifestPayloadMatches(existingManifest, payload) &&
typeof existingManifest.generatedAt === "string"
? existingManifest.generatedAt
: new Date().toISOString(),
};
fs.writeFileSync(MANIFEST_PATH, `${JSON.stringify(manifest, null, 2)}\n`);
execSync(`bunx --bun @biomejs/biome format --write ${MANIFEST_PATH}`, {
cwd: PACKAGE_DIR,
stdio: "inherit",
});
}
function buildTypescript(): void {
if (!fs.existsSync(DIST_DIR)) {
fs.mkdirSync(DIST_DIR, { recursive: true });
}
execSync("bunx --bun tsc -p tsconfig.json", {
cwd: PACKAGE_DIR,
stdio: "inherit",
});
}
function ensureCliShebang(): void {
const cliPath = path.join(DIST_DIR, "cli.js");
if (!fs.existsSync(cliPath)) return;
let content = fs.readFileSync(cliPath, "utf-8");
if (!content.startsWith("#!")) {
content = `#!/usr/bin/env node\n${content}`;
fs.writeFileSync(cliPath, content);
}
fs.chmodSync(cliPath, 0o755);
}
prepareTemplates();
buildTypescript();
ensureCliShebang();