material.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import path from 'path';
  6. import { readdirSync } from 'fs';
  7. import { getIndexTsFile } from '../utils/ts-file';
  8. import { LoadedNpmPkg } from '../utils/npm';
  9. export class Material {
  10. protected static _all_materials_cache: Material[] = [];
  11. static ALL_TYPES = [
  12. 'components',
  13. 'effects',
  14. 'plugins',
  15. 'shared',
  16. 'validate',
  17. 'form-plugins',
  18. 'hooks',
  19. ];
  20. constructor(public type: string, public name: string, public formMaterialPkg: LoadedNpmPkg) {}
  21. get fullName() {
  22. return `${this.type}/${this.name}`;
  23. }
  24. get sourceDir() {
  25. return path.join(this.formMaterialPkg.srcPath, this.type, this.name);
  26. }
  27. get indexFile() {
  28. return getIndexTsFile(this.sourceDir);
  29. }
  30. get allExportNames() {
  31. return this.indexFile?.allExportNames || [];
  32. }
  33. static listAll(formMaterialPkg: LoadedNpmPkg): Material[] {
  34. if (!this._all_materials_cache.length) {
  35. this._all_materials_cache = Material.ALL_TYPES.map((type) => {
  36. const materialsPath: string = path.join(formMaterialPkg.srcPath, type);
  37. return readdirSync(materialsPath)
  38. .map((_path: string) => {
  39. if (_path === 'index.ts') {
  40. return null;
  41. }
  42. return new Material(type, _path, formMaterialPkg);
  43. })
  44. .filter((material): material is Material => material !== null);
  45. }).flat();
  46. }
  47. return this._all_materials_cache;
  48. }
  49. }