fix: simplify add-guild script using line-based insertion

This commit is contained in:
2026-04-05 18:51:35 +00:00
parent c9bed19689
commit 9aa85fdbc5

View File

@@ -1,21 +1,12 @@
#!/usr/bin/env node #!/usr/bin/env node
/**
* Add a guild entry to the discord-guilds SKILL.md table
* Usage: add-guild <guild-id> <description>
*/
import fs from "node:fs"; import fs from "node:fs";
import path from "node:path"; import path from "node:path";
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(fileURLToPath(import.meta.url));
const __dirname = path.dirname(__filename);
const SKILL_PATH = path.resolve(__dirname, "../SKILL.md"); const SKILL_PATH = path.resolve(__dirname, "../SKILL.md");
function main() {
const args = process.argv.slice(2); const args = process.argv.slice(2);
if (args.length < 2) { if (args.length < 2) {
console.error("Usage: add-guild <guild-id> <description>"); console.error("Usage: add-guild <guild-id> <description>");
process.exit(1); process.exit(1);
@@ -24,33 +15,29 @@ function main() {
const guildId = args[0]; const guildId = args[0];
const description = args.slice(1).join(" "); const description = args.slice(1).join(" ");
// Validate guild ID is numeric
if (!/^\d+$/.test(guildId)) { if (!/^\d+$/.test(guildId)) {
console.error("Error: guild-id must be numeric (Discord snowflake)"); console.error("Error: guild-id must be numeric (Discord snowflake)");
process.exit(1); process.exit(1);
} }
// Read existing SKILL.md
let content = fs.readFileSync(SKILL_PATH, "utf8"); let content = fs.readFileSync(SKILL_PATH, "utf8");
// Find the table and insert new row
const newRow = `| ${guildId} | ${description} |`; const newRow = `| ${guildId} | ${description} |`;
// Look for the table pattern and insert after the header // Find separator line and insert after it
const tablePattern = /(\| guild-id \| description \|\n\|[-\s|]+\|)/; const lines = content.split("\n");
let insertIndex = -1;
for (let i = 0; i < lines.length; i++) {
if (/^\|[-\s|]+\|$/.test(lines[i]) && i > 0 && lines[i-1].includes("guild-id")) {
insertIndex = i;
break;
}
}
if (!tablePattern.test(content)) { if (insertIndex === -1) {
console.error("Error: Could not find guild table in SKILL.md"); console.error("Error: Could not find guild table in SKILL.md");
process.exit(1); process.exit(1);
} }
// Insert new row after the table header lines.splice(insertIndex + 1, 0, newRow);
content = content.replace(tablePattern, `$1\n${newRow}`); fs.writeFileSync(SKILL_PATH, lines.join("\n"), "utf8");
// Write back
fs.writeFileSync(SKILL_PATH, content, "utf8");
console.log(`✓ Added guild: ${guildId} - ${description}`); console.log(`✓ Added guild: ${guildId} - ${description}`);
}
main();