|
1 | | -export { Definition } from '../../mapper/src/definition/remote.js'; |
2 | | -export type { Feature } from '../../mapper/src/definition/remote.js'; |
| 1 | +import {readdir, readFile} from "node:fs/promises"; |
| 2 | +import {join, extname} from "node:path"; |
| 3 | + |
| 4 | +import {FlowType} from "@code0-tech/tucana/pb/shared.flow_definition_pb.js"; |
| 5 | +import {RuntimeFunctionDefinition} from "@code0-tech/tucana/pb/shared.runtime_function_pb.js"; |
| 6 | +import {DefinitionDataType} from "@code0-tech/tucana/pb/shared.data_type_pb.js"; |
| 7 | + |
| 8 | +export const enum MetaType { |
| 9 | + FlowType = "FlowType", |
| 10 | + DataType = "DataType", |
| 11 | + RuntimeFunction = "RuntimeFunction", |
| 12 | +} |
| 13 | + |
| 14 | +export interface DefinitionError { |
| 15 | + definition: string; |
| 16 | + definition_type: MetaType; |
| 17 | + error: string; |
| 18 | +} |
| 19 | + |
| 20 | +export interface Feature { |
| 21 | + name: string; |
| 22 | + data_types: DefinitionDataType[]; |
| 23 | + flow_types: FlowType[]; |
| 24 | + runtime_functions: RuntimeFunctionDefinition[]; |
| 25 | + errors: DefinitionError[]; |
| 26 | +} |
| 27 | + |
| 28 | +export class Reader { |
| 29 | + static async fromPath(root: string): Promise<Feature[]> { |
| 30 | + const features = new Map<string, Feature>(); |
| 31 | + |
| 32 | + for (const featureDir of await safeReadDir(root)) { |
| 33 | + if (!featureDir.isDirectory()) continue; |
| 34 | + const featureName = featureDir.name; |
| 35 | + const featurePath = join(root, featureName); |
| 36 | + |
| 37 | + for (const typeDir of await safeReadDir(featurePath)) { |
| 38 | + const type = toMetaType(typeDir.name); |
| 39 | + if (!type) continue; |
| 40 | + |
| 41 | + const typePath = join(featurePath, typeDir.name); |
| 42 | + const jsonPaths = await collectJsonFiles(typePath); |
| 43 | + |
| 44 | + for (const file of jsonPaths) { |
| 45 | + const def = await readFile(file, "utf8"); |
| 46 | + const f = features.get(featureName) ?? emptyFeature(featureName); |
| 47 | + addDefinition(f, def, type); |
| 48 | + features.set(featureName, f); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return Array.from(features.values()); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +const toMetaType = (folder: string): MetaType | null => |
| 58 | + ({ |
| 59 | + flow_type: MetaType.FlowType, |
| 60 | + data_type: MetaType.DataType, |
| 61 | + runtime_definition: MetaType.RuntimeFunction |
| 62 | + } as const)[folder] ?? null; |
| 63 | + |
| 64 | +const emptyFeature = (name: string): Feature => ({ |
| 65 | + name, |
| 66 | + data_types: [], |
| 67 | + flow_types: [], |
| 68 | + runtime_functions: [], |
| 69 | + errors: [], |
| 70 | +}); |
| 71 | + |
| 72 | +const safeReadDir = async (p: string) => { |
| 73 | + try { |
| 74 | + return await readdir(p, {withFileTypes: true}); |
| 75 | + } catch { |
| 76 | + return []; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +const collectJsonFiles = async (dir: string): Promise<string[]> => { |
| 81 | + const entries = await safeReadDir(dir); |
| 82 | + const files = entries.filter(e => e.isFile() && extname(e.name) === ".json").map(e => join(dir, e.name)); |
| 83 | + |
| 84 | + // include one nested level |
| 85 | + for (const e of entries.filter(e => e.isDirectory())) { |
| 86 | + const sub = (await safeReadDir(join(dir, e.name))) |
| 87 | + .filter(s => s.isFile() && extname(s.name) === ".json") |
| 88 | + .map(s => join(dir, e.name, s.name)); |
| 89 | + files.push(...sub); |
| 90 | + } |
| 91 | + return files; |
| 92 | +}; |
| 93 | + |
| 94 | +const addDefinition = (feature: Feature, def: string, type: MetaType) => { |
| 95 | + try { |
| 96 | + if (type === MetaType.DataType) feature.data_types.push(DefinitionDataType.fromJsonString(def)); |
| 97 | + else if (type === MetaType.FlowType) feature.flow_types.push(FlowType.fromJsonString(def)); |
| 98 | + else feature.runtime_functions.push(RuntimeFunctionDefinition.fromJsonString(def)); |
| 99 | + } catch (err) { |
| 100 | + feature.errors.push({ |
| 101 | + definition: extractIdentifier(def, type), |
| 102 | + definition_type: type, |
| 103 | + error: err instanceof Error ? err.message : String(err), |
| 104 | + }); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +const extractIdentifier = (def: string, type: MetaType): string => { |
| 109 | + const key = type === MetaType.RuntimeFunction ? "runtime_name" : "identifier"; |
| 110 | + return def.match(new RegExp(`"${key}"\\s*:\\s*"([^"]+)"`))?.[1] ?? def; |
| 111 | +}; |
| 112 | + |
0 commit comments