@supatype/cli 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +96 -85
- package/.turbo/turbo-typecheck.log +1 -1
- package/dist/commands/admin.d.ts +28 -1
- package/dist/commands/admin.d.ts.map +1 -1
- package/dist/commands/admin.js +273 -111
- package/dist/commands/admin.js.map +1 -1
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/dev.js +2 -0
- package/dist/commands/dev.js.map +1 -1
- package/dist/commands/init.d.ts +5 -0
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +70 -1
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/push.js +3 -3
- package/dist/commands/push.js.map +1 -1
- package/dist/compose-rename.d.ts +10 -0
- package/dist/compose-rename.d.ts.map +1 -0
- package/dist/compose-rename.js +67 -0
- package/dist/compose-rename.js.map +1 -0
- package/dist/dev-compose.d.ts.map +1 -1
- package/dist/dev-compose.js +34 -50
- package/dist/dev-compose.js.map +1 -1
- package/dist/dev-ports.d.ts +27 -0
- package/dist/dev-ports.d.ts.map +1 -0
- package/dist/dev-ports.js +171 -0
- package/dist/dev-ports.js.map +1 -0
- package/dist/dev-session-lock.d.ts +25 -0
- package/dist/dev-session-lock.d.ts.map +1 -0
- package/dist/dev-session-lock.js +81 -0
- package/dist/dev-session-lock.js.map +1 -0
- package/dist/dev-shutdown.d.ts +18 -2
- package/dist/dev-shutdown.d.ts.map +1 -1
- package/dist/dev-shutdown.js +69 -5
- package/dist/dev-shutdown.js.map +1 -1
- package/dist/env-file.d.ts +5 -0
- package/dist/env-file.d.ts.map +1 -0
- package/dist/env-file.js +33 -0
- package/dist/env-file.js.map +1 -0
- package/dist/self-host-compose.d.ts.map +1 -1
- package/dist/self-host-compose.js +2 -1
- package/dist/self-host-compose.js.map +1 -1
- package/package.json +3 -1
- package/src/commands/admin.ts +361 -136
- package/src/commands/dev.ts +3 -0
- package/src/commands/init.ts +93 -1
- package/src/commands/push.ts +3 -3
- package/src/compose-rename.ts +76 -0
- package/src/dev-compose.ts +44 -50
- package/src/dev-ports.ts +212 -0
- package/src/dev-session-lock.ts +101 -0
- package/src/dev-shutdown.ts +98 -5
- package/src/env-file.ts +37 -0
- package/src/self-host-compose.ts +2 -1
- package/tests/admin-ensure.test.ts +59 -0
- package/tests/dev-ports.test.ts +41 -0
- package/tests/dev-session-lock.test.ts +54 -0
- package/tests/dev-ui.test.ts +24 -1
- package/tsconfig.tsbuildinfo +1 -1
package/src/commands/admin.ts
CHANGED
|
@@ -1,19 +1,42 @@
|
|
|
1
1
|
// ─── Admin panel CLI commands (Gap Appendices task 48) ──────────────────────
|
|
2
2
|
//
|
|
3
3
|
// `npx supatype admin create-user` — create an admin user in the project's
|
|
4
|
-
//
|
|
4
|
+
// auth.users table. First admin is ensured on `supatype dev` or `supatype push`.
|
|
5
5
|
|
|
6
6
|
import type { Command } from "commander"
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { spawnSync } from "node:child_process"
|
|
8
|
+
import { existsSync } from "node:fs"
|
|
9
|
+
import { dirname, join, resolve } from "node:path"
|
|
10
|
+
import bcrypt from "bcryptjs"
|
|
11
|
+
import type { Pool, QueryResult } from "pg"
|
|
9
12
|
import { loadConfig } from "../config.js"
|
|
10
|
-
import {
|
|
11
|
-
|
|
13
|
+
import {
|
|
14
|
+
connectionString,
|
|
15
|
+
resolveRuntimeProvider,
|
|
16
|
+
type SupatypeProjectConfig,
|
|
17
|
+
} from "../project-config.js"
|
|
18
|
+
import { readEnvValue, upsertEnvFile } from "../env-file.js"
|
|
19
|
+
import { hasEngineOverride } from "../binary-cache.js"
|
|
12
20
|
import { confirm as uiConfirm } from "../ui/confirm.js"
|
|
13
21
|
import { error, info, plain } from "../ui/messages.js"
|
|
14
22
|
import { promptText } from "../ui/prompts.js"
|
|
23
|
+
import { isInteractive } from "../ui/interactive.js"
|
|
15
24
|
|
|
16
|
-
const
|
|
25
|
+
export const ADMIN_EMAIL_ENV = "SUPATYPE_ADMIN_EMAIL"
|
|
26
|
+
export const ADMIN_PASSWORD_ENV = "SUPATYPE_ADMIN_PASSWORD"
|
|
27
|
+
|
|
28
|
+
const BCRYPT_ROUNDS = 10
|
|
29
|
+
|
|
30
|
+
export interface EnsureFirstAdminOptions {
|
|
31
|
+
email?: string
|
|
32
|
+
password?: string
|
|
33
|
+
cwd?: string
|
|
34
|
+
role?: string
|
|
35
|
+
connection?: string
|
|
36
|
+
compose?: { project: string; composePath: string }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type DbQuery = (sql: string, params?: unknown[]) => Promise<QueryResult>
|
|
17
40
|
|
|
18
41
|
export function registerAdmin(program: Command): void {
|
|
19
42
|
const adminCmd = program
|
|
@@ -55,72 +78,12 @@ export function registerAdmin(program: Command): void {
|
|
|
55
78
|
|
|
56
79
|
info(`Creating admin user: ${email} (role: ${role})...`)
|
|
57
80
|
|
|
58
|
-
// We use pg directly to insert into the auth.users table
|
|
59
81
|
const pg = await importPg()
|
|
60
82
|
const pool = new pg.Pool({ connectionString: connection, max: 2 })
|
|
61
83
|
|
|
62
84
|
try {
|
|
63
|
-
|
|
64
|
-
await pool
|
|
65
|
-
CREATE SCHEMA IF NOT EXISTS auth;
|
|
66
|
-
|
|
67
|
-
CREATE TABLE IF NOT EXISTS auth.users (
|
|
68
|
-
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
69
|
-
instance_id UUID,
|
|
70
|
-
aud TEXT DEFAULT 'authenticated',
|
|
71
|
-
role TEXT DEFAULT 'authenticated',
|
|
72
|
-
email TEXT UNIQUE,
|
|
73
|
-
encrypted_password TEXT,
|
|
74
|
-
email_confirmed_at TIMESTAMPTZ DEFAULT now(),
|
|
75
|
-
raw_app_meta_data JSONB DEFAULT '{}',
|
|
76
|
-
raw_user_meta_data JSONB DEFAULT '{}',
|
|
77
|
-
created_at TIMESTAMPTZ DEFAULT now(),
|
|
78
|
-
updated_at TIMESTAMPTZ DEFAULT now(),
|
|
79
|
-
confirmation_token TEXT DEFAULT '',
|
|
80
|
-
recovery_token TEXT DEFAULT '',
|
|
81
|
-
email_change_token_new TEXT DEFAULT '',
|
|
82
|
-
email_change TEXT DEFAULT ''
|
|
83
|
-
);
|
|
84
|
-
`)
|
|
85
|
-
|
|
86
|
-
// Check if user already exists
|
|
87
|
-
const existing = await pool.query(
|
|
88
|
-
`SELECT id FROM auth.users WHERE email = $1`,
|
|
89
|
-
[email.toLowerCase()],
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
if (existing.rows.length > 0) {
|
|
93
|
-
error(`User with email "${email}" already exists.`)
|
|
94
|
-
info(`To update their role, use: supatype admin set-role --email ${email} --role ${role}`)
|
|
95
|
-
process.exit(1)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Hash the password (bcrypt-style for GoTrue compatibility)
|
|
99
|
-
const passwordHash = await hashPassword(password)
|
|
100
|
-
|
|
101
|
-
// Insert the admin user with the admin role in app_metadata
|
|
102
|
-
const appMetadata = JSON.stringify({ role, provider: "email", providers: ["email"] })
|
|
103
|
-
const userMetadata = JSON.stringify({})
|
|
104
|
-
|
|
105
|
-
const result = await pool.query(
|
|
106
|
-
`INSERT INTO auth.users (
|
|
107
|
-
email, encrypted_password, role, aud,
|
|
108
|
-
raw_app_meta_data, raw_user_meta_data,
|
|
109
|
-
email_confirmed_at, created_at, updated_at
|
|
110
|
-
) VALUES (
|
|
111
|
-
$1, $2, 'authenticated', 'authenticated',
|
|
112
|
-
$3::jsonb, $4::jsonb,
|
|
113
|
-
now(), now(), now()
|
|
114
|
-
) RETURNING id, email`,
|
|
115
|
-
[email.toLowerCase(), passwordHash, appMetadata, userMetadata],
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
const user = result.rows[0] as { id: string; email: string }
|
|
119
|
-
|
|
120
|
-
info("Admin user created successfully.")
|
|
121
|
-
plain(` ID: ${user.id}`)
|
|
122
|
-
plain(` Email: ${user.email}`)
|
|
123
|
-
plain(` Role: ${role}`)
|
|
85
|
+
await ensureAuthUsersTable(pool)
|
|
86
|
+
await createAdminUser(pool, email, password, role)
|
|
124
87
|
info("This user can now log in to the admin panel at /admin")
|
|
125
88
|
} catch (err) {
|
|
126
89
|
const message =
|
|
@@ -239,88 +202,356 @@ export function registerAdmin(program: Command): void {
|
|
|
239
202
|
})
|
|
240
203
|
}
|
|
241
204
|
|
|
242
|
-
|
|
243
|
-
|
|
205
|
+
/** @deprecated Use ensureFirstAdminUser */
|
|
206
|
+
export const promptFirstAdminUser = ensureFirstAdminUser
|
|
244
207
|
|
|
245
|
-
|
|
208
|
+
/**
|
|
209
|
+
* Ensure a first admin user exists (idempotent). Called from `dev` and `push`
|
|
210
|
+
* when auth.users is ready and no admin users exist yet.
|
|
211
|
+
*/
|
|
212
|
+
export async function ensureFirstAdminUser(
|
|
246
213
|
connection: string,
|
|
214
|
+
options: EnsureFirstAdminOptions = {},
|
|
247
215
|
): Promise<void> {
|
|
248
216
|
const pg = await importPg()
|
|
249
217
|
const pool = new pg.Pool({ connectionString: connection, max: 2 })
|
|
250
|
-
|
|
251
218
|
try {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
SELECT FROM information_schema.tables
|
|
256
|
-
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
257
|
-
) as exists`,
|
|
258
|
-
)
|
|
259
|
-
if (!tableExists.rows[0]?.exists) return
|
|
260
|
-
|
|
261
|
-
// Check if any admin users exist
|
|
262
|
-
const adminCount = await pool.query(
|
|
263
|
-
`SELECT COUNT(*) as count FROM auth.users
|
|
264
|
-
WHERE raw_app_meta_data->>'role' IS NOT NULL
|
|
265
|
-
AND raw_app_meta_data->>'role' != 'authenticated'`,
|
|
219
|
+
await ensureFirstAdminWithQuery(
|
|
220
|
+
(sql, params) => pool.query(sql, params),
|
|
221
|
+
options,
|
|
266
222
|
)
|
|
223
|
+
} catch {
|
|
224
|
+
// Non-fatal — skip when DB is unreachable or auth schema is not ready
|
|
225
|
+
} finally {
|
|
226
|
+
await pool.end()
|
|
227
|
+
}
|
|
228
|
+
}
|
|
267
229
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
230
|
+
/**
|
|
231
|
+
* Resolve DB access for the current project (host URL or compose exec when DB
|
|
232
|
+
* is not published to the host).
|
|
233
|
+
*/
|
|
234
|
+
export async function ensureFirstAdminUserForProject(
|
|
235
|
+
cwd: string,
|
|
236
|
+
config: SupatypeProjectConfig,
|
|
237
|
+
options: EnsureFirstAdminOptions = {},
|
|
238
|
+
): Promise<void> {
|
|
239
|
+
const root = resolve(cwd)
|
|
240
|
+
const merged: EnsureFirstAdminOptions = { cwd: root, ...options }
|
|
241
|
+
|
|
242
|
+
if (
|
|
243
|
+
resolveRuntimeProvider(config) === "docker" &&
|
|
244
|
+
merged.compose &&
|
|
245
|
+
!hasEngineOverride(config)
|
|
246
|
+
) {
|
|
247
|
+
try {
|
|
248
|
+
await ensureFirstAdminWithQuery(
|
|
249
|
+
(sql, params) => composeExecQuery(root, merged.compose!, sql, params),
|
|
250
|
+
merged,
|
|
251
|
+
)
|
|
252
|
+
} catch {
|
|
253
|
+
// Non-fatal
|
|
280
254
|
}
|
|
255
|
+
return
|
|
256
|
+
}
|
|
281
257
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
258
|
+
const connection =
|
|
259
|
+
merged.compose && hasEngineOverride(config)
|
|
260
|
+
? hostComposeDbUrlFromEnv(root)
|
|
261
|
+
: options.connection ?? readEnvValue(root, "DATABASE_URL", connectionString(config))
|
|
262
|
+
|
|
263
|
+
await ensureFirstAdminUser(connection, merged)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async function ensureFirstAdminWithQuery(
|
|
267
|
+
query: DbQuery,
|
|
268
|
+
options: EnsureFirstAdminOptions,
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
const cwd = options.cwd ? resolve(options.cwd) : process.cwd()
|
|
271
|
+
|
|
272
|
+
if (!(await authUsersTableExists(query))) return
|
|
273
|
+
if (await hasAdminUsers(query)) return
|
|
274
|
+
|
|
275
|
+
const credentials = await resolveAdminCredentials(options, cwd)
|
|
276
|
+
if (!credentials) {
|
|
277
|
+
if (!isInteractive()) {
|
|
278
|
+
info(
|
|
279
|
+
"No admin users found. Set SUPATYPE_ADMIN_EMAIL / SUPATYPE_ADMIN_PASSWORD in .env, " +
|
|
280
|
+
"or run: supatype admin create-user",
|
|
281
|
+
)
|
|
286
282
|
}
|
|
283
|
+
return
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const role = options.role ?? "admin"
|
|
287
|
+
await createAdminUser(query, credentials.email, credentials.password, role, { quiet: true })
|
|
288
|
+
clearAdminSeedPassword(cwd)
|
|
289
|
+
info(`Admin user "${credentials.email}" created (role: ${role}).`)
|
|
290
|
+
info("Log in at /admin after starting the dev server.")
|
|
291
|
+
}
|
|
287
292
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
293
|
+
async function resolveAdminCredentials(
|
|
294
|
+
options: EnsureFirstAdminOptions,
|
|
295
|
+
cwd: string,
|
|
296
|
+
): Promise<{ email: string; password: string } | null> {
|
|
297
|
+
const envEmail = options.email ?? readEnvValue(cwd, ADMIN_EMAIL_ENV, "").trim()
|
|
298
|
+
const envPassword =
|
|
299
|
+
options.password ?? readEnvValue(cwd, ADMIN_PASSWORD_ENV, "").trim()
|
|
300
|
+
|
|
301
|
+
if (envEmail && envPassword) {
|
|
302
|
+
if (!envEmail.includes("@")) {
|
|
303
|
+
info("Invalid admin email in .env. Skipping admin user creation.")
|
|
304
|
+
return null
|
|
292
305
|
}
|
|
306
|
+
if (envPassword.length < 8) {
|
|
307
|
+
info("Admin password in .env is too short (min 8 chars). Skipping.")
|
|
308
|
+
return null
|
|
309
|
+
}
|
|
310
|
+
return { email: envEmail, password: envPassword }
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (!isInteractive()) return null
|
|
314
|
+
|
|
315
|
+
info("No admin users found for the admin panel.")
|
|
316
|
+
const createAdmin = await uiConfirm("Create an admin user now?")
|
|
317
|
+
if (!createAdmin) {
|
|
318
|
+
info("Skipped. You can create one later with: supatype admin create-user")
|
|
319
|
+
return null
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const email = await promptText("Admin email")
|
|
323
|
+
if (!email || !email.includes("@")) {
|
|
324
|
+
info("Invalid email. Skipping admin user creation.")
|
|
325
|
+
return null
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const password = await promptText("Admin password (min 8 chars)")
|
|
329
|
+
if (!password || password.length < 8) {
|
|
330
|
+
info("Password too short. Skipping admin user creation.")
|
|
331
|
+
return null
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return { email, password }
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function clearAdminSeedPassword(cwd: string): void {
|
|
338
|
+
upsertEnvFile(cwd, {}, [ADMIN_PASSWORD_ENV])
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export async function hashPasswordForAuth(password: string): Promise<string> {
|
|
342
|
+
return bcrypt.hash(password, BCRYPT_ROUNDS)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async function createAdminUser(
|
|
346
|
+
db: Pool | DbQuery,
|
|
347
|
+
email: string,
|
|
348
|
+
password: string,
|
|
349
|
+
role: string,
|
|
350
|
+
opts: { quiet?: boolean } = {},
|
|
351
|
+
): Promise<{ id: string; email: string }> {
|
|
352
|
+
const query: DbQuery =
|
|
353
|
+
typeof (db as Pool).query === "function"
|
|
354
|
+
? (sql, params) => (db as Pool).query(sql, params)
|
|
355
|
+
: (db as DbQuery)
|
|
356
|
+
|
|
357
|
+
const normalized = email.toLowerCase()
|
|
358
|
+
const existing = await query(`SELECT id FROM auth.users WHERE email = $1`, [
|
|
359
|
+
normalized,
|
|
360
|
+
])
|
|
361
|
+
if (existing.rows.length > 0) {
|
|
362
|
+
throw new Error(`User with email "${email}" already exists.`)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const passwordHash = await hashPasswordForAuth(password)
|
|
366
|
+
const appMetadata = JSON.stringify({
|
|
367
|
+
role,
|
|
368
|
+
provider: "email",
|
|
369
|
+
providers: ["email"],
|
|
370
|
+
})
|
|
371
|
+
const userMetadata = JSON.stringify({})
|
|
372
|
+
|
|
373
|
+
const result = await query(
|
|
374
|
+
`INSERT INTO auth.users (
|
|
375
|
+
email, encrypted_password, role, aud,
|
|
376
|
+
raw_app_meta_data, raw_user_meta_data,
|
|
377
|
+
email_confirmed_at, created_at, updated_at
|
|
378
|
+
) VALUES (
|
|
379
|
+
$1, $2, 'authenticated', 'authenticated',
|
|
380
|
+
$3::jsonb, $4::jsonb,
|
|
381
|
+
now(), now(), now()
|
|
382
|
+
) RETURNING id, email`,
|
|
383
|
+
[normalized, passwordHash, appMetadata, userMetadata],
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
const user = result.rows[0] as { id: string; email: string }
|
|
387
|
+
if (!opts.quiet) {
|
|
388
|
+
info("Admin user created successfully.")
|
|
389
|
+
plain(` ID: ${user.id}`)
|
|
390
|
+
plain(` Email: ${user.email}`)
|
|
391
|
+
plain(` Role: ${role}`)
|
|
392
|
+
}
|
|
393
|
+
return user
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async function ensureAuthUsersTable(pool: Pool): Promise<void> {
|
|
397
|
+
await pool.query(`
|
|
398
|
+
CREATE SCHEMA IF NOT EXISTS auth;
|
|
399
|
+
|
|
400
|
+
CREATE TABLE IF NOT EXISTS auth.users (
|
|
401
|
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
402
|
+
instance_id UUID,
|
|
403
|
+
aud TEXT DEFAULT 'authenticated',
|
|
404
|
+
role TEXT DEFAULT 'authenticated',
|
|
405
|
+
email TEXT UNIQUE,
|
|
406
|
+
encrypted_password TEXT,
|
|
407
|
+
email_confirmed_at TIMESTAMPTZ DEFAULT now(),
|
|
408
|
+
raw_app_meta_data JSONB DEFAULT '{}',
|
|
409
|
+
raw_user_meta_data JSONB DEFAULT '{}',
|
|
410
|
+
created_at TIMESTAMPTZ DEFAULT now(),
|
|
411
|
+
updated_at TIMESTAMPTZ DEFAULT now(),
|
|
412
|
+
confirmation_token TEXT DEFAULT '',
|
|
413
|
+
recovery_token TEXT DEFAULT '',
|
|
414
|
+
email_change_token_new TEXT DEFAULT '',
|
|
415
|
+
email_change TEXT DEFAULT ''
|
|
416
|
+
);
|
|
417
|
+
`)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
async function authUsersTableExists(query: DbQuery): Promise<boolean> {
|
|
421
|
+
const result = await query(
|
|
422
|
+
`SELECT EXISTS (
|
|
423
|
+
SELECT FROM information_schema.tables
|
|
424
|
+
WHERE table_schema = 'auth' AND table_name = 'users'
|
|
425
|
+
) as exists`,
|
|
426
|
+
)
|
|
427
|
+
return Boolean(result.rows[0]?.exists)
|
|
428
|
+
}
|
|
293
429
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
430
|
+
async function hasAdminUsers(query: DbQuery): Promise<boolean> {
|
|
431
|
+
const adminCount = await query(
|
|
432
|
+
`SELECT COUNT(*)::int as count FROM auth.users
|
|
433
|
+
WHERE raw_app_meta_data->>'role' IS NOT NULL
|
|
434
|
+
AND raw_app_meta_data->>'role' != 'authenticated'`,
|
|
435
|
+
)
|
|
436
|
+
const count = (adminCount.rows[0] as { count: number } | undefined)?.count ?? 0
|
|
437
|
+
return count > 0
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function composeExecQuery(
|
|
441
|
+
cwd: string,
|
|
442
|
+
compose: { project: string; composePath: string },
|
|
443
|
+
sql: string,
|
|
444
|
+
params: unknown[] = [],
|
|
445
|
+
): Promise<QueryResult> {
|
|
446
|
+
const db = readEnvValue(cwd, "POSTGRES_DB", "supatype")
|
|
447
|
+
const user = readEnvValue(cwd, "POSTGRES_USER", "supatype_admin")
|
|
448
|
+
const envFile = join(cwd, ".env")
|
|
449
|
+
const composeDir = dirname(compose.composePath)
|
|
450
|
+
const args = [
|
|
451
|
+
"compose",
|
|
452
|
+
"-p",
|
|
453
|
+
compose.project,
|
|
454
|
+
"--project-directory",
|
|
455
|
+
cwd,
|
|
456
|
+
"-f",
|
|
457
|
+
compose.composePath,
|
|
458
|
+
]
|
|
459
|
+
if (existsSync(envFile)) args.push("--env-file", envFile)
|
|
460
|
+
|
|
461
|
+
if (params.length === 0) {
|
|
462
|
+
args.push("exec", "-T", "db", "psql", "-U", user, "-d", db, "-tAc", sql)
|
|
463
|
+
const result = spawnSync("docker", args, { cwd: composeDir, encoding: "utf8" })
|
|
464
|
+
if (result.status !== 0) {
|
|
465
|
+
throw new Error((result.stderr ?? result.stdout ?? "compose psql failed").trim())
|
|
466
|
+
}
|
|
467
|
+
const text = (result.stdout ?? "").trim()
|
|
468
|
+
if (sql.trim().toUpperCase().startsWith("SELECT")) {
|
|
469
|
+
return Promise.resolve({
|
|
470
|
+
rows: parsePsqlScalarRows(sql, text),
|
|
471
|
+
rowCount: 1,
|
|
472
|
+
command: "SELECT",
|
|
473
|
+
oid: 0,
|
|
474
|
+
fields: [],
|
|
475
|
+
})
|
|
476
|
+
}
|
|
477
|
+
return Promise.resolve({
|
|
478
|
+
rows: [],
|
|
479
|
+
rowCount: 0,
|
|
480
|
+
command: "INSERT",
|
|
481
|
+
oid: 0,
|
|
482
|
+
fields: [],
|
|
299
483
|
})
|
|
484
|
+
}
|
|
300
485
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
486
|
+
args.push(
|
|
487
|
+
"exec",
|
|
488
|
+
"-T",
|
|
489
|
+
"db",
|
|
490
|
+
"psql",
|
|
491
|
+
"-U",
|
|
492
|
+
user,
|
|
493
|
+
"-d",
|
|
494
|
+
db,
|
|
495
|
+
"-v",
|
|
496
|
+
"ON_ERROR_STOP=1",
|
|
497
|
+
"-c",
|
|
498
|
+
interpolateSql(sql, params),
|
|
499
|
+
)
|
|
500
|
+
const result = spawnSync("docker", args, { cwd: composeDir, encoding: "utf8" })
|
|
501
|
+
if (result.status !== 0) {
|
|
502
|
+
throw new Error((result.stderr ?? result.stdout ?? "compose psql failed").trim())
|
|
503
|
+
}
|
|
504
|
+
const stdout = (result.stdout ?? "").trim()
|
|
505
|
+
const idMatch = stdout.match(
|
|
506
|
+
/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\s+\|\s+(.+)/i,
|
|
507
|
+
)
|
|
508
|
+
if (idMatch) {
|
|
509
|
+
return Promise.resolve({
|
|
510
|
+
rows: [{ id: idMatch[1], email: idMatch[2]?.trim() }],
|
|
511
|
+
rowCount: 1,
|
|
512
|
+
command: "INSERT",
|
|
513
|
+
oid: 0,
|
|
514
|
+
fields: [],
|
|
515
|
+
})
|
|
516
|
+
}
|
|
517
|
+
return Promise.resolve({
|
|
518
|
+
rows: [],
|
|
519
|
+
rowCount: 0,
|
|
520
|
+
command: "INSERT",
|
|
521
|
+
oid: 0,
|
|
522
|
+
fields: [],
|
|
523
|
+
})
|
|
524
|
+
}
|
|
313
525
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
} finally {
|
|
319
|
-
await pool.end()
|
|
526
|
+
function parsePsqlScalarRows(sql: string, text: string): Record<string, unknown>[] {
|
|
527
|
+
const upper = sql.toUpperCase()
|
|
528
|
+
if (upper.includes(" AS EXISTS")) {
|
|
529
|
+
return [{ exists: text === "t" }]
|
|
320
530
|
}
|
|
531
|
+
if (upper.includes(" AS COUNT")) {
|
|
532
|
+
return [{ count: Number.parseInt(text, 10) || 0 }]
|
|
533
|
+
}
|
|
534
|
+
if (text === "") return []
|
|
535
|
+
return [{ value: text }]
|
|
321
536
|
}
|
|
322
537
|
|
|
323
|
-
|
|
538
|
+
function interpolateSql(sql: string, params: unknown[]): string {
|
|
539
|
+
return sql.replace(/\$(\d+)/g, (_match, index: string) => {
|
|
540
|
+
const value = params[Number(index) - 1]
|
|
541
|
+
if (value === null || value === undefined) return "NULL"
|
|
542
|
+
if (typeof value === "number" || typeof value === "bigint") return String(value)
|
|
543
|
+
if (typeof value === "boolean") return value ? "TRUE" : "FALSE"
|
|
544
|
+
return `'${String(value).replace(/'/g, "''")}'`
|
|
545
|
+
})
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function hostComposeDbUrlFromEnv(cwd: string): string {
|
|
549
|
+
const port = readEnvValue(cwd, "SUPATYPE_DEV_DB_PORT", "54329")
|
|
550
|
+
const user = readEnvValue(cwd, "POSTGRES_USER", "supatype_admin")
|
|
551
|
+
const pass = readEnvValue(cwd, "POSTGRES_PASSWORD", "postgres")
|
|
552
|
+
const db = readEnvValue(cwd, "POSTGRES_DB", "supatype")
|
|
553
|
+
return `postgresql://${user}:${pass}@127.0.0.1:${port}/${db}?sslmode=disable`
|
|
554
|
+
}
|
|
324
555
|
|
|
325
556
|
async function importPg(): Promise<typeof import("pg")> {
|
|
326
557
|
try {
|
|
@@ -330,9 +561,3 @@ async function importPg(): Promise<typeof import("pg")> {
|
|
|
330
561
|
process.exit(1)
|
|
331
562
|
}
|
|
332
563
|
}
|
|
333
|
-
|
|
334
|
-
async function hashPassword(password: string): Promise<string> {
|
|
335
|
-
const salt = randomBytes(16).toString("hex")
|
|
336
|
-
const derived = (await scryptAsync(password, salt, 64)) as Buffer
|
|
337
|
-
return `${salt}:${derived.toString("hex")}`
|
|
338
|
-
}
|
package/src/commands/dev.ts
CHANGED
|
@@ -266,6 +266,9 @@ ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO authenticate
|
|
|
266
266
|
(e: unknown) => console.error("[supatype] Initial schema push failed:", (e as Error).message),
|
|
267
267
|
)
|
|
268
268
|
|
|
269
|
+
const { ensureFirstAdminUser } = await import("./admin.js")
|
|
270
|
+
await ensureFirstAdminUser(dbURL, { cwd })
|
|
271
|
+
|
|
269
272
|
// ── 10. Spawn supatype-server ─────────────────────────────────────────
|
|
270
273
|
|
|
271
274
|
// Resolve edge functions config: only enable Deno if a functions dir exists.
|