sizuku 0.3.2 → 0.4.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 (204) hide show
  1. package/README.md +103 -0
  2. package/dist/config/index.d.mts +58 -0
  3. package/dist/config/index.mjs +95 -0
  4. package/dist/index.d.mts +10 -0
  5. package/dist/index.mjs +1615 -0
  6. package/package.json +31 -17
  7. package/dist/cli/main.d.ts +0 -10
  8. package/dist/cli/main.js +0 -61
  9. package/dist/cli.d.ts +0 -2
  10. package/dist/cli.js +0 -51
  11. package/dist/config/index.d.ts +0 -27
  12. package/dist/config/index.js +0 -85
  13. package/dist/config/loader.d.ts +0 -19
  14. package/dist/config/loader.js +0 -30
  15. package/dist/generator/engine.d.ts +0 -3
  16. package/dist/generator/engine.js +0 -63
  17. package/dist/generator/mermaid-er/config/index.d.ts +0 -10
  18. package/dist/generator/mermaid-er/config/index.js +0 -12
  19. package/dist/generator/mermaid-er/core/extract-relations.d.ts +0 -8
  20. package/dist/generator/mermaid-er/core/extract-relations.js +0 -17
  21. package/dist/generator/mermaid-er/core.d.ts +0 -6
  22. package/dist/generator/mermaid-er/core.js +0 -54
  23. package/dist/generator/mermaid-er/generator/er-content.d.ts +0 -20
  24. package/dist/generator/mermaid-er/generator/er-content.js +0 -23
  25. package/dist/generator/mermaid-er/generator/index.d.ts +0 -2
  26. package/dist/generator/mermaid-er/generator/index.js +0 -2
  27. package/dist/generator/mermaid-er/generator/relation-line.d.ts +0 -12
  28. package/dist/generator/mermaid-er/generator/relation-line.js +0 -13
  29. package/dist/generator/mermaid-er/generator.d.ts +0 -3
  30. package/dist/generator/mermaid-er/generator.js +0 -14
  31. package/dist/generator/mermaid-er/index.d.ts +0 -12
  32. package/dist/generator/mermaid-er/index.js +0 -33
  33. package/dist/generator/mermaid-er/relationship/build-relation-line.d.ts +0 -14
  34. package/dist/generator/mermaid-er/relationship/build-relation-line.js +0 -34
  35. package/dist/generator/mermaid-er/types.d.ts +0 -21
  36. package/dist/generator/mermaid-er/types.js +0 -1
  37. package/dist/generator/mermaid-er/validator/index.d.ts +0 -8
  38. package/dist/generator/mermaid-er/validator/index.js +0 -74
  39. package/dist/generator/mermaid-er/validator/is-relationship.d.ts +0 -7
  40. package/dist/generator/mermaid-er/validator/is-relationship.js +0 -8
  41. package/dist/generator/mermaid-er/validator/parse-relation-line.d.ts +0 -12
  42. package/dist/generator/mermaid-er/validator/parse-relation-line.js +0 -20
  43. package/dist/generator/mermaid-er/validator/parse-table-info.d.ts +0 -2
  44. package/dist/generator/mermaid-er/validator/parse-table-info.js +0 -71
  45. package/dist/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +0 -7
  46. package/dist/generator/mermaid-er/validator/remove-duplicate-relations.js +0 -9
  47. package/dist/generator/valibot/config/index.d.ts +0 -7
  48. package/dist/generator/valibot/config/index.js +0 -13
  49. package/dist/generator/valibot/core/extract-schema.d.ts +0 -5
  50. package/dist/generator/valibot/core/extract-schema.js +0 -173
  51. package/dist/generator/valibot/core.d.ts +0 -5
  52. package/dist/generator/valibot/core.js +0 -39
  53. package/dist/generator/valibot/generator/infer-input.d.ts +0 -5
  54. package/dist/generator/valibot/generator/infer-input.js +0 -8
  55. package/dist/generator/valibot/generator/relation-valibot-code.d.ts +0 -13
  56. package/dist/generator/valibot/generator/relation-valibot-code.js +0 -19
  57. package/dist/generator/valibot/generator/valibot-code.d.ts +0 -15
  58. package/dist/generator/valibot/generator/valibot-code.js +0 -16
  59. package/dist/generator/valibot/generator/valibot.d.ts +0 -14
  60. package/dist/generator/valibot/generator/valibot.js +0 -15
  61. package/dist/generator/valibot/generator.d.ts +0 -3
  62. package/dist/generator/valibot/generator.js +0 -14
  63. package/dist/generator/valibot/index.d.ts +0 -14
  64. package/dist/generator/valibot/index.js +0 -50
  65. package/dist/generator/zod/config/index.d.ts +0 -8
  66. package/dist/generator/zod/config/index.js +0 -14
  67. package/dist/generator/zod/core/extract-schema.d.ts +0 -7
  68. package/dist/generator/zod/core/extract-schema.js +0 -243
  69. package/dist/generator/zod/core.d.ts +0 -5
  70. package/dist/generator/zod/core.js +0 -39
  71. package/dist/generator/zod/generator/infer.d.ts +0 -5
  72. package/dist/generator/zod/generator/infer.js +0 -8
  73. package/dist/generator/zod/generator/relation-zod-code.d.ts +0 -13
  74. package/dist/generator/zod/generator/relation-zod-code.js +0 -19
  75. package/dist/generator/zod/generator/zod-code.d.ts +0 -17
  76. package/dist/generator/zod/generator/zod-code.js +0 -18
  77. package/dist/generator/zod/generator/zod.d.ts +0 -16
  78. package/dist/generator/zod/generator/zod.js +0 -16
  79. package/dist/generator/zod/generator.d.ts +0 -3
  80. package/dist/generator/zod/generator.js +0 -14
  81. package/dist/generator/zod/index.d.ts +0 -15
  82. package/dist/generator/zod/index.js +0 -54
  83. package/dist/index.d.ts +0 -7
  84. package/dist/index.js +0 -73
  85. package/dist/shared/config/index.d.ts +0 -13
  86. package/dist/shared/config/index.js +0 -10
  87. package/dist/shared/format/index.d.ts +0 -13
  88. package/dist/shared/format/index.js +0 -24
  89. package/dist/shared/fs/index.d.ts +0 -7
  90. package/dist/shared/fs/index.js +0 -16
  91. package/dist/shared/fsp/index.d.ts +0 -27
  92. package/dist/shared/fsp/index.js +0 -38
  93. package/dist/shared/generator/field-definitions.d.ts +0 -12
  94. package/dist/shared/generator/field-definitions.js +0 -12
  95. package/dist/shared/helper/ast-parser.d.ts +0 -3
  96. package/dist/shared/helper/ast-parser.js +0 -202
  97. package/dist/shared/helper/build-schema-extractor.d.ts +0 -25
  98. package/dist/shared/helper/build-schema-extractor.js +0 -33
  99. package/dist/shared/helper/create-extract-field-from-property.d.ts +0 -15
  100. package/dist/shared/helper/create-extract-field-from-property.js +0 -20
  101. package/dist/shared/helper/create-extract-fields-from-call-expression.d.ts +0 -14
  102. package/dist/shared/helper/create-extract-fields-from-call-expression.js +0 -14
  103. package/dist/shared/helper/create-extract-relation-field-from-property.d.ts +0 -12
  104. package/dist/shared/helper/create-extract-relation-field-from-property.js +0 -27
  105. package/dist/shared/helper/extract-schemas.d.ts +0 -133
  106. package/dist/shared/helper/extract-schemas.js +0 -445
  107. package/dist/shared/helper/file-writer.d.ts +0 -3
  108. package/dist/shared/helper/file-writer.js +0 -25
  109. package/dist/shared/helper/find-object-literal-expression.d.ts +0 -12
  110. package/dist/shared/helper/find-object-literal-expression.js +0 -31
  111. package/dist/shared/helper/find-object-literalIn-args.d.ts +0 -2
  112. package/dist/shared/helper/find-object-literalIn-args.js +0 -8
  113. package/dist/shared/helper/is-relation-function.d.ts +0 -10
  114. package/dist/shared/helper/is-relation-function.js +0 -16
  115. package/dist/shared/types.d.ts +0 -9
  116. package/dist/shared/types.js +0 -1
  117. package/dist/shared/utils/capitalize.d.ts +0 -18
  118. package/dist/shared/utils/capitalize.js +0 -20
  119. package/dist/shared/utils/compose.d.ts +0 -101
  120. package/dist/shared/utils/compose.js +0 -124
  121. package/dist/shared/utils/file.d.ts +0 -92
  122. package/dist/shared/utils/file.js +0 -177
  123. package/dist/shared/utils/functional.d.ts +0 -118
  124. package/dist/shared/utils/functional.js +0 -96
  125. package/dist/shared/utils/index.d.ts +0 -20
  126. package/dist/shared/utils/index.js +0 -48
  127. package/dist/shared/utils/string-utils.d.ts +0 -8
  128. package/dist/shared/utils/string-utils.js +0 -28
  129. package/dist/shared/utils/types.d.ts +0 -32
  130. package/dist/shared/utils/types.js +0 -2
  131. package/dist/shared/utils/validation-utils.d.ts +0 -8
  132. package/dist/shared/utils/validation-utils.js +0 -25
  133. package/dist/src/config/index.d.ts +0 -18
  134. package/dist/src/config/index.js +0 -13
  135. package/dist/src/generator/mermaid-er/core/extract-relations.d.ts +0 -8
  136. package/dist/src/generator/mermaid-er/core/extract-relations.js +0 -12
  137. package/dist/src/generator/mermaid-er/generator/er-content.d.ts +0 -9
  138. package/dist/src/generator/mermaid-er/generator/er-content.js +0 -25
  139. package/dist/src/generator/mermaid-er/generator/index.d.ts +0 -2
  140. package/dist/src/generator/mermaid-er/generator/index.js +0 -2
  141. package/dist/src/generator/mermaid-er/generator/relation-line.d.ts +0 -8
  142. package/dist/src/generator/mermaid-er/generator/relation-line.js +0 -14
  143. package/dist/src/generator/mermaid-er/index.d.ts +0 -6
  144. package/dist/src/generator/mermaid-er/index.js +0 -16
  145. package/dist/src/generator/mermaid-er/relationship/build-relation-line.d.ts +0 -15
  146. package/dist/src/generator/mermaid-er/relationship/build-relation-line.js +0 -35
  147. package/dist/src/generator/mermaid-er/types.d.ts +0 -21
  148. package/dist/src/generator/mermaid-er/types.js +0 -1
  149. package/dist/src/generator/mermaid-er/validator/index.d.ts +0 -4
  150. package/dist/src/generator/mermaid-er/validator/index.js +0 -4
  151. package/dist/src/generator/mermaid-er/validator/is-relationship.d.ts +0 -8
  152. package/dist/src/generator/mermaid-er/validator/is-relationship.js +0 -9
  153. package/dist/src/generator/mermaid-er/validator/parse-relation-line.d.ts +0 -13
  154. package/dist/src/generator/mermaid-er/validator/parse-relation-line.js +0 -21
  155. package/dist/src/generator/mermaid-er/validator/parse-table-info.d.ts +0 -8
  156. package/dist/src/generator/mermaid-er/validator/parse-table-info.js +0 -91
  157. package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +0 -7
  158. package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.js +0 -9
  159. package/dist/src/generator/valibot/generator/infer-input.d.ts +0 -5
  160. package/dist/src/generator/valibot/generator/infer-input.js +0 -8
  161. package/dist/src/generator/valibot/generator/valibot-code.d.ts +0 -14
  162. package/dist/src/generator/valibot/generator/valibot-code.js +0 -16
  163. package/dist/src/generator/valibot/generator/valibot.d.ts +0 -13
  164. package/dist/src/generator/valibot/generator/valibot.js +0 -11
  165. package/dist/src/generator/valibot/index.d.ts +0 -9
  166. package/dist/src/generator/valibot/index.js +0 -34
  167. package/dist/src/generator/zod/generator/infer.d.ts +0 -5
  168. package/dist/src/generator/zod/generator/infer.js +0 -8
  169. package/dist/src/generator/zod/generator/zod-code.d.ts +0 -16
  170. package/dist/src/generator/zod/generator/zod-code.js +0 -18
  171. package/dist/src/generator/zod/generator/zod.d.ts +0 -15
  172. package/dist/src/generator/zod/generator/zod.js +0 -12
  173. package/dist/src/generator/zod/index.d.ts +0 -10
  174. package/dist/src/generator/zod/index.js +0 -40
  175. package/dist/src/index.d.ts +0 -10
  176. package/dist/src/index.js +0 -35
  177. package/dist/src/shared/format/index.d.ts +0 -2
  178. package/dist/src/shared/format/index.js +0 -10
  179. package/dist/src/shared/fs/index.d.ts +0 -2
  180. package/dist/src/shared/fs/index.js +0 -10
  181. package/dist/src/shared/fsp/index.d.ts +0 -3
  182. package/dist/src/shared/fsp/index.js +0 -8
  183. package/dist/src/shared/generator/field-definitions.d.ts +0 -12
  184. package/dist/src/shared/generator/field-definitions.js +0 -12
  185. package/dist/src/shared/helper/build-schema-extractor.d.ts +0 -25
  186. package/dist/src/shared/helper/build-schema-extractor.js +0 -33
  187. package/dist/src/shared/helper/create-extract-field-from-property.d.ts +0 -15
  188. package/dist/src/shared/helper/create-extract-field-from-property.js +0 -20
  189. package/dist/src/shared/helper/create-extract-fields-from-call-expression.d.ts +0 -14
  190. package/dist/src/shared/helper/create-extract-fields-from-call-expression.js +0 -14
  191. package/dist/src/shared/helper/create-extract-relation-field-from-property.d.ts +0 -12
  192. package/dist/src/shared/helper/create-extract-relation-field-from-property.js +0 -27
  193. package/dist/src/shared/helper/extract-schemas.d.ts +0 -16
  194. package/dist/src/shared/helper/extract-schemas.js +0 -19
  195. package/dist/src/shared/helper/find-object-literal-expression.d.ts +0 -12
  196. package/dist/src/shared/helper/find-object-literal-expression.js +0 -31
  197. package/dist/src/shared/helper/find-object-literalIn-args.d.ts +0 -2
  198. package/dist/src/shared/helper/find-object-literalIn-args.js +0 -8
  199. package/dist/src/shared/helper/is-relation-function.d.ts +0 -10
  200. package/dist/src/shared/helper/is-relation-function.js +0 -16
  201. package/dist/src/shared/utils/index.d.ts +0 -33
  202. package/dist/src/shared/utils/index.js +0 -61
  203. package/dist/utils/index.d.ts +0 -144
  204. package/dist/utils/index.js +0 -250
