-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathgenerate-marketplace.mjs
More file actions
executable file
·207 lines (175 loc) · 6.38 KB
/
generate-marketplace.mjs
File metadata and controls
executable file
·207 lines (175 loc) · 6.38 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
#!/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");
const EXTERNAL_PLUGINS_DIR = path.join(ROOT_FOLDER, "plugins", "external");
const MARKETPLACE_FILE = path.join(ROOT_FOLDER, ".github/plugin", "marketplace.json");
/**
* Validate an external plugin entry has required fields and a non-local source
* @param {object} plugin - External plugin entry
* @param {string} filename - Source filename (for error messages)
* @returns {string[]} - Array of validation error messages
*/
function validateExternalPlugin(plugin, filename) {
const errors = [];
const prefix = `external/${filename}`;
if (!plugin.name || typeof plugin.name !== "string") {
errors.push(`${prefix}: "name" is required and must be a string`);
}
if (!plugin.description || typeof plugin.description !== "string") {
errors.push(`${prefix}: "description" is required and must be a string`);
}
if (!plugin.version || typeof plugin.version !== "string") {
errors.push(`${prefix}: "version" is required and must be a string`);
}
if (!plugin.source) {
errors.push(`${prefix}: "source" is required`);
} else if (typeof plugin.source === "string") {
errors.push(`${prefix}: "source" must be an object (local file paths are not allowed for external plugins)`);
} else if (typeof plugin.source === "object") {
if (!plugin.source.source) {
errors.push(`${prefix}: "source.source" is required (e.g. "github", "url", "npm", "pip")`);
}
} else {
errors.push(`${prefix}: "source" must be an object`);
}
return errors;
}
/**
* Read external plugin entries from individual JSON files in plugins/external/
* @returns {Array} - Array of external plugin entries (merged as-is)
*/
function readExternalPlugins() {
if (!fs.existsSync(EXTERNAL_PLUGINS_DIR)) {
return [];
}
const files = fs.readdirSync(EXTERNAL_PLUGINS_DIR)
.filter(f => f.endsWith(".json"))
.sort();
if (files.length === 0) {
return [];
}
const plugins = [];
let hasErrors = false;
for (const file of files) {
const filePath = path.join(EXTERNAL_PLUGINS_DIR, file);
try {
const content = fs.readFileSync(filePath, "utf8");
const plugin = JSON.parse(content);
if (typeof plugin !== "object" || Array.isArray(plugin)) {
console.error(`Error: external/${file} must contain a single JSON object`);
hasErrors = true;
continue;
}
const errors = validateExternalPlugin(plugin, file);
if (errors.length > 0) {
errors.forEach(e => console.error(`Error: ${e}`));
hasErrors = true;
} else {
plugins.push(plugin);
}
} catch (error) {
console.error(`Error reading external/${file}: ${error.message}`);
hasErrors = true;
}
}
if (hasErrors) {
console.error("Error: one or more external plugin files contain invalid entries");
process.exit(1);
}
return plugins;
}
/**
* Read plugin metadata from plugin.json file
* @param {string} pluginDir - Path to plugin directory
* @returns {object|null} - Plugin metadata or null if not found
*/
function readPluginMetadata(pluginDir) {
const pluginJsonPath = path.join(pluginDir, ".github/plugin", "plugin.json");
if (!fs.existsSync(pluginJsonPath)) {
console.warn(`Warning: No plugin.json found for ${path.basename(pluginDir)}`);
return null;
}
try {
const content = fs.readFileSync(pluginJsonPath, "utf8");
return JSON.parse(content);
} catch (error) {
console.error(`Error reading plugin.json for ${path.basename(pluginDir)}:`, error.message);
return null;
}
}
/**
* Generate marketplace.json from plugin directories
*/
function generateMarketplace() {
console.log("Generating marketplace.json...");
if (!fs.existsSync(PLUGINS_DIR)) {
console.error(`Error: Plugins directory not found at ${PLUGINS_DIR}`);
process.exit(1);
}
// Read all plugin directories (excluding the 'external' directory)
const pluginDirs = fs.readdirSync(PLUGINS_DIR, { withFileTypes: true })
.filter(entry => entry.isDirectory() && entry.name !== "external")
.map(entry => entry.name)
.sort();
console.log(`Found ${pluginDirs.length} plugin directories`);
// Read metadata for each plugin
const plugins = [];
for (const dirName of pluginDirs) {
const pluginPath = path.join(PLUGINS_DIR, dirName);
const metadata = readPluginMetadata(pluginPath);
if (metadata) {
plugins.push({
name: metadata.name,
source: dirName,
description: metadata.description,
version: metadata.version || "1.0.0"
});
console.log(`✓ Added plugin: ${metadata.name}`);
} else {
console.log(`✗ Skipped: ${dirName} (no valid plugin.json)`);
}
}
// Read external plugins and merge as-is
const externalPlugins = readExternalPlugins();
if (externalPlugins.length > 0) {
console.log(`\nFound ${externalPlugins.length} external plugins`);
// Warn on duplicate names
const localNames = new Set(plugins.map(p => p.name));
for (const ext of externalPlugins) {
if (localNames.has(ext.name)) {
console.warn(`Warning: external plugin "${ext.name}" has the same name as a local plugin`);
}
plugins.push(ext);
console.log(`✓ Added external plugin: ${ext.name}`);
}
}
// Sort all plugins by name (case-insensitive)
plugins.sort((a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }));
// Create marketplace.json structure
const marketplace = {
name: "awesome-copilot",
metadata: {
description: "Community-driven collection of GitHub Copilot plugins, agents, prompts, and skills",
version: "1.0.0",
pluginRoot: "./plugins"
},
owner: {
name: "GitHub",
email: "copilot@github.com"
},
plugins: plugins
};
// Ensure directory exists
const marketplaceDir = path.dirname(MARKETPLACE_FILE);
if (!fs.existsSync(marketplaceDir)) {
fs.mkdirSync(marketplaceDir, { recursive: true });
}
// Write marketplace.json
fs.writeFileSync(MARKETPLACE_FILE, JSON.stringify(marketplace, null, 2) + "\n");
console.log(`\n✓ Successfully generated marketplace.json with ${plugins.length} plugins (${plugins.length - externalPlugins.length} local, ${externalPlugins.length} external)`);
console.log(` Location: ${MARKETPLACE_FILE}`);
}
// Run the script
generateMarketplace();