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.
Files changed (42) hide show
  1. package/README.md +56 -0
  2. package/dist/generator.d.ts +14 -0
  3. package/dist/generator.d.ts.map +1 -0
  4. package/dist/generator.js +165 -0
  5. package/dist/generator.js.map +1 -0
  6. package/dist/go2typebox.d.ts +54 -0
  7. package/dist/go2typebox.d.ts.map +1 -0
  8. package/dist/go2typebox.js +219 -0
  9. package/dist/go2typebox.js.map +1 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +15 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/parser.d.ts +10 -0
  15. package/dist/parser.d.ts.map +1 -0
  16. package/dist/parser.js +87 -0
  17. package/dist/parser.js.map +1 -0
  18. package/dist/pruneTypeboxFile.d.ts +18 -0
  19. package/dist/pruneTypeboxFile.d.ts.map +1 -0
  20. package/dist/pruneTypeboxFile.js +192 -0
  21. package/dist/pruneTypeboxFile.js.map +1 -0
  22. package/dist/sorter.d.ts +15 -0
  23. package/dist/sorter.d.ts.map +1 -0
  24. package/dist/sorter.js +113 -0
  25. package/dist/sorter.js.map +1 -0
  26. package/dist/standardizeTypeboxTypes.d.ts +48 -0
  27. package/dist/standardizeTypeboxTypes.d.ts.map +1 -0
  28. package/dist/standardizeTypeboxTypes.js +351 -0
  29. package/dist/standardizeTypeboxTypes.js.map +1 -0
  30. package/dist/types.d.ts +67 -0
  31. package/dist/types.d.ts.map +1 -0
  32. package/dist/types.js +3 -0
  33. package/dist/types.js.map +1 -0
  34. package/dist/utils.d.ts +19 -0
  35. package/dist/utils.d.ts.map +1 -0
  36. package/dist/utils.js +64 -0
  37. package/dist/utils.js.map +1 -0
  38. package/dist/writer.d.ts +16 -0
  39. package/dist/writer.d.ts.map +1 -0
  40. package/dist/writer.js +105 -0
  41. package/dist/writer.js.map +1 -0
  42. package/package.json +53 -0
