-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathhelpers.ts
More file actions
123 lines (114 loc) · 3.29 KB
/
helpers.ts
File metadata and controls
123 lines (114 loc) · 3.29 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
import path from "path";
import { promises as fs } from "fs";
import dotenv from "dotenv";
import { drizzle, migrate, users, clients } from "@/server/database/sqlite";
import { hash } from "@node-rs/argon2";
import type { UserCredentials } from "@/shared/auth";
import { eq } from "drizzle-orm";
import { z } from "zod";
export async function ask(
question: string,
opts?: { required?: boolean },
): Promise<string> {
return new Promise<string>((resolve) => {
process.stdout.write(question);
process.stdin.once("data", (data) => resolve(data.toString().trim()));
}).then((r) => {
if (opts?.required && !r) return ask(question, opts);
return r;
});
}
export async function createFileWithContent(filePath: string, content: string) {
try {
const f = await fs.open(filePath, "wx");
await f.writeFile(content, { encoding: "utf8" });
await f.close();
} catch (error: unknown) {
if (error instanceof Error && "code" in error && error.code === "EEXIST") {
console.error(`File ${path.basename(filePath)} already exists.`);
} else {
console.error(`Error creating ${path.basename(filePath)} file: `, error);
}
}
}
export async function writeFileWithContent(filePath: string, content: string) {
await fs.writeFile(filePath, content, { encoding: "utf8" });
}
export function configEnv() {
dotenv.config();
if (process.argv.includes("--dev")) {
dotenv.config({
path: path.resolve(process.cwd(), ".env.development.local"),
override: true,
});
process.env = { ...process.env, NODE_ENV: "development" };
}
}
export function configDatabase() {
const db = drizzle(process.env.DATABASE_URL);
migrate(db, {
migrationsFolder: path.resolve(process.cwd(), "./drizzle/sqlite"),
});
db.insert(clients)
.values({
client_id: "swagger-client",
client_info: JSON.stringify({
redirect_uris: ["http://localhost:9200/swagger/oauth2-redirect.html"],
client_name: "Swagger Client",
client_uri: "http://localhost:9200/swagger",
scope: "mcp llm scope123 blob",
client_secret: "swagger-secret",
client_secret_expires_at:
Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
client_id: "swagger-client",
client_id_issued_at: Date.now(),
}),
})
.onConflictDoNothing()
.then(() => null);
return db;
}
export async function createUser(
db: ReturnType<typeof configDatabase>,
{ email, password }: UserCredentials,
scope: string[],
info?: {
firstName?: string;
lastName?: string;
},
) {
email = await z
.string()
.email()
.parseAsync(email)
.catch(() => {
throw new Error(`Invalid email address: ${email}`);
});
await db
.select()
.from(users)
.where(eq(users.email, email))
.then((r) => {
if (r.length > 0) {
throw new Error(`User with email ${email} already exists.`);
}
});
const hashedPassword = await hash(password);
await db.insert(users).values({
email,
password: hashedPassword,
scope: scope.join(" "),
...info,
});
return db
.select()
.from(users)
.where(eq(users.email, email))
.then((r) => r[0])
.then((u) => {
if (u) return u;
throw new Error(
`FATAL: User with email ${email} not found. (not created)`,
);
});
}