-
-
Notifications
You must be signed in to change notification settings - Fork 821
/
Copy pathcommon.ts
186 lines (161 loc) · 5.44 KB
/
common.ts
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
import chalk from 'chalk';
import { UnionToIntersection } from 'hono/utils/types';
import { any, boolean, enum as enum_, literal, object, string, TypeOf, union } from 'zod';
import { dialect } from '../../schemaValidator';
import { outputs } from './outputs';
export type Commands =
| 'introspect'
| 'generate'
| 'check'
| 'up'
| 'drop'
| 'push';
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
type LastTupleElement<TArr extends any[]> = TArr extends [
...start: infer _,
end: infer Last,
] ? Last
: never;
export type UniqueArrayOfUnion<TUnion, TArray extends TUnion[]> = Exclude<
TUnion,
TArray[number]
> extends never ? [TUnion]
: [...TArray, Exclude<TUnion, TArray[number]>];
export const assertCollisions = <
T extends Record<string, unknown>,
TKeys extends (keyof T)[],
TRemainingKeys extends Exclude<keyof T, TKeys[number] | 'config'>[],
Exhaustive extends TRemainingKeys,
UNIQ extends UniqueArrayOfUnion<TRemainingKeys[number], Exhaustive>,
>(
command: Commands,
options: T,
whitelist: Exclude<TKeys, 'config'>,
remainingKeys: UniqueArrayOfUnion<TRemainingKeys[number], Exhaustive>,
): IsUnion<LastTupleElement<UNIQ>> extends false ? 'cli' | 'config' : TKeys => {
const { config, ...rest } = options;
let atLeastOneParam = false;
for (const key of Object.keys(rest)) {
if (whitelist.includes(key)) continue;
atLeastOneParam = atLeastOneParam || rest[key] !== undefined;
}
if (!config && atLeastOneParam) {
return 'cli' as any;
}
if (!atLeastOneParam) {
return 'config' as any;
}
// if config and cli - return error - write a reason
console.log(outputs.common.ambiguousParams(command));
process.exit(1);
};
export const sqliteDriversLiterals = [
literal('turso'),
literal('d1-http'),
literal('expo'),
] as const;
export const postgresqlDriversLiterals = [
literal('aws-data-api'),
literal('pglite'),
] as const;
export const prefixes = [
'index',
'timestamp',
'supabase',
'unix',
'none',
] as const;
export const prefix = enum_(prefixes);
export type Prefix = (typeof prefixes)[number];
{
const _: Prefix = '' as TypeOf<typeof prefix>;
}
export const sqliteDriver = union(sqliteDriversLiterals);
export const postgresDriver = union(postgresqlDriversLiterals);
export const driver = union([sqliteDriver, postgresDriver]);
export const configMigrations = object({
table: string().optional(),
schema: string().optional(),
prefix: prefix.optional().default('index'),
}).optional();
export const configCommonSchema = object({
dialect: dialect,
schema: union([string(), string().array()]).optional(),
out: string().optional(),
breakpoints: boolean().optional().default(true),
verbose: boolean().optional().default(false),
driver: driver.optional(),
tablesFilter: union([string(), string().array()]).optional(),
schemaFilter: union([string(), string().array()]).default(['public']),
migrations: configMigrations,
dbCredentials: any().optional(),
}).passthrough();
export const casing = union([literal('camel'), literal('preserve')]).default(
'camel',
);
export const introspectParams = object({
schema: union([string(), string().array()]).optional(),
out: string().optional().default('./drizzle'),
breakpoints: boolean().default(true),
tablesFilter: union([string(), string().array()]).optional(),
schemaFilter: union([string(), string().array()]).default(['public']),
introspect: object({
casing,
}).default({ casing: 'camel' }),
});
export type IntrospectParams = TypeOf<typeof introspectParams>;
export type Casing = TypeOf<typeof casing>;
export const configIntrospectCliSchema = object({
schema: union([string(), string().array()]).optional(),
out: string().optional().default('./drizzle'),
breakpoints: boolean().default(true),
tablesFilter: union([string(), string().array()]).optional(),
schemaFilter: union([string(), string().array()]).default(['public']),
introspectCasing: union([literal('camel'), literal('preserve')]).default(
'camel',
),
});
export const configGenerateSchema = object({
schema: union([string(), string().array()]),
out: string().optional().default('./drizzle'),
breakpoints: boolean().default(true),
});
export type GenerateSchema = TypeOf<typeof configGenerateSchema>;
export const configPushSchema = object({
dialect: dialect,
schema: union([string(), string().array()]),
tablesFilter: union([string(), string().array()]).optional(),
schemaFilter: union([string(), string().array()]).default(['public']),
verbose: boolean().default(false),
strict: boolean().default(false),
out: string().optional(),
});
export type CliConfig = TypeOf<typeof configCommonSchema>;
export const drivers = ['turso', 'd1-http', 'expo', 'aws-data-api', 'pglite'] as const;
export type Driver = (typeof drivers)[number];
const _: Driver = '' as TypeOf<typeof driver>;
export const wrapParam = (
name: string,
param: any | undefined,
optional: boolean = false,
type?: 'url' | 'secret',
) => {
const check = `[${chalk.green('✓')}]`;
const cross = `[${chalk.red('x')}]`;
if (typeof param === 'string') {
if (param.length === 0) {
return ` ${cross} ${name}: ''`;
}
if (type === 'secret') {
return ` ${check} ${name}: '*****'`;
} else if (type === 'url') {
return ` ${check} ${name}: '${param.replace(/(?<=:\/\/[^:\n]*:)([^@]*)/, '****')}'`;
}
return ` ${check} ${name}: '${param}'`;
}
if (optional) {
return chalk.gray(` ${name}?: `);
}
return ` ${cross} ${name}: ${chalk.gray('undefined')}`;
};