-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfig.ts
More file actions
executable file
·198 lines (171 loc) · 5.15 KB
/
Copy pathconfig.ts
File metadata and controls
executable file
·198 lines (171 loc) · 5.15 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
196
197
198
import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as url from "node:url";
import { Project } from "ts-morph";
import { tsImport } from "tsx/esm/api";
import type { DrizzleToZeroSchema } from "../relations";
import {
parse as JsoncParse,
type ParseError as JsoncParseError,
} from "jsonc-parser";
export const defaultConfigFilePath = "drizzle-zero.config.ts";
export const getDefaultConfigFilePath = async () => {
const fullConfigPath = path.resolve(process.cwd(), defaultConfigFilePath);
try {
await fs.access(fullConfigPath);
} catch (error) {
return null;
}
return "drizzle-zero.config.ts";
};
export const getConfigFromFile = async ({
configFilePath,
tsProject,
}: {
configFilePath: string;
tsProject: Project;
}) => {
const fullConfigPath = path.resolve(process.cwd(), configFilePath);
try {
await fs.access(fullConfigPath);
} catch (error) {
throw new Error(
`❌ drizzle-zero: Failed to find config file at ${fullConfigPath}`,
);
}
const zeroConfigFilePathUrl = url.pathToFileURL(fullConfigPath).href;
const zeroConfigImport = await tsImport(
zeroConfigFilePathUrl,
import.meta.url,
);
const exportName = zeroConfigImport?.default ? "default" : "schema";
const zeroSchema = zeroConfigImport?.default ?? zeroConfigImport?.schema;
const typeDeclarations = await getZeroSchemaDefsFromConfig({
tsProject,
configPath: fullConfigPath,
exportName,
});
return {
type: "config",
zeroSchema: zeroSchema as DrizzleToZeroSchema<any> | undefined,
exportName,
zeroSchemaTypeDeclarations: typeDeclarations,
} as const;
};
export async function getZeroSchemaDefsFromConfig({
tsProject,
configPath,
exportName,
}: {
tsProject: Project;
configPath: string;
exportName: string;
}) {
const fileName = configPath.slice(configPath.lastIndexOf("/") + 1);
const sourceFile = tsProject.getSourceFile(fileName);
if (!sourceFile) {
throw new Error(
`❌ drizzle-zero: Failed to find type definitions for ${fileName}`,
);
}
const exportDeclarations = sourceFile.getExportedDeclarations();
for (const [name, declarations] of exportDeclarations.entries()) {
for (const declaration of declarations) {
if (exportName === name) {
return [name, declaration] as const;
}
}
}
throw new Error(
`❌ drizzle-zero: No config type found in the config file - did you export \`default\` or \`schema\`? Found: ${sourceFile
.getVariableDeclarations()
.map((v) => v.getName())
.join(", ")}`,
);
}
export async function discoverAllTsConfigs(
initialTsConfigPath: string,
): Promise<Set<string>> {
const processedPaths = new Set<string>();
const toProcess = [path.resolve(initialTsConfigPath)];
const processTsConfig = async (tsConfigPath: string) => {
if (processedPaths.has(tsConfigPath)) {
return [];
}
processedPaths.add(tsConfigPath);
try {
const tsConfigContent = await fs.readFile(tsConfigPath, "utf-8");
const errors: JsoncParseError[] = [];
const tsConfig = JsoncParse(tsConfigContent, errors) as {
references?: { path: string }[];
};
if (errors.length > 0) {
console.warn(
`⚠️ drizzle-zero: Found syntax errors in ${path.relative(process.cwd(), tsConfigPath)}. The resolver will attempt to continue.`,
);
}
if (!tsConfig?.references) {
return [];
}
const tsConfigDir = path.dirname(tsConfigPath);
const newPathsToProcess: string[] = [];
for (const ref of tsConfig.references) {
const referencedTsConfigPath = await resolveReferencePath(
ref.path,
tsConfigDir,
);
if (
referencedTsConfigPath &&
!processedPaths.has(referencedTsConfigPath)
) {
newPathsToProcess.push(referencedTsConfigPath);
}
}
return newPathsToProcess;
} catch (error) {
if (
error instanceof Error &&
"code" in error &&
error.code === "ENOENT"
) {
console.warn(
`⚠️ drizzle-zero: Could not find tsconfig file: ${tsConfigPath}`,
);
} else {
throw new Error(
`❌ drizzle-zero: Error processing tsconfig file: ${tsConfigPath}: ${error}`,
);
}
return [];
}
};
while (toProcess.length > 0) {
const newPaths = (
await Promise.all(toProcess.splice(0).map(processTsConfig))
).flat();
if (newPaths.length > 0) {
toProcess.push(...newPaths);
}
}
return processedPaths;
}
async function resolveReferencePath(
refPath: string,
tsConfigDir: string,
): Promise<string | undefined> {
const resolvedPath = path.resolve(tsConfigDir, refPath);
// The reference can be a directory or a file.
// We assume that if it's a directory, then it contains a 'tsconfig.json' file.
try {
const stats = await fs.stat(resolvedPath);
if (stats.isDirectory()) {
return path.join(resolvedPath, "tsconfig.json");
}
return resolvedPath;
} catch {
console.warn(
`⚠️ drizzle-zero: Could not resolve reference path: ${refPath}`,
);
return;
}
}