142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
#!/usr/bin/env node
|
|||
|
|
import fs from 'node:fs/promises';
|
||
|
|
import path from 'node:path';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
|
||
|
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
|
const __dirname = path.dirname(__filename);
|
||
|
|
const repoRoot = path.resolve(__dirname, '..', '..');
|
||
|
|
|
||
|
|
const registryBase = 'https://registry.npmjs.org';
|
||
|
|
const sections = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'];
|
||
|
|
|
||
|
|
function shouldSkipSpec(spec) {
|
||
|
|
if (!spec) return true;
|
||
|
|
const lower = spec.toLowerCase();
|
||
|
|
return (
|
||
|
|
lower.startsWith('file:') ||
|
||
|
|
lower.startsWith('workspace:') ||
|
||
|
|
lower.startsWith('link:') ||
|
||
|
|
lower.startsWith('github:') ||
|
||
|
|
lower.startsWith('git+') ||
|
||
|
|
lower.startsWith('http://') ||
|
||
|
|
lower.startsWith('https://') ||
|
||
|
|
lower.startsWith('npm:')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
function isSimpleRange(spec) {
|
||
|
|
if (!spec) return false;
|
||
|
|
if (spec === '*' || spec === 'latest') return true;
|
||
|
|
if (spec.includes(' ') || spec.includes('||')) return false;
|
||
|
|
return /^[~^]?\d+(\.\d+){0,2}([-.][0-9A-Za-z-.]+)?$/.test(spec);
|
||
|
|
}
|
||
|
|
|
||
|
|
function buildNextSpec(currentSpec, latest) {
|
||
|
|
if (currentSpec === '*' || currentSpec === 'latest') {
|
||
|
|
return `^${latest}`;
|
||
|
|
}
|
||
|
|
if (currentSpec.startsWith('~')) return `~${latest}`;
|
||
|
|
if (currentSpec.startsWith('^')) return `^${latest}`;
|
||
|
|
return `^${latest}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchLatestVersion(pkgName) {
|
||
|
|
const encoded = encodeURIComponent(pkgName);
|
||
|
|
const res = await fetch(`${registryBase}/${encoded}`);
|
||
|
|
if (!res.ok) {
|
||
|
|
throw new Error(`Failed to fetch ${pkgName}: ${res.status} ${res.statusText}`);
|
||
|
|
}
|
||
|
|
const data = await res.json();
|
||
|
|
const latest = data?.['dist-tags']?.latest;
|
||
|
|
if (!latest) {
|
||
|
|
throw new Error(`Missing dist-tags.latest for ${pkgName}`);
|
||
|
|
}
|
||
|
|
return latest;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function loadJson(filePath) {
|
||
|
|
const raw = await fs.readFile(filePath, 'utf8');
|
||
|
|
return JSON.parse(raw);
|
||
|
|
}
|
||
|
|
|
||
|
|
async function saveJson(filePath, data) {
|
||
|
|
const next = JSON.stringify(data, null, 2) + '\n';
|
||
|
|
await fs.writeFile(filePath, next, 'utf8');
|
||
|
|
}
|
||
|
|
|
||
|
|
async function updatePackageJson(packageJsonPath) {
|
||
|
|
const pkg = await loadJson(packageJsonPath);
|
||
|
|
let changed = false;
|
||
|
|
const changes = [];
|
||
|
|
|
||
|
|
for (const section of sections) {
|
||
|
|
const deps = pkg[section];
|
||
|
|
if (!deps) continue;
|
||
|
|
|
||
|
|
const entries = Object.entries(deps);
|
||
|
|
for (const [name, spec] of entries) {
|
||
|
|
if (shouldSkipSpec(spec) || !isSimpleRange(spec)) continue;
|
||
|
|
let latest;
|
||
|
|
try {
|
||
|
|
latest = await fetchLatestVersion(name);
|
||
|
|
} catch (err) {
|
||
|
|
console.warn(`[skip] ${name}: ${err.message}`);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
const nextSpec = buildNextSpec(spec, latest);
|
||
|
|
if (nextSpec !== spec) {
|
||
|
|
deps[name] = nextSpec;
|
||
|
|
changed = true;
|
||
|
|
changes.push({ name, from: spec, to: nextSpec, section });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (changed) {
|
||
|
|
await saveJson(packageJsonPath, pkg);
|
||
|
|
}
|
||
|
|
|
||
|
|
return { changed, changes };
|
||
|
|
}
|
||
|
|
|
||
|
|
// MediaEngine has no root package.json / npm workspaces (unlike the monorepo this was extracted
|
||
|
|
// from), so there's no "workspaces" array to discover packages from. webui/ is the only local npm
|
||
|
|
// package whose dependency versions this repo owns and should bump directly. shared/package.json
|
||
|
|
// is intentionally excluded: it's a vendored, byte-for-byte copy synced in from the separate
|
||
|
|
// shared repo (see scripts/sync-shared.mjs) and any local edit to it would just be overwritten by
|
||
|
|
// the next sync — dependency bumps for shared/ belong upstream in that repo instead.
|
||
|
|
const LOCAL_PACKAGE_DIRS = ['media-webui'];
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
const packageJsonPaths = LOCAL_PACKAGE_DIRS.map(dir => path.join(repoRoot, dir, 'package.json'));
|
||
|
|
|
||
|
|
const results = [];
|
||
|
|
for (const pkgPath of packageJsonPaths) {
|
||
|
|
try {
|
||
|
|
const result = await updatePackageJson(pkgPath);
|
||
|
|
results.push({ pkgPath, ...result });
|
||
|
|
} catch (err) {
|
||
|
|
console.error(`[error] ${pkgPath}: ${err.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const updated = results.filter(r => r.changed);
|
||
|
|
const totalChanges = updated.reduce((sum, r) => sum + r.changes.length, 0);
|
||
|
|
|
||
|
|
console.log(`Updated ${updated.length} package.json file(s).`);
|
||
|
|
console.log(`Total dependency updates: ${totalChanges}`);
|
||
|
|
|
||
|
|
for (const { pkgPath, changes } of updated) {
|
||
|
|
const rel = path.relative(repoRoot, pkgPath);
|
||
|
|
console.log(`\n${rel}`);
|
||
|
|
for (const change of changes) {
|
||
|
|
console.log(` ${change.section}: ${change.name} ${change.from} -> ${change.to}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch(err => {
|
||
|
|
console.error(err);
|
||
|
|
process.exitCode = 1;
|
||
|
|
});
|