__CLI_NAME__.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env node
  2. /**
  3. * __CLI_NAME__ — __SKILL_NAME__ 的命令行入口
  4. * ---------------------------------------------------------------------------
  5. * 用法:npx --yes __SKILL_NAME__@latest <command> [args]
  6. */
  7. import { VERSION, SKILL_NAME, greet, resolveToken } from '../lib/index.mjs';
  8. const C = {
  9. reset: '\x1b[0m',
  10. bold: '\x1b[1m',
  11. dim: '\x1b[2m',
  12. red: '\x1b[31m',
  13. green: '\x1b[32m',
  14. cyan: '\x1b[36m',
  15. };
  16. const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
  17. const c = (code, s) => (useColor ? `${code}${s}${C.reset}` : s);
  18. function printHelp() {
  19. console.log(`
  20. ${c(C.bold, '__CLI_NAME__')} · __SUMMARY__ v${VERSION}
  21. ${c(C.bold, '用法')}
  22. __CLI_NAME__ <command> [args]
  23. ${c(C.bold, '命令')}
  24. ${c(C.cyan, 'greet')} [name] 示例命令
  25. ${c(C.cyan, 'auth')} 检查 Fmode 凭据是否可用
  26. ${c(C.cyan, 'workspace')} 安装到当前工作区(npx 约定入口)
  27. ${c(C.cyan, '--help')} 显示帮助
  28. ${c(C.cyan, '--version')} 显示版本
  29. `);
  30. }
  31. async function cmdGreet(args) {
  32. console.log(greet(args[0] || 'world'));
  33. return 0;
  34. }
  35. async function cmdAuth() {
  36. const r = await resolveToken();
  37. if (r) {
  38. const masked = r.token.length > 12 ? `${r.token.slice(0, 6)}...${r.token.slice(-4)}` : '***';
  39. console.log(`${c(C.green, '✅')} 凭据可用:${r.source}(${masked})`);
  40. return 0;
  41. }
  42. console.log(`${c(C.red, '❌')} 未找到可用凭据。`);
  43. console.log(`
  44. 请任选一种方式配置:
  45. 1) export FMODE_API_TOKEN='sk-...'
  46. 2) export FMODE_SESSION_TOKEN='r:...' ${c(C.dim, '# 登录 FMODE Studio 取得,将自动换取 API token')}
  47. 3) 在 ~/.fmode/config.json 写入 { "fmodeApiToken": "sk-..." }
  48. `);
  49. return 1;
  50. }
  51. async function cmdWorkspace() {
  52. console.log(`${c(C.green, '✅')} ${SKILL_NAME} 已就绪(v${VERSION})。`);
  53. console.log(` 在 Agent 会话中直接描述你的任务即可触发本技能。`);
  54. return 0;
  55. }
  56. async function main() {
  57. const argv = process.argv.slice(2);
  58. const cmd = argv[0];
  59. if (!cmd || cmd === '--help' || cmd === '-h') {
  60. printHelp();
  61. return 0;
  62. }
  63. if (cmd === '--version' || cmd === '-v') {
  64. console.log(VERSION);
  65. return 0;
  66. }
  67. switch (cmd) {
  68. case 'greet':
  69. return cmdGreet(argv.slice(1));
  70. case 'auth':
  71. return cmdAuth();
  72. case 'workspace':
  73. return cmdWorkspace();
  74. default:
  75. console.error(`${c(C.red, '✗')} 未知命令:${cmd}`);
  76. console.error(` 运行 ${c(C.bold, '__CLI_NAME__ --help')} 查看可用命令。`);
  77. return 2;
  78. }
  79. }
  80. main()
  81. .then((code) => process.exit(code))
  82. .catch((err) => {
  83. console.error(`${c(C.red, '✗')} ${err?.stack || err}`);
  84. process.exit(1);
  85. });