Fork of github.com/did-method-plc/did-method-plc
1#!/usr/bin/env ts-node
2
3import * as fs from 'fs/promises'
4import * as path from 'path'
5
6export async function main() {
7 const now = new Date()
8 const prefix = now.toISOString().replace(/[^a-z0-9]/gi, '') // Order of migrations matches alphabetical order of their names
9 const name = process.argv[2]
10 if (!name || !name.match(/^[a-z0-9-]+$/)) {
11 process.exitCode = 1
12 return console.error(
13 'Must pass a migration name consisting of lowercase digits, numbers, and dashes.',
14 )
15 }
16 const filename = `${prefix}-${name}`
17 const dir = path.join(__dirname, '..', 'src', 'migrations')
18
19 await fs.writeFile(path.join(dir, `${filename}.ts`), template, { flag: 'wx' })
20 await fs.writeFile(
21 path.join(dir, 'index.ts'),
22 `export * as _${prefix} from './${filename}'\n`,
23 { flag: 'a' },
24 )
25}
26
27const template = `import { Kysely } from 'kysely'
28
29export async function up(db: Kysely<unknown>): Promise<void> {
30 // Migration code
31}
32
33export async function down(db: Kysely<unknown>): Promise<void> {
34 // Migration code
35}
36`
37
38main()