@xrpckit/codegen 0.0.1
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/dist/index.d.ts +64 -0
- package/dist/index.js +116 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ContractDefinition } from '@xrpckit/parser';
|
|
2
|
+
|
|
3
|
+
declare class CodeWriter {
|
|
4
|
+
private lines;
|
|
5
|
+
private indentLevel;
|
|
6
|
+
private indentString;
|
|
7
|
+
writeLine(text: string): this;
|
|
8
|
+
write(text: string): this;
|
|
9
|
+
indent(): this;
|
|
10
|
+
unindent(): this;
|
|
11
|
+
newLine(): this;
|
|
12
|
+
block(fn: () => void): this;
|
|
13
|
+
l(text: string): this;
|
|
14
|
+
n(): this;
|
|
15
|
+
i(): this;
|
|
16
|
+
u(): this;
|
|
17
|
+
brace(fn: () => void): this;
|
|
18
|
+
template(strings: TemplateStringsArray, ...values: unknown[]): this;
|
|
19
|
+
toString(): string;
|
|
20
|
+
reset(): this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface GeneratorConfig {
|
|
24
|
+
outputDir: string;
|
|
25
|
+
packageName?: string;
|
|
26
|
+
framework?: string;
|
|
27
|
+
options?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
interface GeneratedFiles {
|
|
30
|
+
types?: string;
|
|
31
|
+
server?: string;
|
|
32
|
+
client?: string;
|
|
33
|
+
validation?: string;
|
|
34
|
+
}
|
|
35
|
+
declare abstract class BaseCodeGenerator {
|
|
36
|
+
protected writer: CodeWriter;
|
|
37
|
+
protected config: GeneratorConfig;
|
|
38
|
+
constructor(config: GeneratorConfig);
|
|
39
|
+
abstract generate(contract: ContractDefinition): GeneratedFiles;
|
|
40
|
+
protected toPascalCase(str: string): string;
|
|
41
|
+
protected toCamelCase(str: string): string;
|
|
42
|
+
protected toSnakeCase(str: string): string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Converts a string to PascalCase
|
|
47
|
+
* @param str - The string to convert (e.g., "hello-world" or "hello_world")
|
|
48
|
+
* @returns The PascalCase version (e.g., "HelloWorld")
|
|
49
|
+
*/
|
|
50
|
+
declare function toPascalCase(str: string): string;
|
|
51
|
+
/**
|
|
52
|
+
* Converts a string to camelCase
|
|
53
|
+
* @param str - The string to convert
|
|
54
|
+
* @returns The camelCase version (e.g., "helloWorld")
|
|
55
|
+
*/
|
|
56
|
+
declare function toCamelCase(str: string): string;
|
|
57
|
+
/**
|
|
58
|
+
* Converts a string to snake_case
|
|
59
|
+
* @param str - The string to convert
|
|
60
|
+
* @returns The snake_case version (e.g., "hello_world")
|
|
61
|
+
*/
|
|
62
|
+
declare function toSnakeCase(str: string): string;
|
|
63
|
+
|
|
64
|
+
export { BaseCodeGenerator, CodeWriter, type GeneratedFiles, type GeneratorConfig, toCamelCase, toPascalCase, toSnakeCase };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// src/code-writer.ts
|
|
2
|
+
var CodeWriter = class {
|
|
3
|
+
lines = [];
|
|
4
|
+
indentLevel = 0;
|
|
5
|
+
indentString = " ";
|
|
6
|
+
// 4 spaces
|
|
7
|
+
// Main methods
|
|
8
|
+
writeLine(text) {
|
|
9
|
+
const indent = this.indentString.repeat(this.indentLevel);
|
|
10
|
+
this.lines.push(indent + text);
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
write(text) {
|
|
14
|
+
if (this.lines.length === 0) {
|
|
15
|
+
this.lines.push("");
|
|
16
|
+
}
|
|
17
|
+
const lastLine = this.lines[this.lines.length - 1];
|
|
18
|
+
this.lines[this.lines.length - 1] = lastLine + text;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
indent() {
|
|
22
|
+
this.indentLevel++;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
unindent() {
|
|
26
|
+
this.indentLevel = Math.max(0, this.indentLevel - 1);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
newLine() {
|
|
30
|
+
this.lines.push("");
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
block(fn) {
|
|
34
|
+
this.writeLine("{");
|
|
35
|
+
this.indent();
|
|
36
|
+
fn();
|
|
37
|
+
this.unindent();
|
|
38
|
+
this.writeLine("}");
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
// Short aliases for more concise DSL
|
|
42
|
+
l(text) {
|
|
43
|
+
return this.writeLine(text);
|
|
44
|
+
}
|
|
45
|
+
n() {
|
|
46
|
+
return this.newLine();
|
|
47
|
+
}
|
|
48
|
+
i() {
|
|
49
|
+
return this.indent();
|
|
50
|
+
}
|
|
51
|
+
u() {
|
|
52
|
+
return this.unindent();
|
|
53
|
+
}
|
|
54
|
+
// Common patterns
|
|
55
|
+
brace(fn) {
|
|
56
|
+
return this.block(fn);
|
|
57
|
+
}
|
|
58
|
+
// Template literal support
|
|
59
|
+
template(strings, ...values) {
|
|
60
|
+
let result = "";
|
|
61
|
+
for (let i = 0; i < strings.length; i++) {
|
|
62
|
+
result += strings[i];
|
|
63
|
+
if (i < values.length) {
|
|
64
|
+
result += String(values[i]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return this.writeLine(result);
|
|
68
|
+
}
|
|
69
|
+
toString() {
|
|
70
|
+
return this.lines.join("\n");
|
|
71
|
+
}
|
|
72
|
+
reset() {
|
|
73
|
+
this.lines = [];
|
|
74
|
+
this.indentLevel = 0;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// src/base-generator.ts
|
|
80
|
+
var BaseCodeGenerator = class {
|
|
81
|
+
writer;
|
|
82
|
+
config;
|
|
83
|
+
constructor(config) {
|
|
84
|
+
this.writer = new CodeWriter();
|
|
85
|
+
this.config = config;
|
|
86
|
+
}
|
|
87
|
+
toPascalCase(str) {
|
|
88
|
+
return str.split(/[-_]/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
89
|
+
}
|
|
90
|
+
toCamelCase(str) {
|
|
91
|
+
const pascal = this.toPascalCase(str);
|
|
92
|
+
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
93
|
+
}
|
|
94
|
+
toSnakeCase(str) {
|
|
95
|
+
return str.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// src/utils.ts
|
|
100
|
+
function toPascalCase(str) {
|
|
101
|
+
return str.split(/[-_]/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
102
|
+
}
|
|
103
|
+
function toCamelCase(str) {
|
|
104
|
+
const pascal = toPascalCase(str);
|
|
105
|
+
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
106
|
+
}
|
|
107
|
+
function toSnakeCase(str) {
|
|
108
|
+
return str.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
109
|
+
}
|
|
110
|
+
export {
|
|
111
|
+
BaseCodeGenerator,
|
|
112
|
+
CodeWriter,
|
|
113
|
+
toCamelCase,
|
|
114
|
+
toPascalCase,
|
|
115
|
+
toSnakeCase
|
|
116
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xrpckit/codegen",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/mwesox/xrpc.git",
|
|
9
|
+
"directory": "packages/codegen"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"bun": "./src/index.ts",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format esm --dts --clean"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@xrpckit/parser": "^0.0.1"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^22.0.0",
|
|
34
|
+
"tsup": "^8.0.0",
|
|
35
|
+
"typescript": "^5.0.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|