package/dist/writer.js ADDED
@@ -0,0 +1,105 @@
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.writeOutputFile = writeOutputFile;
37
+ const fs = __importStar(require("fs"));
38
+ const path = __importStar(require("path"));
39
+ const generator_1 = require("./generator");
40
+ const utils_1 = require("./utils");
41
+ /** Creates the parent directory of `outputFile` if it does not exist. */
42
+ function ensureOutputDir(outputFile) {
43
+ const outputDir = path.dirname(outputFile);
44
+ if (!fs.existsSync(outputDir)) {
45
+ fs.mkdirSync(outputDir, { recursive: true });
46
+ }
47
+ }
48
+ /** True if `existing` contains an import from `@sinclair/typebox`. */
49
+ function hasTypeboxImports(existing) {
50
+ return /from\s+['"]@sinclair\/typebox['"]\s*;?/.test(existing);
51
+ }
52
+ /** True if `existing` already exports a schema or type for `structName` (Pascal export name). */
53
+ function alreadyHasStruct(existing, structName) {
54
+ const exportName = (0, utils_1.toPascalExportName)(structName);
55
+ const esc = (0, utils_1.escapeRegex)(exportName);
56
+ const re = new RegExp(`\\bexport\\s+const\\s+${esc}Schema\\b|\\bexport\\s+type\\s+${esc}\\b`, 'm');
57
+ return re.test(existing);
58
+ }
59
+ /** Collects identifiers matching `export const *Schema` from `existing`. */
60
+ function collectKnownSchemaIdents(existing) {
61
+ const out = new Set();
62
+ for (const m of existing.matchAll(/\bexport\s+const\s+([A-Za-z_]\w*Schema)\b/g)) {
63
+ out.add(m[1]);
64
+ }
65
+ return out;
66
+ }
67
+ /**
68
+ * Writes or appends generated TypeBox for `sortedStructs` to `outputFile` according to `mode`.
69
+ * In append mode, skips structs already present and merges imports when needed.
70
+ */
71
+ function writeOutputFile(params) {
72
+ const { outputFile, sortedStructs, structMap, typeAliases, sliceTypeAliases, mode, emptyStructStrategy, warn, } = params;
73
+ ensureOutputDir(outputFile);
74
+ if (mode === 'overwrite' || !fs.existsSync(outputFile)) {
75
+ const generatedBody = (0, generator_1.buildTypeBoxSource)(sortedStructs, structMap, {
76
+ emptyStructStrategy,
77
+ typeAliases,
78
+ sliceTypeAliases,
79
+ warn,
80
+ });
81
+ const body = `${(0, generator_1.getTypeboxImports)()}\n\n${generatedBody}`;
82
+ fs.writeFileSync(outputFile, body, 'utf-8');
83
+ return;
84
+ }
85
+ const existing = fs.readFileSync(outputFile, 'utf-8');
86
+ const knownSchemaIdents = collectKnownSchemaIdents(existing);
87
+ const parts = [];
88
+ if (!hasTypeboxImports(existing)) {
89
+ parts.push((0, generator_1.getTypeboxImports)(), '');
90
+ }
91
+ parts.push(existing.trimEnd(), '');
92
+ const missingStructs = sortedStructs.filter((s) => !alreadyHasStruct(existing, s.name));
93
+ if (missingStructs.length > 0) {
94
+ const appended = (0, generator_1.buildTypeBoxSource)(missingStructs, structMap, {
95
+ knownSchemaIdents,
96
+ emptyStructStrategy,
97
+ typeAliases,
98
+ sliceTypeAliases,
99
+ warn,
100
+ }).trimEnd();
101
+ parts.push(appended, '');
102
+ }
103
+ fs.writeFileSync(outputFile, parts.join('\n'), 'utf-8');
104
+ }
105
+ //# sourceMappingURL=writer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writer.js","sourceRoot":"","sources":["../src/writer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,0CA6DC;AAhHD,uCAAyB;AACzB,2CAA6B;AAC7B,2CAAoE;AAQpE,mCAA0D;AAE1D,yEAAyE;AACzE,SAAS,eAAe,CAAC,UAAkB;IACzC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC9B,EAAE,CAAC,SAAS,CAAC,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,sEAAsE;AACtE,SAAS,iBAAiB,CAAC,QAAgB;IACzC,OAAO,wCAAwC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACjE,CAAC;AAED,iGAAiG;AACjG,SAAS,gBAAgB,CAAC,QAAgB,EAAE,UAAkB;IAC5D,MAAM,UAAU,GAAG,IAAA,0BAAkB,EAAC,UAAU,CAAC,CAAC;IAClD,MAAM,GAAG,GAAG,IAAA,mBAAW,EAAC,UAAU,CAAC,CAAC;IACpC,MAAM,EAAE,GAAG,IAAI,MAAM,CACnB,yBAAyB,GAAG,kCAAkC,GAAG,KAAK,EACtE,GAAG,CACJ,CAAC;IACF,OAAO,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC3B,CAAC;AAED,4EAA4E;AAC5E,SAAS,wBAAwB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,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC;IAEnC,MAAM,cAAc,GAAG,aAAa,CAAC,MAAM,CACzC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,gBAAgB,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,CAC3C,CAAC;IAEF,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC9B,MAAM,QAAQ,GAAG,IAAA,8BAAkB,EAAC,cAAc,EAAE,SAAS,EAAE;YAC7D,iBAAiB;YACjB,mBAAmB;YACnB,WAAW;YACX,gBAAgB;YAChB,IAAI;SACL,CAAC,CAAC,OAAO,EAAE,CAAC;QACb,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAC3B,CAAC;IAED,EAAE,CAAC,aAAa,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;AAC1D,CAAC"}
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "go2typebox",
3
+ "version": "0.1.0",
4
+ "description": "Convert Go structs to Sinclair TypeBox schemas; optional post-process and prune helpers",
5
+ "license": "MIT",
6
+ "author": "rahimov.eskender",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/Eskender1985/go2typebox.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/Eskender1985/go2typebox/issues"
13
+ },
14
+ "homepage": "https://github.com/Eskender1985/go2typebox#readme",
15
+ "keywords": [
16
+ "go",
17
+ "golang",
18
+ "typebox",
19
+ "sinclair",
20
+ "schema",
21
+ "codegen"
22
+ ],
23
+ "type": "commonjs",
24
+ "main": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./dist/index.d.ts",
29
+ "require": "./dist/index.js",
30
+ "import": "./dist/index.js"
31
+ }
32
+ },
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "scripts": {
37
+ "build": "tsc -p tsconfig.json",
38
+ "prepublishOnly": "npm run build"
39
+ },
40
+ "engines": {
41
+ "node": ">=18"
42
+ },
43
+ "peerDependencies": {
44
+ "@sinclair/typebox": ">=0.34.0"
45
+ },
46
+ "dependencies": {
47
+ "typescript": "^5.4.0"
48
+ },
49
+ "devDependencies": {
50
+ "@sinclair/typebox": "^0.34.41",
51
+ "@types/node": "^20.12.7"
52
+ }
53
+ }