-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.ts
More file actions
101 lines (80 loc) · 2.79 KB
/
Copy pathreader.ts
File metadata and controls
101 lines (80 loc) · 2.79 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
import * as fs from 'fs';
import * as path from 'path';
import {Meta, MetaType} from "../index";
export const Reader = (rootPath: string): Meta[] => {
const result: Meta[] = [];
try {
const features = fs.readdirSync(rootPath, { withFileTypes: true });
for (const featureDirent of features) {
if (!featureDirent.isDirectory()) continue;
const featurePath = path.join(rootPath, featureDirent.name);
const featureName = featureDirent.name;
const typeDirs = fs.readdirSync(featurePath, { withFileTypes: true });
for (const typeDirent of typeDirs) {
if (!typeDirent.isDirectory()) continue;
const metaType = matchMetaType(typeDirent.name);
if (!metaType) continue;
const typePath = path.join(featurePath, typeDirent.name);
const definitions = fs.readdirSync(typePath, { withFileTypes: true });
for (const def of definitions) {
const defPath = path.join(typePath, def.name);
if (def.isFile()) {
const meta = MetaReader(featureName, metaType, defPath);
if (meta) result.push(meta);
} else if (def.isDirectory()) {
const subDefinitions = fs.readdirSync(defPath, { withFileTypes: true });
for (const subDef of subDefinitions) {
const subPath = path.join(defPath, subDef.name);
if (!subDef.isFile()) continue;
const meta = MetaReader(featureName, metaType, subPath);
if (meta) result.push(meta);
}
}
}
}
}
return result
} catch (err) {
console.error(`Error reading path ${rootPath}:`, err);
return [];
}
}
const MetaReader = (name: string, type: MetaType, filePath: string): Meta | null => {
let content: string;
try {
content = fs.readFileSync(filePath, 'utf-8');
} catch (err) {
console.error(`Error reading file: ${filePath}`, err);
return null;
}
const lines = content.split('\n');
let insideCode = false;
const currentBlock: string[] = [];
const codeSnippets: string[] = [];
for (const line of lines) {
if (line.includes('```')) {
insideCode = !insideCode;
if (!insideCode) {
codeSnippets.push(currentBlock.join(' '));
currentBlock.length = 0;
}
continue;
}
if (insideCode) {
currentBlock.push(line);
}
}
return { name, type, data: codeSnippets };
}
function matchMetaType(name: string): MetaType | null {
switch (name) {
case 'flow_type':
return MetaType.FlowType;
case 'data_type':
return MetaType.DataType;
case 'runtime_definition':
return MetaType.RuntimeFunction;
default:
return null;
}
}