-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcp-zod-to-json-schema.sh
More file actions
executable file
·144 lines (122 loc) · 4.23 KB
/
cp-zod-to-json-schema.sh
File metadata and controls
executable file
·144 lines (122 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
set -euo pipefail
# Find the version from package.json (devDependencies or dependencies)
VERSION=$(jq -r '.devDependencies["zod-to-json-schema"] // .dependencies["zod-to-json-schema"]' package.json)
if [[ -z "$VERSION" || "$VERSION" == "null" ]]; then
echo "Could not find zod-to-json-schema version in package.json" >&2
exit 1
fi
echo "zod-to-json-schema version: $VERSION"
# Get the git commit hash for that version from npm
GIT_HEAD=$(npm view zod-to-json-schema@"$VERSION" gitHead)
# rm quotes that npm annoyingly adds if parent command has --json
GIT_HEAD="${GIT_HEAD%\"}"
GIT_HEAD="${GIT_HEAD#\"}"
if [[ -z "$GIT_HEAD" ]]; then
echo "Could not find git commit hash for zod-to-json-schema@$VERSION from npm" >&2
exit 1
fi
echo "Git commit hash: $GIT_HEAD"
# Remove temp dir if it exists
rm -rf /tmp/zod-to-json-schema-temp
# Clone the repo
if ! git clone https://github.com/StefanTerdell/zod-to-json-schema.git /tmp/zod-to-json-schema-temp; then
echo "Failed to clone zod-to-json-schema repo" >&2
exit 1
fi
cd /tmp/zod-to-json-schema-temp
# Checkout the correct commit
if ! git checkout "$GIT_HEAD"; then
echo "Failed to checkout commit $GIT_HEAD" >&2
exit 1
fi
cd - > /dev/null
# Copy src directory
rm -rf src/zod-to-json-schema
if ! cp -r /tmp/zod-to-json-schema-temp/src src/zod-to-json-schema; then
echo "Failed to copy src directory" >&2
exit 1
fi
# Clean up
echo "Cleaning up temp directory"
rm -rf /tmp/zod-to-json-schema-temp
find src/zod-to-json-schema -type f -name '*.ts' -exec node -e '
import * as fs from "fs";
import * as path from "path";
let content = fs.readFileSync("{}", "utf8");
content = content
// zod v4 contains zod v3 under "zod/v3", so replace all imports for that to reduce chances of type-errors.
.replaceAll(`from "zod"`, `from "zod/v3"`)
.replaceAll(`from "../Refs";`, `from "../Refs.js";`)
.replaceAll(`from "./Refs";`, `from "./Refs.js";`)
.replaceAll(`from "./parseTypes";`, `from "./parseTypes.js";`)
.split(";")
.map(s => {
if (!s.trim().startsWith("import {")) return s;
const imports = s.split("{")[1].split("}")[0].trim()
.split(",")
.map(i => i.trim())
.filter(i => i !== "ZodFirstPartyTypeKind")
.filter(Boolean);
if (imports.length === 0) {
return "";
}
if (imports.every(i => i[0] === i[0].toUpperCase())) {
return `import type { ${imports.join(", ")} }${s.split("}")[1]}`;
}
const typeified = imports.map(i => i.match(/^[A-Z]/) ? `type ${i}` : i);
return `import { ${typeified.join(", ")} }${s.split("}")[1]}`;
})
.filter(Boolean)
.join(";");
content = content.replaceAll(";import", ";\nimport");
if (content.includes("ZodFirstPartyTypeKind")) {
let relPath = path.relative(path.dirname("{}"), path.join(process.cwd(), "src/zod-to-json-schema/ZodFirstPartyTypeKind.js"));
if (!relPath.startsWith(".")) relPath = "./" + relPath;
content = [
`import {ZodFirstPartyTypeKind} from ${JSON.stringify(relPath)};`,
content,
].join("\n");
}
fs.writeFileSync("{}", content);
' {} ';'
echo '/** copy-pasted from zod v3, to minimize diff vs zod-to-json-schema */
export enum ZodFirstPartyTypeKind {
ZodString = "ZodString",
ZodNumber = "ZodNumber",
ZodNaN = "ZodNaN",
ZodBigInt = "ZodBigInt",
ZodBoolean = "ZodBoolean",
ZodDate = "ZodDate",
ZodSymbol = "ZodSymbol",
ZodUndefined = "ZodUndefined",
ZodNull = "ZodNull",
ZodAny = "ZodAny",
ZodUnknown = "ZodUnknown",
ZodNever = "ZodNever",
ZodVoid = "ZodVoid",
ZodArray = "ZodArray",
ZodObject = "ZodObject",
ZodUnion = "ZodUnion",
ZodDiscriminatedUnion = "ZodDiscriminatedUnion",
ZodIntersection = "ZodIntersection",
ZodTuple = "ZodTuple",
ZodRecord = "ZodRecord",
ZodMap = "ZodMap",
ZodSet = "ZodSet",
ZodFunction = "ZodFunction",
ZodLazy = "ZodLazy",
ZodLiteral = "ZodLiteral",
ZodEnum = "ZodEnum",
ZodEffects = "ZodEffects",
ZodNativeEnum = "ZodNativeEnum",
ZodOptional = "ZodOptional",
ZodNullable = "ZodNullable",
ZodDefault = "ZodDefault",
ZodCatch = "ZodCatch",
ZodPromise = "ZodPromise",
ZodBranded = "ZodBranded",
ZodPipeline = "ZodPipeline",
ZodReadonly = "ZodReadonly"
}' > src/zod-to-json-schema/ZodFirstPartyTypeKind.ts
echo "Done! src/zod-to-json-schema is now at zod-to-json-schema@$VERSION ($GIT_HEAD)"