UNPKG

4.5 kBJavaScriptView Raw
1var __create = Object.create;
2var __defProp = Object.defineProperty;
3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4var __getOwnPropNames = Object.getOwnPropertyNames;
5var __getProtoOf = Object.getPrototypeOf;
6var __hasOwnProp = Object.prototype.hasOwnProperty;
7var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8var __export = (target, all) => {
9 __markAsModule(target);
10 for (var name in all)
11 __defProp(target, name, { get: all[name], enumerable: true });
12};
13var __reExport = (target, module2, desc) => {
14 if (module2 && typeof module2 === "object" || typeof module2 === "function") {
15 for (let key of __getOwnPropNames(module2))
16 if (!__hasOwnProp.call(target, key) && key !== "default")
17 __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
18 }
19 return target;
20};
21var __toModule = (module2) => {
22 return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
23};
24__export(exports, {
25 default: () => src_default
26});
27var import_os = __toModule(require("os"));
28var import_path = __toModule(require("path"));
29var import_fs = __toModule(require("fs"));
30var import_utils = __toModule(require("./utils"));
31var src_default = async (moduleName, currentVersion, options) => {
32 const { level, timeout, ignoreInformalVersion } = {
33 level: ["major", "minor", "patch"],
34 timeout: 24 * 60 * 60 * 1e3,
35 ignoreInformalVersion: true,
36 ...options
37 };
38 const curVersion = (0, import_utils.formatVersion)(currentVersion);
39 const cacheDir = (0, import_path.join)((0, import_os.tmpdir)(), `npmModInfoCache`);
40 if (!(0, import_fs.existsSync)(cacheDir)) {
41 (0, import_fs.mkdirSync)(cacheDir, 511);
42 }
43 const cacheFile = (0, import_path.join)(cacheDir, `${(moduleName + "_" + currentVersion).replace(/[^\w]/g, "_")}_cache.json`);
44 let cache;
45 if ((0, import_fs.existsSync)(cacheFile)) {
46 cache = JSON.parse((0, import_fs.readFileSync)(cacheFile, "utf-8"));
47 }
48 if ((cache == null ? void 0 : cache.time) && Date.now() - cache.time < timeout) {
49 return cache.value;
50 }
51 let npmCmd = `npm`;
52 if (process.env.LANG === "zh_CN.UTF-8") {
53 npmCmd = "npm --registry=https://registry.npmmirror.com";
54 }
55 let result = {
56 update: false,
57 tips: [],
58 version: ""
59 };
60 try {
61 const data = await (0, import_utils.exec)({
62 cmd: `${npmCmd} view ${moduleName} --json`,
63 baseDir: process.env.HOME,
64 timeout: 2e3
65 });
66 const { versions, "module-info-tips": tipRules = [] } = JSON.parse(data);
67 let filterVersions = versions.map(import_utils.formatVersion).filter((version) => {
68 if (ignoreInformalVersion && version.tag) {
69 return;
70 }
71 if (level.includes("patch")) {
72 if (version.major === curVersion.major && version.minor === curVersion.minor && version.pacth > curVersion.pacth) {
73 return true;
74 }
75 }
76 if (level.includes("minor")) {
77 if (version.major === curVersion.major && version.minor > curVersion.minor) {
78 return true;
79 }
80 }
81 if (level.includes("major")) {
82 if (version.major > curVersion.major) {
83 return true;
84 }
85 }
86 });
87 filterVersions = filterVersions.sort((aVer, bVer) => {
88 return bVer.score - aVer.score;
89 });
90 let update = false;
91 let newVersion;
92 if (filterVersions.length) {
93 update = true;
94 newVersion = filterVersions[0].version;
95 }
96 const tips = tipRules.filter((rule) => {
97 if (!(rule == null ? void 0 : rule.tip)) {
98 return false;
99 }
100 const ignore = [].concat(rule.ignore || []).find((rule2) => (0, import_utils.matchVersion)(curVersion, rule2));
101 if (ignore) {
102 return false;
103 }
104 const match = [].concat(rule.match || []).find((rule2) => (0, import_utils.matchVersion)(curVersion, rule2));
105 if (match) {
106 return true;
107 }
108 return false;
109 });
110 result = {
111 update,
112 version: newVersion,
113 tips: tips.map((rule) => rule.tip)
114 };
115 } catch (e) {
116 }
117 (0, import_fs.writeFileSync)(cacheFile, JSON.stringify({ time: Date.now(), value: result }));
118 return result;
119};
120// Annotate the CommonJS export names for ESM import in node:
1210 && (module.exports = {});