ali-skills 0.2.0 → 0.2.1-beta.0

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.
Files changed (2) hide show
  1. package/bin/cli.mjs +69 -20
  2. package/package.json +1 -4
package/bin/cli.mjs CHANGED
@@ -1,9 +1,39 @@
1
1
  #!/usr/bin/env node
2
2
  import { execSync } from 'child_process';
3
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
4
+ import { join } from 'path';
5
+ import { homedir } from 'os';
6
+ import { pathToFileURL } from 'url';
3
7
 
4
- const INTERNAL_REGISTRY = 'https://registry.anpm.alibaba-inc.com';
8
+ const REGISTRY = 'https://registry.anpm.alibaba-inc.com';
9
+ const PKG = '@ali/cli-skills';
10
+ const CACHE_DIR = join(homedir(), '.ali-skills');
11
+ const PKG_DIR = join(CACHE_DIR, 'node_modules', PKG);
5
12
 
6
- function getRegistry() {
13
+ /** 读取缓存中已安装的版本号 */
14
+ function getInstalledVersion() {
15
+ try {
16
+ return JSON.parse(readFileSync(join(PKG_DIR, 'package.json'), 'utf8')).version || null;
17
+ } catch {
18
+ return null;
19
+ }
20
+ }
21
+
22
+ /** 从内网 registry 查最新版本,超时 5s 返回 null */
23
+ function getLatestVersion() {
24
+ try {
25
+ return execSync(`npm view ${PKG} version --registry ${REGISTRY}`, {
26
+ encoding: 'utf8',
27
+ timeout: 5000,
28
+ stdio: ['pipe', 'pipe', 'pipe'],
29
+ }).trim();
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ /** 获取用户当前配置的 npm registry */
36
+ function getUserRegistry() {
7
37
  try {
8
38
  return execSync('npm config get registry', { encoding: 'utf8' }).trim().replace(/\/$/, '');
9
39
  } catch {
@@ -11,15 +41,29 @@ function getRegistry() {
11
41
  }
12
42
  }
13
43
 
14
- try {
15
- await import('@ali/cli-skills/bin/cli.mjs');
16
- } catch (err) {
17
- if (err.code === 'ERR_MODULE_NOT_FOUND' || err.code === 'MODULE_NOT_FOUND') {
18
- const registry = getRegistry();
19
- const isInternal = registry.includes('alibaba-inc.com') || registry.includes('anpm.alibaba');
44
+ /** 安装指定版本到缓存目录 */
45
+ function install(version) {
46
+ mkdirSync(CACHE_DIR, { recursive: true });
47
+ const pkgJson = join(CACHE_DIR, 'package.json');
48
+ if (!existsSync(pkgJson)) {
49
+ writeFileSync(pkgJson, '{"private":true}');
50
+ }
51
+ execSync(
52
+ `npm install ${PKG}@${version} --registry ${REGISTRY} --prefix "${CACHE_DIR}" --no-audit --no-fund --loglevel=error`,
53
+ { stdio: 'inherit' },
54
+ );
55
+ }
56
+
57
+ const installedVersion = getInstalledVersion();
58
+ const latestVersion = getLatestVersion();
59
+
60
+ // 无法联通 registry,且本地无缓存 → 提示配置
61
+ if (!latestVersion && !installedVersion) {
62
+ const registry = getUserRegistry();
63
+ const isInternal = registry.includes('alibaba-inc.com') || registry.includes('anpm.alibaba');
20
64
 
21
- if (!isInternal) {
22
- console.error(`
65
+ if (!isInternal) {
66
+ console.error(`
23
67
  错误:当前 npm registry 不是阿里内网源,无法安装内部依赖包 @ali/cli-skills
24
68
 
25
69
  当前 registry: ${registry || '(未知)'}
@@ -27,22 +71,27 @@ try {
27
71
  请选择以下方式之一重新执行:
28
72
 
29
73
  1. 配置内网源后重新执行:
30
- npm config set registry ${INTERNAL_REGISTRY}
74
+ npm config set registry ${REGISTRY}
31
75
  npx ali-skills
32
76
 
33
77
  2. 使用 tnpx 执行:
34
78
  tnpx ali-skills
35
79
 
36
80
  3. 临时指定 registry 执行:
37
- npx --registry ${INTERNAL_REGISTRY} ali-skills
81
+ npx --registry ${REGISTRY} ali-skills
38
82
  `);
39
- } else {
40
- console.error(`
41
- 错误:@ali/cli-skills 未找到,请尝试重新安装:
42
- npx --registry ${INTERNAL_REGISTRY} ali-skills@latest
43
- `);
44
- }
45
- process.exit(1);
83
+ } else {
84
+ console.error(`错误:无法从内网 registry 获取 ${PKG},请检查网络后重试`);
46
85
  }
47
- throw err;
86
+ process.exit(1);
87
+ }
88
+
89
+ // 有新版本 → 自动安装/更新
90
+ if (latestVersion && latestVersion !== installedVersion) {
91
+ const action = installedVersion ? `更新 ${installedVersion} → ${latestVersion}` : `安装 ${latestVersion}`;
92
+ process.stderr.write(`[ali-skills] ${PKG} ${action}...\n`);
93
+ install(latestVersion);
48
94
  }
95
+
96
+ // 执行
97
+ await import(pathToFileURL(join(PKG_DIR, 'bin', 'cli.mjs')).href);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ali-skills",
3
- "version": "0.2.0",
3
+ "version": "0.2.1-beta.0",
4
4
  "description": "The open agent skills ecosystem",
5
5
  "type": "module",
6
6
  "bin": {
@@ -31,9 +31,6 @@
31
31
  },
32
32
  "author": "",
33
33
  "license": "MIT",
34
- "optionalDependencies": {
35
- "@ali/cli-skills": "^2.1.8"
36
- },
37
34
  "engines": {
38
35
  "node": ">=18"
39
36
  },