|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +export enum MetaType { |
| 5 | + FlowType = 'FlowType', |
| 6 | + DataType = 'DataType', |
| 7 | + RuntimeFunction = 'RuntimeFunction', |
| 8 | +} |
| 9 | + |
| 10 | +export interface Meta { |
| 11 | + name: string; |
| 12 | + type: MetaType; |
| 13 | + data: string[]; |
| 14 | +} |
| 15 | + |
| 16 | +export const Reader = (rootPath: string): Meta[] => { |
| 17 | + const result: Meta[] = []; |
| 18 | + |
| 19 | + try { |
| 20 | + const features = fs.readdirSync(rootPath, { withFileTypes: true }); |
| 21 | + |
| 22 | + for (const featureDirent of features) { |
| 23 | + if (!featureDirent.isDirectory()) continue; |
| 24 | + |
| 25 | + const featurePath = path.join(rootPath, featureDirent.name); |
| 26 | + const featureName = featureDirent.name; |
| 27 | + |
| 28 | + const typeDirs = fs.readdirSync(featurePath, { withFileTypes: true }); |
| 29 | + |
| 30 | + for (const typeDirent of typeDirs) { |
| 31 | + if (!typeDirent.isDirectory()) continue; |
| 32 | + |
| 33 | + const metaType = matchMetaType(typeDirent.name); |
| 34 | + if (!metaType) continue; |
| 35 | + |
| 36 | + const typePath = path.join(featurePath, typeDirent.name); |
| 37 | + const definitions = fs.readdirSync(typePath, { withFileTypes: true }); |
| 38 | + |
| 39 | + for (const def of definitions) { |
| 40 | + const defPath = path.join(typePath, def.name); |
| 41 | + |
| 42 | + if (def.isFile()) { |
| 43 | + const meta = MetaReader(featureName, metaType, defPath); |
| 44 | + if (meta) result.push(meta); |
| 45 | + } else if (def.isDirectory()) { |
| 46 | + const subDefinitions = fs.readdirSync(defPath, { withFileTypes: true }); |
| 47 | + |
| 48 | + for (const subDef of subDefinitions) { |
| 49 | + const subPath = path.join(defPath, subDef.name); |
| 50 | + if (!subDef.isFile()) continue; |
| 51 | + |
| 52 | + const meta = MetaReader(featureName, metaType, subPath); |
| 53 | + if (meta) result.push(meta); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return result |
| 61 | + } catch (err) { |
| 62 | + console.error(`Error reading path ${rootPath}:`, err); |
| 63 | + return []; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const MetaReader = (name: string, type: MetaType, filePath: string): Meta | null => { |
| 68 | + let content: string; |
| 69 | + |
| 70 | + try { |
| 71 | + content = fs.readFileSync(filePath, 'utf-8'); |
| 72 | + } catch (err) { |
| 73 | + console.error(`Error reading file: ${filePath}`, err); |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + const lines = content.split('\n'); |
| 78 | + let insideCode = false; |
| 79 | + const currentBlock: string[] = []; |
| 80 | + const codeSnippets: string[] = []; |
| 81 | + |
| 82 | + for (const line of lines) { |
| 83 | + if (line.includes('```')) { |
| 84 | + insideCode = !insideCode; |
| 85 | + |
| 86 | + if (!insideCode) { |
| 87 | + codeSnippets.push(currentBlock.join(' ')); |
| 88 | + currentBlock.length = 0; |
| 89 | + } |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + if (insideCode) { |
| 94 | + currentBlock.push(line); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return { name, type, data: codeSnippets }; |
| 99 | +} |
| 100 | + |
| 101 | +function matchMetaType(name: string): MetaType | null { |
| 102 | + switch (name) { |
| 103 | + case 'flow_type': |
| 104 | + return MetaType.FlowType; |
| 105 | + case 'data_type': |
| 106 | + return MetaType.DataType; |
| 107 | + case 'runtime_definition': |
| 108 | + return MetaType.RuntimeFunction; |
| 109 | + default: |
| 110 | + return null; |
| 111 | + } |
| 112 | +} |
0 commit comments