-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.mjs
More file actions
183 lines (151 loc) · 4.12 KB
/
Copy pathvalidate.mjs
File metadata and controls
183 lines (151 loc) · 4.12 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import fs from "node:fs";
import path from "node:path";
const ROOT = process.cwd();
const JSON_DIRS = ["agents", "automations", "projects"];
const APP_DIR = "apps";
const errors = [];
function fail(message) {
errors.push(message);
}
function assert(condition, message) {
if (!condition) {
fail(message);
}
}
function readJson(filePath) {
try {
const raw = fs.readFileSync(filePath, "utf8");
return JSON.parse(raw);
} catch (error) {
fail(`Invalid JSON: ${path.relative(ROOT, filePath)} (${String(error)})`);
return null;
}
}
function listJsonFiles(dirName) {
const dirPath = path.join(ROOT, dirName);
if (!fs.existsSync(dirPath)) {
fail(`Missing directory: ${dirName}`);
return [];
}
const files = fs
.readdirSync(dirPath, { withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
.map((entry) => path.join(dirPath, entry.name));
if (files.length === 0) {
fail(`Directory has no JSON files: ${dirName}`);
}
return files;
}
function listAppDirs(dirName) {
const dirPath = path.join(ROOT, dirName);
if (!fs.existsSync(dirPath)) {
fail(`Missing directory: ${dirName}`);
return [];
}
const dirs = fs
.readdirSync(dirPath, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(dirPath, entry.name));
if (dirs.length === 0) {
fail(`Directory has no app subdirectories: ${dirName}`);
}
return dirs;
}
function validateManifest() {
const filePath = path.join(ROOT, "manifest.json");
if (!fs.existsSync(filePath)) {
fail("Missing manifest.json");
return;
}
const manifest = readJson(filePath);
if (manifest == null) {
return;
}
assert(
typeof manifest.version === "string",
"manifest.version must be a string",
);
assert(typeof manifest.name === "string", "manifest.name must be a string");
assert(
typeof manifest.spaceId === "string",
"manifest.spaceId must be a string",
);
}
function validateAgents() {
for (const filePath of listJsonFiles("agents")) {
const data = readJson(filePath);
if (data == null) {
continue;
}
const rel = path.relative(ROOT, filePath);
assert(
typeof data.version === "string",
`${rel}: version must be a string`,
);
assert(typeof data.name === "string", `${rel}: name must be a string`);
assert(Array.isArray(data.commands), `${rel}: commands must be an array`);
}
}
function validateApps() {
for (const appDir of listAppDirs(APP_DIR)) {
const pkgPath = path.join(appDir, "package.json");
const rel = path.relative(ROOT, appDir);
if (!fs.existsSync(pkgPath)) {
fail(`${rel}: missing package.json`);
continue;
}
const data = readJson(pkgPath);
if (data == null) {
continue;
}
assert(typeof data.name === "string", `${rel}/package.json: name must be a string`);
}
}
function validateAutomations() {
for (const filePath of listJsonFiles("automations")) {
const data = readJson(filePath);
if (data == null) {
continue;
}
const rel = path.relative(ROOT, filePath);
assert(
typeof data.flowTitle === "string",
`${rel}: flowTitle must be a string`,
);
assert(
typeof data.trigger === "object" && data.trigger != null,
`${rel}: trigger object is required`,
);
assert(Array.isArray(data.actions), `${rel}: actions must be an array`);
}
}
function validateProjects() {
for (const filePath of listJsonFiles("projects")) {
const data = readJson(filePath);
if (data == null) {
continue;
}
const rel = path.relative(ROOT, filePath);
assert(
typeof data.root === "object" && data.root != null,
`${rel}: root object is required`,
);
}
}
validateManifest();
for (const dirName of JSON_DIRS) {
listJsonFiles(dirName);
}
listAppDirs(APP_DIR);
validateAgents();
validateApps();
validateAutomations();
validateProjects();
if (errors.length > 0) {
console.error("Bundle validation failed:\n");
for (const message of errors) {
console.error(`- ${message}`);
}
process.exit(1);
}
console.log("Bundle validation passed.");