|
| 1 | +import { readFile } from "node:fs/promises"; |
| 2 | +import process from "node:process"; |
| 3 | + |
| 4 | +const NAME_PATTERN = /^[a-z0-9][a-z0-9._-]*$/; |
| 5 | +const TAG_PATTERN = /^[a-z0-9-]+$/; |
| 6 | +const KIND_VALUES = new Set(["tool", "command", "hook", "provider", "renderer", "flag"]); |
| 7 | +const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/; |
| 8 | +const URI_PATTERN = /^[a-z][a-z\d+.-]*:[^\s]+$/i; |
| 9 | + |
| 10 | +function isObject(value) { |
| 11 | + return value !== null && typeof value === "object" && !Array.isArray(value); |
| 12 | +} |
| 13 | + |
| 14 | +function addError(errors, path, message) { |
| 15 | + errors.push(`${path}: ${message}`); |
| 16 | +} |
| 17 | + |
| 18 | +function validateRegistry(registry) { |
| 19 | + const errors = []; |
| 20 | + |
| 21 | + if (!isObject(registry)) { |
| 22 | + return ["registry: expected an object"]; |
| 23 | + } |
| 24 | + |
| 25 | + if (registry.version !== 1) { |
| 26 | + addError(errors, "version", "must be 1"); |
| 27 | + } |
| 28 | + |
| 29 | + if (!Array.isArray(registry.mods)) { |
| 30 | + return [...errors, "mods: expected an array"]; |
| 31 | + } |
| 32 | + |
| 33 | + if ("hidden" in registry) { |
| 34 | + if (!Array.isArray(registry.hidden)) { |
| 35 | + addError(errors, "hidden", "expected an array"); |
| 36 | + } else if (new Set(registry.hidden).size !== registry.hidden.length) { |
| 37 | + addError(errors, "hidden", "must contain unique values"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const names = new Set(); |
| 42 | + for (const [index, mod] of registry.mods.entries()) { |
| 43 | + const path = `mods[${index}]`; |
| 44 | + if (!isObject(mod)) { |
| 45 | + addError(errors, path, "expected an object"); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + for (const field of ["name", "source", "description", "author", "kinds"]) { |
| 50 | + if (!(field in mod)) { |
| 51 | + addError(errors, `${path}.${field}`, "is required"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (typeof mod.name === "string") { |
| 56 | + if (!NAME_PATTERN.test(mod.name)) { |
| 57 | + addError(errors, `${path}.name`, "must use lowercase letters, numbers, '.', '_' or '-'"); |
| 58 | + } |
| 59 | + if (names.has(mod.name)) { |
| 60 | + addError(errors, `${path}.name`, `duplicates "${mod.name}"`); |
| 61 | + } |
| 62 | + names.add(mod.name); |
| 63 | + } else if ("name" in mod) { |
| 64 | + addError(errors, `${path}.name`, "expected a string"); |
| 65 | + } |
| 66 | + |
| 67 | + if (typeof mod.source !== "string") { |
| 68 | + addError(errors, `${path}.source`, "expected a string"); |
| 69 | + } |
| 70 | + |
| 71 | + if (typeof mod.description !== "string") { |
| 72 | + addError(errors, `${path}.description`, "expected a string"); |
| 73 | + } else if (mod.description.length > 240) { |
| 74 | + addError(errors, `${path}.description`, "must be at most 240 characters"); |
| 75 | + } |
| 76 | + |
| 77 | + if (!isObject(mod.author)) { |
| 78 | + addError(errors, `${path}.author`, "expected an object"); |
| 79 | + } else if (typeof mod.author.name !== "string") { |
| 80 | + addError(errors, `${path}.author.name`, "expected a string"); |
| 81 | + } |
| 82 | + |
| 83 | + if (!Array.isArray(mod.kinds) || mod.kinds.length === 0) { |
| 84 | + addError(errors, `${path}.kinds`, "must contain at least one kind"); |
| 85 | + } else { |
| 86 | + if (new Set(mod.kinds).size !== mod.kinds.length) { |
| 87 | + addError(errors, `${path}.kinds`, "must contain unique values"); |
| 88 | + } |
| 89 | + for (const kind of mod.kinds) { |
| 90 | + if (!KIND_VALUES.has(kind)) { |
| 91 | + addError(errors, `${path}.kinds`, `contains unsupported kind "${kind}"`); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (mod.tags !== undefined) { |
| 97 | + if (!Array.isArray(mod.tags)) { |
| 98 | + addError(errors, `${path}.tags`, "expected an array"); |
| 99 | + } else { |
| 100 | + if (new Set(mod.tags).size !== mod.tags.length) { |
| 101 | + addError(errors, `${path}.tags`, "must contain unique values"); |
| 102 | + } |
| 103 | + for (const tag of mod.tags) { |
| 104 | + if (typeof tag !== "string" || !TAG_PATTERN.test(tag)) { |
| 105 | + addError(errors, `${path}.tags`, `contains invalid tag "${tag}"`); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + for (const field of ["repo", "author.url"]) { |
| 112 | + const value = field === "repo" ? mod.repo : mod.author?.url; |
| 113 | + if (value !== undefined && (typeof value !== "string" || !URI_PATTERN.test(value))) { |
| 114 | + addError(errors, `${path}.${field}`, "must be a URI"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (mod.addedAt !== undefined && (typeof mod.addedAt !== "string" || !DATE_PATTERN.test(mod.addedAt))) { |
| 119 | + addError(errors, `${path}.addedAt`, "must use YYYY-MM-DD format"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return errors; |
| 124 | +} |
| 125 | + |
| 126 | +export { validateRegistry }; |
| 127 | + |
| 128 | +if (process.argv[1] === new URL(import.meta.url).pathname.replaceAll("\\", "/")) { |
| 129 | + const file = process.argv[2] ?? new URL("./registry.json", import.meta.url); |
| 130 | + const registry = JSON.parse(await readFile(file, "utf8")); |
| 131 | + const errors = validateRegistry(registry); |
| 132 | + |
| 133 | + if (errors.length > 0) { |
| 134 | + console.error(errors.map((error) => `- ${error}`).join("\n")); |
| 135 | + process.exitCode = 1; |
| 136 | + } else { |
| 137 | + console.log(`Validated ${registry.mods.length} mod entries.`); |
| 138 | + } |
| 139 | +} |
0 commit comments