|
| 1 | +import { fileURLToPath } from 'url'; |
| 2 | +import path from 'path'; |
| 3 | +import fs from 'fs'; |
| 4 | + |
| 5 | +import { ProjectInfo } from './project'; // Import ProjectInfo |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +// Added type definitions |
| 11 | +export interface Material { |
| 12 | + name: string; |
| 13 | + type: string; |
| 14 | + path: string; |
| 15 | + depPackages?: string[]; |
| 16 | + depMaterials?: string[]; |
| 17 | + [key: string]: any; // For other properties from config.json |
| 18 | +} |
| 19 | + |
| 20 | +const _types: string[] = ['components', 'effects', 'utils', 'typings']; |
| 21 | + |
| 22 | +export function listAllMaterials(): Material[] { |
| 23 | + const _materials: Material[] = []; |
| 24 | + |
| 25 | + for (const _type of _types) { |
| 26 | + // 在 Node.js 中,import.meta.dirname 不可用,可使用 import.meta.url 结合 url 模块来获取目录路径 |
| 27 | + |
| 28 | + const materialsPath: string = path.join(__dirname, '..', 'src', _type); |
| 29 | + _materials.push( |
| 30 | + ...fs |
| 31 | + .readdirSync(materialsPath) |
| 32 | + .map((_path: string) => { |
| 33 | + if (_path === 'index.ts') { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + const configPath = path.join(materialsPath, _path, 'config.json'); |
| 38 | + // Check if config.json exists before reading |
| 39 | + if (!fs.existsSync(configPath)) { |
| 40 | + console.warn( |
| 41 | + `Warning: config.json not found for material at ${path.join(materialsPath, _path)}` |
| 42 | + ); |
| 43 | + return null; |
| 44 | + } |
| 45 | + const configContent = fs.readFileSync(configPath, 'utf8'); |
| 46 | + const config = JSON.parse(configContent); |
| 47 | + |
| 48 | + return { |
| 49 | + ...config, |
| 50 | + name: _path, // Assuming the folder name is the material name |
| 51 | + type: _type, |
| 52 | + path: path.join(materialsPath, _path), |
| 53 | + } as Material; |
| 54 | + }) |
| 55 | + .filter((material): material is Material => material !== null) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return _materials; |
| 60 | +} |
| 61 | + |
| 62 | +interface BfsResult { |
| 63 | + allMaterials: Material[]; |
| 64 | + allPackages: string[]; |
| 65 | +} |
| 66 | + |
| 67 | +export function bfsMaterials(material: Material, _materials: Material[] = []): BfsResult { |
| 68 | + function findConfigByName(name: string): Material | undefined { |
| 69 | + return _materials.find( |
| 70 | + (_config) => _config.name === name || `${_config.type}/${_config.name}` === name |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const queue: (Material | undefined)[] = [material]; // Queue can hold undefined if findConfigByName returns undefined |
| 75 | + const allMaterials = new Set<Material>(); |
| 76 | + const allPackages = new Set<string>(); |
| 77 | + |
| 78 | + while (queue.length > 0) { |
| 79 | + const _material = queue.shift(); |
| 80 | + if (!_material || allMaterials.has(_material)) { |
| 81 | + // Check if _material is defined |
| 82 | + continue; |
| 83 | + } |
| 84 | + allMaterials.add(_material); |
| 85 | + |
| 86 | + if (_material.depPackages) { |
| 87 | + for (const _package of _material.depPackages) { |
| 88 | + allPackages.add(_package); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (_material.depMaterials) { |
| 93 | + for (const _materialName of _material.depMaterials) { |
| 94 | + const depMaterial = findConfigByName(_materialName); |
| 95 | + if (depMaterial) { |
| 96 | + // Ensure dependent material is found before adding to queue |
| 97 | + queue.push(depMaterial); |
| 98 | + } else { |
| 99 | + console.warn( |
| 100 | + `Warning: Dependent material "${_materialName}" not found for material "${_material.name}".` |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return { |
| 108 | + allMaterials: Array.from(allMaterials), |
| 109 | + allPackages: Array.from(allPackages), |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +export const copyMaterial = (material: Material, projectInfo: ProjectInfo): void => { |
| 114 | + const sourceDir: string = material.path; |
| 115 | + const materialRoot: string = path.join( |
| 116 | + projectInfo.projectPath, |
| 117 | + 'src', |
| 118 | + 'form-materials', |
| 119 | + `${material.type}` |
| 120 | + ); |
| 121 | + const targetDir = path.join(materialRoot, material.name); |
| 122 | + fs.cpSync(sourceDir, targetDir, { recursive: true }); |
| 123 | + |
| 124 | + let materialRootIndexTs: string = ''; |
| 125 | + const indexTsPath = path.join(materialRoot, 'index.ts'); |
| 126 | + if (fs.existsSync(indexTsPath)) { |
| 127 | + materialRootIndexTs = fs.readFileSync(indexTsPath, 'utf8'); |
| 128 | + } |
| 129 | + if (!materialRootIndexTs.includes(material.name)) { |
| 130 | + fs.writeFileSync( |
| 131 | + indexTsPath, |
| 132 | + `${materialRootIndexTs}${materialRootIndexTs.endsWith('\n') ? '' : '\n'}export * from './${ |
| 133 | + material.name |
| 134 | + }';\n` |
| 135 | + ); |
| 136 | + } |
| 137 | +}; |
0 commit comments