generate-media-library-schema.js 2.0 KB

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