diff --git a/update-index.mjs b/update-index.mjs new file mode 100644 index 0000000..3067b21 --- /dev/null +++ b/update-index.mjs @@ -0,0 +1,117 @@ +// Unattended index updater. +// +// make-index.mjs reads each plugin repo's live HEAD and rewrites index.json from +// scratch. That is right for a release, but unsafe to run unattended: a plugin +// whose PLUGINS branch lags behind what the index already advertises (today: +// dsh-sansheng-liubu is pinned to release/v1.1.1 while the index says 1.1.2) +// would be downgraded, and plugins missing from PLUGINS would be dropped. +// +// This wrapper regenerates, then merges with a no-downgrade rule and pushes only +// when something really changed. Safe to run on a schedule. +// +// node update-index.mjs # pull, regenerate, merge, commit, push +// node update-index.mjs --dry-run # show what would change, write nothing +import { spawnSync } from 'node:child_process' +import { readFileSync, writeFileSync } from 'node:fs' +import { dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' + +const ROOT = dirname(fileURLToPath(import.meta.url)) +const INDEX = join(ROOT, 'index.json') +const DRY = process.argv.includes('--dry-run') +const LOG = join(ROOT, 'update-index.log') + +function log(message) { + const line = `[${new Date().toISOString()}] ${message}` + console.log(line) + try { + const previous = readFileSync(LOG, 'utf8').split('\n').filter(Boolean).slice(-500).join('\n') + writeFileSync(LOG, `${previous}${previous === '' ? '' : '\n'}${line}\n`, 'utf8') + } catch { + // Logging must never break the update. + } +} + +function run(command, args) { + const result = spawnSync(command, args, { cwd: ROOT, encoding: 'utf8' }) + if (result.status !== 0) { + throw new Error(`${command} ${args.join(' ')} failed (${result.status}): ${(result.stderr || '').trim().slice(0, 400)}`) + } + return (result.stdout || '').trim() +} + +/** Compare dotted versions; returns >0 when left is newer. */ +function compareVersions(left, right) { + const a = String(left ?? '').split('.').map((part) => Number.parseInt(part, 10) || 0) + const b = String(right ?? '').split('.').map((part) => Number.parseInt(part, 10) || 0) + for (let index = 0; index < Math.max(a.length, b.length); index += 1) { + const diff = (a[index] ?? 0) - (b[index] ?? 0) + if (diff !== 0) return diff + } + return 0 +} + +try { + log('update-index: start') + run('git', ['pull', '--ff-only', '--quiet']) + + const before = JSON.parse(readFileSync(INDEX, 'utf8')) + run(process.execPath, [join(ROOT, 'make-index.mjs')]) + const generated = JSON.parse(readFileSync(INDEX, 'utf8')) + + const existing = new Map((before.plugins ?? []).map((plugin) => [plugin.name, plugin])) + const merged = [] + const notes = [] + for (const plugin of generated.plugins ?? []) { + const current = existing.get(plugin.name) + if (current === undefined) { + merged.push(plugin) + notes.push(`add ${plugin.name} ${plugin.versions?.[0]?.version}`) + continue + } + const nextVersion = plugin.versions?.[0]?.version + const currentVersion = current.versions?.[0]?.version + if (compareVersions(nextVersion, currentVersion) < 0) { + merged.push(current) + notes.push(`keep ${plugin.name} ${currentVersion} (regenerator saw ${nextVersion})`) + continue + } + if (compareVersions(nextVersion, currentVersion) === 0 + && plugin.versions?.[0]?.source?.commit === current.versions?.[0]?.source?.commit) { + merged.push(current) + continue + } + merged.push(plugin) + notes.push(`update ${plugin.name} ${currentVersion} -> ${nextVersion}`) + } + // Plugins the regenerator no longer knows about stay in the index. + for (const [name, plugin] of existing) { + if (!merged.some((item) => item.name === name)) { + merged.push(plugin) + notes.push(`keep ${name} (absent from generator)`) + } + } + + const next = { ...generated, plugins: merged } + const text = `${JSON.stringify(next, null, 2)}\n` + const unchanged = text === readFileSync(INDEX, 'utf8') + if (unchanged) { + log('update-index: nothing to do') + process.exit(0) + } + for (const note of notes) log(` ${note}`) + if (DRY) { + log('update-index: dry run, not writing') + process.exit(0) + } + + writeFileSync(INDEX, text, 'utf8') + run('git', ['add', 'index.json']) + run('git', ['-c', 'user.name=newbie', '-c', 'user.email=newbie@git.qyhhh.top', + 'commit', '--quiet', '-m', `market: refresh index\n\n${notes.join('\n')}`]) + run('git', ['push', '--quiet', 'origin', 'HEAD:main']) + log('update-index: pushed') +} catch (error) { + log(`update-index: FAILED ${error instanceof Error ? error.message : String(error)}`) + process.exitCode = 1 +} \ No newline at end of file