|
| 1 | +import { execSync } from 'node:child_process' |
| 2 | +import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs' |
| 3 | +import { readdir } from 'node:fs/promises' |
| 4 | +import { dirname, join, resolve } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | + |
| 7 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 8 | + |
| 9 | +type CliArgs = { |
| 10 | + migrationName: string |
| 11 | + drizzleDir: string |
| 12 | + showHelp: boolean |
| 13 | +} |
| 14 | + |
| 15 | +function parseArgs(argv: string[]): CliArgs { |
| 16 | + let migrationName = 'install-eql' |
| 17 | + let drizzleDir = 'drizzle' |
| 18 | + let showHelp = false |
| 19 | + |
| 20 | + for (let i = 0; i < argv.length; i++) { |
| 21 | + const arg = argv[i] |
| 22 | + if (arg === '--help' || arg === '-h') { |
| 23 | + showHelp = true |
| 24 | + } else if (arg === '--name' || arg === '-n') { |
| 25 | + migrationName = argv[++i] ?? migrationName |
| 26 | + } else if (arg === '--out' || arg === '-o') { |
| 27 | + drizzleDir = argv[++i] ?? drizzleDir |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return { migrationName, drizzleDir, showHelp } |
| 32 | +} |
| 33 | + |
| 34 | +function printHelp(): void { |
| 35 | + console.log(` |
| 36 | +Usage: generate-eql-migration [options] |
| 37 | +
|
| 38 | +Generate a Drizzle migration that installs CipherStash EQL |
| 39 | +
|
| 40 | +Options: |
| 41 | + -n, --name <name> Migration name (default: "install-eql") |
| 42 | + -o, --out <dir> Output directory (default: "drizzle") |
| 43 | + -h, --help Display this help message |
| 44 | +
|
| 45 | +Examples: |
| 46 | + npx generate-eql-migration |
| 47 | + npx generate-eql-migration --name setup-eql |
| 48 | + npx generate-eql-migration --out migrations |
| 49 | + |
| 50 | + # Or with your package manager: |
| 51 | + pnpm generate-eql-migration |
| 52 | + yarn generate-eql-migration |
| 53 | + bun generate-eql-migration |
| 54 | +`) |
| 55 | +} |
| 56 | + |
| 57 | +async function main(): Promise<void> { |
| 58 | + let migrationPath: string | null = null |
| 59 | + const args = parseArgs(process.argv.slice(2)) |
| 60 | + |
| 61 | + if (args.showHelp) { |
| 62 | + printHelp() |
| 63 | + process.exit(0) |
| 64 | + } |
| 65 | + |
| 66 | + console.log('🔐 Generating EQL migration for Drizzle...\n') |
| 67 | + |
| 68 | + try { |
| 69 | + console.log(`📝 Generating custom migration: ${args.migrationName}`) |
| 70 | + execSync(`npx drizzle-kit generate --custom --name=${args.migrationName}`, { |
| 71 | + stdio: 'inherit', |
| 72 | + }) |
| 73 | + } catch (error) { |
| 74 | + console.error('❌ Failed to generate custom migration') |
| 75 | + console.error('Make sure drizzle-kit is installed in your project.') |
| 76 | + process.exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + const schemaPackagePath = resolve(__dirname, '../../schema') |
| 81 | + const sqlFileName = 'cipherstash-encrypt-2-1-8.sql' |
| 82 | + const sqlSourcePath = join(schemaPackagePath, sqlFileName) |
| 83 | + |
| 84 | + if (!existsSync(sqlSourcePath)) { |
| 85 | + throw new Error(`Could not find EQL SQL file at: ${sqlSourcePath}`) |
| 86 | + } |
| 87 | + |
| 88 | + const eqlSql = readFileSync(sqlSourcePath, 'utf-8') |
| 89 | + |
| 90 | + const drizzlePath = resolve(process.cwd(), args.drizzleDir) |
| 91 | + if (!existsSync(drizzlePath)) { |
| 92 | + throw new Error( |
| 93 | + `Drizzle directory not found: ${drizzlePath}\nMake sure to run this command from your project root.`, |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + const files = await readdir(drizzlePath) |
| 98 | + const migrationFile = files |
| 99 | + .filter( |
| 100 | + (file) => file.endsWith('.sql') && file.includes(args.migrationName), |
| 101 | + ) |
| 102 | + .sort() |
| 103 | + .pop() |
| 104 | + |
| 105 | + if (!migrationFile) { |
| 106 | + throw new Error( |
| 107 | + `Could not find migration file for: ${args.migrationName}\nLooked in: ${drizzlePath}`, |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + migrationPath = join(drizzlePath, migrationFile) |
| 112 | + console.log(`\n📄 Writing EQL SQL to: ${migrationFile}`) |
| 113 | + |
| 114 | + writeFileSync(migrationPath, eqlSql, 'utf-8') |
| 115 | + |
| 116 | + console.log('\n✅ Successfully created EQL migration!') |
| 117 | + console.log('\nNext steps:') |
| 118 | + console.log(` 1. Review the migration: ${migrationPath}`) |
| 119 | + console.log(' 2. Run migrations: npx drizzle-kit migrate') |
| 120 | + console.log( |
| 121 | + ' (or use your package manager: pnpm/yarn/bun drizzle-kit migrate)', |
| 122 | + ) |
| 123 | + } catch (error) { |
| 124 | + if (migrationPath && existsSync(migrationPath)) { |
| 125 | + try { |
| 126 | + unlinkSync(migrationPath) |
| 127 | + console.error(`\n🗑️ Cleaned up migration file: ${migrationPath}`) |
| 128 | + } catch (cleanupError) { |
| 129 | + console.error(`\n⚠️ Failed to cleanup migration file: ${migrationPath}`) |
| 130 | + } |
| 131 | + } |
| 132 | + throw error |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void main().catch((error: unknown) => { |
| 137 | + const message = error instanceof Error ? error.message : String(error) |
| 138 | + console.error('❌ Error:', message) |
| 139 | + process.exit(1) |
| 140 | +}) |
0 commit comments