-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathadd-plugin.js
More file actions
357 lines (307 loc) · 10.3 KB
/
Copy pathadd-plugin.js
File metadata and controls
357 lines (307 loc) · 10.3 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env node
/**
* Registers a Tabame plugin found under plugins/<PluginFolder> into
* resources/plugins.json.
*
* Usage:
* node add-plugin.js <PluginFolder>
* node add-plugin.js missing
* node add-plugin.js check
*
* The "missing" command registers every repository plugin folder that is
* not already present in resources/plugins.json. The "check" command lists
* locally installed plugins that are not present in the registry.
*/
const fs = require("fs");
const path = require("path");
function fail(message) {
console.error(`Error: ${message}`);
process.exit(1);
}
function parseJson(raw, fileName) {
// UTF-8 files created by some Windows tools can begin with a byte-order
// mark. JSON.parse rejects that character, even though editors commonly
// display the file as valid JSON.
const withoutBom = raw.replace(/^\uFEFF/, "");
try {
return JSON.parse(withoutBom);
} catch (err) {
fail(`${fileName} is not valid JSON: ${err.message}`);
}
}
const repoRoot = __dirname;
const branch = "main";
const pluginsPath = path.join(repoRoot, "plugins");
const registryPath = path.join(repoRoot, "resources", "plugins.json");
function readRegistry() {
const registryRaw = fs.readFileSync(registryPath, "utf8");
const registry = parseJson(registryRaw, "resources/plugins.json");
if (
!registry ||
typeof registry !== "object" ||
!Array.isArray(registry.plugins)
) {
fail('resources/plugins.json is missing a "plugins" array.');
}
return registry;
}
function writeRegistry(registry) {
// Write plain UTF-8, no BOM. This keeps special characters intact.
fs.writeFileSync(registryPath, JSON.stringify(registry, null, 2), "utf8");
}
function validateManifest(manifest, pluginFolder) {
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
fail("plugin.json must contain a JSON object.");
}
for (const field of [
"name",
"keyword",
"description",
"icon",
"runtime",
"entry",
"version",
]) {
const value = manifest[field];
if (value === undefined || value === null || String(value).trim() === "") {
fail(`plugin.json must contain a '${field}' value. ${pluginFolder}`);
}
}
}
function buildPluginRegistration(pluginFolder) {
const pluginPath = path.join(pluginsPath, pluginFolder);
const manifestPath = path.join(pluginPath, "plugin.json");
if (!fs.existsSync(manifestPath) || !fs.statSync(manifestPath).isFile()) {
fail(`No plugin.json found in plugins/${pluginFolder}.`);
}
// Always read as UTF-8 explicitly. This avoids the mangled special
// character issue from the old PowerShell version.
const manifestRaw = fs.readFileSync(manifestPath, "utf8");
const manifest = parseJson(
manifestRaw,
`plugins/${pluginFolder}/plugin.json`,
);
validateManifest(manifest, pluginFolder);
const pluginId = manifest.id || pluginFolder;
const githubPath = `https://github.com/Far-Se/tabame/tree/${branch}/plugins/${pluginFolder}`;
const rawPath = `https://raw.githubusercontent.com/Far-Se/tabame/${branch}/plugins/${pluginFolder}`;
// Plain JS objects preserve insertion order for string keys.
const files = {
"plugin.json": `${rawPath}/plugin.json`,
};
for (const entry of fs.readdirSync(pluginPath, { withFileTypes: true })) {
if (!entry.isFile()) continue;
const ext = path.extname(entry.name);
if (ext === ".py" || ext === ".js") {
files[entry.name] = `${rawPath}/${entry.name}`;
}
}
// Font assets can be organised into nested font-family directories, so
// preserve their relative paths.
const fontsPath = path.join(pluginPath, "fonts");
if (fs.existsSync(fontsPath) && fs.statSync(fontsPath).isDirectory()) {
const walk = (dir) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(full);
} else if (entry.isFile()) {
const relativePath = path
.relative(pluginPath, full)
.split(path.sep)
.join("/");
files[relativePath] = `${rawPath}/${relativePath}`;
}
}
};
walk(fontsPath);
}
for (const file of ["config.example.json", "README.md"]) {
const filePath = path.join(pluginPath, file);
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
files[file] = `${rawPath}/${file}`;
}
}
return {
pluginId,
registration: {
id: pluginId,
name: manifest.name,
keyword: manifest.keyword,
description: manifest.description,
icon: manifest.icon,
runtime: manifest.runtime,
author: "Far-Se",
version: manifest.version ?? "1.0.0",
homepage: githubPath,
files,
},
};
}
function addPlugin(pluginFolder) {
const registry = readRegistry();
const { pluginId, registration } = buildPluginRegistration(pluginFolder);
if (registry.plugins.some((plugin) => plugin && plugin.id === pluginId)) {
fail(`A plugin with id '${pluginId}' is already registered.`);
}
registry.plugins.push(registration);
writeRegistry(registry);
console.log(`Added '${pluginId}' to resources/plugins.json.`);
}
function addMissingPlugins() {
const registry = readRegistry();
if (!fs.existsSync(pluginsPath) || !fs.statSync(pluginsPath).isDirectory()) {
fail("The repository plugins folder does not exist.");
}
const registeredIds = new Set(
registry.plugins
.filter(
(plugin) => plugin && plugin.id !== undefined && plugin.id !== null,
)
.map((plugin) => String(plugin.id)),
);
const additions = [];
const pluginFolders = fs
.readdirSync(pluginsPath, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.sort((left, right) => left.name.localeCompare(right.name));
for (const entry of pluginFolders) {
const manifestPath = path.join(pluginsPath, entry.name, "plugin.json");
if (!fs.existsSync(manifestPath) || !fs.statSync(manifestPath).isFile()) {
console.warn(`Skipping '${entry.name}': no plugin.json found.`);
continue;
}
const { pluginId, registration } = buildPluginRegistration(entry.name);
if (registeredIds.has(String(pluginId))) continue;
registeredIds.add(String(pluginId));
additions.push({ folder: entry.name, pluginId, registration });
}
if (additions.length === 0) {
console.log("No missing plugins found.");
return;
}
for (const addition of additions) {
registry.plugins.push(addition.registration);
}
writeRegistry(registry);
console.log(
`Added ${additions.length} missing plugin(s) to resources/plugins.json:`,
);
for (const addition of additions) {
console.log(`- ${addition.pluginId} (plugins/${addition.folder})`);
}
}
function normalizeIdentity(value) {
return String(value)
.toLowerCase()
.replace(/[^a-z0-9]/g, "");
}
function addIdentityFromUrl(identities, value) {
if (typeof value !== "string") return;
const match = value.match(/\/plugins\/([^/?#]+)/i);
if (!match) return;
try {
identities.add(normalizeIdentity(decodeURIComponent(match[1])));
} catch {
identities.add(normalizeIdentity(match[1]));
}
}
function getRegistryIdentities(registry) {
const identities = new Set();
for (const plugin of registry.plugins) {
if (!plugin || typeof plugin !== "object") continue;
if (plugin.id !== undefined && plugin.id !== null) {
identities.add(normalizeIdentity(plugin.id));
}
addIdentityFromUrl(identities, plugin.homepage);
if (plugin.files && typeof plugin.files === "object") {
addIdentityFromUrl(identities, plugin.files["plugin.json"]);
}
}
return identities;
}
function readLocalPluginInfo(pluginPath, folder) {
const manifestPath = path.join(pluginPath, "plugin.json");
if (!fs.existsSync(manifestPath) || !fs.statSync(manifestPath).isFile()) {
return { id: folder };
}
try {
const manifest = JSON.parse(
fs.readFileSync(manifestPath, "utf8").replace(/^\uFEFF/, ""),
);
return {
id:
manifest &&
manifest.id !== undefined &&
manifest.id !== null &&
String(manifest.id).trim() !== ""
? String(manifest.id)
: folder,
};
} catch {
return { id: folder, invalidManifest: true };
}
}
function checkLocalPlugins() {
const localAppData = process.env.LOCALAPPDATA;
if (!localAppData) {
fail("The LOCALAPPDATA environment variable is not set.");
}
const localPluginsPath = path.join("E:\\Resources\\Backup", "tabame-plugins");
if (
!fs.existsSync(localPluginsPath) ||
!fs.statSync(localPluginsPath).isDirectory()
) {
console.log(`Local plugins folder not found: ${localPluginsPath}`);
return;
}
const registry = readRegistry();
const registeredIdentities = getRegistryIdentities(registry);
const localPlugins = fs
.readdirSync(localPluginsPath, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.sort((left, right) => left.name.localeCompare(right.name))
.map((entry) => {
const info = readLocalPluginInfo(
path.join(localPluginsPath, entry.name),
entry.name,
);
return {
folder: entry.name,
id: info.id,
invalidManifest: info.invalidManifest,
};
});
const missing = localPlugins.filter((plugin) => {
const identities = [plugin.folder, plugin.id].map(normalizeIdentity);
return !identities.some((identity) => registeredIdentities.has(identity));
});
if (missing.length === 0) {
console.log("All local plugins are registered in resources/plugins.json.");
return;
}
console.log("Local plugins not registered in resources/plugins.json:");
for (const plugin of missing) {
if (["atm"].includes(plugin.folder)) continue;
const details = plugin.id !== plugin.folder ? ` (id: ${plugin.id})` : "";
const manifestWarning = plugin.invalidManifest
? " [invalid plugin.json]"
: "";
console.log(`- ${plugin.folder}${details}${manifestWarning}`);
}
}
const command = process.argv[2];
if (!command) {
fail(
"Usage: node add-plugin.js <PluginFolder>\n" +
" node add-plugin.js missing\n" +
" node add-plugin.js check",
);
}
if (command === "missing") {
addMissingPlugins();
} else if (command === "check") {
checkLocalPlugins();
} else {
addPlugin(command);
}