-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
195 lines (173 loc) · 5.17 KB
/
Copy pathindex.ts
File metadata and controls
195 lines (173 loc) · 5.17 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Command } from "commander";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { pathToFileURL } from "node:url";
import { Project } from "ts-morph";
import {
getConfigFromFile,
getDefaultConfigFilePath,
discoverAllTsConfigs,
} from "./config";
import { getDefaultConfig } from "./drizzle-kit";
import { getGeneratedSchema } from "./shared";
const defaultConfigFile = "./drizzle-zero.config.ts";
const defaultOutputFile = "./zero-schema.gen.ts";
const defaultTsConfigFile = "./tsconfig.json";
const defaultDrizzleKitConfigPath = "./drizzle.config.ts";
export async function loadPrettier() {
try {
return await import("prettier");
} catch (_) {}
try {
const path = require.resolve("prettier", { paths: [process.cwd()] });
return await import(pathToFileURL(path).href);
} catch {
throw new Error(
"⚠️ drizzle-zero: prettier could not be found. Install it locally with\n npm i -D prettier",
);
}
}
export async function formatSchema(schema: string): Promise<string> {
try {
const prettier = await loadPrettier();
return prettier.format(schema, {
parser: "typescript",
});
} catch (error) {
console.warn("⚠️ drizzle-zero: prettier not found, skipping formatting");
return schema;
}
}
export interface GeneratorOptions {
config?: string;
tsConfigPath?: string;
format?: boolean;
outputFilePath?: string;
drizzleSchemaPath?: string;
drizzleKitConfigPath?: string;
debug?: boolean;
jsFileExtension?: boolean;
}
async function main(opts: GeneratorOptions = {}) {
const {
config,
tsConfigPath,
format,
outputFilePath,
drizzleSchemaPath,
drizzleKitConfigPath,
debug,
jsFileExtension,
} = { ...opts };
const resolvedTsConfigPath = tsConfigPath ?? defaultTsConfigFile;
const resolvedOutputFilePath = outputFilePath ?? defaultOutputFile;
const defaultConfigFilePath = await getDefaultConfigFilePath();
const configFilePath = config ?? defaultConfigFilePath;
if (!configFilePath) {
console.log(
"😶🌫️ drizzle-zero: Using all tables/columns from Drizzle schema",
);
}
const allTsConfigPaths = await discoverAllTsConfigs(resolvedTsConfigPath);
const tsProject = new Project({
tsConfigFilePath: resolvedTsConfigPath,
skipAddingFilesFromTsConfig: true,
});
for (const tsConfigPath of allTsConfigPaths) {
tsProject.addSourceFilesFromTsConfig(tsConfigPath);
}
const result = configFilePath
? await getConfigFromFile({
configFilePath,
tsProject,
})
: await getDefaultConfig({
drizzleSchemaPath,
drizzleKitConfigPath,
tsProject,
debug: Boolean(debug),
});
if (!result?.zeroSchema) {
console.error(
"❌ drizzle-zero: No config found in the config file - did you export `default` or `schema`?",
);
process.exit(1);
}
let zeroSchemaGenerated = await getGeneratedSchema({
tsProject,
result,
outputFilePath: resolvedOutputFilePath,
jsFileExtension: Boolean(jsFileExtension),
});
if (format) {
zeroSchemaGenerated = await formatSchema(zeroSchemaGenerated);
}
return zeroSchemaGenerated;
}
async function cli() {
const program = new Command();
program
.name("drizzle-zero")
.description("The CLI for converting Drizzle ORM schemas to Zero schemas");
program
.command("generate")
.option(
"-c, --config <input-file>",
`Path to the ${defaultConfigFile} configuration file`,
)
.option("-s, --schema <input-file>", `Path to the Drizzle schema file`)
.option(
"-k, --drizzle-kit-config <input-file>",
`Path to the Drizzle Kit config file`,
defaultDrizzleKitConfigPath,
)
.option(
"-o, --output <output-file>",
`Path to the generated output file`,
defaultOutputFile,
)
.option(
"-t, --tsconfig <tsconfig-file>",
`Path to the custom tsconfig file`,
defaultTsConfigFile,
)
.option("-f, --format", `Format the generated schema`, false)
.option("-d, --debug", `Enable debug mode`)
.option(
"-j, --js-file-extension",
`Add a .js file extension to the output (for usage without \"bundler\" module resolution)`,
false,
)
.action(async (command) => {
console.log(`⚙️ drizzle-zero: Generating zero schema...`);
const zeroSchema = await main({
config: command.config,
tsConfigPath: command.tsconfig,
format: command.format,
outputFilePath: command.output,
drizzleSchemaPath: command.schema,
drizzleKitConfigPath: command.drizzleKitConfig,
debug: command.debug,
jsFileExtension: command.jsFileExtension,
});
if (command.output) {
await fs.writeFile(
path.resolve(process.cwd(), command.output),
zeroSchema,
);
console.log(
`✅ drizzle-zero: Zero schema written to ${command.output}`,
);
} else {
console.log("drizzle-zero: ", {
schema: zeroSchema,
});
}
});
program.parse();
}
// Run the main function
cli().catch((error) => {
console.error("❌ drizzle-zero error:", error);
process.exit(1);
});