Add a seed script
This commit is contained in:
parent
1a7e93d74d
commit
9fff95e786
2 changed files with 23 additions and 0 deletions
3
justfile
3
justfile
|
|
@ -1,6 +1,9 @@
|
||||||
default:
|
default:
|
||||||
@just --list
|
@just --list
|
||||||
|
|
||||||
|
seed secret="dev" name="":
|
||||||
|
bun src/seed.ts "{{secret}}" "{{name}}"
|
||||||
|
|
||||||
dev:
|
dev:
|
||||||
bun run dev
|
bun run dev
|
||||||
|
|
||||||
|
|
|
||||||
20
src/seed.ts
Normal file
20
src/seed.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#!/usr/bin/env bun
|
||||||
|
|
||||||
|
// Idempotent device seeding
|
||||||
|
|
||||||
|
import { createDevice, getDeviceBySecret, initDb } from "./db";
|
||||||
|
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
const secret = args[0] || "dev";
|
||||||
|
const name = args[1] || null;
|
||||||
|
|
||||||
|
initDb();
|
||||||
|
|
||||||
|
const existing = getDeviceBySecret(secret);
|
||||||
|
if (existing) {
|
||||||
|
console.log(`device already exists: id=${existing.id} secret=${existing.secret} name=${existing.name}`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
const device = createDevice(secret, name);
|
||||||
|
console.log(`created device: id=${device.id} secret=${device.secret} name=${device.name}`);
|
||||||
Loading…
Reference in a new issue