|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import { PLUGINS_DIR } from "./constants.mjs"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Convert commands references to skills references in a plugin.json |
| 9 | + * @param {string} pluginJsonPath - Path to the plugin.json file |
| 10 | + * @returns {object} Result with success status and details |
| 11 | + */ |
| 12 | +function updatePluginManifest(pluginJsonPath) { |
| 13 | + const pluginDir = path.dirname(path.dirname(path.dirname(pluginJsonPath))); |
| 14 | + const pluginName = path.basename(pluginDir); |
| 15 | + |
| 16 | + console.log(`\nProcessing plugin: ${pluginName}`); |
| 17 | + |
| 18 | + // Read and parse plugin.json |
| 19 | + let plugin; |
| 20 | + try { |
| 21 | + const content = fs.readFileSync(pluginJsonPath, "utf8"); |
| 22 | + plugin = JSON.parse(content); |
| 23 | + } catch (error) { |
| 24 | + console.log(` ✗ Error reading/parsing: ${error.message}`); |
| 25 | + return { success: false, name: pluginName, reason: "parse-error" }; |
| 26 | + } |
| 27 | + |
| 28 | + // Check if plugin has commands field |
| 29 | + if (!plugin.commands || !Array.isArray(plugin.commands)) { |
| 30 | + console.log(` ℹ No commands field found`); |
| 31 | + return { success: false, name: pluginName, reason: "no-commands" }; |
| 32 | + } |
| 33 | + |
| 34 | + const commandCount = plugin.commands.length; |
| 35 | + console.log(` Found ${commandCount} command(s) to convert`); |
| 36 | + |
| 37 | + // Convert commands to skills format |
| 38 | + // Commands: "./commands/foo.md" → Skills: "./skills/foo/" |
| 39 | + const skills = plugin.commands.map((cmd) => { |
| 40 | + const basename = path.basename(cmd, ".md"); |
| 41 | + return `./skills/${basename}/`; |
| 42 | + }); |
| 43 | + |
| 44 | + // Initialize skills array if it doesn't exist |
| 45 | + if (!plugin.skills) { |
| 46 | + plugin.skills = []; |
| 47 | + } |
| 48 | + |
| 49 | + // Add converted commands to skills array |
| 50 | + plugin.skills.push(...skills); |
| 51 | + |
| 52 | + // Remove commands field |
| 53 | + delete plugin.commands; |
| 54 | + |
| 55 | + // Write updated plugin.json |
| 56 | + try { |
| 57 | + fs.writeFileSync( |
| 58 | + pluginJsonPath, |
| 59 | + JSON.stringify(plugin, null, 2) + "\n", |
| 60 | + "utf8" |
| 61 | + ); |
| 62 | + console.log(` ✓ Converted ${commandCount} command(s) to skills`); |
| 63 | + return { success: true, name: pluginName, count: commandCount }; |
| 64 | + } catch (error) { |
| 65 | + console.log(` ✗ Error writing file: ${error.message}`); |
| 66 | + return { success: false, name: pluginName, reason: "write-error" }; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Main function to update all plugin manifests |
| 72 | + */ |
| 73 | +function main() { |
| 74 | + console.log("=".repeat(60)); |
| 75 | + console.log("Updating Plugin Manifests: Commands → Skills"); |
| 76 | + console.log("=".repeat(60)); |
| 77 | + |
| 78 | + // Check if plugins directory exists |
| 79 | + if (!fs.existsSync(PLUGINS_DIR)) { |
| 80 | + console.error(`Error: Plugins directory not found: ${PLUGINS_DIR}`); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + // Find all plugin.json files |
| 85 | + const pluginDirs = fs |
| 86 | + .readdirSync(PLUGINS_DIR, { withFileTypes: true }) |
| 87 | + .filter((entry) => entry.isDirectory()) |
| 88 | + .map((entry) => entry.name); |
| 89 | + |
| 90 | + console.log(`Found ${pluginDirs.length} plugin directory(ies)\n`); |
| 91 | + |
| 92 | + const results = { |
| 93 | + updated: [], |
| 94 | + noCommands: [], |
| 95 | + failed: [], |
| 96 | + }; |
| 97 | + |
| 98 | + // Process each plugin |
| 99 | + for (const dirName of pluginDirs) { |
| 100 | + const pluginJsonPath = path.join( |
| 101 | + PLUGINS_DIR, |
| 102 | + dirName, |
| 103 | + ".github/plugin", |
| 104 | + "plugin.json" |
| 105 | + ); |
| 106 | + |
| 107 | + if (!fs.existsSync(pluginJsonPath)) { |
| 108 | + console.log(`\nSkipping ${dirName}: no plugin.json found`); |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + const result = updatePluginManifest(pluginJsonPath); |
| 113 | + if (result.success) { |
| 114 | + results.updated.push({ name: result.name, count: result.count }); |
| 115 | + } else if (result.reason === "no-commands") { |
| 116 | + results.noCommands.push(result.name); |
| 117 | + } else { |
| 118 | + results.failed.push(result.name); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Print summary |
| 123 | + console.log("\n" + "=".repeat(60)); |
| 124 | + console.log("Update Summary"); |
| 125 | + console.log("=".repeat(60)); |
| 126 | + console.log(`✓ Updated plugins: ${results.updated.length}`); |
| 127 | + console.log(`ℹ No commands field: ${results.noCommands.length}`); |
| 128 | + console.log(`✗ Failed: ${results.failed.length}`); |
| 129 | + console.log(`Total processed: ${pluginDirs.length}`); |
| 130 | + |
| 131 | + if (results.updated.length > 0) { |
| 132 | + console.log("\nUpdated plugins:"); |
| 133 | + results.updated.forEach(({ name, count }) => |
| 134 | + console.log(` - ${name} (${count} command(s) → skills)`) |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + if (results.failed.length > 0) { |
| 139 | + console.log("\nFailed updates:"); |
| 140 | + results.failed.forEach((name) => console.log(` - ${name}`)); |
| 141 | + } |
| 142 | + |
| 143 | + console.log("\n✅ Plugin manifest updates complete!"); |
| 144 | + console.log( |
| 145 | + "\nNext steps:\n" + |
| 146 | + "1. Run 'npm run plugin:validate' to validate all updated plugins\n" + |
| 147 | + "2. Test that plugins work correctly\n" |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +// Run the update |
| 152 | +main(); |
0 commit comments