generate-brief-rule-patterns.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { CASE_PATTERNS, PATTERN_LIBRARY } = require('../mcp/src/features/tihao-sourcing/brief-rule-patterns');
  5. function main() {
  6. const args = parseArgs(process.argv.slice(2));
  7. const outputDir = path.resolve(args.output || path.join(__dirname, '..', 'docs'));
  8. fs.mkdirSync(outputDir, { recursive: true });
  9. const outFile = path.join(outputDir, 'brief-rule-patterns.json');
  10. fs.writeFileSync(outFile, JSON.stringify({
  11. generatedAt: new Date().toISOString(),
  12. caseCount: CASE_PATTERNS.length,
  13. casePatterns: CASE_PATTERNS,
  14. patternLibrary: Object.entries(PATTERN_LIBRARY).map(([key, item]) => ({
  15. key,
  16. label: item.label,
  17. note: item.note,
  18. regex: item.regex.source
  19. }))
  20. }, null, 2), 'utf8');
  21. console.log(JSON.stringify({ outFile, caseCount: CASE_PATTERNS.length, patternCount: Object.keys(PATTERN_LIBRARY).length }, null, 2));
  22. }
  23. function parseArgs(argv) {
  24. const args = {};
  25. for (let index = 0; index < argv.length; index += 1) {
  26. const raw = argv[index];
  27. if (!raw.startsWith('--')) continue;
  28. const key = raw.slice(2).replace(/-([a-z])/g, (_, char) => char.toUpperCase());
  29. const next = argv[index + 1];
  30. if (!next || next.startsWith('--')) args[key] = true;
  31. else {
  32. args[key] = next;
  33. index += 1;
  34. }
  35. }
  36. return args;
  37. }
  38. if (require.main === module) {
  39. try {
  40. main();
  41. } catch (error) {
  42. console.error(error && error.stack ? error.stack : String(error));
  43. process.exit(1);
  44. }
  45. }
  46. module.exports = { main };