gf-packages-cli 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/common/updateCheck.js +47 -10
- package/package.json +1 -1
|
@@ -1,22 +1,55 @@
|
|
|
1
1
|
const { execSync } = require('child_process');
|
|
2
|
+
const http = require('http');
|
|
3
|
+
const https = require('https');
|
|
2
4
|
const chalk = require('chalk').default;
|
|
3
5
|
const inquirer = require('inquirer');
|
|
4
6
|
const pkg = require('../../package.json');
|
|
5
7
|
|
|
6
8
|
const PKG_NAME = pkg.name;
|
|
7
9
|
const CURRENT_VERSION = pkg.version;
|
|
8
|
-
const REGISTRY_URL = `https://registry.npmjs.org/${PKG_NAME}/latest`;
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
/** 用内置 http/https 模块请求 JSON(兼容所有 Node 版本,不依赖 fetch)
|
|
12
|
+
* @param {string} url
|
|
13
|
+
* @param {number} timeoutMs
|
|
14
|
+
* @returns {Promise<object|null>}
|
|
15
|
+
*/
|
|
16
|
+
function httpGetJson(url, timeoutMs) {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
const lib = url.startsWith('https:') ? https : http;
|
|
19
|
+
const req = lib.get(url, { timeout: timeoutMs }, (res) => {
|
|
20
|
+
let body = '';
|
|
21
|
+
res.setEncoding('utf-8');
|
|
22
|
+
res.on('data', (c) => { body += c; });
|
|
23
|
+
res.on('end', () => {
|
|
24
|
+
try { resolve(JSON.parse(body)); }
|
|
25
|
+
catch { resolve(null); }
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
req.on('timeout', () => { req.destroy(); resolve(null); });
|
|
29
|
+
req.on('error', () => resolve(null));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 依次尝试的 registry 源:优先 npm 配置的镜像,再官方,最后 npmmirror */
|
|
34
|
+
function getRegistryCandidates() {
|
|
35
|
+
const list = [];
|
|
12
36
|
try {
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
37
|
+
const reg = execSync('npm config get registry', { encoding: 'utf-8' }).trim();
|
|
38
|
+
if (reg && /^https?:\/\//.test(reg)) list.push(reg.replace(/\/+$/, ''));
|
|
39
|
+
} catch { }
|
|
40
|
+
list.push('https://registry.npmjs.org');
|
|
41
|
+
list.push('https://registry.npmmirror.com');
|
|
42
|
+
return [...new Set(list)];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 请求 npm registry 获取最新版本号,全部失败返回 null */
|
|
46
|
+
async function getLatestVersion() {
|
|
47
|
+
for (const base of getRegistryCandidates()) {
|
|
48
|
+
const url = `${base}/${PKG_NAME}/latest`;
|
|
49
|
+
const data = await httpGetJson(url, 5000);
|
|
50
|
+
if (data && data.version) return data.version;
|
|
19
51
|
}
|
|
52
|
+
return null;
|
|
20
53
|
}
|
|
21
54
|
|
|
22
55
|
// 判断 latest 是否比当前版本新
|
|
@@ -38,7 +71,11 @@ function isNewer(latest) {
|
|
|
38
71
|
*/
|
|
39
72
|
async function autoUpdate() {
|
|
40
73
|
const latest = await getLatestVersion();
|
|
41
|
-
if (!latest)
|
|
74
|
+
if (!latest) {
|
|
75
|
+
// 不再静默:让用户知道为何没有更新提示
|
|
76
|
+
console.log(chalk.gray('更新检查失败(无法访问npm仓库),已跳过'));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
42
79
|
if (!isNewer(latest)) return;
|
|
43
80
|
|
|
44
81
|
console.log(chalk.yellow(`\n发现新版本: v${CURRENT_VERSION} -> v${latest}`));
|