-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdrizzle-kit.ts
More file actions
executable file
·160 lines (137 loc) · 4.26 KB
/
drizzle-kit.ts
File metadata and controls
executable file
·160 lines (137 loc) · 4.26 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
import type {Config} from 'drizzle-kit';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as url from 'node:url';
import type {Project} from 'ts-morph';
import {drizzleZeroConfig, type DrizzleToZeroSchema} from '../relations';
import {tsImportShared} from './ts-import';
import {ensureSourceFileInProject} from './ts-project';
export const getDefaultConfig = async ({
drizzleSchemaPath,
drizzleKitConfigPath,
tsProject,
debug,
suppressDefaultsWarning,
}: {
drizzleSchemaPath: string | undefined;
drizzleKitConfigPath: string | undefined;
tsProject: Project;
debug?: boolean;
suppressDefaultsWarning?: boolean;
}) => {
const {drizzleSchemaPath: resolvedDrizzleSchemaPath, casing: drizzleCasing} =
await getFullDrizzleSchemaFilePath({
drizzleSchemaPath,
drizzleKitConfigPath,
});
const resolvedDrizzleSchemaPathUrl = url.pathToFileURL(
resolvedDrizzleSchemaPath,
).href;
const drizzleSchema = await tsImportShared(
resolvedDrizzleSchemaPathUrl,
import.meta.url,
);
const zeroSchema = drizzleZeroConfig(drizzleSchema, {
casing: drizzleCasing ?? undefined,
debug: Boolean(debug),
suppressDefaultsWarning: Boolean(suppressDefaultsWarning),
});
ensureSourceFileInProject({
tsProject,
filePath: resolvedDrizzleSchemaPath,
debug: Boolean(debug),
});
return {
type: 'drizzle-kit',
zeroSchema: zeroSchema as DrizzleToZeroSchema<any> | undefined,
drizzleSchemaSourceFile: getDrizzleSchemaSourceFile({
tsProject,
drizzleSchemaPath: resolvedDrizzleSchemaPath,
}),
drizzleCasing,
} as const;
};
export const getFullDrizzleSchemaFilePath = async ({
drizzleKitConfigPath,
drizzleSchemaPath,
}: {
drizzleKitConfigPath: string | undefined;
drizzleSchemaPath: string | undefined;
}) => {
const typeModuleErrorMessage = `. You may need to add \` "type": "module" \` to your package.json.`;
if (drizzleSchemaPath) {
const fullPath = path.resolve(process.cwd(), drizzleSchemaPath);
try {
await fs.access(fullPath);
return {
drizzleSchemaPath: fullPath,
casing: null,
};
} catch {
console.error(
`❌ drizzle-zero: could not find Drizzle schema file at ${fullPath}`,
);
process.exit(1);
}
}
if (drizzleKitConfigPath) {
const fullPath = path.resolve(process.cwd(), drizzleKitConfigPath);
try {
await fs.access(fullPath);
const drizzleKitConfigFilePathUrl = url.pathToFileURL(fullPath).href;
const drizzleKitConfigImport = await tsImportShared(
drizzleKitConfigFilePathUrl,
import.meta.url,
);
const drizzleKitConfig = drizzleKitConfigImport?.default as Config;
try {
if (Array.isArray(drizzleKitConfig.schema)) {
throw new Error(
'❌ drizzle-zero: Drizzle Kit config schema is an array. Please specify a single schema file for imports to be able to work correctly.',
);
}
if (drizzleKitConfig.schema) {
const fullPath = path.resolve(process.cwd(), drizzleKitConfig.schema);
await fs.access(fullPath);
return {
drizzleSchemaPath: fullPath,
casing: drizzleKitConfig.casing ?? null,
};
}
} catch (error) {
console.error(
`❌ drizzle-zero: could not find Drizzle file pulled from Drizzle Kit config at ${JSON.stringify(drizzleKitConfig)}`,
error,
);
process.exit(1);
}
return {
drizzleSchemaPath: fullPath,
casing: drizzleKitConfig.casing ?? null,
};
} catch (error) {
console.error(
`❌ drizzle-zero: could not find Drizzle Kit config file at ${drizzleKitConfigPath}${typeModuleErrorMessage}`,
error,
);
process.exit(1);
}
}
console.error(`❌ drizzle-zero: could not find Drizzle Kit config file`);
process.exit(1);
};
export function getDrizzleSchemaSourceFile({
tsProject,
drizzleSchemaPath,
}: {
tsProject: Project;
drizzleSchemaPath: string;
}) {
const sourceFile = tsProject.getSourceFile(drizzleSchemaPath);
if (!sourceFile) {
throw new Error(
`❌ drizzle-zero: Failed to find type definitions for ${drizzleSchemaPath}`,
);
}
return sourceFile;
}