@namewta/skills-hub 0.1.17 → 0.1.18

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 CHANGED
@@ -2,7 +2,9 @@ import * as prompts from '@clack/prompts';
2
2
  import pc from 'picocolors';
3
3
  import stringWidth from 'string-width';
4
4
  import wrapAnsi from 'wrap-ansi';
5
- import { createInstaller, select, main } from './install.mjs';
5
+ import { join } from 'node:path';
6
+ import { createInstaller, select, main, readUtf8, repositoryRoot } from './install.mjs';
7
+ import { isNpmInstallation, latestUpdate, installUpdate } from './scripts/update-cli.mjs';
6
8
  import { treeSelect } from './tree-select.mjs';
7
9
  export { matchesSkillSearch } from './tree-select.mjs';
8
10
 
@@ -12,7 +14,8 @@ export function formatSummary(names, columns = process.stdout.columns ?? 80) {
12
14
  return wrapAnsi(`${pc.bold(label)}${' '.repeat(Math.max(1, 8 - stringWidth(label)))}${names.join('、')}`, width, { hard: true });
13
15
  }
14
16
 
15
- export async function interactive({ argv = [], project = process.cwd(), ui = prompts, choose = treeSelect } = {}) {
17
+ export async function interactive({ argv = [], project = process.cwd(), ui = prompts, choose = treeSelect,
18
+ checkUpdate = isNpmInstallation(repositoryRoot) ? latestUpdate : undefined, update = installUpdate } = {}) {
16
19
  if (argv.some(value => !['install', '--replace', '--dry-run'].includes(value))) {
17
20
  throw new Error('交互安装只接受 install、--replace 和 --dry-run。');
18
21
  }
@@ -23,6 +26,19 @@ export async function interactive({ argv = [], project = process.cwd(), ui = pro
23
26
  };
24
27
  try {
25
28
  ui.intro(pc.cyan('skills-hub · 按需安装中文技能'));
29
+ const dryRun = argv.includes('--dry-run');
30
+ if (checkUpdate && !dryRun) {
31
+ const { version } = JSON.parse(readUtf8(join(repositoryRoot, 'package.json')));
32
+ const latest = await checkUpdate(version);
33
+ if (latest && await ask(ui.select({
34
+ message: `skills-hub ${latest} 已发布,当前版本为 ${version}`,
35
+ options: [{ value: true, label: '更新' }, { value: false, label: '跳过本次' }],
36
+ initialValue: false,
37
+ }))) {
38
+ try { return update(latest, { root: repositoryRoot, project, argv }); }
39
+ catch (error) { ui.note(error.message, '更新失败,可以继续使用当前版本'); }
40
+ }
41
+ }
26
42
  ui.note(project, '目标项目(技能写入 .agents/skills/)');
27
43
  const installer = createInstaller();
28
44
  const skills = installer.catalog();
@@ -35,7 +51,6 @@ export async function interactive({ argv = [], project = process.cwd(), ui = pro
35
51
  if (selected.some(skill => ['dev', 'system', 'ops'].includes(skill.domain))) {
36
52
  ui.note('将补齐所选领域的持久化目录和简短 AGENTS.md;固定模板自动更新,业务记录保留,预览不写入。', '领域初始化');
37
53
  }
38
- const dryRun = argv.includes('--dry-run');
39
54
  if (!await ask(ui.confirm({ message: dryRun ? '检查安装计划?(不写入文件)' : '安装或更新以上技能?(有差异的现有技能会先备份)', initialValue: true }))) {
40
55
  ui.cancel('已取消,未修改文件。'); return 0;
41
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@namewta/skills-hub",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "独立中文 Agent Skills:按领域安装,覆盖开发交付、项目知识与系统维护。",
5
5
  "type": "module",
6
6
  "bin": {
@@ -10,6 +10,8 @@
10
10
  "install.mjs",
11
11
  "cli.mjs",
12
12
  "tree-select.mjs",
13
+ "scripts/npm-cli.mjs",
14
+ "scripts/update-cli.mjs",
13
15
  "scripts/init-persistence.mjs",
14
16
  "scripts/persistence/",
15
17
  "skills/",
@@ -0,0 +1,33 @@
1
+ import { existsSync, realpathSync } from 'node:fs';
2
+ import { delimiter, dirname, join } from 'node:path';
3
+ import { spawnSync } from 'node:child_process';
4
+
5
+ export function findNpmCli(environment = process.env) {
6
+ const candidates = [
7
+ environment.npm_execpath,
8
+ join(dirname(process.execPath), 'node_modules/npm/bin/npm-cli.js'),
9
+ join(dirname(process.execPath), '../lib/node_modules/npm/bin/npm-cli.js'),
10
+ ];
11
+ for (const directory of (environment.PATH ?? environment.Path ?? '').split(delimiter)) {
12
+ if (!directory) continue;
13
+ candidates.push(join(directory, 'npm'), join(directory, 'node_modules/npm/bin/npm-cli.js'));
14
+ }
15
+ for (const candidate of candidates.filter(Boolean)) {
16
+ if (!existsSync(candidate)) continue;
17
+ const resolved = realpathSync(candidate);
18
+ if (resolved.endsWith('npm-cli.js')) return resolved;
19
+ }
20
+ throw new Error('未找到随 Node.js 安装的 npm CLI;请使用完整的 Node.js 发行版。');
21
+ }
22
+
23
+ export function runNpm(arguments_, options = {}) {
24
+ const result = spawnSync(process.execPath, [findNpmCli(options.env), ...arguments_], {
25
+ windowsHide: true,
26
+ encoding: 'utf8', maxBuffer: 32 * 1024 * 1024, ...options,
27
+ });
28
+ if (result.error) throw result.error;
29
+ if (result.status !== 0) {
30
+ throw new Error(`npm ${arguments_[0]} 失败(${result.status}):\n${result.stdout ?? ''}${result.stderr ?? ''}`);
31
+ }
32
+ return result;
33
+ }
@@ -0,0 +1,51 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import { existsSync, realpathSync } from 'node:fs';
3
+ import { basename, dirname, join, resolve } from 'node:path';
4
+ import { findNpmCli, runNpm } from './npm-cli.mjs';
5
+
6
+ const packageName = '@namewta/skills-hub';
7
+ const registry = 'https://registry.npmjs.org';
8
+ const stable = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
9
+
10
+ export function isNpmInstallation(root) {
11
+ return basename(root) === 'skills-hub' && basename(dirname(root)) === '@namewta'
12
+ && basename(dirname(dirname(root))) === 'node_modules';
13
+ }
14
+
15
+ export async function latestUpdate(version, request = fetch) {
16
+ const current = stable.exec(version.split(/[+-]/, 1)[0]);
17
+ if (!current) return;
18
+ try {
19
+ const response = await request(`${registry}/@namewta%2Fskills-hub/latest`, {
20
+ signal: AbortSignal.timeout(1800),
21
+ });
22
+ if (!response.ok) return;
23
+ const metadata = await response.json();
24
+ const latest = typeof metadata?.version === 'string' && stable.exec(metadata.version);
25
+ if (!latest || metadata.name !== packageName) return;
26
+ const difference = latest.slice(1).map((part, i) => BigInt(part) - BigInt(current[i + 1])).find(part => part !== 0n);
27
+ if (difference > 0n || (difference === undefined && version.includes('-'))) return metadata.version;
28
+ } catch {
29
+ // 离线、超时或注册表响应不可用时继续安装技能。
30
+ }
31
+ }
32
+
33
+ export function installUpdate(version, { root, project, argv }) {
34
+ if (!stable.test(version)) throw new Error('无效的 npm 更新版本。');
35
+ const options = { cwd: project, timeout: 120_000 };
36
+ const globalRoot = runNpm(['root', '--global'], options).stdout.trim();
37
+ const globalPackage = join(globalRoot, packageName);
38
+ const global = existsSync(globalPackage) && realpathSync(globalPackage) === realpathSync(root);
39
+ const spec = `${packageName}@${version}`;
40
+ const flags = ['--ignore-scripts', `--registry=${registry}`];
41
+ let command;
42
+ if (global) {
43
+ runNpm(['install', '--global', ...flags, '--no-audit', '--no-fund', spec], options);
44
+ command = [resolve(globalPackage, 'install.mjs'), ...argv];
45
+ } else {
46
+ command = [findNpmCli(), 'exec', '--yes', ...flags, `--package=${spec}`, '--', 'skills-hub', ...argv];
47
+ }
48
+ const result = spawnSync(process.execPath, command, { cwd: project, windowsHide: true, stdio: 'inherit' });
49
+ if (result.error) throw result.error;
50
+ return result.status ?? 1;
51
+ }