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.
- package/README.md +103 -0
- package/dist/config/index.d.mts +58 -0
- package/dist/config/index.mjs +95 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +1615 -0
- package/package.json +31 -17
- package/dist/cli/main.d.ts +0 -10
- package/dist/cli/main.js +0 -61
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -51
- package/dist/config/index.d.ts +0 -27
- package/dist/config/index.js +0 -85
- package/dist/config/loader.d.ts +0 -19
- package/dist/config/loader.js +0 -30
- package/dist/generator/engine.d.ts +0 -3
- package/dist/generator/engine.js +0 -63
- package/dist/generator/mermaid-er/config/index.d.ts +0 -10
- package/dist/generator/mermaid-er/config/index.js +0 -12
- package/dist/generator/mermaid-er/core/extract-relations.d.ts +0 -8
- package/dist/generator/mermaid-er/core/extract-relations.js +0 -17
- package/dist/generator/mermaid-er/core.d.ts +0 -6
- package/dist/generator/mermaid-er/core.js +0 -54
- package/dist/generator/mermaid-er/generator/er-content.d.ts +0 -20
- package/dist/generator/mermaid-er/generator/er-content.js +0 -23
- package/dist/generator/mermaid-er/generator/index.d.ts +0 -2
- package/dist/generator/mermaid-er/generator/index.js +0 -2
- package/dist/generator/mermaid-er/generator/relation-line.d.ts +0 -12
- package/dist/generator/mermaid-er/generator/relation-line.js +0 -13
- package/dist/generator/mermaid-er/generator.d.ts +0 -3
- package/dist/generator/mermaid-er/generator.js +0 -14
- package/dist/generator/mermaid-er/index.d.ts +0 -12
- package/dist/generator/mermaid-er/index.js +0 -33
- package/dist/generator/mermaid-er/relationship/build-relation-line.d.ts +0 -14
- package/dist/generator/mermaid-er/relationship/build-relation-line.js +0 -34
- package/dist/generator/mermaid-er/types.d.ts +0 -21
- package/dist/generator/mermaid-er/types.js +0 -1
- package/dist/generator/mermaid-er/validator/index.d.ts +0 -8
- package/dist/generator/mermaid-er/validator/index.js +0 -74
- package/dist/generator/mermaid-er/validator/is-relationship.d.ts +0 -7
- package/dist/generator/mermaid-er/validator/is-relationship.js +0 -8
- package/dist/generator/mermaid-er/validator/parse-relation-line.d.ts +0 -12
- package/dist/generator/mermaid-er/validator/parse-relation-line.js +0 -20
- package/dist/generator/mermaid-er/validator/parse-table-info.d.ts +0 -2
- package/dist/generator/mermaid-er/validator/parse-table-info.js +0 -71
- package/dist/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +0 -7
- package/dist/generator/mermaid-er/validator/remove-duplicate-relations.js +0 -9
- package/dist/generator/valibot/config/index.d.ts +0 -7
- package/dist/generator/valibot/config/index.js +0 -13
- package/dist/generator/valibot/core/extract-schema.d.ts +0 -5
- package/dist/generator/valibot/core/extract-schema.js +0 -173
- package/dist/generator/valibot/core.d.ts +0 -5
- package/dist/generator/valibot/core.js +0 -39
- package/dist/generator/valibot/generator/infer-input.d.ts +0 -5
- package/dist/generator/valibot/generator/infer-input.js +0 -8
- package/dist/generator/valibot/generator/relation-valibot-code.d.ts +0 -13
- package/dist/generator/valibot/generator/relation-valibot-code.js +0 -19
- package/dist/generator/valibot/generator/valibot-code.d.ts +0 -15
- package/dist/generator/valibot/generator/valibot-code.js +0 -16
- package/dist/generator/valibot/generator/valibot.d.ts +0 -14
- package/dist/generator/valibot/generator/valibot.js +0 -15
- package/dist/generator/valibot/generator.d.ts +0 -3
- package/dist/generator/valibot/generator.js +0 -14
- package/dist/generator/valibot/index.d.ts +0 -14
- package/dist/generator/valibot/index.js +0 -50
- package/dist/generator/zod/config/index.d.ts +0 -8
- package/dist/generator/zod/config/index.js +0 -14
- package/dist/generator/zod/core/extract-schema.d.ts +0 -7
- package/dist/generator/zod/core/extract-schema.js +0 -243
- package/dist/generator/zod/core.d.ts +0 -5
- package/dist/generator/zod/core.js +0 -39
- package/dist/generator/zod/generator/infer.d.ts +0 -5
- package/dist/generator/zod/generator/infer.js +0 -8
- package/dist/generator/zod/generator/relation-zod-code.d.ts +0 -13
- package/dist/generator/zod/generator/relation-zod-code.js +0 -19
- package/dist/generator/zod/generator/zod-code.d.ts +0 -17
- package/dist/generator/zod/generator/zod-code.js +0 -18
- package/dist/generator/zod/generator/zod.d.ts +0 -16
- package/dist/generator/zod/generator/zod.js +0 -16
- package/dist/generator/zod/generator.d.ts +0 -3
- package/dist/generator/zod/generator.js +0 -14
- package/dist/generator/zod/index.d.ts +0 -15
- package/dist/generator/zod/index.js +0 -54
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -73
- package/dist/shared/config/index.d.ts +0 -13
- package/dist/shared/config/index.js +0 -10
- package/dist/shared/format/index.d.ts +0 -13
- package/dist/shared/format/index.js +0 -24
- package/dist/shared/fs/index.d.ts +0 -7
- package/dist/shared/fs/index.js +0 -16
- package/dist/shared/fsp/index.d.ts +0 -27
- package/dist/shared/fsp/index.js +0 -38
- package/dist/shared/generator/field-definitions.d.ts +0 -12
- package/dist/shared/generator/field-definitions.js +0 -12
- package/dist/shared/helper/ast-parser.d.ts +0 -3
- package/dist/shared/helper/ast-parser.js +0 -202
- package/dist/shared/helper/build-schema-extractor.d.ts +0 -25
- package/dist/shared/helper/build-schema-extractor.js +0 -33
- package/dist/shared/helper/create-extract-field-from-property.d.ts +0 -15
- package/dist/shared/helper/create-extract-field-from-property.js +0 -20
- package/dist/shared/helper/create-extract-fields-from-call-expression.d.ts +0 -14
- package/dist/shared/helper/create-extract-fields-from-call-expression.js +0 -14
- package/dist/shared/helper/create-extract-relation-field-from-property.d.ts +0 -12
- package/dist/shared/helper/create-extract-relation-field-from-property.js +0 -27
- package/dist/shared/helper/extract-schemas.d.ts +0 -133
- package/dist/shared/helper/extract-schemas.js +0 -445
- package/dist/shared/helper/file-writer.d.ts +0 -3
- package/dist/shared/helper/file-writer.js +0 -25
- package/dist/shared/helper/find-object-literal-expression.d.ts +0 -12
- package/dist/shared/helper/find-object-literal-expression.js +0 -31
- package/dist/shared/helper/find-object-literalIn-args.d.ts +0 -2
- package/dist/shared/helper/find-object-literalIn-args.js +0 -8
- package/dist/shared/helper/is-relation-function.d.ts +0 -10
- package/dist/shared/helper/is-relation-function.js +0 -16
- package/dist/shared/types.d.ts +0 -9
- package/dist/shared/types.js +0 -1
- package/dist/shared/utils/capitalize.d.ts +0 -18
- package/dist/shared/utils/capitalize.js +0 -20
- package/dist/shared/utils/compose.d.ts +0 -101
- package/dist/shared/utils/compose.js +0 -124
- package/dist/shared/utils/file.d.ts +0 -92
- package/dist/shared/utils/file.js +0 -177
- package/dist/shared/utils/functional.d.ts +0 -118
- package/dist/shared/utils/functional.js +0 -96
- package/dist/shared/utils/index.d.ts +0 -20
- package/dist/shared/utils/index.js +0 -48
- package/dist/shared/utils/string-utils.d.ts +0 -8
- package/dist/shared/utils/string-utils.js +0 -28
- package/dist/shared/utils/types.d.ts +0 -32
- package/dist/shared/utils/types.js +0 -2
- package/dist/shared/utils/validation-utils.d.ts +0 -8
- package/dist/shared/utils/validation-utils.js +0 -25
- package/dist/src/config/index.d.ts +0 -18
- package/dist/src/config/index.js +0 -13
- package/dist/src/generator/mermaid-er/core/extract-relations.d.ts +0 -8
- package/dist/src/generator/mermaid-er/core/extract-relations.js +0 -12
- package/dist/src/generator/mermaid-er/generator/er-content.d.ts +0 -9
- package/dist/src/generator/mermaid-er/generator/er-content.js +0 -25
- package/dist/src/generator/mermaid-er/generator/index.d.ts +0 -2
- package/dist/src/generator/mermaid-er/generator/index.js +0 -2
- package/dist/src/generator/mermaid-er/generator/relation-line.d.ts +0 -8
- package/dist/src/generator/mermaid-er/generator/relation-line.js +0 -14
- package/dist/src/generator/mermaid-er/index.d.ts +0 -6
- package/dist/src/generator/mermaid-er/index.js +0 -16
- package/dist/src/generator/mermaid-er/relationship/build-relation-line.d.ts +0 -15
- package/dist/src/generator/mermaid-er/relationship/build-relation-line.js +0 -35
- package/dist/src/generator/mermaid-er/types.d.ts +0 -21
- package/dist/src/generator/mermaid-er/types.js +0 -1
- package/dist/src/generator/mermaid-er/validator/index.d.ts +0 -4
- package/dist/src/generator/mermaid-er/validator/index.js +0 -4
- package/dist/src/generator/mermaid-er/validator/is-relationship.d.ts +0 -8
- package/dist/src/generator/mermaid-er/validator/is-relationship.js +0 -9
- package/dist/src/generator/mermaid-er/validator/parse-relation-line.d.ts +0 -13
- package/dist/src/generator/mermaid-er/validator/parse-relation-line.js +0 -21
- package/dist/src/generator/mermaid-er/validator/parse-table-info.d.ts +0 -8
- package/dist/src/generator/mermaid-er/validator/parse-table-info.js +0 -91
- package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.d.ts +0 -7
- package/dist/src/generator/mermaid-er/validator/remove-duplicate-relations.js +0 -9
- package/dist/src/generator/valibot/generator/infer-input.d.ts +0 -5
- package/dist/src/generator/valibot/generator/infer-input.js +0 -8
- package/dist/src/generator/valibot/generator/valibot-code.d.ts +0 -14
- package/dist/src/generator/valibot/generator/valibot-code.js +0 -16
- package/dist/src/generator/valibot/generator/valibot.d.ts +0 -13
- package/dist/src/generator/valibot/generator/valibot.js +0 -11
- package/dist/src/generator/valibot/index.d.ts +0 -9
- package/dist/src/generator/valibot/index.js +0 -34
- package/dist/src/generator/zod/generator/infer.d.ts +0 -5
- package/dist/src/generator/zod/generator/infer.js +0 -8
- package/dist/src/generator/zod/generator/zod-code.d.ts +0 -16
- package/dist/src/generator/zod/generator/zod-code.js +0 -18
- package/dist/src/generator/zod/generator/zod.d.ts +0 -15
- package/dist/src/generator/zod/generator/zod.js +0 -12
- package/dist/src/generator/zod/index.d.ts +0 -10
- package/dist/src/generator/zod/index.js +0 -40
- package/dist/src/index.d.ts +0 -10
- package/dist/src/index.js +0 -35
- package/dist/src/shared/format/index.d.ts +0 -2
- package/dist/src/shared/format/index.js +0 -10
- package/dist/src/shared/fs/index.d.ts +0 -2
- package/dist/src/shared/fs/index.js +0 -10
- package/dist/src/shared/fsp/index.d.ts +0 -3
- package/dist/src/shared/fsp/index.js +0 -8
- package/dist/src/shared/generator/field-definitions.d.ts +0 -12
- package/dist/src/shared/generator/field-definitions.js +0 -12
- package/dist/src/shared/helper/build-schema-extractor.d.ts +0 -25
- package/dist/src/shared/helper/build-schema-extractor.js +0 -33
- package/dist/src/shared/helper/create-extract-field-from-property.d.ts +0 -15
- package/dist/src/shared/helper/create-extract-field-from-property.js +0 -20
- package/dist/src/shared/helper/create-extract-fields-from-call-expression.d.ts +0 -14
- package/dist/src/shared/helper/create-extract-fields-from-call-expression.js +0 -14
- package/dist/src/shared/helper/create-extract-relation-field-from-property.d.ts +0 -12
- package/dist/src/shared/helper/create-extract-relation-field-from-property.js +0 -27
- package/dist/src/shared/helper/extract-schemas.d.ts +0 -16
- package/dist/src/shared/helper/extract-schemas.js +0 -19
- package/dist/src/shared/helper/find-object-literal-expression.d.ts +0 -12
- package/dist/src/shared/helper/find-object-literal-expression.js +0 -31
- package/dist/src/shared/helper/find-object-literalIn-args.d.ts +0 -2
- package/dist/src/shared/helper/find-object-literalIn-args.js +0 -8
- package/dist/src/shared/helper/is-relation-function.d.ts +0 -10
- package/dist/src/shared/helper/is-relation-function.js +0 -16
- package/dist/src/shared/utils/index.d.ts +0 -33
- package/dist/src/shared/utils/index.js +0 -61
- package/dist/utils/index.d.ts +0 -144
- package/dist/utils/index.js +0 -250
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { capitalize, inferInput } from '../../../utils/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Generates Valibot relation schema code from a relation schema AST extraction.
|
|
4
|
-
*/
|
|
5
|
-
export function relationValibotCode(schema, withType) {
|
|
6
|
-
const base = schema.baseName;
|
|
7
|
-
const relName = `${schema.name}Schema`;
|
|
8
|
-
const baseSchema = `${capitalize(base)}Schema`;
|
|
9
|
-
const fields = schema.fields.map((f) => `${f.name}:${f.definition}`).join(',');
|
|
10
|
-
const objectType = schema.objectType === 'strict'
|
|
11
|
-
? 'strictObject'
|
|
12
|
-
: schema.objectType === 'loose'
|
|
13
|
-
? 'looseObject'
|
|
14
|
-
: 'object';
|
|
15
|
-
const obj = `\nexport const ${capitalize(relName)} = v.${objectType}({...${baseSchema}.entries,${fields}})`;
|
|
16
|
-
if (withType)
|
|
17
|
-
return `${obj}\n\n${inferInput(schema.name)}\n`;
|
|
18
|
-
return `${obj}`;
|
|
19
|
-
}
|
|
@@ -1,15 +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
|
-
objectType?: 'strict' | 'loose';
|
|
15
|
-
}, comment: boolean, type: boolean): string;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { inferInput } from '../../../utils/index.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,14 +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
|
-
objectType?: 'strict' | 'loose';
|
|
14
|
-
}, comment: boolean): string;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { capitalize, fieldDefinitions } from '../../../utils/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* @param schema
|
|
4
|
-
* @param config
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
export function valibot(schema, comment) {
|
|
8
|
-
const res = fieldDefinitions(schema, comment);
|
|
9
|
-
const objectType = schema.objectType === 'strict'
|
|
10
|
-
? 'strictObject'
|
|
11
|
-
: schema.objectType === 'loose'
|
|
12
|
-
? 'looseObject'
|
|
13
|
-
: 'object';
|
|
14
|
-
return `export const ${capitalize(schema.name)}Schema = v.${objectType}({${res}})`;
|
|
15
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Result, err, ok } from 'neverthrow';
|
|
2
|
-
import { generateValibotFile } from './core.js';
|
|
3
|
-
export const generateValibotSchemas = (schema) => {
|
|
4
|
-
try {
|
|
5
|
-
if (schema.tables.length === 0) {
|
|
6
|
-
return err(new Error('No tables found in schema'));
|
|
7
|
-
}
|
|
8
|
-
const valibotContent = generateValibotFile(schema.tables);
|
|
9
|
-
return ok(valibotContent);
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
return err(new Error(`Failed to generate Valibot schemas: ${error instanceof Error ? error.message : String(error)}`));
|
|
13
|
-
}
|
|
14
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate Valibot schema
|
|
3
|
-
* @param code - The code to generate Valibot schema from
|
|
4
|
-
* @param output - The output file path
|
|
5
|
-
* @param comment - Whether to include comments in the generated code
|
|
6
|
-
* @param type - Whether to include type information in the generated code
|
|
7
|
-
*/
|
|
8
|
-
export declare function sizukuValibot(code: string[], output: `${string}.ts`, comment?: boolean, type?: boolean, relation?: boolean): Promise<{
|
|
9
|
-
ok: true;
|
|
10
|
-
value: undefined;
|
|
11
|
-
} | {
|
|
12
|
-
ok: false;
|
|
13
|
-
error: string;
|
|
14
|
-
}>;
|
|
@@ -1,50 +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 { extractRelationSchemas, extractSchemas } from '../../shared/helper/extract-schemas.js';
|
|
5
|
-
import { relationValibotCode } from './generator/relation-valibot-code.js';
|
|
6
|
-
import { valibotCode } from './generator/valibot-code.js';
|
|
7
|
-
/**
|
|
8
|
-
* Generate Valibot schema
|
|
9
|
-
* @param code - The code to generate Valibot schema from
|
|
10
|
-
* @param output - The output file path
|
|
11
|
-
* @param comment - Whether to include comments in the generated code
|
|
12
|
-
* @param type - Whether to include type information in the generated code
|
|
13
|
-
*/
|
|
14
|
-
export async function sizukuValibot(code, output, comment, type, relation) {
|
|
15
|
-
const baseSchemas = extractSchemas(code, 'valibot');
|
|
16
|
-
const relationSchemas = extractRelationSchemas(code, 'valibot');
|
|
17
|
-
const valibotGeneratedCode = [
|
|
18
|
-
"import * as v from 'valibot'",
|
|
19
|
-
'',
|
|
20
|
-
...baseSchemas.map((schema) => valibotCode(schema, comment ?? false, type ?? false)),
|
|
21
|
-
...(relation
|
|
22
|
-
? relationSchemas.map((schema) => relationValibotCode(schema, type ?? false))
|
|
23
|
-
: []),
|
|
24
|
-
].join('\n');
|
|
25
|
-
const mkdirResult = await mkdir(path.dirname(output));
|
|
26
|
-
if (!mkdirResult.ok) {
|
|
27
|
-
return {
|
|
28
|
-
ok: false,
|
|
29
|
-
error: mkdirResult.error,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
const fmtResult = await fmt(valibotGeneratedCode);
|
|
33
|
-
if (!fmtResult.ok) {
|
|
34
|
-
return {
|
|
35
|
-
ok: false,
|
|
36
|
-
error: fmtResult.error,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const writeFileResult = await writeFile(output, fmtResult.value);
|
|
40
|
-
if (!writeFileResult.ok) {
|
|
41
|
-
return {
|
|
42
|
-
ok: false,
|
|
43
|
-
error: writeFileResult.error,
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
ok: true,
|
|
48
|
-
value: undefined,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Config } from '../../../shared/config/index.js'
|
|
2
|
-
/**
|
|
3
|
-
* Loads the configuration from the `sizuku.json` file or returns the default configuration.
|
|
4
|
-
*
|
|
5
|
-
* @function getConfig
|
|
6
|
-
* @returns The configuration object.
|
|
7
|
-
*/
|
|
8
|
-
export declare function getConfig(): Config
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import { DEFAULT_CONFIG } from '../../../shared/config/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Loads the configuration from the `sizuku.json` file or returns the default configuration.
|
|
5
|
-
*
|
|
6
|
-
* @function getConfig
|
|
7
|
-
* @returns The configuration object.
|
|
8
|
-
*/
|
|
9
|
-
export function getConfig() {
|
|
10
|
-
const config = fs.existsSync('sizuku-zod.json')
|
|
11
|
-
? { ...DEFAULT_CONFIG, ...JSON.parse(fs.readFileSync('sizuku-zod.json', 'utf-8')) }
|
|
12
|
-
: DEFAULT_CONFIG;
|
|
13
|
-
return config;
|
|
14
|
-
}
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import { Node, Project } from 'ts-morph';
|
|
2
|
-
/**
|
|
3
|
-
* Check if comment contains metadata
|
|
4
|
-
*/
|
|
5
|
-
const isMetadataComment = (text) => {
|
|
6
|
-
return text.includes('@z.') || text.includes('@v.') || text.includes('@relation.');
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Extract field comments that appear before a specific line position
|
|
10
|
-
*/
|
|
11
|
-
const extractFieldComments = (sourceText, fieldStartPos) => {
|
|
12
|
-
const beforeField = sourceText.substring(0, fieldStartPos);
|
|
13
|
-
const lines = beforeField.split('\n');
|
|
14
|
-
const reverseIndex = lines
|
|
15
|
-
.map((line, index) => ({ line: line.trim(), index }))
|
|
16
|
-
.reverse()
|
|
17
|
-
.reduce((acc, { line }) => {
|
|
18
|
-
if (acc.shouldStop)
|
|
19
|
-
return acc;
|
|
20
|
-
if (line.startsWith('///')) {
|
|
21
|
-
return {
|
|
22
|
-
commentLines: [line, ...acc.commentLines],
|
|
23
|
-
shouldStop: false,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (line === '') {
|
|
27
|
-
return acc;
|
|
28
|
-
}
|
|
29
|
-
return { commentLines: acc.commentLines, shouldStop: true };
|
|
30
|
-
}, { commentLines: [], shouldStop: false });
|
|
31
|
-
return reverseIndex.commentLines;
|
|
32
|
-
};
|
|
33
|
-
/**
|
|
34
|
-
* Parse comment lines and extract Zod definition and description
|
|
35
|
-
*/
|
|
36
|
-
const parseFieldComments = (commentLines) => {
|
|
37
|
-
const cleanLines = commentLines
|
|
38
|
-
.map((line) => line.replace(/^\/\/\/\s*/, '').trim())
|
|
39
|
-
.filter((line) => line.length > 0);
|
|
40
|
-
const zodDefinition = cleanLines.find((line) => line.startsWith('@z.'))?.replace(/^@/, '') ?? '';
|
|
41
|
-
const descriptionLines = cleanLines.filter((line) => !isMetadataComment(line));
|
|
42
|
-
const description = descriptionLines.length > 0 ? descriptionLines.join(' ') : undefined;
|
|
43
|
-
return { zodDefinition, description };
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Extract field information from object property
|
|
47
|
-
*/
|
|
48
|
-
const extractFieldFromProperty = (property, sourceText) => {
|
|
49
|
-
if (!Node.isPropertyAssignment(property))
|
|
50
|
-
return null;
|
|
51
|
-
const fieldName = property.getName();
|
|
52
|
-
if (!fieldName)
|
|
53
|
-
return null;
|
|
54
|
-
const fieldStartPos = property.getStart();
|
|
55
|
-
const commentLines = extractFieldComments(sourceText, fieldStartPos);
|
|
56
|
-
const { zodDefinition, description } = parseFieldComments(commentLines);
|
|
57
|
-
return {
|
|
58
|
-
name: fieldName,
|
|
59
|
-
definition: zodDefinition,
|
|
60
|
-
description,
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* Convert table name to Schema name (e.g., 'user' -> 'UserSchema', 'post' -> 'PostSchema')
|
|
65
|
-
*/
|
|
66
|
-
const toSchemaName = (tableName) => `${tableName.charAt(0).toUpperCase() + tableName.slice(1)}Schema`;
|
|
67
|
-
/**
|
|
68
|
-
* Extract relation field with type inference
|
|
69
|
-
*/
|
|
70
|
-
const extractRelationFieldFromProperty = (property, sourceText) => {
|
|
71
|
-
if (!Node.isPropertyAssignment(property))
|
|
72
|
-
return null;
|
|
73
|
-
const fieldName = property.getName();
|
|
74
|
-
if (!fieldName)
|
|
75
|
-
return null;
|
|
76
|
-
const initializer = property.getInitializer();
|
|
77
|
-
if (!Node.isCallExpression(initializer)) {
|
|
78
|
-
return {
|
|
79
|
-
name: fieldName,
|
|
80
|
-
definition: '',
|
|
81
|
-
description: undefined,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
const expression = initializer.getExpression();
|
|
85
|
-
if (!Node.isIdentifier(expression)) {
|
|
86
|
-
return {
|
|
87
|
-
name: fieldName,
|
|
88
|
-
definition: '',
|
|
89
|
-
description: undefined,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
const functionName = expression.getText();
|
|
93
|
-
const args = initializer.getArguments();
|
|
94
|
-
if (args.length === 0) {
|
|
95
|
-
return {
|
|
96
|
-
name: fieldName,
|
|
97
|
-
definition: '',
|
|
98
|
-
description: undefined,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
const firstArg = args[0];
|
|
102
|
-
if (!Node.isIdentifier(firstArg)) {
|
|
103
|
-
return {
|
|
104
|
-
name: fieldName,
|
|
105
|
-
definition: '',
|
|
106
|
-
description: undefined,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
const referencedTable = firstArg.getText();
|
|
110
|
-
const schemaName = toSchemaName(referencedTable);
|
|
111
|
-
const zodDefinition = functionName === 'many'
|
|
112
|
-
? `z.array(${schemaName})` // many(post) -> z.array(PostSchema)
|
|
113
|
-
: functionName === 'one'
|
|
114
|
-
? schemaName // one(user, {...}) -> UserSchema
|
|
115
|
-
: '';
|
|
116
|
-
const fieldStartPos = property.getStart();
|
|
117
|
-
const commentLines = extractFieldComments(sourceText, fieldStartPos);
|
|
118
|
-
const { description } = parseFieldComments(commentLines);
|
|
119
|
-
return {
|
|
120
|
-
name: fieldName,
|
|
121
|
-
definition: zodDefinition,
|
|
122
|
-
description,
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Extract object literal from any expression
|
|
127
|
-
*/
|
|
128
|
-
const extractObjectLiteralFromExpression = (expression) => {
|
|
129
|
-
// Direct object literal
|
|
130
|
-
if (Node.isObjectLiteralExpression(expression)) {
|
|
131
|
-
return expression;
|
|
132
|
-
}
|
|
133
|
-
if (Node.isParenthesizedExpression(expression)) {
|
|
134
|
-
const inner = expression.getExpression();
|
|
135
|
-
return Node.isObjectLiteralExpression(inner) ? inner : null;
|
|
136
|
-
}
|
|
137
|
-
if (Node.isArrowFunction(expression)) {
|
|
138
|
-
const body = expression.getBody();
|
|
139
|
-
if (Node.isObjectLiteralExpression(body)) {
|
|
140
|
-
return body;
|
|
141
|
-
}
|
|
142
|
-
if (Node.isParenthesizedExpression(body)) {
|
|
143
|
-
const inner = body.getExpression();
|
|
144
|
-
return Node.isObjectLiteralExpression(inner) ? inner : null;
|
|
145
|
-
}
|
|
146
|
-
if (Node.isBlock(body)) {
|
|
147
|
-
const returnStatement = body.getStatements().find((stmt) => Node.isReturnStatement(stmt));
|
|
148
|
-
if (returnStatement && Node.isReturnStatement(returnStatement)) {
|
|
149
|
-
const returnExpression = returnStatement.getExpression();
|
|
150
|
-
return returnExpression && Node.isObjectLiteralExpression(returnExpression)
|
|
151
|
-
? returnExpression
|
|
152
|
-
: null;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
};
|
|
158
|
-
/**
|
|
159
|
-
* Find object literal in call expression arguments
|
|
160
|
-
*/
|
|
161
|
-
const findObjectLiteralInArgs = (callExpr) => {
|
|
162
|
-
const args = callExpr.getArguments();
|
|
163
|
-
for (const arg of args) {
|
|
164
|
-
const objectLiteral = extractObjectLiteralFromExpression(arg);
|
|
165
|
-
if (objectLiteral) {
|
|
166
|
-
return objectLiteral;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return null;
|
|
170
|
-
};
|
|
171
|
-
/**
|
|
172
|
-
* Determine if this is a relation-like function call
|
|
173
|
-
*/
|
|
174
|
-
const isRelationFunction = (callExpr) => {
|
|
175
|
-
const expression = callExpr.getExpression();
|
|
176
|
-
if (!Node.isIdentifier(expression))
|
|
177
|
-
return false;
|
|
178
|
-
const functionName = expression.getText();
|
|
179
|
-
return functionName === 'relations' || functionName.includes('relation');
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Extract fields from any call expression
|
|
183
|
-
*/
|
|
184
|
-
const extractFieldsFromCallExpression = (callExpr, sourceText) => {
|
|
185
|
-
const objectLiteral = findObjectLiteralInArgs(callExpr);
|
|
186
|
-
if (!objectLiteral)
|
|
187
|
-
return [];
|
|
188
|
-
const isRelation = isRelationFunction(callExpr);
|
|
189
|
-
return objectLiteral
|
|
190
|
-
.getProperties()
|
|
191
|
-
.map((prop) => isRelation
|
|
192
|
-
? extractRelationFieldFromProperty(prop, sourceText)
|
|
193
|
-
: extractFieldFromProperty(prop, sourceText))
|
|
194
|
-
.filter((field) => field !== null);
|
|
195
|
-
};
|
|
196
|
-
/**
|
|
197
|
-
* Extract a single schema (variable declaration)
|
|
198
|
-
*/
|
|
199
|
-
const extractSchemaFromDeclaration = (declaration, sourceText) => {
|
|
200
|
-
if (!Node.isVariableDeclaration(declaration))
|
|
201
|
-
return null;
|
|
202
|
-
const name = declaration.getName();
|
|
203
|
-
if (!name)
|
|
204
|
-
return null;
|
|
205
|
-
const initializer = declaration.getInitializer();
|
|
206
|
-
if (Node.isCallExpression(initializer)) {
|
|
207
|
-
if (isRelationFunction(initializer))
|
|
208
|
-
return null;
|
|
209
|
-
const fields = extractFieldsFromCallExpression(initializer, sourceText);
|
|
210
|
-
return { name, fields };
|
|
211
|
-
}
|
|
212
|
-
if (Node.isObjectLiteralExpression(initializer)) {
|
|
213
|
-
const fields = initializer
|
|
214
|
-
.getProperties()
|
|
215
|
-
.map((prop) => extractFieldFromProperty(prop, sourceText))
|
|
216
|
-
.filter((field) => field !== null);
|
|
217
|
-
return { name, fields };
|
|
218
|
-
}
|
|
219
|
-
return { name, fields: [] };
|
|
220
|
-
};
|
|
221
|
-
/**
|
|
222
|
-
* Extract schemas from lines of code
|
|
223
|
-
* @param lines - Lines of code
|
|
224
|
-
* @returns Schemas
|
|
225
|
-
*/
|
|
226
|
-
export function extractSchemas(lines) {
|
|
227
|
-
const sourceCode = lines.join('\n');
|
|
228
|
-
const project = new Project({
|
|
229
|
-
useInMemoryFileSystem: true,
|
|
230
|
-
compilerOptions: {
|
|
231
|
-
allowJs: true,
|
|
232
|
-
skipLibCheck: true,
|
|
233
|
-
},
|
|
234
|
-
});
|
|
235
|
-
const sourceFile = project.createSourceFile('temp.ts', sourceCode);
|
|
236
|
-
const sourceText = sourceFile.getFullText();
|
|
237
|
-
return sourceFile
|
|
238
|
-
.getVariableStatements()
|
|
239
|
-
.filter((stmt) => stmt.hasExportKeyword())
|
|
240
|
-
.flatMap((stmt) => stmt.getDeclarations())
|
|
241
|
-
.map((decl) => extractSchemaFromDeclaration(decl, sourceText))
|
|
242
|
-
.filter((schema) => schema !== null);
|
|
243
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { TableInfo, ColumnInfo } from '../../shared/utils/types.js';
|
|
2
|
-
export declare const generateZodSchema: (table: TableInfo) => string;
|
|
3
|
-
export declare const generateZodProperty: (column: ColumnInfo) => string;
|
|
4
|
-
export declare const generateDefaultZodSchema: (column: ColumnInfo) => string;
|
|
5
|
-
export declare const generateZodFile: (tables: TableInfo[]) => string;
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
// 完全純粋なZodスキーマ生成ユーティリティ - 外部import禁止
|
|
2
|
-
import { toPascalCase } from '../../shared/utils/string-utils.js';
|
|
3
|
-
export const generateZodSchema = (table) => {
|
|
4
|
-
const schemaName = `${toPascalCase(table.name)}Schema`;
|
|
5
|
-
const typeName = toPascalCase(table.name);
|
|
6
|
-
const properties = table.columns.map(column => generateZodProperty(column)).join('\n ');
|
|
7
|
-
return `export const ${schemaName} = z.object({
|
|
8
|
-
${properties}
|
|
9
|
-
})
|
|
10
|
-
export type ${typeName} = z.infer<typeof ${schemaName}>`;
|
|
11
|
-
};
|
|
12
|
-
export const generateZodProperty = (column) => {
|
|
13
|
-
const comment = column.comment ? `/** ${column.comment} */\n ` : '';
|
|
14
|
-
const schema = column.zodSchema || generateDefaultZodSchema(column);
|
|
15
|
-
return `${comment}${column.name}: ${schema},`;
|
|
16
|
-
};
|
|
17
|
-
export const generateDefaultZodSchema = (column) => {
|
|
18
|
-
if (column.type === 'varchar') {
|
|
19
|
-
if (column.length === 36) {
|
|
20
|
-
return 'z.uuid()';
|
|
21
|
-
}
|
|
22
|
-
if (column.length) {
|
|
23
|
-
return `z.string().max(${column.length})`;
|
|
24
|
-
}
|
|
25
|
-
return 'z.string()';
|
|
26
|
-
}
|
|
27
|
-
if (column.type === 'int') {
|
|
28
|
-
return 'z.number().int()';
|
|
29
|
-
}
|
|
30
|
-
if (column.type === 'text') {
|
|
31
|
-
return 'z.string()';
|
|
32
|
-
}
|
|
33
|
-
return 'z.unknown()';
|
|
34
|
-
};
|
|
35
|
-
export const generateZodFile = (tables) => {
|
|
36
|
-
const imports = 'import * as z from \'zod\'\n\n';
|
|
37
|
-
const schemas = tables.map(table => generateZodSchema(table)).join('\n\n');
|
|
38
|
-
return imports + schemas;
|
|
39
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates Zod relation schema code from a relation schema AST extraction.
|
|
3
|
-
*/
|
|
4
|
-
export declare function relationZodCode(schema: {
|
|
5
|
-
name: string;
|
|
6
|
-
baseName: string;
|
|
7
|
-
fields: {
|
|
8
|
-
name: string;
|
|
9
|
-
definition: string;
|
|
10
|
-
description?: string;
|
|
11
|
-
}[];
|
|
12
|
-
objectType?: 'strict' | 'loose';
|
|
13
|
-
}, withType: boolean): string;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { capitalize, infer } from '../../../utils/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Generates Zod relation schema code from a relation schema AST extraction.
|
|
4
|
-
*/
|
|
5
|
-
export function relationZodCode(schema, withType) {
|
|
6
|
-
const base = schema.baseName;
|
|
7
|
-
const relName = `${schema.name}Schema`;
|
|
8
|
-
const baseSchema = `${capitalize(base)}Schema`;
|
|
9
|
-
const fields = schema.fields.map((f) => `${f.name}:${f.definition}`).join(',');
|
|
10
|
-
const objectType = schema.objectType === 'strict'
|
|
11
|
-
? 'strictObject'
|
|
12
|
-
: schema.objectType === 'loose'
|
|
13
|
-
? 'looseObject'
|
|
14
|
-
: 'object';
|
|
15
|
-
const obj = `\nexport const ${capitalize(relName)} = z.${objectType}({...${baseSchema}.shape,${fields}})`;
|
|
16
|
-
if (withType)
|
|
17
|
-
return `${obj}\n\n${infer(schema.name)}\n`;
|
|
18
|
-
return `${obj}`;
|
|
19
|
-
}
|
|
@@ -1,17 +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
|
-
objectType?: 'strict' | 'loose';
|
|
17
|
-
}, comment: boolean, type: boolean): string;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { infer } from '../../../utils/index.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,16 +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
|
-
objectType?: 'strict' | 'loose';
|
|
16
|
-
}, comment: boolean): string;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { capitalize, fieldDefinitions } from '../../../utils/index.js';
|
|
2
|
-
/**
|
|
3
|
-
* Generates a Zod schema for a given schema and config.
|
|
4
|
-
*
|
|
5
|
-
* @param schema - The schema to generate code for.
|
|
6
|
-
* @param config - The configuration for the code generation.
|
|
7
|
-
* @returns The generated Zod schema.
|
|
8
|
-
*/
|
|
9
|
-
export function zod(schema, comment) {
|
|
10
|
-
const objectType = schema.objectType === 'strict'
|
|
11
|
-
? 'strictObject'
|
|
12
|
-
: schema.objectType === 'loose'
|
|
13
|
-
? 'looseObject'
|
|
14
|
-
: 'object';
|
|
15
|
-
return `export const ${capitalize(schema.name)}Schema = z.${objectType}({${fieldDefinitions(schema, comment)}})`;
|
|
16
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Result, err, ok } from 'neverthrow';
|
|
2
|
-
import { generateZodFile } from './core.js';
|
|
3
|
-
export const generateZodSchemas = (schema) => {
|
|
4
|
-
try {
|
|
5
|
-
if (schema.tables.length === 0) {
|
|
6
|
-
return err(new Error('No tables found in schema'));
|
|
7
|
-
}
|
|
8
|
-
const zodContent = generateZodFile(schema.tables);
|
|
9
|
-
return ok(zodContent);
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
return err(new Error(`Failed to generate Zod schemas: ${error instanceof Error ? error.message : String(error)}`));
|
|
13
|
-
}
|
|
14
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generate Zod schema
|
|
3
|
-
* @param code - The code to generate Zod schema from
|
|
4
|
-
* @param output - The output file path
|
|
5
|
-
* @param comment - Whether to include comments in the generated code
|
|
6
|
-
* @param type - Whether to include type information in the generated code
|
|
7
|
-
* @param zod - The Zod version to use
|
|
8
|
-
*/
|
|
9
|
-
export declare function sizukuZod(code: string[], output: `${string}.ts`, comment?: boolean, type?: boolean, zod?: 'v4' | 'mini' | '@hono/zod-openapi', relation?: boolean): Promise<{
|
|
10
|
-
readonly ok: true;
|
|
11
|
-
readonly value: undefined;
|
|
12
|
-
} | {
|
|
13
|
-
readonly ok: false;
|
|
14
|
-
readonly error: string;
|
|
15
|
-
}>;
|