zodrift 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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +50 -0
- package/dist/commands/check.d.ts +1 -0
- package/dist/commands/check.js +52 -0
- package/dist/commands/codegen.d.ts +1 -0
- package/dist/commands/codegen.js +63 -0
- package/dist/commands/fix.d.ts +1 -0
- package/dist/commands/fix.js +113 -0
- package/dist/commands/forms.d.ts +1 -0
- package/dist/commands/forms.js +41 -0
- package/dist/commands/openapi.d.ts +1 -0
- package/dist/commands/openapi.js +49 -0
- package/dist/core/checker.d.ts +2 -0
- package/dist/core/checker.js +39 -0
- package/dist/core/compare.d.ts +2 -0
- package/dist/core/compare.js +190 -0
- package/dist/core/emit.d.ts +10 -0
- package/dist/core/emit.js +229 -0
- package/dist/core/file-discovery.d.ts +5 -0
- package/dist/core/file-discovery.js +37 -0
- package/dist/core/pairing.d.ts +6 -0
- package/dist/core/pairing.js +27 -0
- package/dist/core/ts-parser.d.ts +5 -0
- package/dist/core/ts-parser.js +239 -0
- package/dist/core/type-utils.d.ts +16 -0
- package/dist/core/type-utils.js +139 -0
- package/dist/core/types.d.ts +133 -0
- package/dist/core/types.js +1 -0
- package/dist/core/zod-parser.d.ts +5 -0
- package/dist/core/zod-parser.js +321 -0
- package/dist/reporters/index.d.ts +2 -0
- package/dist/reporters/index.js +14 -0
- package/dist/reporters/json.d.ts +2 -0
- package/dist/reporters/json.js +18 -0
- package/dist/reporters/pretty.d.ts +2 -0
- package/dist/reporters/pretty.js +35 -0
- package/dist/reporters/sarif.d.ts +2 -0
- package/dist/reporters/sarif.js +85 -0
- package/dist/utils/args.d.ts +9 -0
- package/dist/utils/args.js +55 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 greyllmmoder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# zodrift
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/zodrift)
|
|
4
|
+
[](https://github.com/greyllmmoder/zodrift/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Catch drift between your TypeScript types and Zod schemas.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
|
|
12
|
+
export interface User {
|
|
13
|
+
name: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
age: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const UserSchema = z.object({
|
|
19
|
+
name: z.string(),
|
|
20
|
+
email: z.string(),
|
|
21
|
+
age: z.string(),
|
|
22
|
+
role: z.string(),
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx zodrift check
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
What it catches:
|
|
33
|
+
- missing fields
|
|
34
|
+
- extra fields
|
|
35
|
+
- required vs optional mismatch
|
|
36
|
+
- basic type mismatch
|
|
37
|
+
|
|
38
|
+
Roadmap:
|
|
39
|
+
- nested support
|
|
40
|
+
- arrays
|
|
41
|
+
- unions
|
|
42
|
+
- JSON output
|
|
43
|
+
- GitHub Action
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { runCheckCommand } from "./commands/check.js";
|
|
3
|
+
import { runCodegenCommand } from "./commands/codegen.js";
|
|
4
|
+
import { runFixCommand } from "./commands/fix.js";
|
|
5
|
+
import { runFormsCommand } from "./commands/forms.js";
|
|
6
|
+
import { runOpenApiCommand } from "./commands/openapi.js";
|
|
7
|
+
import { parseArgs } from "./utils/args.js";
|
|
8
|
+
function printHelp() {
|
|
9
|
+
process.stdout.write(`zodrift
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
zodrift check [--pattern <glob>] [--format pretty|json|sarif] [--out <file>] [--changed]
|
|
13
|
+
zodrift fix [--pattern <glob>] [--target schema] [--dry-run] [--write]
|
|
14
|
+
zodrift codegen [--from ts|zod] [--pattern <glob>] [--out-dir <dir>] [--write]
|
|
15
|
+
zodrift openapi [--pattern <glob>] [--out <file>]
|
|
16
|
+
zodrift forms [--pattern <glob>] [--out <file>]
|
|
17
|
+
|
|
18
|
+
Examples:
|
|
19
|
+
npx zodrift check
|
|
20
|
+
npx zodrift check --pattern "examples/**/*.ts"
|
|
21
|
+
npx zodrift check --format json --out reports/zodrift.json
|
|
22
|
+
npx zodrift fix --pattern "src/**/*.ts" --dry-run
|
|
23
|
+
`);
|
|
24
|
+
}
|
|
25
|
+
function main() {
|
|
26
|
+
const parsed = parseArgs(process.argv.slice(2));
|
|
27
|
+
const command = parsed.command;
|
|
28
|
+
if (!command || command === "help" || command === "--help" || command === "-h") {
|
|
29
|
+
printHelp();
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
switch (command) {
|
|
33
|
+
case "check":
|
|
34
|
+
return runCheckCommand(parsed.flags);
|
|
35
|
+
case "fix":
|
|
36
|
+
return runFixCommand(parsed.flags);
|
|
37
|
+
case "codegen":
|
|
38
|
+
return runCodegenCommand(parsed.flags);
|
|
39
|
+
case "openapi":
|
|
40
|
+
return runOpenApiCommand(parsed.flags);
|
|
41
|
+
case "forms":
|
|
42
|
+
return runFormsCommand(parsed.flags);
|
|
43
|
+
default:
|
|
44
|
+
process.stderr.write(`Unknown command: ${command}\n\n`);
|
|
45
|
+
printHelp();
|
|
46
|
+
return 2;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const exitCode = main();
|
|
50
|
+
process.exit(exitCode);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runCheckCommand(flags: Map<string, string | boolean>): number;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { runCheck } from "../core/checker.js";
|
|
4
|
+
import { renderOutput } from "../reporters/index.js";
|
|
5
|
+
import { flagBoolean, flagNumber, flagString } from "../utils/args.js";
|
|
6
|
+
export function runCheckCommand(flags) {
|
|
7
|
+
const cwd = process.cwd();
|
|
8
|
+
const pattern = flagString(flags, "pattern", "**/*.{ts,tsx}");
|
|
9
|
+
const format = flagString(flags, "format", "pretty");
|
|
10
|
+
const maxIssues = flagNumber(flags, "max-issues");
|
|
11
|
+
const changedOnly = flagBoolean(flags, "changed", false);
|
|
12
|
+
const result = runCheck({
|
|
13
|
+
cwd,
|
|
14
|
+
pattern,
|
|
15
|
+
format,
|
|
16
|
+
maxIssues,
|
|
17
|
+
changedOnly,
|
|
18
|
+
});
|
|
19
|
+
const rendered = renderOutput(format, {
|
|
20
|
+
result,
|
|
21
|
+
cwd,
|
|
22
|
+
});
|
|
23
|
+
const outFile = flagString(flags, "out", "").trim();
|
|
24
|
+
if (outFile) {
|
|
25
|
+
const resolvedOut = path.resolve(cwd, outFile);
|
|
26
|
+
fs.mkdirSync(path.dirname(resolvedOut), { recursive: true });
|
|
27
|
+
fs.writeFileSync(resolvedOut, rendered, "utf8");
|
|
28
|
+
if (format === "pretty") {
|
|
29
|
+
process.stdout.write(`${rendered}\n`);
|
|
30
|
+
}
|
|
31
|
+
process.stdout.write(`Saved ${format} report to ${resolvedOut}\n`);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
process.stdout.write(`${rendered}\n`);
|
|
35
|
+
}
|
|
36
|
+
const failOn = flagString(flags, "fail-on", "all");
|
|
37
|
+
const hasErrors = result.errors.length > 0;
|
|
38
|
+
const hasDrift = result.totalIssues > 0;
|
|
39
|
+
if (hasErrors) {
|
|
40
|
+
return 2;
|
|
41
|
+
}
|
|
42
|
+
if (failOn === "drift" && hasDrift) {
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
if (failOn === "error") {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
if (failOn === "all" && (hasDrift || hasErrors)) {
|
|
49
|
+
return hasDrift ? 1 : 2;
|
|
50
|
+
}
|
|
51
|
+
return hasDrift ? 1 : 0;
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runCodegenCommand(flags: Map<string, string | boolean>): number;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { discoverSourceFiles } from "../core/file-discovery.js";
|
|
4
|
+
import { typeNodeToTs, typeNodeToZod } from "../core/emit.js";
|
|
5
|
+
import { parseTypeDeclarations } from "../core/ts-parser.js";
|
|
6
|
+
import { parseSchemaDeclarations } from "../core/zod-parser.js";
|
|
7
|
+
import { flagBoolean, flagString } from "../utils/args.js";
|
|
8
|
+
export function runCodegenCommand(flags) {
|
|
9
|
+
const cwd = process.cwd();
|
|
10
|
+
const pattern = flagString(flags, "pattern", "**/*.{ts,tsx}");
|
|
11
|
+
const from = flagString(flags, "from", "ts");
|
|
12
|
+
const write = flagBoolean(flags, "write", false);
|
|
13
|
+
const outDir = path.resolve(cwd, flagString(flags, "out-dir", "generated"));
|
|
14
|
+
const files = discoverSourceFiles({
|
|
15
|
+
cwd,
|
|
16
|
+
pattern,
|
|
17
|
+
changedOnly: false,
|
|
18
|
+
});
|
|
19
|
+
let output = "";
|
|
20
|
+
if (from === "ts") {
|
|
21
|
+
const parsed = parseTypeDeclarations(files);
|
|
22
|
+
if (parsed.errors.length > 0) {
|
|
23
|
+
process.stdout.write(`${parsed.errors.join("\n")}\n`);
|
|
24
|
+
return 2;
|
|
25
|
+
}
|
|
26
|
+
output = [
|
|
27
|
+
'import { z } from "zod";',
|
|
28
|
+
"",
|
|
29
|
+
...parsed.declarations.map((declaration) => `export const ${declaration.name}Schema = ${typeNodeToZod(declaration.node)};`),
|
|
30
|
+
"",
|
|
31
|
+
].join("\n");
|
|
32
|
+
}
|
|
33
|
+
else if (from === "zod") {
|
|
34
|
+
const parsed = parseSchemaDeclarations(files);
|
|
35
|
+
if (parsed.errors.length > 0) {
|
|
36
|
+
process.stdout.write(`${parsed.errors.join("\n")}\n`);
|
|
37
|
+
return 2;
|
|
38
|
+
}
|
|
39
|
+
output = [
|
|
40
|
+
...parsed.declarations.map((declaration) => {
|
|
41
|
+
const name = declaration.name.endsWith("Schema")
|
|
42
|
+
? declaration.name.slice(0, -"Schema".length)
|
|
43
|
+
: declaration.name;
|
|
44
|
+
return `export type ${name} = ${typeNodeToTs(declaration.node)};`;
|
|
45
|
+
}),
|
|
46
|
+
"",
|
|
47
|
+
].join("\n");
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
process.stdout.write("Invalid --from value. Use --from ts or --from zod.\n");
|
|
51
|
+
return 2;
|
|
52
|
+
}
|
|
53
|
+
const outputFile = path.join(outDir, from === "ts" ? "schemas.ts" : "types.ts");
|
|
54
|
+
if (!write) {
|
|
55
|
+
process.stdout.write(output);
|
|
56
|
+
process.stdout.write(`\nDry run only. Re-run with --write to save ${path.relative(cwd, outputFile)}\n`);
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
60
|
+
fs.writeFileSync(outputFile, output, "utf8");
|
|
61
|
+
process.stdout.write(`Generated ${path.relative(cwd, outputFile)}\n`);
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runFixCommand(flags: Map<string, string | boolean>): number;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { runCheck } from "../core/checker.js";
|
|
4
|
+
import { flagBoolean, flagString } from "../utils/args.js";
|
|
5
|
+
const PRIMITIVE_METHODS = new Set(["string", "number", "boolean", "unknown", "any"]);
|
|
6
|
+
function addOptional(line) {
|
|
7
|
+
if (line.includes(".optional()")) {
|
|
8
|
+
return line;
|
|
9
|
+
}
|
|
10
|
+
return line.replace(/(z\.[A-Za-z_$][A-Za-z0-9_$]*\([^\n]*\))(?!\s*\.optional\(\))/, "$1.optional()");
|
|
11
|
+
}
|
|
12
|
+
function removeOptional(line) {
|
|
13
|
+
return line.replace(/\.optional\(\)/, "");
|
|
14
|
+
}
|
|
15
|
+
function swapPrimitive(line, from, to) {
|
|
16
|
+
const pattern = new RegExp(`\\bz\\.${from}\\s*\\(`);
|
|
17
|
+
return line.replace(pattern, `z.${to}(`);
|
|
18
|
+
}
|
|
19
|
+
export function runFixCommand(flags) {
|
|
20
|
+
const cwd = process.cwd();
|
|
21
|
+
const pattern = flagString(flags, "pattern", "**/*.{ts,tsx}");
|
|
22
|
+
const write = flagBoolean(flags, "write", false);
|
|
23
|
+
const dryRun = flagBoolean(flags, "dry-run", !write);
|
|
24
|
+
const target = flagString(flags, "target", "schema");
|
|
25
|
+
if (target !== "schema") {
|
|
26
|
+
process.stdout.write("Only --target schema is supported in this version.\n");
|
|
27
|
+
return 2;
|
|
28
|
+
}
|
|
29
|
+
const result = runCheck({
|
|
30
|
+
cwd,
|
|
31
|
+
pattern,
|
|
32
|
+
format: "pretty",
|
|
33
|
+
changedOnly: false,
|
|
34
|
+
});
|
|
35
|
+
const fileLines = new Map();
|
|
36
|
+
const edits = [];
|
|
37
|
+
function loadLines(filePath) {
|
|
38
|
+
const abs = path.resolve(filePath);
|
|
39
|
+
const cached = fileLines.get(abs);
|
|
40
|
+
if (cached) {
|
|
41
|
+
return cached;
|
|
42
|
+
}
|
|
43
|
+
const lines = fs.readFileSync(abs, "utf8").split(/\r?\n/);
|
|
44
|
+
fileLines.set(abs, lines);
|
|
45
|
+
return lines;
|
|
46
|
+
}
|
|
47
|
+
for (const pair of result.pairs) {
|
|
48
|
+
for (const issue of pair.issues) {
|
|
49
|
+
const location = issue.schemaLocation;
|
|
50
|
+
if (!location) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const lines = loadLines(location.filePath);
|
|
54
|
+
const lineIndex = location.line - 1;
|
|
55
|
+
if (lineIndex < 0 || lineIndex >= lines.length) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const original = lines[lineIndex];
|
|
59
|
+
let updated = original;
|
|
60
|
+
let reason = "";
|
|
61
|
+
if (issue.kind === "optional_mismatch" &&
|
|
62
|
+
issue.typeValue === "optional" &&
|
|
63
|
+
issue.schemaValue === "required") {
|
|
64
|
+
updated = addOptional(original);
|
|
65
|
+
reason = "add optional() to match TypeScript optional field";
|
|
66
|
+
}
|
|
67
|
+
else if (issue.kind === "optional_mismatch" &&
|
|
68
|
+
issue.typeValue === "required" &&
|
|
69
|
+
issue.schemaValue === "optional") {
|
|
70
|
+
updated = removeOptional(original);
|
|
71
|
+
reason = "remove optional() to match TypeScript required field";
|
|
72
|
+
}
|
|
73
|
+
else if (issue.kind === "type_mismatch" &&
|
|
74
|
+
issue.typeValue &&
|
|
75
|
+
issue.schemaValue &&
|
|
76
|
+
PRIMITIVE_METHODS.has(issue.typeValue) &&
|
|
77
|
+
PRIMITIVE_METHODS.has(issue.schemaValue)) {
|
|
78
|
+
updated = swapPrimitive(original, issue.schemaValue, issue.typeValue);
|
|
79
|
+
reason = `replace z.${issue.schemaValue} with z.${issue.typeValue}`;
|
|
80
|
+
}
|
|
81
|
+
if (!reason || updated === original) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
lines[lineIndex] = updated;
|
|
85
|
+
edits.push({
|
|
86
|
+
filePath: location.filePath,
|
|
87
|
+
line: location.line,
|
|
88
|
+
reason,
|
|
89
|
+
before: original,
|
|
90
|
+
after: updated,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (edits.length === 0) {
|
|
95
|
+
process.stdout.write("No safe autofixes available for the current drift set.\n");
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
for (const edit of edits) {
|
|
99
|
+
process.stdout.write(`- ${edit.filePath}:${edit.line} ${edit.reason}\n`);
|
|
100
|
+
process.stdout.write(` before: ${edit.before.trim()}\n`);
|
|
101
|
+
process.stdout.write(` after : ${edit.after.trim()}\n`);
|
|
102
|
+
}
|
|
103
|
+
if (dryRun || !write) {
|
|
104
|
+
process.stdout.write(`\nDry run complete. ${edits.length} safe edits proposed.\n`);
|
|
105
|
+
process.stdout.write("Re-run with --write to apply changes.\n");
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
for (const [filePath, lines] of fileLines.entries()) {
|
|
109
|
+
fs.writeFileSync(filePath, `${lines.join("\n")}\n`, "utf8");
|
|
110
|
+
}
|
|
111
|
+
process.stdout.write(`\nApplied ${edits.length} edits.\n`);
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runFormsCommand(flags: Map<string, string | boolean>): number;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { discoverSourceFiles } from "../core/file-discovery.js";
|
|
4
|
+
import { typeNodeToFormFields } from "../core/emit.js";
|
|
5
|
+
import { parseSchemaDeclarations } from "../core/zod-parser.js";
|
|
6
|
+
import { flagString } from "../utils/args.js";
|
|
7
|
+
export function runFormsCommand(flags) {
|
|
8
|
+
const cwd = process.cwd();
|
|
9
|
+
const pattern = flagString(flags, "pattern", "**/*.{ts,tsx}");
|
|
10
|
+
const outFile = flagString(flags, "out", "").trim();
|
|
11
|
+
const files = discoverSourceFiles({
|
|
12
|
+
cwd,
|
|
13
|
+
pattern,
|
|
14
|
+
changedOnly: false,
|
|
15
|
+
});
|
|
16
|
+
const parsed = parseSchemaDeclarations(files);
|
|
17
|
+
if (parsed.errors.length > 0) {
|
|
18
|
+
process.stdout.write(`${parsed.errors.join("\n")}\n`);
|
|
19
|
+
return 2;
|
|
20
|
+
}
|
|
21
|
+
const forms = {};
|
|
22
|
+
for (const schema of parsed.declarations) {
|
|
23
|
+
const name = schema.name.endsWith("Schema")
|
|
24
|
+
? schema.name.slice(0, -"Schema".length)
|
|
25
|
+
: schema.name;
|
|
26
|
+
forms[name] = {
|
|
27
|
+
schema: schema.name,
|
|
28
|
+
fields: typeNodeToFormFields(schema.node),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const content = `${JSON.stringify({ forms }, null, 2)}\n`;
|
|
32
|
+
if (!outFile) {
|
|
33
|
+
process.stdout.write(content);
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
const resolvedOut = path.resolve(cwd, outFile);
|
|
37
|
+
fs.mkdirSync(path.dirname(resolvedOut), { recursive: true });
|
|
38
|
+
fs.writeFileSync(resolvedOut, content, "utf8");
|
|
39
|
+
process.stdout.write(`Wrote forms metadata to ${resolvedOut}\n`);
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runOpenApiCommand(flags: Map<string, string | boolean>): number;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { discoverSourceFiles } from "../core/file-discovery.js";
|
|
4
|
+
import { typeNodeToOpenApi } from "../core/emit.js";
|
|
5
|
+
import { parseSchemaDeclarations } from "../core/zod-parser.js";
|
|
6
|
+
import { flagString } from "../utils/args.js";
|
|
7
|
+
export function runOpenApiCommand(flags) {
|
|
8
|
+
const cwd = process.cwd();
|
|
9
|
+
const pattern = flagString(flags, "pattern", "**/*.{ts,tsx}");
|
|
10
|
+
const outFile = flagString(flags, "out", "").trim();
|
|
11
|
+
const files = discoverSourceFiles({
|
|
12
|
+
cwd,
|
|
13
|
+
pattern,
|
|
14
|
+
changedOnly: false,
|
|
15
|
+
});
|
|
16
|
+
const parsed = parseSchemaDeclarations(files);
|
|
17
|
+
if (parsed.errors.length > 0) {
|
|
18
|
+
process.stdout.write(`${parsed.errors.join("\n")}\n`);
|
|
19
|
+
return 2;
|
|
20
|
+
}
|
|
21
|
+
const components = {};
|
|
22
|
+
for (const schema of parsed.declarations) {
|
|
23
|
+
const name = schema.name.endsWith("Schema")
|
|
24
|
+
? schema.name.slice(0, -"Schema".length)
|
|
25
|
+
: schema.name;
|
|
26
|
+
components[name] = typeNodeToOpenApi(schema.node);
|
|
27
|
+
}
|
|
28
|
+
const payload = {
|
|
29
|
+
openapi: "3.1.0",
|
|
30
|
+
info: {
|
|
31
|
+
title: "zodrift generated spec",
|
|
32
|
+
version: "0.1.0",
|
|
33
|
+
},
|
|
34
|
+
paths: {},
|
|
35
|
+
components: {
|
|
36
|
+
schemas: components,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
const content = `${JSON.stringify(payload, null, 2)}\n`;
|
|
40
|
+
if (!outFile) {
|
|
41
|
+
process.stdout.write(content);
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
const resolvedOut = path.resolve(cwd, outFile);
|
|
45
|
+
fs.mkdirSync(path.dirname(resolvedOut), { recursive: true });
|
|
46
|
+
fs.writeFileSync(resolvedOut, content, "utf8");
|
|
47
|
+
process.stdout.write(`Wrote OpenAPI spec to ${resolvedOut}\n`);
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { comparePair } from "./compare.js";
|
|
2
|
+
import { discoverSourceFiles } from "./file-discovery.js";
|
|
3
|
+
import { pairTypesWithSchemas } from "./pairing.js";
|
|
4
|
+
import { parseTypeDeclarations } from "./ts-parser.js";
|
|
5
|
+
import { parseSchemaDeclarations } from "./zod-parser.js";
|
|
6
|
+
export function runCheck(options) {
|
|
7
|
+
const files = discoverSourceFiles({
|
|
8
|
+
cwd: options.cwd,
|
|
9
|
+
pattern: options.pattern,
|
|
10
|
+
changedOnly: options.changedOnly,
|
|
11
|
+
});
|
|
12
|
+
const typeParsed = parseTypeDeclarations(files);
|
|
13
|
+
const schemaParsed = parseSchemaDeclarations(files);
|
|
14
|
+
const paired = pairTypesWithSchemas(typeParsed.declarations, schemaParsed.declarations);
|
|
15
|
+
const pairs = [];
|
|
16
|
+
let totalIssues = 0;
|
|
17
|
+
for (const pair of paired.pairs) {
|
|
18
|
+
const issues = comparePair(pair);
|
|
19
|
+
const cappedIssues = options.maxIssues && options.maxIssues > 0
|
|
20
|
+
? issues.slice(0, options.maxIssues)
|
|
21
|
+
: issues;
|
|
22
|
+
totalIssues += cappedIssues.length;
|
|
23
|
+
pairs.push({
|
|
24
|
+
typeName: pair.typeDecl.name,
|
|
25
|
+
schemaName: pair.schemaDecl.name,
|
|
26
|
+
typeDecl: pair.typeDecl,
|
|
27
|
+
schemaDecl: pair.schemaDecl,
|
|
28
|
+
issues: cappedIssues,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
pairs,
|
|
33
|
+
totalIssues,
|
|
34
|
+
checkedPairs: pairs.length,
|
|
35
|
+
unmatchedTypes: paired.unmatchedTypes,
|
|
36
|
+
unmatchedSchemas: paired.unmatchedSchemas,
|
|
37
|
+
errors: [...typeParsed.errors, ...schemaParsed.errors],
|
|
38
|
+
};
|
|
39
|
+
}
|