|
| 1 | +import semver from "semver"; |
| 2 | + |
| 3 | +import process from "node:process"; |
| 4 | +import path from "node:path"; |
| 5 | +import colors from "ansi-colors"; |
| 6 | +import npmFetch from "npm-registry-fetch"; |
| 7 | +import { execShellCommand } from "./shell.mjs"; |
| 8 | +import { readFile } from "./io.mjs"; |
| 9 | + |
| 10 | +function longestCommonPrefix(a, b) { |
| 11 | + let i = 0; |
| 12 | + while (a[i] && b[i] && a[i] === b[i]) { |
| 13 | + i++; |
| 14 | + } |
| 15 | + return i; |
| 16 | +} |
| 17 | + |
| 18 | +async function getCurrentVersion(name) { |
| 19 | + const result = JSON.parse(await readFile(path.resolve(process.env.INIT_CWD, "package.json"))); |
| 20 | + |
| 21 | + if (result.dependencies && name in result.dependencies) { |
| 22 | + return semver.minVersion(result.dependencies[name]).version; |
| 23 | + } |
| 24 | + if (result.devDependencies && name in result.devDependencies) { |
| 25 | + return semver.minVersion(result.devDependencies[name]).version; |
| 26 | + } |
| 27 | + return null; |
| 28 | +} |
| 29 | + |
| 30 | +async function getLatestVersion(name) { |
| 31 | + const registry = npmFetch.pickRegistry(name, {}); |
| 32 | + const url = new URL(encodeURIComponent(name), registry.endsWith("/") ? registry : `${registry}/`); |
| 33 | + |
| 34 | + const stream = npmFetch.json.stream(url.href, "$*", { |
| 35 | + headers: { |
| 36 | + "user-agent": "node/${process.version}", |
| 37 | + accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*", |
| 38 | + spec: name, |
| 39 | + offline: false, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + const versions = {}; |
| 44 | + |
| 45 | + for await (const { key, value } of stream) { |
| 46 | + if (key === "versions") { |
| 47 | + Object.assign(versions, value); |
| 48 | + break; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + let newest = null; |
| 53 | + |
| 54 | + for (const version in versions) { |
| 55 | + const wantedEngine = versions[version].engines?.node; |
| 56 | + |
| 57 | + if (wantedEngine && !semver.satisfies(process.version, wantedEngine)) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + if (!newest || semver.gt(version, newest)) { |
| 62 | + newest = version; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return newest; |
| 67 | +} |
| 68 | + |
| 69 | +export async function run(options) { |
| 70 | + const outdated = []; |
| 71 | + |
| 72 | + for (const name of ["@lastui/dependencies", "@lastui/rocker"]) { |
| 73 | + const current = await getCurrentVersion(name); |
| 74 | + if (!current) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + const latest = await getLatestVersion(name); |
| 79 | + |
| 80 | + if (!latest) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (semver.gt(latest, current)) { |
| 85 | + outdated.push({ name, current, latest }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (outdated.length === 0) { |
| 90 | + console.log(colors.green("Up to date")); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + for (const { name, current, latest } of outdated) { |
| 95 | + const idx = longestCommonPrefix(latest, current); |
| 96 | + |
| 97 | + console.log(`Bumping ${colors.cyan.bold(name)} to ${current.substring(0, idx)}${colors.cyan(latest.substring(idx))}`); |
| 98 | + |
| 99 | + await execShellCommand( |
| 100 | + `npm --prefix=${process.env.INIT_CWD} i --no-progress --package-lock-only --save-exact --no-fund --no-audit ${name}@${latest}`, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + console.log(colors.green("Installing upgrade")); |
| 105 | + await execShellCommand(`npm --prefix=${process.env.INIT_CWD} ci --no-progress --ci --no-fund`); |
| 106 | +} |
0 commit comments