@@ -1,91 +0,0 @@
1
- import { Node, Project } from 'ts-morph';
2
- /**
3
- * Get the base builder name from an expression.
4
- *
5
- * @param expr - The expression to get the builder name from.
6
- * @returns The base builder name.
7
- */
8
- function baseBuilderName(expr) {
9
- if (Node.isIdentifier(expr))
10
- return expr.getText();
11
- if (Node.isCallExpression(expr) || Node.isPropertyAccessExpression(expr))
12
- return baseBuilderName(expr.getExpression());
13
- return '';
14
- }
15
- /**
16
- * Type guard for FieldInfo.
17
- *
18
- * @param v - The value to check.
19
- * @returns True if the value is a FieldInfo.
20
- */
21
- function isFieldInfo(v) {
22
- return v !== null;
23
- }
24
- /**
25
- * Parse table information from code.
26
- *
27
- * @param code - The code to parse.
28
- * @returns Array of table information.
29
- */
30
- export function parseTableInfo(code) {
31
- const source = code.join('\n');
32
- const file = new Project({ useInMemoryFileSystem: true }).createSourceFile('temp.ts', source);
33
- return file
34
- .getVariableStatements()
35
- .filter((stmt) => stmt.isExported())
36
- .flatMap((stmt) => {
37
- const decl = stmt.getDeclarations()[0];
38
- if (!Node.isVariableDeclaration(decl))
39
- return [];
40
- const varName = decl.getName();
41
- if (varName.toLowerCase().includes('relation'))
42
- return [];
43
- const init = decl.getInitializer();
44
- if (!(init && Node.isCallExpression(init)))
45
- return [];
46
- const callee = init.getExpression().getText();
47
- if (!callee.endsWith('Table') || callee === 'relations')
48
- return [];
49
- const objLit = init.getArguments()[1];
50
- if (!(objLit && Node.isObjectLiteralExpression(objLit)))
51
- return [];
52
- const fields = objLit
53
- .getProperties()
54
- .filter(Node.isPropertyAssignment)
55
- .map((prop) => {
56
- const keyNode = prop.getNameNode();
57
- if (!Node.isIdentifier(keyNode))
58
- return null;
59
- const fieldName = keyNode.getText();
60
- const initExpr = prop.getInitializer();
61
- if (!(initExpr && Node.isCallExpression(initExpr)))
62
- return null;
63
- const fieldType = baseBuilderName(initExpr);
64
- const initText = initExpr.getText();
65
- const lineIdx = prop.getStartLineNumber() - 1;
66
- const baseDesc = code
67
- .slice(0, lineIdx)
68
- .reverse()
69
- .find((line) => {
70
- const t = line.trim();
71
- return (t.startsWith('///') &&
72
- !t.includes('@z.') &&
73
- !t.includes('@v.') &&
74
- !t.includes('@relation'));
75
- })
76
- ?.replace(/^\s*\/\/\/\s*/, '') ?? '';
77
- const prefix = initText.includes('.primaryKey()')
78
- ? '(PK) '
79
- : initText.includes('.references(')
80
- ? '(FK) '
81
- : '';
82
- return {
83
- name: fieldName,
84
- type: fieldType,
85
- description: `${prefix}${baseDesc}`.trim(),
86
- };
87
- })
88
- .filter(isFieldInfo);
89
- return [{ name: varName, fields }];
90
- });
91
- }
@@ -1,7 +0,0 @@
1
- /**
2
- * Remove duplicate relations.
3
- *
4
- * @param relations - The relations to remove duplicates from.
5
- * @returns The relations without duplicates.
6
- */
7
- export declare function removeDuplicateRelations(relations: readonly string[]): readonly string[];
@@ -1,9 +0,0 @@
1
- /**
2
- * Remove duplicate relations.
3
- *
4
- * @param relations - The relations to remove duplicates from.
5
- * @returns The relations without duplicates.
6
- */
7
- export function removeDuplicateRelations(relations) {
8
- return [...new Set(relations)];
9
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * @param name
3
- * @returns
4
- */
5
- export declare function inferInput(name: string): string;
@@ -1,8 +0,0 @@
1
- import { capitalize } from '../../../shared/utils/index.js';
2
- /**
3
- * @param name
4
- * @returns
5
- */
6
- export function inferInput(name) {
7
- return `export type ${capitalize(name)} = v.InferInput<typeof ${capitalize(name)}Schema>`;
8
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * @param schema
3
- * @param comment
4
- * @param type
5
- * @returns
6
- */
7
- export declare function valibotCode(schema: {
8
- name: string;
9
- fields: {
10
- name: string;
11
- definition: string;
12
- description?: string;
13
- }[];
14
- }, comment: boolean, type: boolean): string;
@@ -1,16 +0,0 @@
1
- import { inferInput } from './infer-input.js';
2
- import { valibot } from './valibot.js';
3
- /**
4
- * @param schema
5
- * @param comment
6
- * @param type
7
- * @returns
8
- */
9
- export function valibotCode(schema, comment, type) {
10
- const valibotSchema = valibot(schema, comment);
11
- if (type) {
12
- const valibotInfer = inferInput(schema.name);
13
- return `${valibotSchema}\n\n${valibotInfer}\n`;
14
- }
15
- return `${valibotSchema}\n`;
16
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * @param schema
3
- * @param config
4
- * @returns
5
- */
6
- export declare function valibot(schema: {
7
- name: string;
8
- fields: {
9
- name: string;
10
- definition: string;
11
- description?: string;
12
- }[];
13
- }, comment: boolean): string;
@@ -1,11 +0,0 @@
1
- import { fieldDefinitions } from '../../../shared/generator/field-definitions.js';
2
- import { schemaName } from '../../../shared/utils/index.js';
3
- /**
4
- * @param schema
5
- * @param config
6
- * @returns
7
- */
8
- export function valibot(schema, comment) {
9
- const res = fieldDefinitions(schema, comment);
10
- return `export const ${schemaName(schema.name)} = v.object({${res}})`;
11
- }
@@ -1,9 +0,0 @@
1
- import type { Result } from 'neverthrow';
2
- /**
3
- * Generate Valibot schema
4
- * @param code - The code to generate Valibot schema from
5
- * @param output - The output file path
6
- * @param comment - Whether to include comments in the generated code
7
- * @param type - Whether to include type information in the generated code
8
- */
9
- export declare function sizukuValibot(code: string[], output: `${string}.ts`, comment?: boolean, type?: boolean): Promise<Result<void, Error>>;
@@ -1,34 +0,0 @@
1
- import path from 'node:path';
2
- import { fmt } from '../../shared/format/index.js';
3
- import { mkdir, writeFile } from '../../shared/fsp/index.js';
4
- import { buildSchemaExtractor } from '../../shared/helper/build-schema-extractor.js';
5
- import { createExtractFieldFromProperty } from '../../shared/helper/create-extract-field-from-property.js';
6
- import { createExtractFieldsFromCallExpression } from '../../shared/helper/create-extract-fields-from-call-expression.js';
7
- import { createExtractRelationFieldFromProperty } from '../../shared/helper/create-extract-relation-field-from-property.js';
8
- import { extractSchemas } from '../../shared/helper/extract-schemas.js';
9
- import { findObjectLiteralExpression } from '../../shared/helper/find-object-literal-expression.js';
10
- import { findObjectLiteralInArgs } from '../../shared/helper/find-object-literalIn-args.js';
11
- import { isRelationFunctionCall } from '../../shared/helper/is-relation-function.js';
12
- import { parseFieldComments } from '../../shared/utils/index.js';
13
- import { valibotCode } from './generator/valibot-code.js';
14
- /**
15
- * Generate Valibot schema
16
- * @param code - The code to generate Valibot schema from
17
- * @param output - The output file path
18
- * @param comment - Whether to include comments in the generated code
19
- * @param type - Whether to include type information in the generated code
20
- */
21
- export async function sizukuValibot(code, output, comment, type) {
22
- const extractField = createExtractFieldFromProperty((lines) => parseFieldComments(lines, '@v.'));
23
- const extractRelationField = createExtractRelationFieldFromProperty((lines) => parseFieldComments(lines, '@v.'), 'v');
24
- const extractFieldsFromCall = createExtractFieldsFromCallExpression(extractField, extractRelationField, findObjectLiteralExpression, findObjectLiteralInArgs, isRelationFunctionCall);
25
- const extractSchema = buildSchemaExtractor(extractFieldsFromCall, extractField);
26
- const valibotGeneratedCode = [
27
- 'import * as v from "valibot"',
28
- '',
29
- ...extractSchemas(code, extractSchema).map((schema) => valibotCode(schema, comment ?? false, type ?? false)),
30
- ].join('\n');
31
- return await mkdir(path.dirname(output))
32
- .andThen(() => fmt(valibotGeneratedCode))
33
- .andThen((formatted) => writeFile(output, formatted));
34
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * @param name
3
- * @returns
4
- */
5
- export declare function infer(name: string): string;
@@ -1,8 +0,0 @@
1
- /**
2
- * @param name
3
- * @returns
4
- */
5
- export function infer(name) {
6
- const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
7
- return `export type ${capitalizedName} = z.infer<typeof ${capitalizedName}Schema>`;
8
- }
@@ -1,16 +0,0 @@
1
- /**
2
- * Generates Zod code for a given schema and config.
3
- *
4
- * @function generateZodCode
5
- * @param schema - The schema to generate code for.
6
- * @param config - The configuration for the code generation.
7
- * @returns The generated Zod code.
8
- */
9
- export declare function zodCode(schema: {
10
- name: string;
11
- fields: {
12
- name: string;
13
- definition: string;
14
- description?: string;
15
- }[];
16
- }, comment: boolean, type: boolean): string;
@@ -1,18 +0,0 @@
1
- import { infer } from './infer.js';
2
- import { zod } from './zod.js';
3
- /**
4
- * Generates Zod code for a given schema and config.
5
- *
6
- * @function generateZodCode
7
- * @param schema - The schema to generate code for.
8
- * @param config - The configuration for the code generation.
9
- * @returns The generated Zod code.
10
- */
11
- export function zodCode(schema, comment, type) {
12
- const zodSchema = zod(schema, comment);
13
- if (type) {
14
- const zInfer = infer(schema.name);
15
- return `${zodSchema}\n\n${zInfer}\n`;
16
- }
17
- return `${zodSchema}\n`;
18
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * Generates a Zod schema for a given schema and config.
3
- *
4
- * @param schema - The schema to generate code for.
5
- * @param config - The configuration for the code generation.
6
- * @returns The generated Zod schema.
7
- */
8
- export declare function zod(schema: {
9
- name: string;
10
- fields: {
11
- name: string;
12
- definition: string;
13
- description?: string;
14
- }[];
15
- }, comment: boolean): string;
@@ -1,12 +0,0 @@
1
- import { fieldDefinitions } from '../../../shared/generator/field-definitions.js';
2
- import { schemaName } from '../../../shared/utils/index.js';
3
- /**
4
- * Generates a Zod schema for a given schema and config.
5
- *
6
- * @param schema - The schema to generate code for.
7
- * @param config - The configuration for the code generation.
8
- * @returns The generated Zod schema.
9
- */
10
- export function zod(schema, comment) {
11
- return `export const ${schemaName(schema.name)} = z.object({${fieldDefinitions(schema, comment)}})`;
12
- }
@@ -1,10 +0,0 @@
1
- import type { Result } from 'neverthrow';
2
- /**
3
- * Generate Zod schema
4
- * @param code - The code to generate Zod schema from
5
- * @param output - The output file path
6
- * @param comment - Whether to include comments in the generated code
7
- * @param type - Whether to include type information in the generated code
8
- * @param zod - The Zod version to use
9
- */
10
- export declare function sizukuZod(code: string[], output: `${string}.ts`, comment?: boolean, type?: boolean, zod?: 'v4' | 'mini' | '@hono/zod-openapi'): Promise<Result<void, Error>>;
@@ -1,40 +0,0 @@
1
- import path from 'node:path';
2
- import { fmt } from '../../shared/format/index.js';
3
- import { mkdir, writeFile } from '../../shared/fsp/index.js';
4
- import { buildSchemaExtractor } from '../../shared/helper/build-schema-extractor.js';
5
- import { createExtractFieldFromProperty } from '../../shared/helper/create-extract-field-from-property.js';
6
- import { createExtractFieldsFromCallExpression } from '../../shared/helper/create-extract-fields-from-call-expression.js';
7
- import { createExtractRelationFieldFromProperty } from '../../shared/helper/create-extract-relation-field-from-property.js';
8
- import { extractSchemas } from '../../shared/helper/extract-schemas.js';
9
- import { findObjectLiteralExpression } from '../../shared/helper/find-object-literal-expression.js';
10
- import { findObjectLiteralInArgs } from '../../shared/helper/find-object-literalIn-args.js';
11
- import { isRelationFunctionCall } from '../../shared/helper/is-relation-function.js';
12
- import { parseFieldComments } from '../../shared/utils/index.js';
13
- import { zodCode } from './generator/zod-code.js';
14
- /**
15
- * Generate Zod schema
16
- * @param code - The code to generate Zod schema from
17
- * @param output - The output file path
18
- * @param comment - Whether to include comments in the generated code
19
- * @param type - Whether to include type information in the generated code
20
- * @param zod - The Zod version to use
21
- */
22
- export async function sizukuZod(code, output, comment, type, zod) {
23
- const extractField = createExtractFieldFromProperty((lines) => parseFieldComments(lines, '@z.'));
24
- const extractRelationField = createExtractRelationFieldFromProperty((lines) => parseFieldComments(lines, '@z.'), 'z');
25
- const extractFieldsFromCall = createExtractFieldsFromCallExpression(extractField, extractRelationField, findObjectLiteralExpression, findObjectLiteralInArgs, isRelationFunctionCall);
26
- const extractSchema = buildSchemaExtractor(extractFieldsFromCall, extractField);
27
- const importLine = zod === 'mini'
28
- ? `import * as z from 'zod/mini'`
29
- : zod === '@hono/zod-openapi'
30
- ? `import { z } from '@hono/zod-openapi'`
31
- : `import * as z from 'zod'`;
32
- const zodGeneratedCode = [
33
- importLine,
34
- '',
35
- ...extractSchemas(code, extractSchema).map((schema) => zodCode(schema, comment ?? false, type ?? false)),
36
- ].join('\n');
37
- return await mkdir(path.dirname(output))
38
- .andThen(() => fmt(zodGeneratedCode))
39
- .andThen((formatted) => writeFile(output, formatted));
40
- }
@@ -1,10 +0,0 @@
1
- import type { Result } from 'neverthrow';
2
- import type { Config } from './config/index.js';
3
- /**
4
- * Generate schemas from Drizzle table definitions.
5
- *
6
- * @param code - The code lines to process.
7
- * @param config - The configuration object.
8
- * @returns A Result indicating success or failure.
9
- */
10
- export declare function generateSchemas(code: string[], config: Config): Promise<Result<void, Error>>;
package/dist/src/index.js DELETED
@@ -1,35 +0,0 @@
1
- import { err, ok } from 'neverthrow';
2
- import { sizukuMermaidER } from './generator/mermaid-er/index.js';
3
- import { sizukuValibot } from './generator/valibot/index.js';
4
- import { sizukuZod } from './generator/zod/index.js';
5
- /**
6
- * Generate schemas from Drizzle table definitions.
7
- *
8
- * @param code - The code lines to process.
9
- * @param config - The configuration object.
10
- * @returns A Result indicating success or failure.
11
- */
12
- export async function generateSchemas(code, config) {
13
- if (!config.input) {
14
- return err(new Error('input is not found'));
15
- }
16
- const zodResult = config.zod?.output
17
- ? await sizukuZod(code, config.zod.output, config.zod.comment, config.zod.type, config.zod.zod)
18
- : ok(undefined);
19
- if (zodResult.isErr()) {
20
- return err(zodResult.error);
21
- }
22
- const valibotResult = config.valibot?.output
23
- ? await sizukuValibot(code, config.valibot.output, config.valibot.comment, config.valibot.type)
24
- : ok(undefined);
25
- if (valibotResult.isErr()) {
26
- return err(valibotResult.error);
27
- }
28
- const mermaidResult = config.mermaid?.output
29
- ? await sizukuMermaidER(code, config.mermaid.output)
30
- : ok(undefined);
31
- if (mermaidResult.isErr()) {
32
- return err(mermaidResult.error);
33
- }
34
- return ok(undefined);
35
- }
@@ -1,2 +0,0 @@
1
- import { ResultAsync } from 'neverthrow';
2
- export declare function fmt(code: string): ResultAsync<string, Error>;
@@ -1,10 +0,0 @@
1
- import { ResultAsync } from 'neverthrow';
2
- import { format } from 'prettier';
3
- export function fmt(code) {
4
- return ResultAsync.fromPromise(format(code, {
5
- parser: 'typescript',
6
- printWidth: 100,
7
- singleQuote: true,
8
- semi: false,
9
- }), (e) => new Error(String(e)));
10
- }
@@ -1,2 +0,0 @@
1
- import type { Result } from 'neverthrow';
2
- export declare function readFileSync(path: string): Result<string, Error>;
@@ -1,10 +0,0 @@
1
- import fs from 'node:fs';
2
- import { err, ok } from 'neverthrow';
3
- export function readFileSync(path) {
4
- try {
5
- return ok(fs.readFileSync(path, 'utf-8'));
6
- }
7
- catch (e) {
8
- return err(new Error(String(e)));
9
- }
10
- }
@@ -1,3 +0,0 @@
1
- import { ResultAsync } from 'neverthrow';
2
- export declare function mkdir(path: string): ResultAsync<string | undefined, Error>;
3
- export declare function writeFile(path: string, data: string): ResultAsync<void, Error>;
@@ -1,8 +0,0 @@
1
- import fsp from 'node:fs/promises';
2
- import { ResultAsync } from 'neverthrow';
3
- export function mkdir(path) {
4
- return ResultAsync.fromPromise(fsp.mkdir(path, { recursive: true }), (e) => new Error(String(e)));
5
- }
6
- export function writeFile(path, data) {
7
- return ResultAsync.fromPromise(fsp.writeFile(path, data), (e) => new Error(String(e)));
8
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * @param schema
3
- * @returns
4
- */
5
- export declare function fieldDefinitions(schema: {
6
- name: string;
7
- fields: {
8
- name: string;
9
- definition: string;
10
- description?: string;
11
- }[];
12
- }, comment: boolean): string;
@@ -1,12 +0,0 @@
1
- /**
2
- * @param schema
3
- * @returns
4
- */
5
- export function fieldDefinitions(schema, comment) {
6
- return schema.fields
7
- .map(({ name, definition, description }) => {
8
- const commentCode = description && comment ? `/**\n* ${description}\n*/\n` : '';
9
- return `${commentCode}${name}:${definition}`;
10
- })
11
- .join(',\n');
12
- }
@@ -1,25 +0,0 @@
1
- import type { CallExpression } from 'ts-morph';
2
- import { Node } from 'ts-morph';
3
- /**
4
- * Creates a schema extractor from customizable strategies.
5
- *
6
- * @param extractFieldsFromCall - Function to extract fields from a call expression (e.g. mysqlTable(...)).
7
- * @param extractFieldFromProperty - Function to extract a single field from an object literal property.
8
- * @returns A function that extracts a schema from a variable declaration node.
9
- */
10
- export declare function buildSchemaExtractor(extractFieldsFromCall: (call: CallExpression, sourceText: string) => {
11
- name: string;
12
- definition: string;
13
- description?: string;
14
- }[], extractFieldFromProperty: (prop: Node, sourceText: string) => {
15
- name: string;
16
- definition: string;
17
- description?: string;
18
- } | null): (declaration: Node, sourceText: string) => {
19
- name: string;
20
- fields: {
21
- name: string;
22
- definition: string;
23
- description?: string;
24
- }[];
25
- } | null;
@@ -1,33 +0,0 @@
1
- import { Node } from 'ts-morph';
2
- import { isRelationFunctionCall } from '../../shared/helper/is-relation-function.js';
3
- /**
4
- * Creates a schema extractor from customizable strategies.
5
- *
6
- * @param extractFieldsFromCall - Function to extract fields from a call expression (e.g. mysqlTable(...)).
7
- * @param extractFieldFromProperty - Function to extract a single field from an object literal property.
8
- * @returns A function that extracts a schema from a variable declaration node.
9
- */
10
- export function buildSchemaExtractor(extractFieldsFromCall, extractFieldFromProperty) {
11
- return (declaration, sourceText) => {
12
- if (!Node.isVariableDeclaration(declaration))
13
- return null;
14
- const name = declaration.getName();
15
- if (!name)
16
- return null;
17
- const initializer = declaration.getInitializer();
18
- if (Node.isCallExpression(initializer)) {
19
- if (isRelationFunctionCall(initializer))
20
- return null;
21
- const fields = extractFieldsFromCall(initializer, sourceText);
22
- return { name, fields };
23
- }
24
- if (Node.isObjectLiteralExpression(initializer)) {
25
- const fields = initializer
26
- .getProperties()
27
- .map((prop) => extractFieldFromProperty(prop, sourceText))
28
- .filter((field) => field !== null);
29
- return { name, fields };
30
- }
31
- return { name, fields: [] };
32
- };
33
- }
@@ -1,15 +0,0 @@
1
- import { Node } from 'ts-morph';
2
- /**
3
- * Creates a field extractor function using a custom parseFieldComments implementation.
4
- *
5
- * @param parseFieldComments - A function that parses comment lines into { definition, description }
6
- * @returns A property node extractor function.
7
- */
8
- export declare function createExtractFieldFromProperty(parseFieldComments: (commentLines: string[]) => {
9
- definition: string;
10
- description?: string;
11
- }): (property: Node, sourceText: string) => {
12
- name: string;
13
- definition: string;
14
- description?: string;
15
- } | null;
@@ -1,20 +0,0 @@
1
- import { Node } from 'ts-morph';
2
- import { extractFieldComments } from '../../shared/utils/index.js';
3
- /**
4
- * Creates a field extractor function using a custom parseFieldComments implementation.
5
- *
6
- * @param parseFieldComments - A function that parses comment lines into { definition, description }
7
- * @returns A property node extractor function.
8
- */
9
- export function createExtractFieldFromProperty(parseFieldComments) {
10
- return (property, sourceText) => {
11
- if (!Node.isPropertyAssignment(property))
12
- return null;
13
- const name = property.getName();
14
- if (!name)
15
- return null;
16
- const commentLines = extractFieldComments(sourceText, property.getStart());
17
- const { definition, description } = parseFieldComments(commentLines);
18
- return { name, definition, description };
19
- };
20
- }
@@ -1,14 +0,0 @@
1
- import type { CallExpression, Node, ObjectLiteralExpression } from 'ts-morph';
2
- export declare function createExtractFieldsFromCallExpression(extractFieldFromProperty: (property: Node, sourceText: string) => {
3
- name: string;
4
- definition: string;
5
- description?: string;
6
- } | null, extractRelationFieldFromProperty: (property: Node, sourceText: string) => {
7
- name: string;
8
- definition: string;
9
- description?: string;
10
- } | null, findObjectLiteralExpression: (expr: Node) => ObjectLiteralExpression | null, findObjectLiteralInArgs: (call: CallExpression, finder: (expr: Node) => ObjectLiteralExpression | null) => ObjectLiteralExpression | null, isRelationFunctionCall: (call: CallExpression) => boolean): (callExpr: CallExpression, sourceText: string) => {
11
- name: string;
12
- definition: string;
13
- description?: string;
14
- }[];
@@ -1,14 +0,0 @@
1
- export function createExtractFieldsFromCallExpression(extractFieldFromProperty, extractRelationFieldFromProperty, findObjectLiteralExpression, findObjectLiteralInArgs, isRelationFunctionCall) {
2
- return (callExpr, sourceText) => {
3
- const objectLiteral = findObjectLiteralInArgs(callExpr, findObjectLiteralExpression);
4
- if (!objectLiteral)
5
- return [];
6
- const isRelation = isRelationFunctionCall(callExpr);
7
- return objectLiteral
8
- .getProperties()
9
- .map((prop) => isRelation
10
- ? extractRelationFieldFromProperty(prop, sourceText)
11
- : extractFieldFromProperty(prop, sourceText))
12
- .filter((field) => field !== null);
13
- };
14
- }
@@ -1,12 +0,0 @@
1
- import { Node } from 'ts-morph';
2
- export declare const createExtractRelationFieldFromProperty: (parseFieldComments: (lines: string[]) => {
3
- definition: string;
4
- description?: string;
5
- }, prefix: "v" | "z") => (property: Node, sourceText: string) => {
6
- name: string;
7
- fields: {
8
- name: string;
9
- definition: string;
10
- description?: string;
11
- }[];
12
- }["fields"][0] | null;