UNPKG

upd

Version:

Upgrade NPM Package Dependencies

383 lines (356 loc) 15.5 kB
#!/usr/bin/env node /*! ** UPD -- Upgrade NPM Package Dependencies ** Copyright (c) 2015-2025 Dr. Ralf S. Engelschall <rse@engelschall.com> ** ** Permission is hereby granted, free of charge, to any person obtaining ** a copy of this software and associated documentation files (the ** "Software"), to deal in the Software without restriction, including ** without limitation the rights to use, copy, modify, merge, publish, ** distribute, sublicense, and/or sell copies of the Software, and to ** permit persons to whom the Software is furnished to do so, subject to ** the following conditions: ** ** The above copyright notice and this permission notice shall be included ** in all copies or substantial portions of the Software. ** ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* internal requirements */ import fs from "fs" /* external requirements */ import yargs from "yargs" import chalk from "chalk" import stripAnsi from "strip-ansi" import diff from "fast-diff" import Table from "cli-table3" import escRE from "escape-string-regexp" import micromatch from "micromatch" import updateNotifier from "update-notifier" import semver from "semver" import JsonAsty from "json-asty" import pacote from "pacote" import awaity from "awaity" import Progress from "progress" import prettyBytes from "pretty-bytes" import ducky from "ducky" ;(async () => { /* load my own information */ const packageInfo = JSON.parse(await fs.promises.readFile(new URL("./package.json", import.meta.url))) /* automatic update notification (with 2 days check interval) */ const notifier = updateNotifier({ pkg: packageInfo, updateCheckInterval: 1000 * 60 * 60 * 24 * 2 }) notifier.notify() /* command-line option parsing */ const argv = yargs() /* eslint @stylistic/indent: off */ .usage("Usage: $0 [-h] [-V] [-q] [-n] [-C] [-m <name>] [-f <file>] [-g] [-a] [-c <concurrency>] [<pattern> ...]") .help("h").alias("h", "help").default("h", false) .describe("h", "show usage help") .boolean("V").alias("V", "version").default("V", false) .describe("V", "show program version information") .boolean("q").alias("q", "quiet").default("q", false) .describe("q", "quiet operation (do not output upgrade information)") .boolean("n").alias("n", "nop").default("n", false) .describe("n", "no operation (do not modify package configuration file)") .boolean("C").alias("C", "noColor").default("C", false) .describe("C", "do not use any colors in output") .string("f").nargs("f", 1).alias("f", "file").default("f", "-") .describe("f", "package configuration to use (\"package.json\")") .boolean("g").alias("g", "greatest").default("g", false) .describe("g", "use greatest version (instead of latest stable one)") .boolean("a").alias("a", "all").default("a", false) .describe("a", "show all packages (instead of just updated ones)") .number("c").nargs("c", 1).alias("c", "concurrency").default("c", 8) .describe("c", "number of concurrent network connections to NPM registry") .version(false) .strict() .showHelpOnFail(true) .demand(0) .parse(process.argv.slice(2)) /* short-circuit processing of "-V" command-line option */ if (argv.version) { process.stderr.write(`${packageInfo.name} ${packageInfo.version} <${packageInfo.homepage}>\n`) process.stderr.write(`${packageInfo.description}\n`) process.stderr.write(`Copyright (c) 2015-2025 ${packageInfo.author.name} <${packageInfo.author.url}>\n`) process.stderr.write(`Licensed under ${packageInfo.license} <http://spdx.org/licenses/${packageInfo.license}.html>\n`) process.exit(0) } /* determine configuration file */ if (argv.file === "-") argv.file = "package.json" /* read old configuration file */ if (!fs.existsSync(argv.file)) throw new Error(`cannot find NPM package configuration file under path "${argv.file}"`) let pkgTXT try { pkgTXT = fs.readFileSync(argv.file, { encoding: "utf8" }) } catch (err) { throw new Error(`failed to read NPM package configuration file "${argv.file}": ${err.message}`) } /* parse configuration file content */ const pkgOBJ = JSON.parse(pkgTXT) const pkgAST = JsonAsty.parse(pkgTXT) /* honor embedded command line arguments */ if (pkgOBJ.upd !== undefined) { let args if (ducky.validate(pkgOBJ.upd, "[ string* ]")) args = pkgOBJ.upd else if (ducky.validate(pkgOBJ.upd, "string")) args = pkgOBJ.upd.split(/\s+/) else throw new Error(`invalid field "upd" in configuration file "${argv.file}" ` + "(expected string or array of strings)") argv._ = args.concat(argv._) } /* determine the old NPM module versions (via local package.json) */ const manifest = {} const mixin = (section) => { if (typeof pkgOBJ[section] !== "object") return for (const module of Object.keys(pkgOBJ[section])) { const sOld = pkgOBJ[section][module] let vOld = sOld let state = !( argv._.length === 0 || micromatch( [ module ], (argv._[0].match(/^!/) !== null ? [ "**" ] : []).concat(argv._) ).length > 0 ) ? "ignored" : "todo" if (state === "todo") { /* extract semantic version number from dependency string */ const m = sOld.match(/^\s*(?:[\^~]\s*)?(\d+[^<>=|\s]*)\s*$/) if (m !== null) { vOld = m[1] state = "check" } else state = "skipped" } if (manifest[module] === undefined) manifest[module] = [] manifest[module].push({ section, sOld, vOld, sNew: sOld, vNew: vOld, state }) } } mixin("optionalDependencies") mixin("peerDependencies") mixin("devDependencies") mixin("dependencies") /* helper function for retrieving package.json from NPM registry */ const fetchPackageInfo = (name) => { return pacote.packument(name, { userAgent: `${packageInfo.name}/${packageInfo.version}`, timeout: 20 * 1000 }).catch((err) => { throw new Error(`package information retrival failed: ${err}`) }) } /* pre-compile the package.json AST query (for locating the AST node of a module inside a particular section) */ const astQuery = !argv.nop ? pkgAST.compile(` .// member [ ..// member [ / string [ pos() == 1 && @value == {section} ] ] && / string [ pos() == 1 && @value == {module} ] ] / * [ pos() == 2 ] `) : null /* determine the new NPM module versions (via remote package.json) */ const checked = {} for (const name of Object.keys(manifest)) { for (const spec of manifest[name]) { if (spec.state === "check") checked[name] = true } } const progressMax = Object.keys(checked).length let progressLine = `checking: ${chalk.blue(":bar")} :percent :elapseds :bytes: ${chalk.blue(":msg")} ` if (argv.noColor) progressLine = stripAnsi(progressLine) const progressBar = new Progress(progressLine, { complete: "\u{2588}", incomplete: "\u{254c}", width: 24, total: progressMax, stream: process.stdout, clear: true }) let bytes = 0 const results = await awaity.mapLimit(Object.keys(checked), (name) => { let msg = name if (msg.length > 24) msg = `${msg.substring(0, 19)}...` msg = msg.padEnd(24, " ") return fetchPackageInfo(name.toLowerCase()).then((body) => { bytes += JSON.stringify(body).length progressBar.tick(1, { bytes: prettyBytes(bytes).replace(/ /g, ""), msg }) if (progressBar.complete) process.stderr.write("\r") return { name, data: body } }, (error) => { progressBar.tick(1, { bytes: prettyBytes(bytes).replace(/ /g, ""), msg }) if (progressBar.complete) process.stderr.write("\r") return { name, error } }) }, argv.concurrency) let updates = 0 let errors = 0 for (let i = 0; i < results.length; i++) { const { name, data, error } = results[i] if (error && !data) { manifest[name].forEach((spec) => { if (spec.state === "check") spec.state = "error" }) errors++ continue } let vNew if (argv.greatest) { const versions = Object.keys(data.versions).sort((a, b) => { return semver.rcompare(a, b) }) vNew = versions[0] } else { vNew = data["dist-tags"].latest if (vNew === undefined) throw new Error(`no "latest" version found for module "${name}"`) } for (const spec of manifest[name]) { if (spec.state === "check") { spec.vNew = vNew spec.sNew = vNew if (spec.vOld === spec.vNew) spec.state = "kept" else if (semver.gt(spec.vOld, spec.vNew)) spec.state = "kept" else { spec.state = "updated" updates++ /* update manifest */ const re = new RegExp(escRE(spec.vOld), "") spec.sNew = spec.sOld.replace(re, spec.vNew) if (spec.sNew === spec.sOld) throw new Error(`failed to update module "${name}" version string "${spec.sOld}" ` + `from "${spec.vOld}" to "${spec.vNew}" in manifest`) /* update package.json */ if (!argv.nop) { const nodes = pkgAST.execute(astQuery, { section: spec.section, module: name }) if (nodes.length === 0) throw new Error(`failed to find module "${name}" in section "${spec.section}" ` + "of \"package.json\" AST: no such entry found") if (nodes.length > 1) throw new Error(`failed to locate module "${name}" in section "${spec.section}" ` + "of \"package.json\" AST: multiple entries found") const node = nodes[0] node.set({ body: JSON.stringify(spec.sNew), value: spec.sNew }) } } } } } /* utility function: mark a piece of text against another one */ const mark = (color, text, other) => { const result = diff(text, other) let output = "" for (const chunk of result) { if (chunk[0] === diff.INSERT) output += chalk[color](chunk[1]) else if (chunk[0] === diff.EQUAL) output += chunk[1] } return output } /* prepare for a nice-looking table output of the dependency upgrades */ let table = new Table({ head: [ chalk.reset.bold("MODULE NAME"), chalk.reset.bold("VERSION ") + chalk.red.bold("OLD"), chalk.reset.bold("VERSION ") + chalk.green.bold("NEW"), chalk.reset.bold("STATE") ], colWidths: [ 37, 14, 14, 9 ], style: { "padding-left": 1, "padding-right": 1, border: [ "grey" ], compact: true }, chars: { "left-mid": "", mid: "", "mid-mid": "", "right-mid": "" } }) /* iterate over all the dependencies */ for (const name of Object.keys(manifest)) { for (const spec of manifest[name]) { /* short-circuit processing */ if (!(spec.state === "updated" || spec.state === "error" || argv.all)) continue /* determine module name column */ const module = spec.state === "updated" ? chalk.reset(name) : chalk.grey(name) /* determine older/newer columns */ const older = spec.state === "updated" ? mark("red", spec.sNew, spec.sOld) : chalk.grey(spec.sOld) const newer = spec.state === "updated" ? mark("green", spec.sOld, spec.sNew) : chalk.grey(spec.sNew) /* determine state column */ let state if (argv.nop) state = chalk.grey("kept") else if (spec.state === "updated") state = chalk.green(spec.state) else if (spec.state === "error") state = chalk.red(spec.state) else state = chalk.grey(spec.state) /* print the module name, new and old version */ table.push([ module, older, newer, state ]) } } if (!argv.quiet && (updates || errors || argv.all)) { let output = table.toString() if (argv.noColor) output = stripAnsi(output) process.stdout.write(output + "\n") } /* display total results */ if (!argv.quiet && !(updates || errors || argv.all)) { table = new Table({ head: [], colWidths: [ 77 ], colAligns: [ "center" ], style: { "padding-left": 1, "padding-right": 1, border: [ "grey" ], compact: true }, chars: { "left-mid": "", mid: "", "mid-mid": "", "right-mid": "" } }) table.push([ chalk.green("ALL PACKAGE DEPENDENCIES UP-TO-DATE") ]) let output = table.toString() if (argv.noColor) output = stripAnsi(output) process.stdout.write(output + "\n") } /* write new configuration file */ if (updates && !argv.nop) { pkgTXT = JsonAsty.unparse(pkgAST) try { fs.writeFileSync(argv.file, pkgTXT, { encoding: "utf8" }) } catch (err) { throw new Error(`failed to write NPM package configuration file "${argv.file}": ${err.message}`) } } })().catch((err) => { /* fatal error */ process.stderr.write(chalk.red("ERROR:") + " " + err.stack + "\n") process.exit(1) })