Skip to content

Commit 5b61b72

Browse files
committed
feat: added definition reader
1 parent 6bd0c9d commit 5b61b72

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

reader/ts/index.d.ts

Whitespace-only changes.

reader/ts/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {Definition} from './src/parser';
2+
3+
export {Definition};

reader/ts/src/parser.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {Meta, MetaType, Reader} from './reader';
2+
import {DataType, FlowType, RuntimeFunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
3+
4+
export interface Feature {
5+
name: string;
6+
data_types: DataType[];
7+
flow_types: FlowType[];
8+
runtime_functions: RuntimeFunctionDefinition[];
9+
}
10+
11+
export const Definition = (rootPath: string): Feature[] => {
12+
const meta = Reader(rootPath);
13+
if (!meta) return [];
14+
const features: Feature[] = [];
15+
16+
for (const m of meta) {
17+
let feature = features.find((f) => f.name === m.name);
18+
19+
if (feature) {
20+
appendMeta(feature, m);
21+
} else {
22+
feature = {
23+
name: m.name,
24+
data_types: [],
25+
flow_types: [],
26+
runtime_functions: [],
27+
};
28+
appendMeta(feature, m);
29+
features.push(feature);
30+
}
31+
}
32+
33+
return features;
34+
}
35+
36+
function appendMeta(feature: Feature, meta: Meta): void {
37+
for (const definition of meta.data) {
38+
try {
39+
switch (meta.type) {
40+
case MetaType.DataType: {
41+
const parsed = JSON.parse(definition) as DataType;
42+
feature.data_types.push(parsed);
43+
break;
44+
}
45+
case MetaType.FlowType: {
46+
const parsed = JSON.parse(definition) as FlowType;
47+
feature.flow_types.push(parsed);
48+
break;
49+
}
50+
case MetaType.RuntimeFunction: {
51+
const parsed = JSON.parse(definition) as RuntimeFunctionDefinition;
52+
feature.runtime_functions.push(parsed);
53+
break;
54+
}
55+
}
56+
} catch (err: any) {
57+
console.error(`Error parsing ${meta.type} ${meta.name}:`, err);
58+
}
59+
}
60+
}

reader/ts/src/reader.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)