pack-release.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env node
  2. /**
  3. * pack-release.mjs —— 生成可直接上传 SkillHub 的发布 zip
  4. *
  5. * 产物:release/fmode-skills-<version>-<date>.zip
  6. * 内容:dist/ 全部技能 + 根 SKILL.md + README + manifest(不含内部脚本、不含缓存)
  7. *
  8. * 上传方式:SkillHub 后台「上传技能包」→ 选这个 zip
  9. */
  10. import fs from 'node:fs';
  11. import path from 'node:path';
  12. import { execFileSync } from 'node:child_process';
  13. import { fileURLToPath } from 'node:url';
  14. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  15. const ROOT = path.resolve(__dirname, '..');
  16. const DIST = path.join(ROOT, 'dist');
  17. const RELEASE = path.join(ROOT, 'release');
  18. const C = { g: '\x1b[32m', y: '\x1b[33m', r: '\x1b[31m', d: '\x1b[2m', b: '\x1b[1m', x: '\x1b[0m' };
  19. if (!fs.existsSync(DIST)) {
  20. console.error('dist 不存在,先跑 npm run build');
  21. process.exit(1);
  22. }
  23. // 版本
  24. const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'));
  25. const version = pkg.version || '1.0.0';
  26. const date = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  27. const stamp = `${version}-${date}`;
  28. fs.mkdirSync(RELEASE, { recursive: true });
  29. const stageDir = path.join(RELEASE, `.stage-${stamp}`);
  30. fs.rmSync(stageDir, { recursive: true, force: true });
  31. fs.mkdirSync(stageDir, { recursive: true });
  32. /** 递归复制,跳过 .git / 缓存 / 内部脚本 */
  33. const SKIP = [
  34. /(^|\/)\.git(\/|$)/, /(^|\/)__pycache__(\/|$)/, /(^|\/)node_modules(\/|$)/,
  35. /\.pyc$/, /\.log$/, /\.bak/, /\.DS_Store$/,
  36. /(^|\/)gen-.*\.(py|sh)$/, // 内部生成脚本
  37. /(^|\/)\.legacy(\/|$)/,
  38. /(^|\/)\.cache(\/|$)/,
  39. ];
  40. function copyTree(src, dest, rel = '') {
  41. fs.mkdirSync(dest, { recursive: true });
  42. for (const e of fs.readdirSync(src, { withFileTypes: true })) {
  43. const relPath = rel ? `${rel}/${e.name}` : e.name;
  44. if (SKIP.some(re => re.test(relPath))) continue;
  45. const s = path.join(src, e.name);
  46. const d = path.join(dest, e.name);
  47. if (e.isDirectory()) copyTree(s, d, relPath);
  48. else if (e.isFile()) fs.copyFileSync(s, d);
  49. }
  50. }
  51. // 1. 复制 dist 里的技能
  52. copyTree(DIST, stageDir);
  53. // 2. 复制根级 meta(SKILL.md / README / manifest)
  54. for (const f of ['SKILL.md', 'README.md', 'skill-package-manifest.json']) {
  55. const src = path.join(ROOT, f);
  56. if (fs.existsSync(src)) fs.copyFileSync(src, path.join(stageDir, f));
  57. }
  58. // 3. 生成/更新 package.json(SkillHub 读这个)
  59. const outPkg = {
  60. name: 'fmode-skills',
  61. version,
  62. description: pkg.description,
  63. type: 'module',
  64. license: pkg.license || 'MPL-2.0',
  65. keywords: pkg.keywords,
  66. engines: pkg.engines,
  67. scripts: {
  68. install: 'node scripts/installer.mjs --all',
  69. check: 'node scripts/installer.mjs --check',
  70. },
  71. };
  72. fs.writeFileSync(path.join(stageDir, 'package.json'), JSON.stringify(outPkg, null, 2) + '\n');
  73. // 4. 检查 installer 是否存在(技能安装入口)
  74. const installerSrc = path.join(ROOT, 'scripts', 'installer.mjs');
  75. if (fs.existsSync(installerSrc)) {
  76. fs.mkdirSync(path.join(stageDir, 'scripts'), { recursive: true });
  77. fs.copyFileSync(installerSrc, path.join(stageDir, 'scripts', 'installer.mjs'));
  78. }
  79. // 5. 统计 & 自检
  80. function walk(d, out = []) {
  81. for (const e of fs.readdirSync(d, { withFileTypes: true })) {
  82. const p = path.join(d, e.name);
  83. if (e.isDirectory()) walk(p, out);
  84. else out.push(p);
  85. }
  86. return out;
  87. }
  88. const files = walk(stageDir);
  89. const skills = fs.existsSync(path.join(stageDir, 'skills'))
  90. ? fs.readdirSync(path.join(stageDir, 'skills')).filter(n =>
  91. fs.statSync(path.join(stageDir, 'skills', n)).isDirectory())
  92. : [];
  93. // 自检:密钥模式
  94. const SECRET_RE = [
  95. /\bsk-[a-zA-Z0-9]{20,}/,
  96. /\br:[a-z0-9]{20,}/,
  97. /AKIA[0-9A-Z]{16}/,
  98. /-----BEGIN [A-Z ]*PRIVATE KEY-----/,
  99. /(192\.168\.[0-9]{1,3}\.[0-9]{1,3})/,
  100. /(139\.159\.253\.131|172\.0\.0\.100)/,
  101. /(db\.fmode\.cn|userland\.fmode\.cn)/,
  102. ];
  103. let leaks = 0;
  104. for (const f of files) {
  105. if (!/\.(md|json|mjs|js|sh|py|txt|html|css)$/i.test(f)) continue;
  106. const c = fs.readFileSync(f, 'utf8');
  107. for (const re of SECRET_RE) {
  108. if (re.test(c)) {
  109. console.log(`${C.r}✗ 疑似敏感${C.x} ${path.relative(stageDir, f)} → ${re}`);
  110. leaks++;
  111. }
  112. }
  113. }
  114. // 6. 打 zip
  115. const zipName = `fmode-skills-${stamp}.zip`;
  116. const zipPath = path.join(RELEASE, zipName);
  117. fs.rmSync(zipPath, { force: true });
  118. try {
  119. execFileSync('zip', ['-rq', zipName, '.'], { cwd: stageDir, stdio: 'pipe' });
  120. } catch (e) {
  121. // 无 zip 命令,用 Node 的 python 兜底
  122. try {
  123. execFileSync('python3', ['-c', `
  124. import zipfile, os
  125. z = zipfile.ZipFile(${JSON.stringify(zipPath)}, 'w', zipfile.ZIP_DEFLATED)
  126. for root, dirs, fs in os.walk(${JSON.stringify(stageDir)}):
  127. for f in fs:
  128. p = os.path.join(root, f)
  129. z.write(p, os.path.relpath(p, ${JSON.stringify(stageDir)}))
  130. z.close()
  131. `], { stdio: 'pipe' });
  132. } catch (e2) {
  133. console.error('打包失败:无 zip 命令且 python 兜底也失败');
  134. process.exit(1);
  135. }
  136. }
  137. const size = fs.statSync(zipPath).size;
  138. fs.rmSync(stageDir, { recursive: true, force: true });
  139. console.log(`\n${C.b}发布包已生成${C.x}\n`);
  140. console.log(` 文件: ${zipPath}`);
  141. console.log(` 大小: ${(size / 1024).toFixed(1)} KB`);
  142. console.log(` 技能: ${skills.length} 个 — ${skills.join(', ')}`);
  143. console.log(` 文件数: ${files.length}`);
  144. console.log(` 敏感自检: ${leaks === 0 ? `${C.g}通过(0 处)${C.x}` : `${C.r}${leaks} 处需处理${C.x}`}`);
  145. console.log(`\n${C.d}上传:SkillHub 后台 → 上传技能包 → 选此 zip${C.x}\n`);
  146. process.exit(leaks > 0 ? 1 : 0);