generate-pgy-schema.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { SOURCES, DIMENSIONS, CORE_FIELDS, INDEXES, buildDdl, buildQueryMap } = require('../mcp/src/features/tihao-sourcing/pgy-schema');
  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 files = {
  10. schema: path.join(outputDir, 'pgy-field-schema.json'),
  11. ddl: path.join(outputDir, 'pgy-collection-ddl.txt'),
  12. queryMap: path.join(outputDir, 'pgy-sourcing-query-map.md')
  13. };
  14. fs.writeFileSync(files.schema, JSON.stringify({
  15. collection: 'pgy_blogger',
  16. generatedAt: new Date().toISOString(),
  17. sources: SOURCES,
  18. dimensions: DIMENSIONS,
  19. coreFields: CORE_FIELDS,
  20. indexes: INDEXES
  21. }, null, 2), 'utf8');
  22. fs.writeFileSync(files.ddl, buildDdl(), 'utf8');
  23. fs.writeFileSync(files.queryMap, buildQueryMap(), 'utf8');
  24. console.log(JSON.stringify({
  25. outputDir,
  26. coreFieldCount: Object.keys(CORE_FIELDS).length,
  27. dimensionCount: Object.keys(DIMENSIONS).length,
  28. indexCount: INDEXES.length,
  29. files
  30. }, null, 2));
  31. }
  32. function parseArgs(argv) {
  33. const args = {};
  34. for (let index = 0; index < argv.length; index += 1) {
  35. const raw = argv[index];
  36. if (!raw.startsWith('--')) continue;
  37. const key = raw.slice(2).replace(/-([a-z])/g, (_, char) => char.toUpperCase());
  38. const next = argv[index + 1];
  39. if (!next || next.startsWith('--')) args[key] = true;
  40. else {
  41. args[key] = next;
  42. index += 1;
  43. }
  44. }
  45. return args;
  46. }
  47. if (require.main === module) {
  48. try {
  49. main();
  50. } catch (error) {
  51. console.error(error && error.stack ? error.stack : String(error));
  52. process.exit(1);
  53. }
  54. }
  55. module.exports = { main };