@code0-tech/definition-reader 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +20 -0
- package/index.js +3 -0
- package/package.json +31 -0
- package/src/parser.js +55 -0
- package/src/parser.ts +54 -0
- package/src/reader.js +126 -0
- package/src/reader.ts +101 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {DataType, FlowType, RuntimeFunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
|
|
2
|
+
|
|
3
|
+
export enum MetaType {
|
|
4
|
+
FlowType = 'FlowType',
|
|
5
|
+
DataType = 'DataType',
|
|
6
|
+
RuntimeFunction = 'RuntimeFunction',
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Meta {
|
|
10
|
+
name: string;
|
|
11
|
+
type: MetaType;
|
|
12
|
+
data: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Feature {
|
|
16
|
+
name: string;
|
|
17
|
+
dataTypes: DataType[];
|
|
18
|
+
flowTypes: FlowType[];
|
|
19
|
+
runtimeFunctions: RuntimeFunctionDefinition[];
|
|
20
|
+
}
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@code0-tech/definition-reader",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Reader for Code0-Definitions",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc"
|
|
10
|
+
},
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@code0-tech/sagittarius-graphql-types": "^0.0.0-f91466f0f343596ad170e7e5c5316a70b072594d",
|
|
15
|
+
"@types/node": "^24.1.0",
|
|
16
|
+
"typescript": "^5.8.3"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/code0-tech/code0-definitions.git"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"index.d.ts",
|
|
25
|
+
"index.js",
|
|
26
|
+
"package.json"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/parser.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Definition = void 0;
|
|
4
|
+
const reader_1 = require("./reader");
|
|
5
|
+
const index_1 = require("../index");
|
|
6
|
+
const Definition = (rootPath) => {
|
|
7
|
+
const meta = (0, reader_1.Reader)(rootPath);
|
|
8
|
+
if (!meta)
|
|
9
|
+
return [];
|
|
10
|
+
const features = [];
|
|
11
|
+
for (const m of meta) {
|
|
12
|
+
let feature = features.find((f) => f.name === m.name);
|
|
13
|
+
if (feature) {
|
|
14
|
+
appendMeta(feature, m);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
feature = {
|
|
18
|
+
name: m.name,
|
|
19
|
+
dataTypes: [],
|
|
20
|
+
flowTypes: [],
|
|
21
|
+
runtimeFunctions: [],
|
|
22
|
+
};
|
|
23
|
+
appendMeta(feature, m);
|
|
24
|
+
features.push(feature);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return features;
|
|
28
|
+
};
|
|
29
|
+
exports.Definition = Definition;
|
|
30
|
+
function appendMeta(feature, meta) {
|
|
31
|
+
for (const definition of meta.data) {
|
|
32
|
+
try {
|
|
33
|
+
switch (meta.type) {
|
|
34
|
+
case index_1.MetaType.DataType: {
|
|
35
|
+
const parsed = JSON.parse(definition);
|
|
36
|
+
feature.dataTypes.push(parsed);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case index_1.MetaType.FlowType: {
|
|
40
|
+
const parsed = JSON.parse(definition);
|
|
41
|
+
feature.flowTypes.push(parsed);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case index_1.MetaType.RuntimeFunction: {
|
|
45
|
+
const parsed = JSON.parse(definition);
|
|
46
|
+
feature.runtimeFunctions.push(parsed);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
console.error(`Error parsing ${meta.type} ${meta.name}:`, err);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {Reader} from './reader';
|
|
2
|
+
import {DataType, FlowType, RuntimeFunctionDefinition} from "@code0-tech/sagittarius-graphql-types";
|
|
3
|
+
import {Feature, Meta, MetaType} from "../index";
|
|
4
|
+
|
|
5
|
+
export const Definition = (rootPath: string): Feature[] => {
|
|
6
|
+
const meta = Reader(rootPath);
|
|
7
|
+
if (!meta) return [];
|
|
8
|
+
const features: Feature[] = [];
|
|
9
|
+
|
|
10
|
+
for (const m of meta) {
|
|
11
|
+
let feature = features.find((f) => f.name === m.name);
|
|
12
|
+
|
|
13
|
+
if (feature) {
|
|
14
|
+
appendMeta(feature, m);
|
|
15
|
+
} else {
|
|
16
|
+
feature = {
|
|
17
|
+
name: m.name,
|
|
18
|
+
dataTypes: [],
|
|
19
|
+
flowTypes: [],
|
|
20
|
+
runtimeFunctions: [],
|
|
21
|
+
};
|
|
22
|
+
appendMeta(feature, m);
|
|
23
|
+
features.push(feature);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return features;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function appendMeta(feature: Feature, meta: Meta): void {
|
|
31
|
+
for (const definition of meta.data) {
|
|
32
|
+
try {
|
|
33
|
+
switch (meta.type) {
|
|
34
|
+
case MetaType.DataType: {
|
|
35
|
+
const parsed = JSON.parse(definition) as DataType;
|
|
36
|
+
feature.dataTypes.push(parsed);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case MetaType.FlowType: {
|
|
40
|
+
const parsed = JSON.parse(definition) as FlowType;
|
|
41
|
+
feature.flowTypes.push(parsed);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case MetaType.RuntimeFunction: {
|
|
45
|
+
const parsed = JSON.parse(definition) as RuntimeFunctionDefinition;
|
|
46
|
+
feature.runtimeFunctions.push(parsed);
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} catch (err: any) {
|
|
51
|
+
console.error(`Error parsing ${meta.type} ${meta.name}:`, err);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/reader.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.Reader = void 0;
|
|
37
|
+
const fs = __importStar(require("fs"));
|
|
38
|
+
const path = __importStar(require("path"));
|
|
39
|
+
const index_1 = require("../index");
|
|
40
|
+
const Reader = (rootPath) => {
|
|
41
|
+
const result = [];
|
|
42
|
+
try {
|
|
43
|
+
const features = fs.readdirSync(rootPath, { withFileTypes: true });
|
|
44
|
+
for (const featureDirent of features) {
|
|
45
|
+
if (!featureDirent.isDirectory())
|
|
46
|
+
continue;
|
|
47
|
+
const featurePath = path.join(rootPath, featureDirent.name);
|
|
48
|
+
const featureName = featureDirent.name;
|
|
49
|
+
const typeDirs = fs.readdirSync(featurePath, { withFileTypes: true });
|
|
50
|
+
for (const typeDirent of typeDirs) {
|
|
51
|
+
if (!typeDirent.isDirectory())
|
|
52
|
+
continue;
|
|
53
|
+
const metaType = matchMetaType(typeDirent.name);
|
|
54
|
+
if (!metaType)
|
|
55
|
+
continue;
|
|
56
|
+
const typePath = path.join(featurePath, typeDirent.name);
|
|
57
|
+
const definitions = fs.readdirSync(typePath, { withFileTypes: true });
|
|
58
|
+
for (const def of definitions) {
|
|
59
|
+
const defPath = path.join(typePath, def.name);
|
|
60
|
+
if (def.isFile()) {
|
|
61
|
+
const meta = MetaReader(featureName, metaType, defPath);
|
|
62
|
+
if (meta)
|
|
63
|
+
result.push(meta);
|
|
64
|
+
}
|
|
65
|
+
else if (def.isDirectory()) {
|
|
66
|
+
const subDefinitions = fs.readdirSync(defPath, { withFileTypes: true });
|
|
67
|
+
for (const subDef of subDefinitions) {
|
|
68
|
+
const subPath = path.join(defPath, subDef.name);
|
|
69
|
+
if (!subDef.isFile())
|
|
70
|
+
continue;
|
|
71
|
+
const meta = MetaReader(featureName, metaType, subPath);
|
|
72
|
+
if (meta)
|
|
73
|
+
result.push(meta);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
console.error(`Error reading path ${rootPath}:`, err);
|
|
83
|
+
return [];
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
exports.Reader = Reader;
|
|
87
|
+
const MetaReader = (name, type, filePath) => {
|
|
88
|
+
let content;
|
|
89
|
+
try {
|
|
90
|
+
content = fs.readFileSync(filePath, 'utf-8');
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
console.error(`Error reading file: ${filePath}`, err);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const lines = content.split('\n');
|
|
97
|
+
let insideCode = false;
|
|
98
|
+
const currentBlock = [];
|
|
99
|
+
const codeSnippets = [];
|
|
100
|
+
for (const line of lines) {
|
|
101
|
+
if (line.includes('```')) {
|
|
102
|
+
insideCode = !insideCode;
|
|
103
|
+
if (!insideCode) {
|
|
104
|
+
codeSnippets.push(currentBlock.join(' '));
|
|
105
|
+
currentBlock.length = 0;
|
|
106
|
+
}
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (insideCode) {
|
|
110
|
+
currentBlock.push(line);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { name, type, data: codeSnippets };
|
|
114
|
+
};
|
|
115
|
+
function matchMetaType(name) {
|
|
116
|
+
switch (name) {
|
|
117
|
+
case 'flow_type':
|
|
118
|
+
return index_1.MetaType.FlowType;
|
|
119
|
+
case 'data_type':
|
|
120
|
+
return index_1.MetaType.DataType;
|
|
121
|
+
case 'runtime_definition':
|
|
122
|
+
return index_1.MetaType.RuntimeFunction;
|
|
123
|
+
default:
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/reader.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import {Meta, MetaType} from "../index";
|
|
4
|
+
|
|
5
|
+
export const Reader = (rootPath: string): Meta[] => {
|
|
6
|
+
const result: Meta[] = [];
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
const features = fs.readdirSync(rootPath, { withFileTypes: true });
|
|
10
|
+
|
|
11
|
+
for (const featureDirent of features) {
|
|
12
|
+
if (!featureDirent.isDirectory()) continue;
|
|
13
|
+
|
|
14
|
+
const featurePath = path.join(rootPath, featureDirent.name);
|
|
15
|
+
const featureName = featureDirent.name;
|
|
16
|
+
|
|
17
|
+
const typeDirs = fs.readdirSync(featurePath, { withFileTypes: true });
|
|
18
|
+
|
|
19
|
+
for (const typeDirent of typeDirs) {
|
|
20
|
+
if (!typeDirent.isDirectory()) continue;
|
|
21
|
+
|
|
22
|
+
const metaType = matchMetaType(typeDirent.name);
|
|
23
|
+
if (!metaType) continue;
|
|
24
|
+
|
|
25
|
+
const typePath = path.join(featurePath, typeDirent.name);
|
|
26
|
+
const definitions = fs.readdirSync(typePath, { withFileTypes: true });
|
|
27
|
+
|
|
28
|
+
for (const def of definitions) {
|
|
29
|
+
const defPath = path.join(typePath, def.name);
|
|
30
|
+
|
|
31
|
+
if (def.isFile()) {
|
|
32
|
+
const meta = MetaReader(featureName, metaType, defPath);
|
|
33
|
+
if (meta) result.push(meta);
|
|
34
|
+
} else if (def.isDirectory()) {
|
|
35
|
+
const subDefinitions = fs.readdirSync(defPath, { withFileTypes: true });
|
|
36
|
+
|
|
37
|
+
for (const subDef of subDefinitions) {
|
|
38
|
+
const subPath = path.join(defPath, subDef.name);
|
|
39
|
+
if (!subDef.isFile()) continue;
|
|
40
|
+
|
|
41
|
+
const meta = MetaReader(featureName, metaType, subPath);
|
|
42
|
+
if (meta) result.push(meta);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return result
|
|
50
|
+
} catch (err) {
|
|
51
|
+
console.error(`Error reading path ${rootPath}:`, err);
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const MetaReader = (name: string, type: MetaType, filePath: string): Meta | null => {
|
|
57
|
+
let content: string;
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
content = fs.readFileSync(filePath, 'utf-8');
|
|
61
|
+
} catch (err) {
|
|
62
|
+
console.error(`Error reading file: ${filePath}`, err);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const lines = content.split('\n');
|
|
67
|
+
let insideCode = false;
|
|
68
|
+
const currentBlock: string[] = [];
|
|
69
|
+
const codeSnippets: string[] = [];
|
|
70
|
+
|
|
71
|
+
for (const line of lines) {
|
|
72
|
+
if (line.includes('```')) {
|
|
73
|
+
insideCode = !insideCode;
|
|
74
|
+
|
|
75
|
+
if (!insideCode) {
|
|
76
|
+
codeSnippets.push(currentBlock.join(' '));
|
|
77
|
+
currentBlock.length = 0;
|
|
78
|
+
}
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (insideCode) {
|
|
83
|
+
currentBlock.push(line);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { name, type, data: codeSnippets };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function matchMetaType(name: string): MetaType | null {
|
|
91
|
+
switch (name) {
|
|
92
|
+
case 'flow_type':
|
|
93
|
+
return MetaType.FlowType;
|
|
94
|
+
case 'data_type':
|
|
95
|
+
return MetaType.DataType;
|
|
96
|
+
case 'runtime_definition':
|
|
97
|
+
return MetaType.RuntimeFunction;
|
|
98
|
+
default:
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|