|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * GitHub Stars Updater |
| 5 | + * Updates github-stars.json with new manifest entries |
| 6 | + */ |
| 7 | + |
| 8 | +import fs from 'node:fs' |
| 9 | +import path from 'node:path' |
| 10 | +import { fileURLToPath } from 'node:url' |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url) |
| 13 | +const __dirname = path.dirname(__filename) |
| 14 | + |
| 15 | +/** |
| 16 | + * Get project root directory |
| 17 | + */ |
| 18 | +function getProjectRoot() { |
| 19 | + return path.resolve(__dirname, '../../../../..') |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Get the path to github-stars.json |
| 24 | + */ |
| 25 | +function getGithubStarsPath() { |
| 26 | + return path.join(getProjectRoot(), 'data/github-stars.json') |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Load github-stars.json |
| 31 | + * @returns {Object} The current github-stars data |
| 32 | + */ |
| 33 | +export function loadGithubStars() { |
| 34 | + const filePath = getGithubStarsPath() |
| 35 | + |
| 36 | + if (!fs.existsSync(filePath)) { |
| 37 | + throw new Error(`github-stars.json not found at: ${filePath}`) |
| 38 | + } |
| 39 | + |
| 40 | + const content = fs.readFileSync(filePath, 'utf-8') |
| 41 | + return JSON.parse(content) |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Save github-stars.json |
| 46 | + * @param {Object} data - The github-stars data to save |
| 47 | + */ |
| 48 | +export function saveGithubStars(data) { |
| 49 | + const filePath = getGithubStarsPath() |
| 50 | + const content = `${JSON.stringify(data, null, 2)}\n` |
| 51 | + fs.writeFileSync(filePath, content, 'utf-8') |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Get the category name (plural form) from manifest type |
| 56 | + * @param {string} type - Manifest type (cli, extension, ide, model, provider, vendor) |
| 57 | + * @returns {string} Category name for github-stars.json |
| 58 | + */ |
| 59 | +function getCategoryName(type) { |
| 60 | + const mapping = { |
| 61 | + cli: 'clis', |
| 62 | + extension: 'extensions', |
| 63 | + ide: 'ides', |
| 64 | + model: 'models', |
| 65 | + provider: 'providers', |
| 66 | + vendor: 'vendors', |
| 67 | + } |
| 68 | + |
| 69 | + return mapping[type] || `${type}s` |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Update github-stars.json with a new or updated manifest entry |
| 74 | + * @param {string} type - Manifest type (cli, extension, ide, etc.) |
| 75 | + * @param {string} id - Manifest id |
| 76 | + * @param {Object} options - Options |
| 77 | + * @param {boolean} options.isNew - Whether this is a new entry (true) or update (false) |
| 78 | + * @returns {Object} Result with status and message |
| 79 | + */ |
| 80 | +export function updateGithubStarsEntry(type, id, options = {}) { |
| 81 | + const { isNew = false } = options |
| 82 | + |
| 83 | + try { |
| 84 | + // Load current data |
| 85 | + const githubStars = loadGithubStars() |
| 86 | + const category = getCategoryName(type) |
| 87 | + |
| 88 | + // Ensure category exists |
| 89 | + if (!githubStars[category]) { |
| 90 | + githubStars[category] = {} |
| 91 | + } |
| 92 | + |
| 93 | + // Check if entry already exists |
| 94 | + const exists = id in githubStars[category] |
| 95 | + |
| 96 | + if (isNew && exists) { |
| 97 | + return { |
| 98 | + status: 'skipped', |
| 99 | + message: `Entry "${id}" already exists in github-stars.json under "${category}"`, |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (!isNew && !exists) { |
| 104 | + return { |
| 105 | + status: 'warning', |
| 106 | + message: `Entry "${id}" does not exist in github-stars.json under "${category}", adding as new`, |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Add or update entry with null (stars will be fetched later) |
| 111 | + githubStars[category][id] = null |
| 112 | + |
| 113 | + // Sort entries alphabetically within category |
| 114 | + const sortedCategory = Object.keys(githubStars[category]) |
| 115 | + .sort() |
| 116 | + .reduce((acc, key) => { |
| 117 | + acc[key] = githubStars[category][key] |
| 118 | + return acc |
| 119 | + }, {}) |
| 120 | + |
| 121 | + githubStars[category] = sortedCategory |
| 122 | + |
| 123 | + // Save updated data |
| 124 | + saveGithubStars(githubStars) |
| 125 | + |
| 126 | + return { |
| 127 | + status: 'success', |
| 128 | + message: `Updated github-stars.json: ${category}["${id}"] = null`, |
| 129 | + action: exists ? 'updated' : 'added', |
| 130 | + } |
| 131 | + } catch (error) { |
| 132 | + return { |
| 133 | + status: 'error', |
| 134 | + message: `Failed to update github-stars.json: ${error.message}`, |
| 135 | + error, |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Remove an entry from github-stars.json |
| 142 | + * @param {string} type - Manifest type |
| 143 | + * @param {string} id - Manifest id |
| 144 | + * @returns {Object} Result with status and message |
| 145 | + */ |
| 146 | +export function removeGithubStarsEntry(type, id) { |
| 147 | + try { |
| 148 | + const githubStars = loadGithubStars() |
| 149 | + const category = getCategoryName(type) |
| 150 | + |
| 151 | + if (!githubStars[category] || !(id in githubStars[category])) { |
| 152 | + return { |
| 153 | + status: 'skipped', |
| 154 | + message: `Entry "${id}" not found in github-stars.json under "${category}"`, |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + delete githubStars[category][id] |
| 159 | + saveGithubStars(githubStars) |
| 160 | + |
| 161 | + return { |
| 162 | + status: 'success', |
| 163 | + message: `Removed "${id}" from github-stars.json under "${category}"`, |
| 164 | + } |
| 165 | + } catch (error) { |
| 166 | + return { |
| 167 | + status: 'error', |
| 168 | + message: `Failed to remove entry from github-stars.json: ${error.message}`, |
| 169 | + error, |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * CLI entry point for testing |
| 176 | + */ |
| 177 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 178 | + const [, , command, type, id] = process.argv |
| 179 | + |
| 180 | + if (!command || !['add', 'update', 'remove'].includes(command)) { |
| 181 | + console.error('Usage:') |
| 182 | + console.error(' node github-stars-updater.mjs add <type> <id>') |
| 183 | + console.error(' node github-stars-updater.mjs update <type> <id>') |
| 184 | + console.error(' node github-stars-updater.mjs remove <type> <id>') |
| 185 | + console.error('') |
| 186 | + console.error('Examples:') |
| 187 | + console.error(' node github-stars-updater.mjs add cli cursor-cli') |
| 188 | + console.error(' node github-stars-updater.mjs update extension claude-code') |
| 189 | + console.error(' node github-stars-updater.mjs remove ide windsurf') |
| 190 | + process.exit(1) |
| 191 | + } |
| 192 | + |
| 193 | + if (!type || !id) { |
| 194 | + console.error('Error: type and id are required') |
| 195 | + process.exit(1) |
| 196 | + } |
| 197 | + |
| 198 | + let result |
| 199 | + |
| 200 | + if (command === 'add' || command === 'update') { |
| 201 | + result = updateGithubStarsEntry(type, id, { isNew: command === 'add' }) |
| 202 | + } else { |
| 203 | + result = removeGithubStarsEntry(type, id) |
| 204 | + } |
| 205 | + |
| 206 | + console.log(`Status: ${result.status}`) |
| 207 | + console.log(`Message: ${result.message}`) |
| 208 | + |
| 209 | + if (result.status === 'error') { |
| 210 | + process.exit(1) |
| 211 | + } |
| 212 | +} |
0 commit comments