2017-12-09 07:45:06 +00:00
|
|
|
#!/usr/bin/env node
|
2017-12-03 11:15:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2020-03-10 19:50:59 +00:00
|
|
|
const ansiRegex = require("ansi-regex")();
|
2020-03-25 23:06:31 +00:00
|
|
|
const dns = require("dns");
|
2020-03-08 13:40:48 +00:00
|
|
|
const fetch = require("make-fetch-happen");
|
|
|
|
const minimist = require("minimist");
|
2020-03-08 13:45:20 +00:00
|
|
|
const rat = require("registry-auth-token");
|
2020-03-08 13:40:48 +00:00
|
|
|
const rc = require("rc");
|
2020-03-08 13:45:20 +00:00
|
|
|
const ru = require("registry-auth-token/registry-url");
|
2020-03-08 13:40:48 +00:00
|
|
|
const semver = require("semver");
|
|
|
|
const textTable = require("text-table");
|
2020-03-25 23:06:31 +00:00
|
|
|
const {cwd: cwdFn, stdout, argv, env, exit} = require("process");
|
2020-03-08 13:45:20 +00:00
|
|
|
const {fromUrl} = require("hosted-git-info");
|
2020-03-08 14:59:50 +00:00
|
|
|
const {join, dirname} = require("path");
|
|
|
|
const {lstatSync, readFileSync, truncateSync, writeFileSync, accessSync} = require("fs");
|
2020-03-08 13:40:48 +00:00
|
|
|
const {platform} = require("os");
|
|
|
|
|
2020-03-25 23:06:31 +00:00
|
|
|
env.NODE_ENV = "production";
|
2020-03-08 13:45:20 +00:00
|
|
|
|
2020-03-09 20:54:27 +00:00
|
|
|
const MAX_SOCKETS = 96;
|
2019-12-07 18:43:13 +00:00
|
|
|
const sep = "\0";
|
2020-03-08 14:59:50 +00:00
|
|
|
const cwd = cwdFn();
|
2018-08-23 20:28:33 +00:00
|
|
|
|
2019-12-04 20:15:47 +00:00
|
|
|
// regexes for url dependencies. does only github and only hash or exact semver
|
2019-12-07 18:50:24 +00:00
|
|
|
// https://regex101.com/r/gCZzfK/2
|
2019-12-04 20:15:47 +00:00
|
|
|
const stripRe = /^.*?:\/\/(.*?@)?(github\.com[:/])/i;
|
2020-04-20 09:03:01 +00:00
|
|
|
const partsRe = /^([^/]+)\/([^/#]+)?.*?\/([0-9a-f]+|v?[0-9]+\.[0-9]+\.[0-9]+)$/i;
|
|
|
|
const hashRe = /^[0-9a-f]{7,}$/i;
|
2019-12-04 20:15:47 +00:00
|
|
|
|
2020-03-08 14:54:40 +00:00
|
|
|
const memoize = fn => {
|
2020-03-08 13:45:20 +00:00
|
|
|
const cache = {};
|
|
|
|
return (arg, arg2) => cache[arg] || (cache[arg] = fn(arg, arg2));
|
|
|
|
};
|
|
|
|
|
|
|
|
const esc = str => str.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
|
2020-03-08 16:37:35 +00:00
|
|
|
const hostedGitInfo = memoize(fromUrl);
|
2020-03-08 13:45:20 +00:00
|
|
|
const registryAuthToken = memoize(rat);
|
|
|
|
const registryUrl = memoize(ru);
|
2020-03-08 18:12:16 +00:00
|
|
|
const normalizeUrl = memoize(url => url.endsWith("/") ? url.substring(0, url.length - 1) : url);
|
2020-03-08 13:45:20 +00:00
|
|
|
|
2020-03-10 20:23:07 +00:00
|
|
|
// dns cache
|
|
|
|
const cache = {};
|
|
|
|
const waiting = {};
|
|
|
|
const originalLookup = dns.lookup;
|
|
|
|
dns.lookup = (hostname, opts, callback) => {
|
|
|
|
if (!callback) {
|
|
|
|
callback = opts;
|
|
|
|
opts = undefined;
|
|
|
|
}
|
|
|
|
if (cache[hostname]) {
|
|
|
|
callback(...cache[hostname]);
|
|
|
|
} else {
|
|
|
|
if (!waiting[hostname]) {
|
|
|
|
waiting[hostname] = [callback];
|
|
|
|
originalLookup(hostname, opts, (...args) => {
|
|
|
|
if (!cache[hostname]) cache[hostname] = args;
|
|
|
|
waiting[hostname].forEach(callback => callback(...args));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
waiting[hostname].push(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 23:06:31 +00:00
|
|
|
const args = minimist(argv.slice(2), {
|
2018-02-21 19:44:13 +00:00
|
|
|
boolean: [
|
2018-09-02 19:43:16 +00:00
|
|
|
"c", "color",
|
2018-10-15 06:05:54 +00:00
|
|
|
"E", "error-on-outdated",
|
2019-07-12 14:37:24 +00:00
|
|
|
"U", "error-on-unchanged",
|
2018-09-02 19:43:16 +00:00
|
|
|
"h", "help",
|
|
|
|
"j", "json",
|
|
|
|
"n", "no-color",
|
2018-10-04 05:58:20 +00:00
|
|
|
"u", "update",
|
2018-09-02 19:43:16 +00:00
|
|
|
"v", "version",
|
2018-02-21 19:44:13 +00:00
|
|
|
],
|
2018-07-24 07:40:47 +00:00
|
|
|
string: [
|
2019-11-19 19:52:28 +00:00
|
|
|
"d", "allow-downgrade",
|
2018-09-02 19:43:16 +00:00
|
|
|
"f", "file",
|
|
|
|
"g", "greatest",
|
2020-03-08 18:12:16 +00:00
|
|
|
"G", "githubapi",
|
2018-12-21 22:17:39 +00:00
|
|
|
"m", "minor",
|
|
|
|
"P", "patch",
|
2018-09-02 19:43:16 +00:00
|
|
|
"p", "prerelease",
|
2019-02-25 21:46:39 +00:00
|
|
|
"R", "release",
|
2018-09-02 19:43:16 +00:00
|
|
|
"r", "registry",
|
2018-10-16 15:18:06 +00:00
|
|
|
"t", "types",
|
2018-07-24 07:40:47 +00:00
|
|
|
],
|
2018-02-21 19:44:13 +00:00
|
|
|
alias: {
|
2018-07-10 15:32:19 +00:00
|
|
|
c: "color",
|
2019-11-19 19:52:28 +00:00
|
|
|
d: "allow-downgrade",
|
2018-10-15 06:05:54 +00:00
|
|
|
E: "error-on-outdated",
|
2019-07-12 14:37:24 +00:00
|
|
|
U: "error-on-unchanged",
|
2018-10-16 15:18:06 +00:00
|
|
|
e: "exclude",
|
2018-08-23 20:40:19 +00:00
|
|
|
f: "file",
|
2018-07-19 21:28:41 +00:00
|
|
|
g: "greatest",
|
2020-03-08 18:12:16 +00:00
|
|
|
G: "githubapi",
|
2018-05-31 17:33:22 +00:00
|
|
|
h: "help",
|
|
|
|
i: "include",
|
2018-02-21 19:44:13 +00:00
|
|
|
j: "json",
|
2018-12-21 22:17:39 +00:00
|
|
|
m: "minor",
|
2018-07-10 15:32:19 +00:00
|
|
|
n: "no-color",
|
2018-12-21 22:17:39 +00:00
|
|
|
P: "patch",
|
2018-05-31 17:33:22 +00:00
|
|
|
p: "prerelease",
|
2018-07-24 07:40:47 +00:00
|
|
|
r: "registry",
|
2019-02-25 21:46:39 +00:00
|
|
|
R: "release",
|
2018-11-19 09:18:32 +00:00
|
|
|
s: "semver",
|
2019-07-13 16:41:30 +00:00
|
|
|
S: "sockets",
|
2018-10-16 15:18:06 +00:00
|
|
|
t: "types",
|
2018-05-31 17:33:22 +00:00
|
|
|
u: "update",
|
2018-02-21 19:44:13 +00:00
|
|
|
v: "version",
|
2018-04-30 06:31:42 +00:00
|
|
|
},
|
2017-12-08 13:39:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (args.help) {
|
2020-03-25 23:06:31 +00:00
|
|
|
stdout.write(`usage: updates [options]
|
2017-12-06 18:05:53 +00:00
|
|
|
|
2017-12-03 11:15:02 +00:00
|
|
|
Options:
|
2019-11-19 19:52:28 +00:00
|
|
|
-u, --update Update versions and write package.json
|
|
|
|
-p, --prerelease [<pkg,...>] Consider prerelease versions
|
|
|
|
-R, --release [<pkg,...>] Only use release versions, may downgrade
|
|
|
|
-g, --greatest [<pkg,...>] Prefer greatest over latest version
|
|
|
|
-i, --include <pkg,...> Include only given packages
|
|
|
|
-e, --exclude <pkg,...> Exclude given packages
|
|
|
|
-t, --types <type,...> Check only given dependency types
|
|
|
|
-P, --patch [<pkg,...>] Consider only up to semver-patch
|
|
|
|
-m, --minor [<pkg,...>] Consider only up to semver-minor
|
|
|
|
-d, --allow-downgrade [<pkg,...>] Allow version downgrades when using latest version
|
2020-04-07 15:44:57 +00:00
|
|
|
-E, --error-on-outdated Exit with code 2 when updates are available and 0 when not
|
|
|
|
-U, --error-on-unchanged Exit with code 0 when updates are available and 2 when not
|
2019-11-19 19:52:28 +00:00
|
|
|
-r, --registry <url> Override npm registry URL
|
2020-03-09 16:55:59 +00:00
|
|
|
-G, --githubapi <url> Override Github API URL
|
2019-11-19 19:52:28 +00:00
|
|
|
-f, --file <path> Use given package.json file or module directory
|
|
|
|
-S, --sockets <num> Maximum number of parallel HTTP sockets opened. Default: ${MAX_SOCKETS}
|
|
|
|
-j, --json Output a JSON object
|
|
|
|
-c, --color Force-enable color output
|
|
|
|
-n, --no-color Disable color output
|
|
|
|
-v, --version Print the version
|
|
|
|
-h, --help Print this help
|
2018-03-03 19:39:39 +00:00
|
|
|
|
2017-12-03 11:15:02 +00:00
|
|
|
Examples:
|
|
|
|
$ updates
|
2017-12-03 11:35:25 +00:00
|
|
|
$ updates -u
|
2019-07-19 13:34:04 +00:00
|
|
|
$ updates -u -m -e eslint
|
2019-07-13 16:06:15 +00:00
|
|
|
$ updates -u -U && rm -rf node_modules && npm i
|
2018-03-03 19:39:39 +00:00
|
|
|
`);
|
2020-03-25 23:06:31 +00:00
|
|
|
exit(0);
|
2017-12-06 18:05:53 +00:00
|
|
|
}
|
2017-12-03 11:15:02 +00:00
|
|
|
|
2017-12-08 13:39:52 +00:00
|
|
|
if (args.version) {
|
2020-03-09 16:50:35 +00:00
|
|
|
console.info(require("./package.json").version);
|
2020-03-25 23:06:31 +00:00
|
|
|
exit(0);
|
2017-12-06 18:11:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 13:54:17 +00:00
|
|
|
if (args["no-color"]) {
|
2020-03-25 23:06:31 +00:00
|
|
|
env.NO_COLOR = "0";
|
|
|
|
} else if (args["color"] || stdout.isTTY === undefined) { // winpty compat
|
|
|
|
env.FORCE_COLOR = "1";
|
2019-01-31 13:54:17 +00:00
|
|
|
}
|
2020-03-09 20:44:05 +00:00
|
|
|
const {gray, green, red} = require("colorette");
|
2017-12-09 05:49:35 +00:00
|
|
|
|
2018-09-02 19:43:16 +00:00
|
|
|
const greatest = parseMixedArg(args.greatest);
|
2018-10-04 05:09:13 +00:00
|
|
|
const prerelease = parseMixedArg(args.prerelease);
|
2019-02-25 21:46:39 +00:00
|
|
|
const release = parseMixedArg(args.release);
|
2018-12-21 22:17:39 +00:00
|
|
|
const patch = parseMixedArg(args.patch);
|
|
|
|
const minor = parseMixedArg(args.minor);
|
2019-11-19 19:52:28 +00:00
|
|
|
const allowDowngrade = parseMixedArg(args["allow-downgrade"]);
|
2017-12-03 11:15:02 +00:00
|
|
|
|
2019-06-29 14:44:43 +00:00
|
|
|
const defaultRegistry = "https://registry.npmjs.org";
|
2020-03-08 13:40:48 +00:00
|
|
|
const npmrc = rc("npm", {registry: defaultRegistry});
|
2019-07-13 16:03:47 +00:00
|
|
|
const authTokenOpts = {npmrc, recursive: true};
|
2020-03-08 18:12:16 +00:00
|
|
|
const registry = normalizeUrl(args.registry || npmrc.registry);
|
2019-07-13 16:41:30 +00:00
|
|
|
const maxSockets = typeof args.sockets === "number" ? args.sockets : MAX_SOCKETS;
|
2020-03-08 18:12:16 +00:00
|
|
|
const githubApiUrl = args.githubapi ? normalizeUrl(args.githubapi) : "https://api.github.com";
|
2018-11-27 22:42:10 +00:00
|
|
|
|
|
|
|
let packageFile;
|
2020-03-13 19:55:15 +00:00
|
|
|
const deps = {};
|
|
|
|
const maybeUrlDeps = {};
|
|
|
|
|
2018-11-27 22:42:10 +00:00
|
|
|
if (args.file) {
|
2019-01-19 17:45:23 +00:00
|
|
|
let stat;
|
|
|
|
try {
|
2020-03-08 13:40:48 +00:00
|
|
|
stat = lstatSync(args.file);
|
2019-01-19 17:45:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
finish(new Error(`Unable to open ${args.file}: ${err.message}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat && stat.isFile()) {
|
2018-11-27 22:42:10 +00:00
|
|
|
packageFile = args.file;
|
2019-01-19 17:45:23 +00:00
|
|
|
} else if (stat && stat.isDirectory()) {
|
2020-03-08 13:40:48 +00:00
|
|
|
packageFile = join(args.file, "package.json");
|
2019-01-19 17:45:23 +00:00
|
|
|
} else {
|
|
|
|
finish(new Error(`${args.file} is neither a file nor directory`));
|
2018-11-27 22:42:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-08 14:59:50 +00:00
|
|
|
packageFile = findSync("package.json", cwd);
|
2019-05-29 20:04:21 +00:00
|
|
|
if (!packageFile) {
|
2020-03-08 14:59:50 +00:00
|
|
|
finish(new Error(`Unable to find package.json in ${cwd} or any of its parents`));
|
2019-05-29 20:04:21 +00:00
|
|
|
}
|
2018-11-27 22:42:10 +00:00
|
|
|
}
|
2017-12-03 11:15:02 +00:00
|
|
|
|
2018-10-16 15:18:06 +00:00
|
|
|
let dependencyTypes;
|
|
|
|
if (args.types) {
|
|
|
|
dependencyTypes = Array.isArray(args.types) ? args.types : args.types.split(",");
|
|
|
|
} else {
|
2018-10-16 21:30:11 +00:00
|
|
|
dependencyTypes = [
|
|
|
|
"dependencies",
|
|
|
|
"devDependencies",
|
|
|
|
"peerDependencies",
|
|
|
|
"optionalDependencies",
|
|
|
|
];
|
2018-10-16 15:18:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:05:53 +00:00
|
|
|
let pkg, pkgStr;
|
|
|
|
|
|
|
|
try {
|
2020-03-08 13:40:48 +00:00
|
|
|
pkgStr = readFileSync(packageFile, "utf8");
|
2017-12-06 18:05:53 +00:00
|
|
|
} catch (err) {
|
2018-05-31 17:33:22 +00:00
|
|
|
finish(new Error(`Unable to open package.json: ${err.message}`));
|
2017-12-06 18:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
pkg = JSON.parse(pkgStr);
|
|
|
|
} catch (err) {
|
2018-05-31 17:33:22 +00:00
|
|
|
finish(new Error(`Error parsing package.json: ${err.message}`));
|
2017-12-06 18:05:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 10:23:39 +00:00
|
|
|
function flat(arr) {
|
|
|
|
return [].concat(...arr);
|
|
|
|
}
|
|
|
|
|
2018-04-30 07:11:31 +00:00
|
|
|
let include, exclude;
|
2020-04-20 10:23:39 +00:00
|
|
|
if (args.include && args.include !== true) include = new Set(flat((Array.isArray(args.include) ? args.include : [args.include]).map(item => item.split(","))));
|
|
|
|
if (args.exclude && args.exclude !== true) exclude = new Set(flat((Array.isArray(args.exclude) ? args.exclude : [args.exclude]).map(item => item.split(","))));
|
2020-03-09 21:24:57 +00:00
|
|
|
|
|
|
|
function canInclude(name) {
|
|
|
|
if (exclude && exclude.has(name)) return false;
|
|
|
|
if (include && !include.has(name)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const depType of dependencyTypes) {
|
|
|
|
for (const [name, value] of Object.entries(pkg[depType] || {})) {
|
|
|
|
if (semver.validRange(value) && canInclude(name)) {
|
|
|
|
deps[`${depType}${sep}${name}`] = {old: value};
|
|
|
|
} else if (canInclude(name)) {
|
|
|
|
maybeUrlDeps[`${depType}${sep}${name}`] = {old: value};
|
2018-08-23 20:28:33 +00:00
|
|
|
}
|
2017-12-03 11:15:02 +00:00
|
|
|
}
|
2018-08-23 20:28:33 +00:00
|
|
|
}
|
2017-12-03 11:15:02 +00:00
|
|
|
|
2020-03-09 21:24:57 +00:00
|
|
|
if (!Object.keys(deps).length && !Object.keys(maybeUrlDeps).length) {
|
2018-10-04 05:58:20 +00:00
|
|
|
if (include || exclude) {
|
2018-10-04 05:27:11 +00:00
|
|
|
finish(new Error("No packages match the given filters"));
|
2018-08-25 07:43:37 +00:00
|
|
|
} else {
|
|
|
|
finish(new Error("No packages found"));
|
|
|
|
}
|
2018-04-30 07:05:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:06:58 +00:00
|
|
|
const timeData = [
|
2020-03-08 16:49:43 +00:00
|
|
|
[1e3, 1, "ns"],
|
|
|
|
[1e6, 1e3, "µs"],
|
|
|
|
[1e9, 1e6, "ms"],
|
|
|
|
[60e9, 1e9, "sec"],
|
|
|
|
[3600e9, 60e9, "min"],
|
|
|
|
[86400e9, 3600e9, "hour"],
|
|
|
|
[2592e12, 86400e9, "day"],
|
|
|
|
[31536e12, 2592e12, "month"],
|
|
|
|
[Infinity, 31536e12, "year"],
|
2020-03-08 16:06:58 +00:00
|
|
|
];
|
|
|
|
|
2020-03-08 16:36:23 +00:00
|
|
|
function getAge(isoDateString) {
|
|
|
|
if (!isoDateString) return "";
|
|
|
|
const unix = new Date(isoDateString).getTime() * 1e6;
|
2020-03-08 16:06:58 +00:00
|
|
|
if (Number.isNaN(unix)) return "";
|
|
|
|
const diff = (Date.now() * 1e6) - unix;
|
2020-03-08 16:36:23 +00:00
|
|
|
if (diff <= 0) return "none";
|
2020-03-08 16:06:58 +00:00
|
|
|
|
|
|
|
let value, suffix;
|
|
|
|
for (let i = 0; i <= timeData.length; i++) {
|
|
|
|
const entry = timeData[i];
|
2020-03-08 16:49:43 +00:00
|
|
|
const [end, start, unit] = entry || [];
|
2020-03-08 16:06:58 +00:00
|
|
|
if (entry && end && diff < end) {
|
|
|
|
value = Math.round(diff / start);
|
2020-03-08 16:49:43 +00:00
|
|
|
suffix = `${unit}${(value > 1 && !unit.endsWith("s")) ? "s" : ""}`;
|
2020-03-08 16:06:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${value} ${suffix}`;
|
|
|
|
}
|
|
|
|
|
2020-03-08 14:59:50 +00:00
|
|
|
function findSync(filename, dir, stopDir) {
|
|
|
|
const path = join(dir, filename);
|
|
|
|
|
|
|
|
try {
|
|
|
|
accessSync(path);
|
|
|
|
return path;
|
2020-03-25 23:06:31 +00:00
|
|
|
} catch {}
|
2020-03-08 14:59:50 +00:00
|
|
|
|
|
|
|
const parent = dirname(dir);
|
|
|
|
if ((stopDir && path === stopDir) || parent === dir) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2020-03-08 18:12:16 +00:00
|
|
|
return findSync(filename, parent, stopDir);
|
2020-03-08 14:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 21:25:10 +00:00
|
|
|
function getAuthAndRegistry(name, registry) {
|
|
|
|
if (!name.startsWith("@")) {
|
2019-07-13 16:03:47 +00:00
|
|
|
return [registryAuthToken(registry, authTokenOpts), registry];
|
2019-06-28 21:25:10 +00:00
|
|
|
} else {
|
|
|
|
const scope = (/@[a-z0-9][\w-.]+/.exec(name) || [])[0];
|
2020-03-08 18:12:16 +00:00
|
|
|
const url = normalizeUrl(registryUrl(scope, npmrc));
|
2019-06-28 21:25:10 +00:00
|
|
|
if (url !== registry) {
|
|
|
|
try {
|
2019-07-13 16:03:47 +00:00
|
|
|
const newAuth = registryAuthToken(url, authTokenOpts);
|
2019-06-28 21:25:10 +00:00
|
|
|
if (newAuth && newAuth.token) {
|
|
|
|
return [newAuth, url];
|
|
|
|
}
|
2020-03-25 23:06:31 +00:00
|
|
|
} catch {
|
2019-07-13 16:03:47 +00:00
|
|
|
return [registryAuthToken(registry, authTokenOpts), registry];
|
2019-06-27 17:15:01 +00:00
|
|
|
}
|
2019-06-28 21:25:10 +00:00
|
|
|
} else {
|
2019-07-13 16:03:47 +00:00
|
|
|
return [registryAuthToken(registry, authTokenOpts), registry];
|
2019-06-27 17:15:01 +00:00
|
|
|
}
|
2019-02-11 18:39:40 +00:00
|
|
|
}
|
2019-06-27 17:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetchFromRegistry(name, registry, auth) {
|
2020-03-09 22:57:04 +00:00
|
|
|
const opts = {
|
|
|
|
maxSockets,
|
|
|
|
cacheManager: null,
|
2020-03-10 19:50:59 +00:00
|
|
|
integrity: null,
|
2020-03-09 22:57:04 +00:00
|
|
|
retry: 5,
|
|
|
|
};
|
|
|
|
|
2019-07-13 16:41:30 +00:00
|
|
|
if (auth && auth.token) {
|
|
|
|
opts.headers = {Authorization: `${auth.type} ${auth.token}`};
|
|
|
|
}
|
2020-03-09 20:54:27 +00:00
|
|
|
return fetch(`${registry}/${name.replace(/\//g, "%2f")}`, opts);
|
2019-02-11 18:39:40 +00:00
|
|
|
}
|
2019-02-07 18:21:17 +00:00
|
|
|
|
2019-09-08 19:50:57 +00:00
|
|
|
const get = async (name, type, originalRegistry) => {
|
2019-06-27 17:15:01 +00:00
|
|
|
const [auth, registry] = getAuthAndRegistry(name, originalRegistry);
|
|
|
|
|
2020-03-09 20:54:27 +00:00
|
|
|
const res = await fetchFromRegistry(name, registry, auth);
|
2019-06-29 14:44:43 +00:00
|
|
|
if (res && res.ok) {
|
2019-09-08 19:50:57 +00:00
|
|
|
return [await res.json(), type, registry];
|
2020-03-09 20:54:27 +00:00
|
|
|
} else {
|
|
|
|
if (res && res.status && res.statusText) {
|
|
|
|
throw new Error(`Received ${res.status} ${res.statusText} for ${name} from ${registry}`);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Unable to fetch ${name} from ${registry}`);
|
2019-02-11 18:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 20:41:46 +00:00
|
|
|
};
|
|
|
|
|
2019-06-27 17:15:01 +00:00
|
|
|
const getInfoUrl = ({repository, homepage}, registry, name) => {
|
|
|
|
if (registry === "https://npm.pkg.github.com") {
|
|
|
|
return `https://github.com/${name.replace(/^@/, "")}`;
|
|
|
|
} else if (repository) {
|
2019-07-05 16:35:26 +00:00
|
|
|
const url = typeof repository === "string" ? repository : repository.url;
|
2020-03-08 16:37:35 +00:00
|
|
|
const info = hostedGitInfo(url);
|
2018-11-27 22:34:57 +00:00
|
|
|
if (info && info.browse) return info.browse();
|
2019-07-19 13:32:04 +00:00
|
|
|
if (repository && repository.url && /^https?:/.test(repository.url)) return repository.url;
|
2018-11-20 12:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return homepage || "";
|
|
|
|
};
|
|
|
|
|
2019-05-09 18:27:11 +00:00
|
|
|
function finish(obj, opts = {}) {
|
2017-12-08 13:39:52 +00:00
|
|
|
const output = {};
|
2018-07-24 17:41:44 +00:00
|
|
|
const hadError = obj instanceof Error;
|
|
|
|
|
2017-12-08 13:39:52 +00:00
|
|
|
if (typeof obj === "string") {
|
|
|
|
output.message = obj;
|
2018-07-24 17:41:44 +00:00
|
|
|
} else if (hadError) {
|
2018-11-19 09:18:32 +00:00
|
|
|
output.error = obj.stack;
|
2017-12-08 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:12:16 +00:00
|
|
|
for (const value of Object.values(deps)) {
|
|
|
|
if ("oldPrint" in value) {
|
|
|
|
value.old = value.oldPrint;
|
|
|
|
delete value.oldPrint;
|
|
|
|
}
|
|
|
|
if ("newPrint" in value) {
|
|
|
|
value.new = value.newPrint;
|
|
|
|
delete value.newPrint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:39:52 +00:00
|
|
|
if (args.json) {
|
2018-07-24 17:41:44 +00:00
|
|
|
if (!hadError) {
|
2019-09-08 19:50:57 +00:00
|
|
|
output.results = {};
|
|
|
|
for (const [key, value] of Object.entries(deps)) {
|
2019-12-07 18:43:13 +00:00
|
|
|
const [type, name] = key.split(sep);
|
2019-09-08 19:50:57 +00:00
|
|
|
if (!output.results[type]) output.results[type] = {};
|
|
|
|
output.results[type][name] = value;
|
|
|
|
}
|
2018-07-24 17:41:44 +00:00
|
|
|
}
|
2018-10-25 20:36:41 +00:00
|
|
|
console.info(JSON.stringify(output));
|
2017-12-03 12:17:31 +00:00
|
|
|
} else {
|
2018-07-24 17:41:44 +00:00
|
|
|
if (Object.keys(deps).length && !hadError) {
|
2018-03-03 19:47:48 +00:00
|
|
|
console.info(formatDeps(deps));
|
2017-12-08 13:39:52 +00:00
|
|
|
}
|
|
|
|
if (output.message || output.error) {
|
2019-05-29 20:42:25 +00:00
|
|
|
if (output.message) {
|
|
|
|
console.info(output.message);
|
|
|
|
} else if (output.error) {
|
|
|
|
const lines = output.error.split(/\r?\n/);
|
|
|
|
for (const [index, line] of Object.entries(lines)) {
|
2020-03-09 16:36:15 +00:00
|
|
|
console.info((index === "0" ? red : gray)(line));
|
2019-05-29 20:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-03 12:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 13:39:52 +00:00
|
|
|
|
2018-10-15 06:05:54 +00:00
|
|
|
if (args["error-on-outdated"]) {
|
2020-03-25 23:06:31 +00:00
|
|
|
exit(Object.keys(deps).length ? 2 : 0);
|
2019-07-12 14:37:24 +00:00
|
|
|
} else if (args["error-on-unchanged"]) {
|
2020-03-25 23:06:31 +00:00
|
|
|
exit(Object.keys(deps).length ? 0 : 2);
|
2018-10-15 04:52:37 +00:00
|
|
|
} else {
|
2020-03-25 23:06:31 +00:00
|
|
|
exit(opts.exitCode || (output.error ? 1 : 0));
|
2018-10-15 04:52:37 +00:00
|
|
|
}
|
2017-12-08 13:39:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 23:06:31 +00:00
|
|
|
// preserve file metadata on windows
|
2019-08-20 17:54:07 +00:00
|
|
|
function write(file, content) {
|
2020-03-25 23:06:31 +00:00
|
|
|
const isWindows = platform() === "win32";
|
|
|
|
if (isWindows) truncateSync(file, 0);
|
|
|
|
writeFileSync(file, content, isWindows ? {flag: "r+"} : undefined);
|
2019-08-20 17:54:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-03 13:12:30 +00:00
|
|
|
function highlightDiff(a, b, added) {
|
|
|
|
const aParts = a.split(/\./);
|
|
|
|
const bParts = b.split(/\./);
|
2020-03-09 16:36:15 +00:00
|
|
|
const color = added ? green : red;
|
2018-07-30 16:55:32 +00:00
|
|
|
const versionPartRe = /^[0-9a-zA-Z-.]+$/;
|
2017-12-03 13:12:30 +00:00
|
|
|
let res = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < aParts.length; i++) {
|
|
|
|
if (aParts[i] !== bParts[i]) {
|
2018-07-10 21:04:08 +00:00
|
|
|
if (versionPartRe.test(aParts[i])) {
|
2018-07-25 09:53:50 +00:00
|
|
|
res += color(aParts.slice(i).join("."));
|
2017-12-03 13:12:30 +00:00
|
|
|
} else {
|
2018-04-30 08:42:39 +00:00
|
|
|
res += aParts[i].split("").map(char => {
|
2018-08-23 20:28:33 +00:00
|
|
|
return versionPartRe.test(char) ? color(char) : char;
|
2020-03-08 10:00:46 +00:00
|
|
|
}).join("") + color(`.${aParts.slice(i + 1).join(".")}`);
|
2017-12-03 13:12:30 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-07-10 15:19:11 +00:00
|
|
|
} else {
|
2020-03-08 10:00:46 +00:00
|
|
|
res += `${aParts[i]}.`;
|
2018-07-10 15:19:11 +00:00
|
|
|
}
|
2017-12-03 13:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-12-08 13:39:52 +00:00
|
|
|
function formatDeps() {
|
2020-03-08 16:06:58 +00:00
|
|
|
const arr = [["NAME", "OLD", "NEW", "AGE", "INFO"]];
|
2019-02-04 18:12:15 +00:00
|
|
|
|
2019-09-08 19:50:57 +00:00
|
|
|
for (const [key, data] of Object.entries(deps)) {
|
2019-12-07 18:43:13 +00:00
|
|
|
const [_type, name] = key.split(sep);
|
2019-09-08 19:50:57 +00:00
|
|
|
arr.push([
|
|
|
|
name,
|
2020-03-08 18:12:16 +00:00
|
|
|
highlightDiff(data.old, data.new, false),
|
|
|
|
highlightDiff(data.new, data.old, true),
|
2020-03-08 16:06:58 +00:00
|
|
|
data.age || "",
|
2019-09-08 19:50:57 +00:00
|
|
|
data.info,
|
|
|
|
]);
|
|
|
|
}
|
2019-02-04 18:12:15 +00:00
|
|
|
|
2020-03-08 13:40:48 +00:00
|
|
|
return textTable(arr, {
|
2020-03-10 19:50:59 +00:00
|
|
|
hsep: " ",
|
|
|
|
stringLength: str => str.replace(ansiRegex, "").length,
|
2017-12-03 11:15:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-08 19:50:57 +00:00
|
|
|
function updatePackageJson() {
|
2017-12-03 11:15:02 +00:00
|
|
|
let newPkgStr = pkgStr;
|
2019-02-04 18:12:15 +00:00
|
|
|
|
2019-09-08 19:50:57 +00:00
|
|
|
for (const key of Object.keys(deps)) {
|
2019-12-07 18:43:13 +00:00
|
|
|
const [_type, name] = key.split(sep);
|
2019-09-08 19:50:57 +00:00
|
|
|
const re = new RegExp(`"${esc(name)}": +"${esc(deps[key].old)}"`, "g");
|
|
|
|
newPkgStr = newPkgStr.replace(re, `"${name}": "${deps[key].new}"`);
|
2018-08-23 20:28:33 +00:00
|
|
|
}
|
2019-02-04 18:12:15 +00:00
|
|
|
|
2017-12-03 11:15:02 +00:00
|
|
|
return newPkgStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRange(range, version) {
|
2017-12-10 17:27:29 +00:00
|
|
|
return range.replace(/[0-9]+\.[0-9]+\.[0-9]+(-.+)?/g, version);
|
2017-12-03 11:15:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 19:05:26 +00:00
|
|
|
function isVersionPrerelease(version) {
|
2019-03-28 20:22:49 +00:00
|
|
|
const parsed = semver.parse(version);
|
|
|
|
if (!parsed) return false;
|
|
|
|
return Boolean(parsed.prerelease.length);
|
2019-01-19 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRangePrerelease(range) {
|
|
|
|
// can not use semver.coerce here because it ignores prerelease tags
|
|
|
|
return /[0-9]+\.[0-9]+\.[0-9]+-.+/.test(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rangeToVersion(range) {
|
|
|
|
try {
|
|
|
|
return semver.coerce(range).version;
|
2020-03-25 23:06:31 +00:00
|
|
|
} catch {
|
2019-03-28 20:22:49 +00:00
|
|
|
return null;
|
2019-01-19 19:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:23:42 +00:00
|
|
|
function findVersion(data, versions, {range, semvers, usePre, useRel, useGreatest} = {}) {
|
|
|
|
let tempVersion = rangeToVersion(range);
|
2018-12-22 15:24:24 +00:00
|
|
|
let tempDate = 0;
|
2019-02-25 21:46:39 +00:00
|
|
|
|
2020-03-15 17:23:42 +00:00
|
|
|
semvers = new Set(semvers);
|
|
|
|
usePre = isRangePrerelease(range) || usePre;
|
2019-01-20 21:26:30 +00:00
|
|
|
|
|
|
|
if (usePre) {
|
2020-03-15 17:23:42 +00:00
|
|
|
semvers.add("prerelease");
|
|
|
|
if (semvers.has("patch")) semvers.add("prepatch");
|
|
|
|
if (semvers.has("minor")) semvers.add("preminor");
|
|
|
|
if (semvers.has("major")) semvers.add("premajor");
|
2019-01-20 21:26:30 +00:00
|
|
|
}
|
2018-11-19 09:18:32 +00:00
|
|
|
|
|
|
|
for (const version of versions) {
|
|
|
|
const parsed = semver.parse(version);
|
2020-03-15 17:23:42 +00:00
|
|
|
if (parsed.prerelease.length && (!usePre || useRel)) continue;
|
2018-11-19 09:18:32 +00:00
|
|
|
|
2019-01-20 21:26:30 +00:00
|
|
|
const diff = semver.diff(tempVersion, parsed.version);
|
2020-03-15 17:23:42 +00:00
|
|
|
if (!diff || !semvers.has(diff)) continue;
|
2018-12-22 15:24:24 +00:00
|
|
|
|
2019-06-29 13:25:04 +00:00
|
|
|
// some registries like github don't have data.time available, fall back to greatest on them
|
2020-03-15 17:23:42 +00:00
|
|
|
if (useGreatest || !("time" in data)) {
|
2019-01-20 21:26:30 +00:00
|
|
|
if (semver.gte(semver.coerce(parsed.version).version, tempVersion)) {
|
2019-01-19 15:22:06 +00:00
|
|
|
tempVersion = parsed.version;
|
|
|
|
}
|
2018-12-22 15:24:24 +00:00
|
|
|
} else {
|
|
|
|
const date = (new Date(data.time[version])).getTime();
|
|
|
|
if (date >= 0 && date > tempDate) {
|
|
|
|
tempVersion = parsed.version;
|
|
|
|
tempDate = date;
|
2018-11-19 09:18:32 +00:00
|
|
|
}
|
2018-02-21 19:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-19 09:18:32 +00:00
|
|
|
|
2019-03-28 20:22:49 +00:00
|
|
|
return tempVersion || null;
|
2018-02-21 19:44:13 +00:00
|
|
|
}
|
2018-09-02 19:43:16 +00:00
|
|
|
|
2019-01-19 19:05:26 +00:00
|
|
|
function findNewVersion(data, opts) {
|
2020-05-25 16:14:37 +00:00
|
|
|
if (opts.range === "*") return null; // ignore wildcard
|
|
|
|
if (opts.range.includes("||")) return null; // ignore or-chains
|
2019-06-27 17:15:01 +00:00
|
|
|
const versions = Object.keys(data.versions).filter(version => semver.valid(version));
|
2019-01-19 19:05:26 +00:00
|
|
|
const version = findVersion(data, versions, opts);
|
|
|
|
|
|
|
|
if (opts.useGreatest) {
|
|
|
|
return version;
|
|
|
|
} else {
|
|
|
|
const latestTag = data["dist-tags"].latest;
|
2019-01-20 21:26:30 +00:00
|
|
|
const oldVersion = semver.coerce(opts.range).version;
|
2019-01-20 21:34:40 +00:00
|
|
|
const oldIsPre = isRangePrerelease(opts.range);
|
2019-01-19 19:05:26 +00:00
|
|
|
const newIsPre = isVersionPrerelease(version);
|
2019-11-19 19:52:28 +00:00
|
|
|
const latestIsPre = isVersionPrerelease(latestTag);
|
2019-02-25 21:46:39 +00:00
|
|
|
const isGreater = semver.gt(version, oldVersion);
|
2019-01-19 19:05:26 +00:00
|
|
|
|
|
|
|
// update to new prerelease
|
2019-02-25 21:46:39 +00:00
|
|
|
if (!opts.useRel && opts.usePre || (oldIsPre && newIsPre)) {
|
2019-01-19 19:05:26 +00:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:46:39 +00:00
|
|
|
// downgrade from prerelease to release on --release-only
|
|
|
|
if (opts.useRel && !isGreater && oldIsPre && !newIsPre) {
|
|
|
|
return version;
|
|
|
|
}
|
2019-02-25 20:50:12 +00:00
|
|
|
|
2019-01-19 19:05:26 +00:00
|
|
|
// update from prerelease to release
|
|
|
|
if (oldIsPre && !newIsPre && isGreater) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not downgrade from prerelease to release
|
|
|
|
if (oldIsPre && !newIsPre && !isGreater) {
|
2019-01-20 21:26:30 +00:00
|
|
|
return null;
|
2019-01-19 19:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 21:46:39 +00:00
|
|
|
// check if latestTag is allowed by semvers
|
|
|
|
const diff = semver.diff(oldVersion, latestTag);
|
2020-03-15 17:23:42 +00:00
|
|
|
if (diff && diff !== "prerelease" && !opts.semvers.has(diff.replace(/^pre/, ""))) {
|
2019-02-25 21:46:39 +00:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent upgrading to prerelease with --release-only
|
|
|
|
if (opts.useRel && isVersionPrerelease(latestTag)) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2019-11-19 19:52:51 +00:00
|
|
|
// prevent downgrade to older version except with --allow-downgrade
|
2019-11-19 19:52:28 +00:00
|
|
|
if (semver.lt(latestTag, oldVersion) && !latestIsPre) {
|
2020-03-15 17:26:40 +00:00
|
|
|
if (allowDowngrade === true || (Array.isArray(allowDowngrade) && allowDowngrade.has(data.name))) {
|
2019-11-19 19:52:28 +00:00
|
|
|
return latestTag;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:05:26 +00:00
|
|
|
// in all other cases, return latest dist-tag
|
|
|
|
return latestTag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:06:58 +00:00
|
|
|
// TODO: refactor this mess
|
2019-12-04 20:15:47 +00:00
|
|
|
async function checkUrlDep([key, dep], {useGreatest} = {}) {
|
|
|
|
const stripped = dep.old.replace(stripRe, "");
|
2019-12-06 05:37:39 +00:00
|
|
|
const [_, user, repo, oldRef] = partsRe.exec(stripped) || [];
|
2019-12-04 20:15:47 +00:00
|
|
|
if (!user || !repo || !oldRef) return;
|
|
|
|
|
|
|
|
if (hashRe.test(oldRef)) {
|
2020-03-08 18:12:16 +00:00
|
|
|
const res = await fetch(`${githubApiUrl}/repos/${user}/${repo}/commits`);
|
2019-12-04 20:15:47 +00:00
|
|
|
if (!res || !res.ok) return;
|
|
|
|
const data = await res.json();
|
2020-03-08 16:06:58 +00:00
|
|
|
let {sha: newRef, commit} = data[0];
|
2019-12-04 20:15:47 +00:00
|
|
|
if (!newRef || !newRef.length) return;
|
2020-03-08 16:06:58 +00:00
|
|
|
|
|
|
|
let newDate;
|
|
|
|
if (commit && commit.committer && commit.committer.date) {
|
|
|
|
newDate = commit.committer.date;
|
|
|
|
} else if (commit && commit.auhor && commit.author.date) {
|
|
|
|
newDate = commit.author.date;
|
|
|
|
}
|
|
|
|
|
2019-12-04 20:15:47 +00:00
|
|
|
newRef = newRef.substring(0, oldRef.length);
|
|
|
|
if (oldRef !== newRef) {
|
|
|
|
const newRange = dep.old.replace(oldRef, newRef);
|
2020-03-08 16:06:58 +00:00
|
|
|
return {key, newRange, user, repo, oldRef, newRef, newDate};
|
2019-12-04 20:15:47 +00:00
|
|
|
}
|
2020-03-08 16:13:23 +00:00
|
|
|
} else { // TODO: newDate support
|
2020-03-08 18:12:16 +00:00
|
|
|
const res = await fetch(`${githubApiUrl}/repos/${user}/${repo}/git/refs/tags`);
|
2019-12-04 20:15:47 +00:00
|
|
|
if (!res || !res.ok) return;
|
|
|
|
const data = await res.json();
|
|
|
|
const tags = data.map(entry => entry.ref.replace(/^refs\/tags\//, ""));
|
|
|
|
const oldRefBare = oldRef.replace(/^v/, "");
|
|
|
|
if (!semver.valid(oldRefBare)) return;
|
|
|
|
|
|
|
|
if (!useGreatest) {
|
|
|
|
const lastTag = tags[tags.length - 1];
|
|
|
|
const lastTagBare = lastTag.replace(/^v/, "");
|
|
|
|
if (!semver.valid(lastTagBare)) return;
|
|
|
|
|
|
|
|
if (semver.neq(oldRefBare, lastTagBare)) {
|
|
|
|
const newRange = lastTag;
|
|
|
|
const newRef = lastTag;
|
|
|
|
return {key, newRange, user, repo, oldRef, newRef};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let greatestTag = oldRef;
|
|
|
|
let greatestTagBare = oldRef.replace(/^v/, "");
|
|
|
|
|
|
|
|
for (const tag of tags) {
|
|
|
|
const tagBare = tag.replace(/^v/, "");
|
|
|
|
if (!semver.valid(tagBare)) continue;
|
|
|
|
if (!greatestTag || semver.gt(tagBare, greatestTagBare)) {
|
|
|
|
greatestTag = tag;
|
|
|
|
greatestTagBare = tagBare;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (semver.neq(oldRefBare, greatestTagBare)) {
|
|
|
|
const newRange = greatestTag;
|
|
|
|
const newRef = greatestTag;
|
|
|
|
return {key, newRange, user, repo, oldRef, newRef};
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 18:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 19:43:16 +00:00
|
|
|
function parseMixedArg(arg) {
|
2020-03-09 20:54:27 +00:00
|
|
|
if (arg === undefined) {
|
|
|
|
return false;
|
|
|
|
} else if (arg === "") {
|
2018-09-02 19:43:16 +00:00
|
|
|
return true;
|
|
|
|
} else if (typeof arg === "string") {
|
2020-03-15 17:26:40 +00:00
|
|
|
return arg.includes(",") ? new Set(arg.split(",")) : new Set([arg]);
|
2018-09-02 19:43:16 +00:00
|
|
|
} else if (Array.isArray(arg)) {
|
2020-03-15 17:26:40 +00:00
|
|
|
return new Set(arg);
|
2018-09-02 19:43:16 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 00:28:11 +00:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const dati = await Promise.all(Object.keys(deps).map(key => {
|
2019-12-07 18:43:13 +00:00
|
|
|
const [type, name] = key.split(sep);
|
2019-10-18 00:28:11 +00:00
|
|
|
return get(name, type, registry);
|
|
|
|
}));
|
|
|
|
|
|
|
|
for (const [data, type, registry] of dati) {
|
|
|
|
if (data && data.error) {
|
|
|
|
throw new Error(data.error);
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:26:40 +00:00
|
|
|
const useGreatest = typeof greatest === "boolean" ? greatest : greatest.has(data.name);
|
|
|
|
const usePre = typeof prerelease === "boolean" ? prerelease : prerelease.has(data.name);
|
|
|
|
const useRel = typeof release === "boolean" ? release : release.has(data.name);
|
2019-10-18 00:28:11 +00:00
|
|
|
|
|
|
|
let semvers;
|
2020-03-15 17:26:40 +00:00
|
|
|
if (patch === true || Array.isArray(patch) && patch.has(data.name)) {
|
2020-03-15 17:23:42 +00:00
|
|
|
semvers = new Set(["patch"]);
|
2020-03-15 17:26:40 +00:00
|
|
|
} else if (minor === true || Array.isArray(minor) && minor.has(data.name)) {
|
2020-03-15 17:23:42 +00:00
|
|
|
semvers = new Set(["patch", "minor"]);
|
2019-10-18 00:28:11 +00:00
|
|
|
} else {
|
2020-03-15 17:23:42 +00:00
|
|
|
semvers = new Set(["patch", "minor", "major"]);
|
2019-10-18 00:28:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-07 18:43:13 +00:00
|
|
|
const key = `${type}${sep}${data.name}`;
|
2019-10-18 00:28:11 +00:00
|
|
|
const oldRange = deps[key].old;
|
|
|
|
const newVersion = findNewVersion(data, {usePre, useRel, useGreatest, semvers, range: oldRange});
|
|
|
|
const newRange = updateRange(oldRange, newVersion);
|
|
|
|
|
|
|
|
if (!newVersion || oldRange === newRange) {
|
|
|
|
delete deps[key];
|
|
|
|
} else {
|
|
|
|
deps[key].new = newRange;
|
|
|
|
deps[key].info = getInfoUrl(data.versions[newVersion] || data, registry, data.name);
|
2020-03-08 16:36:23 +00:00
|
|
|
if (data.time && data.time[newVersion]) deps[key].age = getAge(data.time[newVersion]);
|
2019-10-18 00:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-03 18:19:30 +00:00
|
|
|
if (Object.keys(maybeUrlDeps).length) {
|
2019-12-04 20:15:47 +00:00
|
|
|
let results = await Promise.all(Object.entries(maybeUrlDeps).map(([key, dep]) => {
|
2019-12-07 18:43:13 +00:00
|
|
|
const [_, name] = key.split(sep);
|
2020-03-15 17:26:40 +00:00
|
|
|
const useGreatest = typeof greatest === "boolean" ? greatest : greatest.has(name);
|
2019-12-04 20:15:47 +00:00
|
|
|
return checkUrlDep([key, dep], {useGreatest});
|
|
|
|
}));
|
2019-12-03 18:19:30 +00:00
|
|
|
results = results.filter(r => !!r);
|
|
|
|
for (const res of results || []) {
|
2020-03-08 16:06:58 +00:00
|
|
|
const {key, newRange, user, repo, oldRef, newRef, newDate} = res;
|
2019-12-03 18:19:30 +00:00
|
|
|
deps[key] = {
|
|
|
|
old: maybeUrlDeps[key].old,
|
|
|
|
new: newRange,
|
2019-12-04 20:15:47 +00:00
|
|
|
oldPrint: hashRe.test(oldRef) ? oldRef.substring(0, 7) : oldRef,
|
|
|
|
newPrint: hashRe.test(newRef) ? newRef.substring(0, 7) : newRef,
|
2019-12-03 18:19:30 +00:00
|
|
|
info: `https://github.com/${user}/${repo}`,
|
|
|
|
};
|
2020-03-08 16:06:58 +00:00
|
|
|
|
2020-03-08 16:36:23 +00:00
|
|
|
if (newDate) deps[key].age = getAge(newDate);
|
2019-12-03 18:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 00:28:11 +00:00
|
|
|
if (!Object.keys(deps).length) {
|
|
|
|
finish("All packages are up to date.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args.update) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
write(packageFile, updatePackageJson());
|
|
|
|
} catch (err) {
|
|
|
|
finish(new Error(`Error writing ${packageFile}: ${err.message}`));
|
|
|
|
}
|
|
|
|
|
2020-03-09 16:36:15 +00:00
|
|
|
finish(green(`
|
2019-10-18 00:28:11 +00:00
|
|
|
╭────────────────────────╮
|
|
|
|
│ package.json updated │
|
|
|
|
╰────────────────────────╯`.substring(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(finish);
|