install.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. // Copyright (c) 未来飞马
  3. //
  4. // This Source Code Form is subject to the terms of the Mozilla Public
  5. // License, v. 2.0. If a copy of the MPL was not distributed with this
  6. // file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. // skill-heterarchy 安装器 — 把技能文件铺到目标技能目录。
  8. //
  9. // 用法:
  10. // node bin/install.js [目标目录]
  11. // 默认目标:$SKILLS_DIR || /opt/data/skills/skill-heterarchy
  12. import { cp, mkdir, rm } from 'node:fs/promises';
  13. import { existsSync } from 'node:fs';
  14. import { dirname, resolve, join } from 'node:path';
  15. import { fileURLToPath } from 'node:url';
  16. import { VERSION, SKILL_NAME } from '../lib/index.mjs';
  17. const pkgRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
  18. const dest = resolve(process.argv[2] || process.env.SKILLS_DIR || `/opt/data/skills/${SKILL_NAME}`);
  19. // 需要安装的条目(与 package.json files 对齐;LESSONS.md 保留现场,不覆盖)
  20. const ENTRIES = ['SKILL.md', 'README.md', 'LICENSE', 'CHANGELOG.md', 'package.json', 'skill-package-manifest.json', 'lib', 'bin', 'scripts'];
  21. if (!existsSync(join(pkgRoot, 'SKILL.md'))) {
  22. console.error(`✗ 找不到源文件:${join(pkgRoot, 'SKILL.md')}`);
  23. process.exit(1);
  24. }
  25. await mkdir(dest, { recursive: true });
  26. for (const entry of ENTRIES) {
  27. const src = join(pkgRoot, entry);
  28. if (!existsSync(src)) continue;
  29. const dst = join(dest, entry);
  30. await rm(dst, { recursive: true, force: true }); // 覆盖旧版本,避免残留
  31. await cp(src, dst, { recursive: true });
  32. }
  33. // LESSONS.md 若已存在则保留(教训是现场记录,重新安装不应清空)
  34. let kept = '';
  35. try {
  36. const src = join(pkgRoot, 'LESSONS.md');
  37. const dst = join(dest, 'LESSONS.md');
  38. if (existsSync(dst)) kept = '(LESSONS.md 已保留,未覆盖)';
  39. else if (existsSync(src)) await cp(src, dst);
  40. } catch { /* 非致命 */ }
  41. console.log(`${SKILL_NAME} v${VERSION} 已安装 → ${dest}${kept}`);