go2typebox 0.1.0
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/README.md +56 -0
- package/dist/generator.d.ts +14 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +165 -0
- package/dist/generator.js.map +1 -0
- package/dist/go2typebox.d.ts +54 -0
- package/dist/go2typebox.d.ts.map +1 -0
- package/dist/go2typebox.js +219 -0
- package/dist/go2typebox.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/parser.d.ts +10 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +87 -0
- package/dist/parser.js.map +1 -0
- package/dist/pruneTypeboxFile.d.ts +18 -0
- package/dist/pruneTypeboxFile.d.ts.map +1 -0
- package/dist/pruneTypeboxFile.js +192 -0
- package/dist/pruneTypeboxFile.js.map +1 -0
- package/dist/sorter.d.ts +15 -0
- package/dist/sorter.d.ts.map +1 -0
- package/dist/sorter.js +113 -0
- package/dist/sorter.js.map +1 -0
- package/dist/standardizeTypeboxTypes.d.ts +48 -0
- package/dist/standardizeTypeboxTypes.d.ts.map +1 -0
- package/dist/standardizeTypeboxTypes.js +351 -0
- package/dist/standardizeTypeboxTypes.js.map +1 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +64 -0
- package/dist/utils.js.map +1 -0
- package/dist/writer.d.ts +16 -0
- package/dist/writer.d.ts.map +1 -0
- package/dist/writer.js +105 -0
- package/dist/writer.js.map +1 -0
- package/package.json +53 -0
package/dist/parser.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseGoStructs = parseGoStructs;
|
|
4
|
+
exports.parseGoTypeAliases = parseGoTypeAliases;
|
|
5
|
+
exports.parseGoSliceTypeAliases = parseGoSliceTypeAliases;
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
/** Parses the body of a Go `struct { … }` into `GoField` entries (best-effort regex). */
|
|
8
|
+
function parseFields(fieldsStr) {
|
|
9
|
+
const fields = [];
|
|
10
|
+
const lines = fieldsStr.split('\n');
|
|
11
|
+
for (const line of lines) {
|
|
12
|
+
const trimmed = line.trim();
|
|
13
|
+
if (!trimmed)
|
|
14
|
+
continue;
|
|
15
|
+
const fieldMatch = trimmed.match(/^(\w+)\s+((?:\*?)((?:\[\])*\w+(?:\.\w+)?))(?:\s+`([^`]*)`)?/);
|
|
16
|
+
if (!fieldMatch)
|
|
17
|
+
continue;
|
|
18
|
+
const [, name, fullType, baseType, tags] = fieldMatch;
|
|
19
|
+
const isPointer = fullType.startsWith('*');
|
|
20
|
+
const arrayMatches = baseType.match(/\[\]/g);
|
|
21
|
+
const arrayDepth = arrayMatches ? arrayMatches.length : 0;
|
|
22
|
+
const cleanType = baseType.replace(/\[\]/g, '');
|
|
23
|
+
fields.push({
|
|
24
|
+
name,
|
|
25
|
+
jsonName: (0, utils_1.jsonNameFromStructTags)(tags, name),
|
|
26
|
+
type: cleanType,
|
|
27
|
+
isPointer,
|
|
28
|
+
jsonOmitempty: (0, utils_1.hasJsonOmitempty)(tags),
|
|
29
|
+
isArray: arrayDepth > 0,
|
|
30
|
+
arrayDepth,
|
|
31
|
+
tags,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return fields;
|
|
35
|
+
}
|
|
36
|
+
/** Extracts all `type Name struct { … }` blocks from Go source (comments stripped). */
|
|
37
|
+
function parseGoStructs(content) {
|
|
38
|
+
const structs = [];
|
|
39
|
+
const withoutComments = (0, utils_1.removeComments)(content);
|
|
40
|
+
const structRegex = /(?:^|\n)\s*type\s+(\w+)\s+struct\s*\{([\s\S]*?)\}/g;
|
|
41
|
+
let match;
|
|
42
|
+
while ((match = structRegex.exec(withoutComments)) !== null) {
|
|
43
|
+
const structName = match[1];
|
|
44
|
+
const fieldsStr = match[2];
|
|
45
|
+
const fields = parseFields(fieldsStr);
|
|
46
|
+
structs.push({
|
|
47
|
+
name: structName,
|
|
48
|
+
fields,
|
|
49
|
+
dependencies: [],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return structs;
|
|
53
|
+
}
|
|
54
|
+
/** Parses `type Alias = T` (and `type Alias = *T`) aliases into alias → target type string. */
|
|
55
|
+
function parseGoTypeAliases(content) {
|
|
56
|
+
const withoutComments = (0, utils_1.removeComments)(content);
|
|
57
|
+
const aliases = new Map();
|
|
58
|
+
const aliasRegex = /(?:^|\n)\s*type\s+(\w+)\s*=\s*(\*?)([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)\b/g;
|
|
59
|
+
let match;
|
|
60
|
+
while ((match = aliasRegex.exec(withoutComments)) !== null) {
|
|
61
|
+
const alias = match[1];
|
|
62
|
+
const star = match[2] ?? '';
|
|
63
|
+
const target = match[3] ?? '';
|
|
64
|
+
aliases.set(alias, `${star}${target}`);
|
|
65
|
+
}
|
|
66
|
+
return aliases;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Parses slice type aliases: `type IDs []int`, `type Rows [][]string`, `type List []pkg.Item`.
|
|
70
|
+
*/
|
|
71
|
+
function parseGoSliceTypeAliases(content) {
|
|
72
|
+
const withoutComments = (0, utils_1.removeComments)(content);
|
|
73
|
+
const out = new Map();
|
|
74
|
+
const sliceRegex = /(?:^|\n)\s*type\s+(\w+)\s+((?:\[\])+)\s*([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)\b/g;
|
|
75
|
+
let match;
|
|
76
|
+
while ((match = sliceRegex.exec(withoutComments)) !== null) {
|
|
77
|
+
const name = match[1];
|
|
78
|
+
const brackets = match[2];
|
|
79
|
+
const elementType = match[3];
|
|
80
|
+
const arrayDepth = brackets.match(/\[\]/g)?.length ?? 0;
|
|
81
|
+
if (arrayDepth > 0 && name && elementType) {
|
|
82
|
+
out.set(name, { elementType, arrayDepth });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":";;AA4CA,wCAoBC;AAGD,gDAeC;AAKD,0DAkBC;AAxGD,mCAIiB;AAEjB,yFAAyF;AACzF,SAAS,WAAW,CAAC,SAAiB;IACpC,MAAM,MAAM,GAAc,EAAE,CAAC;IAC7B,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAEpC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;QAC5B,IAAI,CAAC,OAAO;YAAE,SAAS;QAEvB,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAC9B,6DAA6D,CAC9D,CAAC;QAEF,IAAI,CAAC,UAAU;YAAE,SAAS;QAE1B,MAAM,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,CAAC,GAAG,UAAU,CAAC;QACtD,MAAM,SAAS,GAAG,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAC3C,MAAM,YAAY,GAAG,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC7C,MAAM,UAAU,GAAG,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;QAC1D,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAEhD,MAAM,CAAC,IAAI,CAAC;YACV,IAAI;YACJ,QAAQ,EAAE,IAAA,8BAAsB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CAAC;QACvB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9B,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,SAAgB,uBAAuB,CAAC,OAAe;IACrD,MAAM,eAAe,GAAG,IAAA,sBAAc,EAAC,OAAO,CAAC,CAAC;IAChD,MAAM,GAAG,GAAwB,IAAI,GAAG,EAAE,CAAC;IAE3C,MAAM,UAAU,GACd,8EAA8E,CAAC;IACjF,IAAI,KAA6B,CAAC;IAClC,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;QAC3D,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAC1B,MAAM,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7B,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC,CAAC;QACxD,IAAI,UAAU,GAAG,CAAC,IAAI,IAAI,IAAI,WAAW,EAAE,CAAC;YAC1C,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;QAC7C,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type PruneTypeboxFileOptions = {
|
|
2
|
+
runPrettier?: boolean;
|
|
3
|
+
prettierCommand?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Removes unused schemas and types from a generated TypeBox file.
|
|
7
|
+
* Roots are every `export const Request*Schema` / `export const Response*Schema`;
|
|
8
|
+
* only those and chains of references to other `*Schema` inside initializers are kept.
|
|
9
|
+
*
|
|
10
|
+
* @param targetFilePath - Path to the TypeScript file to prune in place.
|
|
11
|
+
* @param options - Optional Prettier run after writing.
|
|
12
|
+
* @returns Counts of removed vs kept schema declarations.
|
|
13
|
+
*/
|
|
14
|
+
export declare function pruneTypeboxFileByRequestResponseRoots(targetFilePath: string, options?: PruneTypeboxFileOptions): {
|
|
15
|
+
removedSchemaCount: number;
|
|
16
|
+
keptSchemaCount: number;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=pruneTypeboxFile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pruneTypeboxFile.d.ts","sourceRoot":"","sources":["../src/pruneTypeboxFile.ts"],"names":[],"mappings":"AAsDA,MAAM,MAAM,uBAAuB,GAAG;IACpC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,sCAAsC,CACpD,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,uBAAuB,GAChC;IAAE,kBAAkB,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CA+HzD"}
|
|
@@ -0,0 +1,192 @@
|
|
|
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.pruneTypeboxFileByRequestResponseRoots = pruneTypeboxFileByRequestResponseRoots;
|
|
37
|
+
const child_process_1 = require("child_process");
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const ts = __importStar(require("typescript"));
|
|
41
|
+
/** Returns true if the schema name looks like an RPC root (`Request*Schema` / `Response*Schema`). */
|
|
42
|
+
function isRpcRootSchemaName(name) {
|
|
43
|
+
return (name.endsWith('Schema') &&
|
|
44
|
+
(name.startsWith('Request') || name.startsWith('Response')));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* If the type alias is `export type Foo = Static<typeof FooSchema>`, returns `FooSchema`.
|
|
48
|
+
* Otherwise returns undefined.
|
|
49
|
+
*/
|
|
50
|
+
function extractPairedSchemaFromTypeAlias(node) {
|
|
51
|
+
if (!ts.isTypeReferenceNode(node.type))
|
|
52
|
+
return undefined;
|
|
53
|
+
const tr = node.type;
|
|
54
|
+
if (!ts.isIdentifier(tr.typeName) || tr.typeName.text !== 'Static')
|
|
55
|
+
return undefined;
|
|
56
|
+
const args = tr.typeArguments;
|
|
57
|
+
if (!args || args.length !== 1)
|
|
58
|
+
return undefined;
|
|
59
|
+
const inner = args[0];
|
|
60
|
+
if (!ts.isTypeQueryNode(inner))
|
|
61
|
+
return undefined;
|
|
62
|
+
const q = inner.exprName;
|
|
63
|
+
if (!ts.isIdentifier(q))
|
|
64
|
+
return undefined;
|
|
65
|
+
const schemaName = q.text;
|
|
66
|
+
if (!schemaName.endsWith('Schema'))
|
|
67
|
+
return undefined;
|
|
68
|
+
return schemaName;
|
|
69
|
+
}
|
|
70
|
+
/** Walks `root` and adds every identifier that appears in `schemaNames` to `out`. */
|
|
71
|
+
function collectSchemaRefsInSubtree(root, schemaNames, out) {
|
|
72
|
+
const visit = (n) => {
|
|
73
|
+
if (ts.isIdentifier(n) && schemaNames.has(n.text)) {
|
|
74
|
+
out.add(n.text);
|
|
75
|
+
}
|
|
76
|
+
ts.forEachChild(n, visit);
|
|
77
|
+
};
|
|
78
|
+
visit(root);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Removes unused schemas and types from a generated TypeBox file.
|
|
82
|
+
* Roots are every `export const Request*Schema` / `export const Response*Schema`;
|
|
83
|
+
* only those and chains of references to other `*Schema` inside initializers are kept.
|
|
84
|
+
*
|
|
85
|
+
* @param targetFilePath - Path to the TypeScript file to prune in place.
|
|
86
|
+
* @param options - Optional Prettier run after writing.
|
|
87
|
+
* @returns Counts of removed vs kept schema declarations.
|
|
88
|
+
*/
|
|
89
|
+
function pruneTypeboxFileByRequestResponseRoots(targetFilePath, options) {
|
|
90
|
+
const absolute = path.resolve(targetFilePath);
|
|
91
|
+
const raw = fs.readFileSync(absolute, 'utf8');
|
|
92
|
+
const sourceFile = ts.createSourceFile(absolute, raw, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
93
|
+
const schemaMap = new Map();
|
|
94
|
+
const schemaNames = new Set();
|
|
95
|
+
for (const stmt of sourceFile.statements) {
|
|
96
|
+
if (!ts.isVariableStatement(stmt))
|
|
97
|
+
continue;
|
|
98
|
+
const isExported = stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
99
|
+
if (!isExported)
|
|
100
|
+
continue;
|
|
101
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
102
|
+
if (!ts.isIdentifier(decl.name))
|
|
103
|
+
continue;
|
|
104
|
+
const name = decl.name.text;
|
|
105
|
+
if (!name.endsWith('Schema') || !decl.initializer)
|
|
106
|
+
continue;
|
|
107
|
+
schemaMap.set(name, { stmt });
|
|
108
|
+
schemaNames.add(name);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const deps = new Map();
|
|
112
|
+
for (const [name, { stmt }] of schemaMap) {
|
|
113
|
+
const decl = stmt.declarationList.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === name);
|
|
114
|
+
if (!decl?.initializer)
|
|
115
|
+
continue;
|
|
116
|
+
const refs = new Set();
|
|
117
|
+
collectSchemaRefsInSubtree(decl.initializer, schemaNames, refs);
|
|
118
|
+
refs.delete(name);
|
|
119
|
+
deps.set(name, refs);
|
|
120
|
+
}
|
|
121
|
+
const roots = [...schemaMap.keys()].filter(isRpcRootSchemaName);
|
|
122
|
+
if (roots.length === 0) {
|
|
123
|
+
console.warn('pruneTypeboxFile: no Request*/Response* schemas found, skipping');
|
|
124
|
+
return { removedSchemaCount: 0, keptSchemaCount: schemaMap.size };
|
|
125
|
+
}
|
|
126
|
+
const reachable = new Set();
|
|
127
|
+
const queue = [...roots];
|
|
128
|
+
for (const r of roots)
|
|
129
|
+
reachable.add(r);
|
|
130
|
+
while (queue.length > 0) {
|
|
131
|
+
const n = queue.pop();
|
|
132
|
+
for (const next of deps.get(n) ?? []) {
|
|
133
|
+
if (!reachable.has(next)) {
|
|
134
|
+
reachable.add(next);
|
|
135
|
+
queue.push(next);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const toRemove = [];
|
|
140
|
+
for (const stmt of sourceFile.statements) {
|
|
141
|
+
if (ts.isImportDeclaration(stmt))
|
|
142
|
+
continue;
|
|
143
|
+
if (ts.isVariableStatement(stmt)) {
|
|
144
|
+
const decls = stmt.declarationList.declarations;
|
|
145
|
+
const onlyExportedSchemas = decls.every((d) => ts.isIdentifier(d.name) &&
|
|
146
|
+
d.name.text.endsWith('Schema') &&
|
|
147
|
+
schemaMap.has(d.name.text));
|
|
148
|
+
if (!onlyExportedSchemas)
|
|
149
|
+
continue;
|
|
150
|
+
const allUnreachable = decls.every((d) => ts.isIdentifier(d.name) && !reachable.has(d.name.text));
|
|
151
|
+
if (allUnreachable) {
|
|
152
|
+
toRemove.push([stmt.getFullStart(), stmt.getEnd()]);
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (ts.isTypeAliasDeclaration(stmt)) {
|
|
157
|
+
const isExported = stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
158
|
+
if (!isExported)
|
|
159
|
+
continue;
|
|
160
|
+
const paired = extractPairedSchemaFromTypeAlias(stmt);
|
|
161
|
+
if (paired === undefined)
|
|
162
|
+
continue;
|
|
163
|
+
if (!schemaMap.has(paired))
|
|
164
|
+
continue;
|
|
165
|
+
if (!reachable.has(paired)) {
|
|
166
|
+
toRemove.push([stmt.getFullStart(), stmt.getEnd()]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
toRemove.sort((a, b) => b[0] - a[0]);
|
|
171
|
+
let text = sourceFile.text;
|
|
172
|
+
for (const [from, to] of toRemove) {
|
|
173
|
+
text = text.slice(0, from) + text.slice(to);
|
|
174
|
+
}
|
|
175
|
+
fs.writeFileSync(absolute, text, 'utf8');
|
|
176
|
+
const removedSchemaCount = schemaMap.size - reachable.size;
|
|
177
|
+
const runPrettier = options?.runPrettier !== false;
|
|
178
|
+
if (runPrettier) {
|
|
179
|
+
const prettierCommand = options?.prettierCommand ?? 'npx prettier --write --log-level silent';
|
|
180
|
+
const rel = path.relative(process.cwd(), absolute);
|
|
181
|
+
const arg = rel.startsWith('..') ? absolute : rel;
|
|
182
|
+
(0, child_process_1.execSync)(`${prettierCommand.trim()} "${arg}"`, {
|
|
183
|
+
stdio: 'ignore',
|
|
184
|
+
cwd: process.cwd(),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
const rel = path.relative(process.cwd(), absolute);
|
|
188
|
+
const shown = rel.startsWith('..') ? absolute : rel;
|
|
189
|
+
console.log(`✅ pruneTypebox: reachable *Schema: ${reachable.size}, removed schema declarations: ${removedSchemaCount} → ${shown}`);
|
|
190
|
+
return { removedSchemaCount, keptSchemaCount: reachable.size };
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=pruneTypeboxFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pruneTypeboxFile.js","sourceRoot":"","sources":["../src/pruneTypeboxFile.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,wFAkIC;AAtMD,iDAAyC;AACzC,uCAAyB;AACzB,2CAA6B;AAC7B,+CAAiC;AAEjC,qGAAqG;AACrG,SAAS,mBAAmB,CAAC,IAAY;IACvC,OAAO,CACL,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;QACvB,CAAC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC,CAC5D,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAS,gCAAgC,CACvC,IAA6B;IAE7B,IAAI,CAA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AS,CAAC,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC;IAC3D,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,KAAK,KAAK,CAAC;IACnD,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,eAAe,GACnB,OAAO,EAAE,eAAe,IAAI,yCAAyC,CAAC;QACxE,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;QACnD,MAAM,GAAG,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC;QAClD,IAAA,wBAAQ,EAAC,GAAG,eAAe,CAAC,IAAI,EAAE,KAAK,GAAG,GAAG,EAAE;YAC7C,KAAK,EAAE,QAAQ;YACf,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;SACnB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IACnD,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC;IACpD,OAAO,CAAC,GAAG,CACT,sCAAsC,SAAS,CAAC,IAAI,kCAAkC,kBAAkB,MAAM,KAAK,EAAE,CACtH,CAAC;IAEF,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,SAAS,CAAC,IAAI,EAAE,CAAC;AACjE,CAAC"}
|
package/dist/sorter.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { GoSliceTypeAliasMap, GoStruct, GoTypeAliasMap } from './types';
|
|
2
|
+
export type BuildStructMapOptions = {
|
|
3
|
+
sliceTypeAliases?: GoSliceTypeAliasMap;
|
|
4
|
+
typeAliases?: GoTypeAliasMap;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Fills `dependencies` on each struct and returns a name → struct map.
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildStructMap(structs: GoStruct[], opts?: BuildStructMapOptions): Map<string, GoStruct>;
|
|
10
|
+
/**
|
|
11
|
+
* Returns structs in dependency order (dependencies before dependents).
|
|
12
|
+
* @throws If a cycle is detected among structs in `structMap`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function topologicalSort(structMap: Map<string, GoStruct>): GoStruct[];
|
|
15
|
+
//# sourceMappingURL=sorter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorter.d.ts","sourceRoot":"","sources":["../src/sorter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAkFxE,MAAM,MAAM,qBAAqB,GAAG;IAClC,gBAAgB,CAAC,EAAE,mBAAmB,CAAC;IACvC,WAAW,CAAC,EAAE,cAAc,CAAC;CAC9B,CAAC;AAEF;;GAEG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,QAAQ,EAAE,EACnB,IAAI,CAAC,EAAE,qBAAqB,GAC3B,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAmBvB;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAE,CA+B5E"}
|
package/dist/sorter.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildStructMap = buildStructMap;
|
|
4
|
+
exports.topologicalSort = topologicalSort;
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
/** True if `type` is not a built-in Go type (after simple name check). */
|
|
7
|
+
function isCustomType(type) {
|
|
8
|
+
const builtInTypes = new Set([
|
|
9
|
+
'string',
|
|
10
|
+
'int',
|
|
11
|
+
'int8',
|
|
12
|
+
'int16',
|
|
13
|
+
'int32',
|
|
14
|
+
'int64',
|
|
15
|
+
'uint',
|
|
16
|
+
'uint8',
|
|
17
|
+
'uint16',
|
|
18
|
+
'uint32',
|
|
19
|
+
'uint64',
|
|
20
|
+
'float32',
|
|
21
|
+
'float64',
|
|
22
|
+
'bool',
|
|
23
|
+
'byte',
|
|
24
|
+
'rune',
|
|
25
|
+
'interface{}',
|
|
26
|
+
'any',
|
|
27
|
+
'time.Time',
|
|
28
|
+
'error',
|
|
29
|
+
]);
|
|
30
|
+
return !builtInTypes.has(type);
|
|
31
|
+
}
|
|
32
|
+
/** Follows `typeAliases` from `name` to a non-alias base name (strips leading `*` steps). */
|
|
33
|
+
function resolveTypeAliasChain(name, typeAliases) {
|
|
34
|
+
let normalized = (0, utils_1.stripGoPackagePrefix)(name);
|
|
35
|
+
if (!typeAliases?.size)
|
|
36
|
+
return normalized;
|
|
37
|
+
const visited = new Set();
|
|
38
|
+
while (typeAliases.has(normalized) && !visited.has(normalized)) {
|
|
39
|
+
visited.add(normalized);
|
|
40
|
+
let target = typeAliases.get(normalized);
|
|
41
|
+
while (target.startsWith('*'))
|
|
42
|
+
target = target.slice(1);
|
|
43
|
+
normalized = (0, utils_1.stripGoPackagePrefix)(target);
|
|
44
|
+
}
|
|
45
|
+
return normalized;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Records dependencies on other structs in the same file (including via `type List []Item`).
|
|
49
|
+
*/
|
|
50
|
+
function addDepsForFieldType(rawType, dependencies, structNames, sliceTypeAliases, typeAliases, sliceStack = new Set()) {
|
|
51
|
+
let t = resolveTypeAliasChain(rawType, typeAliases);
|
|
52
|
+
t = (0, utils_1.stripGoPackagePrefix)(t);
|
|
53
|
+
if (!isCustomType(t))
|
|
54
|
+
return;
|
|
55
|
+
if (structNames.has(t)) {
|
|
56
|
+
dependencies.add(t);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const sliceDef = sliceTypeAliases?.get(t);
|
|
60
|
+
if (sliceDef && !sliceStack.has(t)) {
|
|
61
|
+
sliceStack.add(t);
|
|
62
|
+
addDepsForFieldType(sliceDef.elementType, dependencies, structNames, sliceTypeAliases, typeAliases, sliceStack);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Fills `dependencies` on each struct and returns a name → struct map.
|
|
67
|
+
*/
|
|
68
|
+
function buildStructMap(structs, opts) {
|
|
69
|
+
const map = new Map(structs.map((s) => [s.name, s]));
|
|
70
|
+
const { sliceTypeAliases, typeAliases } = opts ?? {};
|
|
71
|
+
for (const struct of structs) {
|
|
72
|
+
const dependencies = new Set();
|
|
73
|
+
for (const field of struct.fields) {
|
|
74
|
+
addDepsForFieldType(field.type, dependencies, map, sliceTypeAliases, typeAliases);
|
|
75
|
+
}
|
|
76
|
+
struct.dependencies = Array.from(dependencies);
|
|
77
|
+
}
|
|
78
|
+
return map;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns structs in dependency order (dependencies before dependents).
|
|
82
|
+
* @throws If a cycle is detected among structs in `structMap`.
|
|
83
|
+
*/
|
|
84
|
+
function topologicalSort(structMap) {
|
|
85
|
+
const visited = new Set();
|
|
86
|
+
const visiting = new Set();
|
|
87
|
+
const result = [];
|
|
88
|
+
/** DFS visit that appends structs after their dependencies. */
|
|
89
|
+
const dfs = (structName) => {
|
|
90
|
+
if (visited.has(structName))
|
|
91
|
+
return;
|
|
92
|
+
if (visiting.has(structName)) {
|
|
93
|
+
throw new Error(`Cyclic dependency detected involving struct ${structName}`);
|
|
94
|
+
}
|
|
95
|
+
visiting.add(structName);
|
|
96
|
+
const struct = structMap.get(structName);
|
|
97
|
+
if (struct) {
|
|
98
|
+
for (const dep of struct.dependencies) {
|
|
99
|
+
if (structMap.has(dep))
|
|
100
|
+
dfs(dep);
|
|
101
|
+
}
|
|
102
|
+
visiting.delete(structName);
|
|
103
|
+
visited.add(structName);
|
|
104
|
+
result.push(struct);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
for (const structName of structMap.keys()) {
|
|
108
|
+
if (!visited.has(structName))
|
|
109
|
+
dfs(structName);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=sorter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorter.js","sourceRoot":"","sources":["../src/sorter.ts"],"names":[],"mappings":";;AA0FA,wCAsBC;AAMD,0CA+BC;AApJD,mCAA+C;AAE/C,0EAA0E;AAC1E,SAAS,YAAY,CAAC,IAAY;IAChC,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC;QAC3B,QAAQ;QACR,KAAK;QACL,MAAM;QACN,OAAO;QACP,OAAO;QACP,OAAO;QACP,MAAM;QACN,OAAO;QACP,QAAQ;QACR,QAAQ;QACR,QAAQ;QACR,SAAS;QACT,SAAS;QACT,MAAM;QACN,MAAM;QACN,MAAM;QACN,aAAa;QACb,KAAK;QACL,WAAW;QACX,OAAO;KACR,CAAC,CAAC;IAEH,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC;AAED,6FAA6F;AAC7F,SAAS,qBAAqB,CAC5B,IAAY,EACZ,WAA4B;IAE5B,IAAI,UAAU,GAAG,IAAA,4BAAoB,EAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,CAAC,WAAW,EAAE,IAAI;QAAE,OAAO,UAAU,CAAC;IAC1C,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,OAAO,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;QAC/D,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACxB,IAAI,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,UAAU,CAAE,CAAC;QAC1C,OAAO,MAA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G,CAAC,UAAU,CAAC,CAAC;YACxB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC;IAEF,KAAK,MAAM,UAAU,IAAI,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;QAC1C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC;YAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IAChD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drops trivial aliases like `export const TypesFooSchema = FooSchema` (same for `Models*`),
|
|
3
|
+
* rewrites all references to the canonical schema name, and removes matching `export type TypesFoo` / `ModelsFoo`.
|
|
4
|
+
*
|
|
5
|
+
* @param content - Full TypeBox module source.
|
|
6
|
+
* @returns Transformed source.
|
|
7
|
+
*/
|
|
8
|
+
export declare function collapseRedundantPrefixedSchemaAliases(content: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Post-processing options for a generated TypeBox file.
|
|
11
|
+
*
|
|
12
|
+
* **JSON-RPC:** by default, helper wrappers and imports are not added.
|
|
13
|
+
* Pass `jsonRpc` to match monorepo-style `getRequestSchema` / `getResponseSchema` usage.
|
|
14
|
+
*/
|
|
15
|
+
export type StandardizeJsonRpcOptions = {
|
|
16
|
+
/** Module path for `getRequestSchema`, `getResponseSchema`, `getRequestArrSchema` (as in `from '…'`). */
|
|
17
|
+
helpersImportPath: string;
|
|
18
|
+
/** Schema const names (`RequestFooSchema`) that should use `getRequestArrSchema`. */
|
|
19
|
+
requestArrSchemaNames?: string[];
|
|
20
|
+
};
|
|
21
|
+
export type StandardizeTypeboxTypesOptions = {
|
|
22
|
+
/**
|
|
23
|
+
* When set, wraps `Request*Schema` / `Response*Schema` with helpers and inserts the import.
|
|
24
|
+
*/
|
|
25
|
+
jsonRpc?: StandardizeJsonRpcOptions;
|
|
26
|
+
maxLinesForEslintDisable?: number;
|
|
27
|
+
runPrettier?: boolean;
|
|
28
|
+
prettierCommand?: string;
|
|
29
|
+
/** Add a “do not edit by hand” banner (default true). */
|
|
30
|
+
addGeneratedBanner?: boolean;
|
|
31
|
+
autoGeneratedMarkerLine?: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* TypeBox ≥0.34: replaces `Type.Ref(x)` with inline `XSchema` for same-file const references.
|
|
35
|
+
*
|
|
36
|
+
* @param content - Module source.
|
|
37
|
+
* @returns Source with refs unwrapped (repeated until stable).
|
|
38
|
+
*/
|
|
39
|
+
export declare function unwrapInlineSchemaRefs(content: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Post-processes a generated TypeBox file: schema postfix, PascalCase exports, unwrap `Type.Ref`,
|
|
42
|
+
* optional JSON-RPC wrappers, redundant alias collapse, banner, max-lines eslint, and Prettier.
|
|
43
|
+
*
|
|
44
|
+
* @param targetFilePath - File to read and write in place.
|
|
45
|
+
* @param options - Pipeline toggles and JSON-RPC / Prettier settings.
|
|
46
|
+
*/
|
|
47
|
+
export declare function standardizeTypeboxTypes(targetFilePath: string, options?: StandardizeTypeboxTypesOptions): Promise<void>;
|
|
48
|
+
//# sourceMappingURL=standardizeTypeboxTypes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standardizeTypeboxTypes.d.ts","sourceRoot":"","sources":["../src/standardizeTypeboxTypes.ts"],"names":[],"mappings":"AAWA;;;;;;GAMG;AACH,wBAAgB,sCAAsC,CACpD,OAAO,EAAE,MAAM,GACd,MAAM,CA6BR;AAsOD;;;;;GAKG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,yGAAyG;IACzG,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qFAAqF;IACrF,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;OAEG;IACH,OAAO,CAAC,EAAE,yBAAyB,CAAC;IACpC,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAClC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,yDAAyD;IACzD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,uBAAuB,CAAC,EAAE,MAAM,CAAC;CAClC,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAS9D;AAqCD;;;;;;GAMG;AACH,wBAAsB,uBAAuB,CAC3C,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE,8BAA8B,GACvC,OAAO,CAAC,IAAI,CAAC,CAoCf"}
|