Skip to content

Commit 8578ba7

Browse files
Merge pull request #21 from code0-tech/15-ts-reader
TypeScript Reader
2 parents 3b04584 + 1884403 commit 8578ba7

File tree

10 files changed

+440
-16
lines changed

10 files changed

+440
-16
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish Package to npmjs
2+
on:
3+
push:
4+
tags:
5+
- '*'
6+
7+
jobs:
8+
publish:
9+
defaults:
10+
run:
11+
working-directory: "./reader/ts"
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: '22.x'
18+
registry-url: 'https://registry.npmjs.org'
19+
- run: npm version from-git --git-tag-version=false
20+
- run: npm ci
21+
- run: npm run build
22+
- run: npm publish
23+
env:
24+
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22
*.DS_Store
3-
.idea
3+
**/node_modules
4+
.idea
5+
reader/ts/src/*.js

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
1-
# Definition CLI
1+
# Definitions
22

3-
## Setup
4-
```bash
5-
cargo build --release
6-
```
3+
## Definition CLI
74

8-
## Usage
5+
### Setup
6+
[Install Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html)
97

8+
### Usage
9+
(Stay inside the root directory when running the command)
1010
### General Report
1111
```bash
12-
./cli report
13-
./cli report -p /path/to/definitions
12+
./cargo run report
13+
./cargo run report -p /path/to/definitions
1414
```
1515

1616
### Feature Report
1717
```bash
18-
./cli feature
19-
./cli feature -f feature_name
20-
./cli feature -f feature_name -p /path/to/definitions
18+
./cargo run feature
19+
./cargo run feature -p /path/to/definitions
20+
./cargo run feature -f feature_name
21+
./cargo run feature -f feature_name -p /path/to/definitions
2122
```
2223

2324
### Watch for Changes
2425
```bash
25-
./cli watch
26-
./cli watch -p /path/to/definitions
26+
./cargo run watch
27+
./cargo run watch -p /path/to/definitions
2728
```
2829

2930
### Definition
3031
```bash
31-
./cli definition -n definition_name
32-
./cli definition -n definition_name -p /path/to/definitions
32+
./cargo run definition -n definition_name
33+
./cargo run definition -n definition_name -p /path/to/definitions
34+
```
35+
36+
## TypeScript Definition Package
37+
38+
### Install Package
39+
```bash
40+
npm i @code0-tech/code0-definition-reader --save-dev
3341
```
42+
43+
### Usage
44+
45+
```ts
46+
const features = Definition("./path/to/definitions")
47+
48+
for (const feature in features) {
49+
const name = feature.name; //name of the feature (e.g. rest)
50+
const dataTypes = fearture.dataTypes; //dataTypes of this feature
51+
const flowTypes = fearture.flowTypes; //flowTypes of this feature
52+
const functions = fearture.runtimeFunctions; //runtimeFunctions of this feature
53+
}
54+
```
55+

reader/ts/index.d.ts

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

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/package-lock.json

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

reader/ts/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "@code0-tech/definition-reader",
3+
"version": "0.0.0",
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+
}

reader/ts/src/parser.ts

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

reader/ts/src/reader.ts

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

0 commit comments

Comments
 (0)