-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
// Require Node.js Dependencies
const fs = require("node:fs/promises");
const path = require("node:path");
// Require Third-party Dependencies
const pacote = require("pacote");
const semver = require("semver");
/**
* @async
* @function fetch
* @description Fetch package metadata with pacote and return all versions information
* @param {!string} name package name
* @param {!string} range package version (range).
* @param {object} [options] options
* @param {string} [options.cwd]
* @param {string} [options.token]
* @returns {Promise<any>}
*/
async function fetch(name, range, { cwd, token }) {
const options = typeof token === "string" ? { token } : {};
try {
const { versions, "dist-tags": { latest } } = await pacote.packument(name, options);
const location = path.join("node_modules", ...name.split("/"));
// NOTE: can we fetch the right current version without fs ?
const rawPkg = await fs.readFile(path.join(cwd, location, "package.json"), "utf-8");
const { version: current } = JSON.parse(rawPkg);
if (semver.eq(latest, current)) {
return {};
}
const wanted = semver.maxSatisfying(Object.keys(versions), range) || latest;
return {
[name]: { current, latest, wanted, location }
};
}
catch (error) {
return {};
}
}
/**
* @async
* @function outdated
* @description Fast Programmaticaly alternative to npm outdated
* @param {string} [cwd] working dir where we will search for packages
* @param {object} [options] options
* @param {boolean} [options.devDependencies=false] search for devDependencies
* @param {string} [options.token] npm token
* @returns {Promise<any>}
*/
async function outdated(cwd = process.cwd(), options = {}) {
const { devDependencies: includeDevDependencies = false, token } = options;
const str = await fs.readFile(path.join(cwd, "package.json"), "utf-8");
const { dependencies = {}, devDependencies = {} } = JSON.parse(str);
const deps = Object.assign(dependencies, includeDevDependencies ? devDependencies : {});
const packagesToUpdate = (await Promise.allSettled(
Object.entries(deps).map(([name, current]) => fetch(name, current, { cwd, token }))
))
.filter((result) => result.status === "fulfilled")
.map((result) => result.value);
return Object.assign(...packagesToUpdate);
}
module.exports = { outdated };