-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
126 lines (109 loc) · 4.03 KB
/
Copy pathindex.ts
File metadata and controls
126 lines (109 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {readdir, readFile} from "node:fs/promises";
import {join, extname} from "node:path";
import {DefinitionDataType, FlowType, FunctionDefinition, RuntimeFunctionDefinition} from "@code0-tech/tucana/shared";
export const enum MetaType {
FlowType = "FlowType",
DataType = "DataType",
RuntimeFunction = "RuntimeFunction",
Function = "Function"
}
export interface DefinitionError {
definition: string;
definition_type: MetaType;
error: string;
}
export interface Feature {
name: string;
data_types: DefinitionDataType[];
flow_types: FlowType[];
runtime_functions: RuntimeFunctionDefinition[];
functions: FunctionDefinition[];
errors: DefinitionError[];
}
export class Reader {
static async fromPath(root: string): Promise<Feature[]> {
const features = new Map<string, Feature>();
for (const featureDir of await safeReadDir(root)) {
if (!featureDir.isDirectory()) continue;
const featureName = featureDir.name;
const featurePath = join(root, featureName);
for (const typeDir of await safeReadDir(featurePath)) {
const type = toMetaType(typeDir.name);
if (!type) continue;
const typePath = join(featurePath, typeDir.name);
const jsonPaths = await collectJsonFiles(typePath);
for (const file of jsonPaths) {
const def = await readFile(file, "utf8");
const f = features.get(featureName) ?? emptyFeature(featureName);
addDefinition(f, def, type);
features.set(featureName, f);
}
}
}
return Array.from(features.values());
}
}
const toMetaType = (folder: string): MetaType | null =>
({
flow_type: MetaType.FlowType,
data_type: MetaType.DataType,
runtime_definition: MetaType.RuntimeFunction,
function: MetaType.Function,
} as const)[folder] ?? null;
const emptyFeature = (name: string): Feature => ({
name,
data_types: [],
flow_types: [],
runtime_functions: [],
functions: [],
errors: [],
});
const safeReadDir = async (p: string) => {
try {
return await readdir(p, {withFileTypes: true});
} catch {
return [];
}
};
const collectJsonFiles = async (dir: string): Promise<string[]> => {
const entries = await safeReadDir(dir);
const files = entries.filter(e => e.isFile() && extname(e.name) === ".json").map(e => join(dir, e.name));
// include one nested level
for (const e of entries.filter(e => e.isDirectory())) {
const sub = (await safeReadDir(join(dir, e.name)))
.filter(s => s.isFile() && extname(s.name) === ".json")
.map(s => join(dir, e.name, s.name));
files.push(...sub);
}
return files;
};
const addDefinition = (feature: Feature, def: string, type: MetaType) => {
try {
switch (type) {
case MetaType.DataType:
feature.data_types.push(DefinitionDataType.fromJsonString(def));
break
case MetaType.FlowType:
feature.flow_types.push(FlowType.fromJsonString(def));
break
case MetaType.RuntimeFunction:
feature.runtime_functions.push(RuntimeFunctionDefinition.fromJsonString(def));
break
case MetaType.Function:
feature.functions.push(FunctionDefinition.fromJsonString(def));
break
default:
throw new Error(`Unknown MetaType: ${type}`);
}
} catch (err) {
feature.errors.push({
definition: extractIdentifier(def, type),
definition_type: type,
error: err instanceof Error ? err.message : String(err),
});
}
};
const extractIdentifier = (def: string, type: MetaType): string => {
const key = type === MetaType.RuntimeFunction ? "runtime_name" : "identifier";
return def.match(new RegExp(`"${key}"\\s*:\\s*"([^"]+)"`))?.[1] ?? def;
};