-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathvalidate-plugins.mjs
More file actions
executable file
·228 lines (198 loc) · 6.53 KB
/
validate-plugins.mjs
File metadata and controls
executable file
·228 lines (198 loc) · 6.53 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import { ROOT_FOLDER } from "./constants.mjs";
const PLUGINS_DIR = path.join(ROOT_FOLDER, "plugins");
// Validation functions
function validateName(name, folderName) {
const errors = [];
if (!name || typeof name !== "string") {
errors.push("name is required and must be a string");
return errors;
}
if (name.length < 1 || name.length > 50) {
errors.push("name must be between 1 and 50 characters");
}
if (!/^[a-z0-9-]+$/.test(name)) {
errors.push("name must contain only lowercase letters, numbers, and hyphens");
}
if (name !== folderName) {
errors.push(`name "${name}" must match folder name "${folderName}"`);
}
return errors;
}
function validateDescription(description) {
if (!description || typeof description !== "string") {
return "description is required and must be a string";
}
if (description.length < 1 || description.length > 500) {
return "description must be between 1 and 500 characters";
}
return null;
}
function validateVersion(version) {
if (!version || typeof version !== "string") {
return "version is required and must be a string";
}
return null;
}
function validateKeywords(keywords) {
if (keywords === undefined) return null;
if (!Array.isArray(keywords)) {
return "keywords must be an array";
}
if (keywords.length > 10) {
return "maximum 10 keywords allowed";
}
for (const keyword of keywords) {
if (typeof keyword !== "string") {
return "all keywords must be strings";
}
if (!/^[a-z0-9-]+$/.test(keyword)) {
return `keyword "${keyword}" must contain only lowercase letters, numbers, and hyphens`;
}
if (keyword.length < 1 || keyword.length > 30) {
return `keyword "${keyword}" must be between 1 and 30 characters`;
}
}
return null;
}
function validateSpecPaths(plugin) {
const errors = [];
const specs = {
agents: { prefix: "./agents/", suffix: ".md", repoDir: "agents", repoSuffix: ".agent.md" },
skills: { prefix: "./skills/", suffix: "/", repoDir: "skills", repoFile: "SKILL.md" },
};
for (const [field, spec] of Object.entries(specs)) {
const arr = plugin[field];
if (arr === undefined) continue;
if (!Array.isArray(arr)) {
errors.push(`${field} must be an array`);
continue;
}
for (let i = 0; i < arr.length; i++) {
const p = arr[i];
if (typeof p !== "string") {
errors.push(`${field}[${i}] must be a string`);
continue;
}
if (!p.startsWith("./")) {
errors.push(`${field}[${i}] must start with "./"`);
continue;
}
if (!p.startsWith(spec.prefix)) {
errors.push(`${field}[${i}] must start with "${spec.prefix}"`);
continue;
}
if (!p.endsWith(spec.suffix)) {
errors.push(`${field}[${i}] must end with "${spec.suffix}"`);
continue;
}
// Validate the source file exists at repo root
const basename = p.slice(spec.prefix.length, p.length - spec.suffix.length);
if (field === "skills") {
const skillDir = path.join(ROOT_FOLDER, spec.repoDir, basename);
const skillFile = path.join(skillDir, spec.repoFile);
if (!fs.existsSync(skillFile)) {
errors.push(`${field}[${i}] source not found: ${spec.repoDir}/${basename}/SKILL.md`);
}
} else {
const srcFile = path.join(ROOT_FOLDER, spec.repoDir, basename + spec.repoSuffix);
if (!fs.existsSync(srcFile)) {
errors.push(`${field}[${i}] source not found: ${spec.repoDir}/${basename}${spec.repoSuffix}`);
}
}
}
}
return errors;
}
function validatePlugin(folderName) {
const pluginDir = path.join(PLUGINS_DIR, folderName);
const errors = [];
// Rule 1: Must have .github/plugin/plugin.json
const pluginJsonPath = path.join(pluginDir, ".github/plugin", "plugin.json");
if (!fs.existsSync(pluginJsonPath)) {
errors.push("missing required file: .github/plugin/plugin.json");
return errors;
}
// Rule 2: Must have README.md
const readmePath = path.join(pluginDir, "README.md");
if (!fs.existsSync(readmePath)) {
errors.push("missing required file: README.md");
}
// Parse plugin.json
let plugin;
try {
const raw = fs.readFileSync(pluginJsonPath, "utf-8");
plugin = JSON.parse(raw);
} catch (err) {
errors.push(`failed to parse plugin.json: ${err.message}`);
return errors;
}
// Rule 3 & 4: name, description, version
const nameErrors = validateName(plugin.name, folderName);
errors.push(...nameErrors);
const descError = validateDescription(plugin.description);
if (descError) errors.push(descError);
const versionError = validateVersion(plugin.version);
if (versionError) errors.push(versionError);
// Rule 5: keywords (or tags for backward compat)
const keywordsError = validateKeywords(plugin.keywords ?? plugin.tags);
if (keywordsError) errors.push(keywordsError);
// Rule 6: agents, commands, skills paths
const specErrors = validateSpecPaths(plugin);
errors.push(...specErrors);
return errors;
}
// Main validation function
function validatePlugins() {
if (!fs.existsSync(PLUGINS_DIR)) {
console.log("No plugins directory found - validation skipped");
return true;
}
const pluginDirs = fs
.readdirSync(PLUGINS_DIR, { withFileTypes: true })
.filter((d) => d.isDirectory() && d.name !== "external")
.map((d) => d.name);
if (pluginDirs.length === 0) {
console.log("No plugin directories found - validation skipped");
return true;
}
console.log(`Validating ${pluginDirs.length} plugins...\n`);
let hasErrors = false;
const seenNames = new Set();
for (const dir of pluginDirs) {
console.log(`Validating ${dir}...`);
const errors = validatePlugin(dir);
if (errors.length > 0) {
console.error(`❌ ${dir}:`);
errors.forEach((e) => console.error(` - ${e}`));
hasErrors = true;
} else {
console.log(`✅ ${dir} is valid`);
}
// Rule 10: duplicate names
if (seenNames.has(dir)) {
console.error(`❌ Duplicate plugin name "${dir}"`);
hasErrors = true;
} else {
seenNames.add(dir);
}
}
if (!hasErrors) {
console.log(`\n✅ All ${pluginDirs.length} plugins are valid`);
}
return !hasErrors;
}
// Run validation
try {
const isValid = validatePlugins();
if (!isValid) {
console.error("\n❌ Plugin validation failed");
process.exit(1);
}
console.log("\n🎉 Plugin validation passed");
} catch (error) {
console.error(`Error during validation: ${error.message}`);
process.exit(1);
}