|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Plugin version check. Compares the local plugin manifest version against |
| 5 | + * origin/main and prints an update notice if the remote version is newer. |
| 6 | + * Exits silently if versions match or on any error. |
| 7 | + * |
| 8 | + * Usage: node check-version.js |
| 9 | + * Functions are also exported for testing. |
| 10 | + */ |
| 11 | + |
| 12 | +const { execFileSync, execSync } = require('child_process'); |
| 13 | +const path = require('path'); |
| 14 | +const fs = require('fs'); |
| 15 | + |
| 16 | +const MARKETPLACE_PATHS = [ |
| 17 | + 'marketplace.json', |
| 18 | + '.plugin/marketplace.json', |
| 19 | + '.claude-plugin/marketplace.json', |
| 20 | +]; |
| 21 | +const PLUGIN_MANIFEST_PATHS = [ |
| 22 | + '.plugin/plugin.json', |
| 23 | + '.claude-plugin/plugin.json', |
| 24 | +]; |
| 25 | + |
| 26 | +function compareSemver(localVersion, remoteVersion) { |
| 27 | + const localParts = localVersion.split('.').map(Number); |
| 28 | + const remoteParts = remoteVersion.split('.').map(Number); |
| 29 | + for (let index = 0; index < 3; index++) { |
| 30 | + if ((remoteParts[index] || 0) > (localParts[index] || 0)) return 1; |
| 31 | + if ((remoteParts[index] || 0) < (localParts[index] || 0)) return -1; |
| 32 | + } |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +function detectHost(env = process.env) { |
| 37 | + return env.COPILOT_CLI === '1' ? 'copilot' : 'claude'; |
| 38 | +} |
| 39 | + |
| 40 | +function formatUpdateMessage( |
| 41 | + pluginName, |
| 42 | + localVersion, |
| 43 | + remoteVersion, |
| 44 | + marketplaceName, |
| 45 | + host = detectHost() |
| 46 | +) { |
| 47 | + const qualifiedName = marketplaceName ? `${pluginName}@${marketplaceName}` : pluginName; |
| 48 | + let message = `\nPlugin update available: ${pluginName} ${localVersion} -> ${remoteVersion}.\n`; |
| 49 | + if (marketplaceName) { |
| 50 | + message += `Run:\n ${host} plugin marketplace update ${marketplaceName}\n ${host} plugin update ${qualifiedName}`; |
| 51 | + } else { |
| 52 | + message += `Run: ${host} plugin update ${qualifiedName}`; |
| 53 | + } |
| 54 | + return message; |
| 55 | +} |
| 56 | + |
| 57 | +function firstExistingPath(root, relativePaths) { |
| 58 | + for (const relativePath of relativePaths) { |
| 59 | + const filePath = path.join(root, relativePath); |
| 60 | + if (fs.existsSync(filePath)) return filePath; |
| 61 | + } |
| 62 | + return null; |
| 63 | +} |
| 64 | + |
| 65 | +function readFirstJson(root, relativePaths) { |
| 66 | + const filePath = firstExistingPath(root, relativePaths); |
| 67 | + if (!filePath) return null; |
| 68 | + return JSON.parse(fs.readFileSync(filePath, 'utf8')); |
| 69 | +} |
| 70 | + |
| 71 | +function readMarketplaceName(gitRoot) { |
| 72 | + const marketplace = readFirstJson(gitRoot, MARKETPLACE_PATHS); |
| 73 | + return marketplace?.name || null; |
| 74 | +} |
| 75 | + |
| 76 | +function readJsonFromGit(ref, relativePaths) { |
| 77 | + for (const relativePath of relativePaths) { |
| 78 | + try { |
| 79 | + const content = execFileSync('git', ['show', `${ref}:${relativePath}`], { |
| 80 | + encoding: 'utf8', |
| 81 | + timeout: 5000, |
| 82 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 83 | + }); |
| 84 | + return JSON.parse(content); |
| 85 | + } catch { |
| 86 | + // Open Plugins and legacy installs use different manifest paths. |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { compareSemver, detectHost, formatUpdateMessage, readMarketplaceName }; |
| 93 | + |
| 94 | +if (require.main === module) { |
| 95 | + try { |
| 96 | + const pluginRoot = path.resolve(__dirname, '..'); |
| 97 | + const pluginJsonPath = firstExistingPath(pluginRoot, PLUGIN_MANIFEST_PATHS); |
| 98 | + if (!pluginJsonPath) process.exit(0); |
| 99 | + |
| 100 | + const localPlugin = JSON.parse(fs.readFileSync(pluginJsonPath, 'utf8')); |
| 101 | + const localVersion = localPlugin.version; |
| 102 | + if (!localVersion) process.exit(0); |
| 103 | + |
| 104 | + const gitRoot = execSync('git rev-parse --show-toplevel', { |
| 105 | + encoding: 'utf8', |
| 106 | + timeout: 5000, |
| 107 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 108 | + }).trim(); |
| 109 | + |
| 110 | + const remoteManifestPaths = PLUGIN_MANIFEST_PATHS.map((manifestPath) => |
| 111 | + path.relative(gitRoot, path.join(pluginRoot, manifestPath)).replace(/\\/g, '/') |
| 112 | + ); |
| 113 | + |
| 114 | + try { |
| 115 | + execSync('git fetch origin main --quiet', { |
| 116 | + encoding: 'utf8', |
| 117 | + timeout: 10000, |
| 118 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 119 | + }); |
| 120 | + } catch { |
| 121 | + // A cached origin/main is sufficient when the network is unavailable. |
| 122 | + } |
| 123 | + |
| 124 | + const remotePlugin = readJsonFromGit('origin/main', remoteManifestPaths); |
| 125 | + if (!remotePlugin?.version) process.exit(0); |
| 126 | + |
| 127 | + if (compareSemver(localVersion, remotePlugin.version) > 0) { |
| 128 | + const pluginName = localPlugin.name || 'model-apps'; |
| 129 | + const marketplaceName = readMarketplaceName(gitRoot); |
| 130 | + console.log( |
| 131 | + formatUpdateMessage(pluginName, localVersion, remotePlugin.version, marketplaceName) |
| 132 | + ); |
| 133 | + } |
| 134 | + } catch { |
| 135 | + // Version checks must never block skill execution. |
| 136 | + } |
| 137 | +} |
0 commit comments