Skip to content

Commit 27a3018

Browse files
authored
Merge pull request #209 from cipherstash/fix-ci
chore: 🔧 PR_BYPASS bundle drizzle migration CLI with tsup
2 parents 5ee8694 + 0c8257a commit 27a3018

File tree

4 files changed

+164
-137
lines changed

4 files changed

+164
-137
lines changed

packages/drizzle/bin/generate-eql-migration.js

Lines changed: 0 additions & 128 deletions
This file was deleted.

packages/drizzle/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"author": "CipherStash <hello@cipherstash.com>",
2323
"type": "module",
2424
"bin": {
25-
"generate-eql-migration": "./bin/generate-eql-migration.js"
25+
"generate-eql-migration": "./dist/bin/generate-eql-migration.js"
2626
},
2727
"exports": {
2828
"./pg": {
@@ -33,12 +33,12 @@
3333
},
3434
"files": [
3535
"dist",
36-
"bin",
3736
"README.md"
3837
],
3938
"scripts": {
4039
"build": "tsup",
4140
"dev": "tsup --watch",
41+
"postbuild": "chmod +x ./dist/bin/generate-eql-migration.js",
4242
"test": "vitest run",
4343
"release": "tsup"
4444
},
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
})

packages/drizzle/tsup.config.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import { defineConfig } from 'tsup'
22

3-
export default defineConfig({
4-
entry: ['src/pg/index.ts'],
5-
outDir: 'dist/pg',
6-
format: ['cjs', 'esm'],
7-
sourcemap: true,
8-
dts: true,
9-
})
3+
export default defineConfig([
4+
{
5+
entry: ['src/pg/index.ts'],
6+
outDir: 'dist/pg',
7+
format: ['cjs', 'esm'],
8+
sourcemap: true,
9+
dts: true,
10+
},
11+
{
12+
entry: ['src/bin/generate-eql-migration.ts'],
13+
outDir: 'dist/bin',
14+
format: ['cjs', 'esm'],
15+
target: 'esnext',
16+
clean: true,
17+
splitting: true,
18+
minify: true,
19+
shims: true,
20+
banner: {
21+
js: '#!/usr/bin/env node',
22+
},
23+
},
24+
])

0 commit comments

Comments
 (0)