luminae-helper 0.0.7 → 0.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/package.json +1 -1
- package/src/cli.js +16 -0
- package/src/utils/version.js +108 -0
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { runInteractive, TERM_GUTTER } from "./lib/ui.js";
|
|
4
|
+
import { printVersion, checkOutdated } from "./utils/version.js";
|
|
4
5
|
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
|
|
8
|
+
// --version / -v
|
|
9
|
+
if (args.includes("--version") || args.includes("-v")) {
|
|
10
|
+
printVersion();
|
|
11
|
+
process.exit(0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// outdated 子命令
|
|
15
|
+
if (args[0] === "outdated") {
|
|
16
|
+
await checkOutdated();
|
|
17
|
+
process.exit(0);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 默认:交互模式
|
|
5
21
|
async function main() {
|
|
6
22
|
try {
|
|
7
23
|
await runInteractive();
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
export const TERM_GUTTER = " ";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 读取当前包的 package.json。
|
|
9
|
+
* import.meta.dirname = <package>/src/utils
|
|
10
|
+
* package.json 在 <package>/,需要上跳两级
|
|
11
|
+
*/
|
|
12
|
+
function readPackageJson() {
|
|
13
|
+
const pkgPath = join(import.meta.dirname, "..", "..", "package.json");
|
|
14
|
+
return JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取当前包名。
|
|
19
|
+
*/
|
|
20
|
+
export function getPackageName() {
|
|
21
|
+
return readPackageJson().name;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 获取当前版本号。
|
|
26
|
+
*/
|
|
27
|
+
export function getVersion() {
|
|
28
|
+
return readPackageJson().version;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 获取当前包的 registry URL。
|
|
33
|
+
* 优先读 publishConfig.registry,否则用 npm 默认 registry。
|
|
34
|
+
*/
|
|
35
|
+
function getRegistry() {
|
|
36
|
+
const pkg = readPackageJson();
|
|
37
|
+
return pkg.publishConfig?.registry || "https://registry.npmjs.org/";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 从 registry 查询指定包的最新版本。
|
|
42
|
+
* @param {string} packageName
|
|
43
|
+
* @param {string} registry
|
|
44
|
+
* @returns {Promise<string|null>} 最新版本号,查询失败返回 null
|
|
45
|
+
*/
|
|
46
|
+
async function fetchLatestVersion(packageName, registry) {
|
|
47
|
+
const url = `${registry}${packageName}`;
|
|
48
|
+
try {
|
|
49
|
+
const res = await fetch(url, {
|
|
50
|
+
signal: AbortSignal.timeout(8000),
|
|
51
|
+
headers: { Accept: "application/json" },
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) return null;
|
|
54
|
+
const data = await res.json();
|
|
55
|
+
// npmjs 返回 data["dist-tags"].latest,私有 registry 格式类似
|
|
56
|
+
return data?.["dist-tags"]?.latest ?? data?.version ?? null;
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 比较两个 semver 版本号(简单比较,仅支持 x.y.z 格式)。
|
|
64
|
+
* @returns {number} 正数=a>b, 0=相等, 负数=a<b
|
|
65
|
+
*/
|
|
66
|
+
function compareSemver(a, b) {
|
|
67
|
+
const pa = a.replace(/^v/, "").split(".").map(Number);
|
|
68
|
+
const pb = b.replace(/^v/, "").split(".").map(Number);
|
|
69
|
+
for (let i = 0; i < 3; i++) {
|
|
70
|
+
if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) - (pb[i] || 0);
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 打印当前版本号。
|
|
77
|
+
*/
|
|
78
|
+
export function printVersion() {
|
|
79
|
+
const pkg = readPackageJson();
|
|
80
|
+
console.log(`${pkg.name}@${pkg.version}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 检查是否有新版本并输出提示。
|
|
85
|
+
*/
|
|
86
|
+
export async function checkOutdated() {
|
|
87
|
+
const pkg = readPackageJson();
|
|
88
|
+
const currentVersion = pkg.version;
|
|
89
|
+
const packageName = pkg.name;
|
|
90
|
+
const registry = getRegistry();
|
|
91
|
+
|
|
92
|
+
console.log(TERM_GUTTER + `当前版本: ${packageName}@${currentVersion}`);
|
|
93
|
+
console.log(TERM_GUTTER + `检查更新中...`);
|
|
94
|
+
|
|
95
|
+
const latestVersion = await fetchLatestVersion(packageName, registry);
|
|
96
|
+
|
|
97
|
+
if (!latestVersion) {
|
|
98
|
+
console.log(chalk.yellow(TERM_GUTTER + "⚠ 无法获取最新版本信息,请检查网络连接"));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (compareSemver(currentVersion, latestVersion) >= 0) {
|
|
103
|
+
console.log(chalk.green(TERM_GUTTER + `✓ 已是最新版本 (${currentVersion})`));
|
|
104
|
+
} else {
|
|
105
|
+
console.log(chalk.yellow(TERM_GUTTER + `⚠ 发现新版本: ${latestVersion}(当前: ${currentVersion})`));
|
|
106
|
+
console.log(TERM_GUTTER + ` 更新命令: npm install -g ${packageName}`);
|
|
107
|
+
}
|
|
108
|
+
}
|