@namewta/skills-hub 0.1.22 → 0.1.23
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/cli.mjs +11 -9
- package/package.json +1 -1
- package/scripts/update-cli.mjs +21 -8
package/cli.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import stringWidth from 'string-width';
|
|
|
4
4
|
import wrapAnsi from 'wrap-ansi';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { createInstaller, select, main, readUtf8, repositoryRoot } from './install.mjs';
|
|
7
|
-
import { isNpmInstallation,
|
|
7
|
+
import { isNpmInstallation, checkLatestVersion, installUpdate } from './scripts/update-cli.mjs';
|
|
8
8
|
import { treeSelect } from './tree-select.mjs';
|
|
9
9
|
export { matchesSkillSearch } from './tree-select.mjs';
|
|
10
10
|
|
|
@@ -15,7 +15,7 @@ export function formatSummary(names, columns = process.stdout.columns ?? 80) {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export async function interactive({ argv = [], project = process.cwd(), ui = prompts, choose = treeSelect,
|
|
18
|
-
|
|
18
|
+
requestVersion = isNpmInstallation(repositoryRoot) ? fetch : undefined, update = installUpdate } = {}) {
|
|
19
19
|
if (argv.some(value => !['install', '--replace', '--dry-run'].includes(value))) {
|
|
20
20
|
throw new Error('交互安装只接受 install、--replace 和 --dry-run。');
|
|
21
21
|
}
|
|
@@ -27,15 +27,17 @@ export async function interactive({ argv = [], project = process.cwd(), ui = pro
|
|
|
27
27
|
try {
|
|
28
28
|
ui.intro(pc.cyan('skills-hub · 按需安装中文技能'));
|
|
29
29
|
const dryRun = argv.includes('--dry-run');
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
const { version } = JSON.parse(readUtf8(join(repositoryRoot, 'package.json')));
|
|
31
|
+
ui.note(version, '当前版本号');
|
|
32
|
+
if (requestVersion && !dryRun) {
|
|
33
|
+
const result = await checkLatestVersion(version, requestVersion);
|
|
34
|
+
ui.note(result?.latest ?? '未检测到最新版本号', '最新版本号');
|
|
35
|
+
if (result?.hasUpdate && await ask(ui.select({
|
|
36
|
+
message: '是否现在更新?',
|
|
37
|
+
options: [{ value: true, label: '是,立即更新' }, { value: false, label: '否,直接进入' }],
|
|
36
38
|
initialValue: false,
|
|
37
39
|
}))) {
|
|
38
|
-
try { return update(latest, { root: repositoryRoot, project, argv }); }
|
|
40
|
+
try { return update(result.latest, { root: repositoryRoot, project, argv }); }
|
|
39
41
|
catch (error) { ui.note(error.message, '更新失败,可以继续使用当前版本'); }
|
|
40
42
|
}
|
|
41
43
|
}
|
package/package.json
CHANGED
package/scripts/update-cli.mjs
CHANGED
|
@@ -12,21 +12,34 @@ export function isNpmInstallation(root) {
|
|
|
12
12
|
&& basename(dirname(dirname(root))) === 'node_modules';
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export async function
|
|
16
|
-
const
|
|
15
|
+
export async function checkLatestVersion(version, request = fetch) {
|
|
16
|
+
const [base, prerelease] = version.split('+', 1)[0].split('-', 2);
|
|
17
|
+
const current = stable.exec(base);
|
|
17
18
|
if (!current) return;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
19
|
+
const controller = new AbortController();
|
|
20
|
+
const readLatest = async () => {
|
|
21
|
+
const response = await request(`${registry}/@namewta%2Fskills-hub/latest`, { signal: controller.signal });
|
|
22
22
|
if (!response.ok) return;
|
|
23
23
|
const metadata = await response.json();
|
|
24
24
|
const latest = typeof metadata?.version === 'string' && stable.exec(metadata.version);
|
|
25
25
|
if (!latest || metadata.name !== packageName) return;
|
|
26
26
|
const difference = latest.slice(1).map((part, i) => BigInt(part) - BigInt(current[i + 1])).find(part => part !== 0n);
|
|
27
|
-
|
|
27
|
+
return { latest: metadata.version, hasUpdate: difference === undefined ? Boolean(prerelease) : difference > 0n };
|
|
28
|
+
};
|
|
29
|
+
let timer;
|
|
30
|
+
try {
|
|
31
|
+
// 即使请求或读取响应体未响应取消,3 秒后也继续启动。
|
|
32
|
+
const deadline = new Promise(resolve => {
|
|
33
|
+
timer = setTimeout(() => {
|
|
34
|
+
resolve();
|
|
35
|
+
controller.abort();
|
|
36
|
+
}, 3_000);
|
|
37
|
+
});
|
|
38
|
+
return await Promise.race([deadline, readLatest()]);
|
|
28
39
|
} catch {
|
|
29
|
-
//
|
|
40
|
+
// 离线或注册表响应不可用时继续安装技能。
|
|
41
|
+
} finally {
|
|
42
|
+
clearTimeout(timer);
|
|
30
43
|
}
|
|
31
44
|
}
|
|
32
45
|
|