Skip to content

Commit a54324f

Browse files
committed
try running db migrations when npm deps are installed
1 parent 928b243 commit a54324f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

MyApp.Client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"typecheck": "tsc",
1212
"test": "vitest",
1313
"test:ui": "vitest --ui",
14-
"test:run": "vitest run"
14+
"test:run": "vitest run",
15+
"postinstall": "node ./postinstall.mjs"
1516
},
1617
"dependencies": {
1718
"@mdx-js/loader": "^3.1.1",

MyApp.Client/postinstall.mjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env node
2+
3+
// Run database migrations on first install
4+
5+
import { execSync } from 'child_process'
6+
import { join, dirname } from 'path'
7+
import { fileURLToPath } from 'url'
8+
9+
const __filename = fileURLToPath(import.meta.url)
10+
const __dirname = dirname(__filename)
11+
12+
function isDotnetInstalled() {
13+
try {
14+
execSync('dotnet --version', { stdio: 'ignore' })
15+
return true
16+
} catch (error) {
17+
return false
18+
}
19+
}
20+
21+
function runMigration() {
22+
const myAppPath = join(__dirname, '..', 'MyApp')
23+
24+
try {
25+
console.log('Running database migration...')
26+
execSync('dotnet run --AppTasks=migrate', {
27+
cwd: myAppPath,
28+
stdio: 'inherit'
29+
})
30+
console.log('Migration completed successfully.')
31+
} catch (error) {
32+
console.error('Migration failed:', error.message)
33+
process.exit(1)
34+
}
35+
}
36+
37+
if (isDotnetInstalled()) {
38+
runMigration()
39+
}

0 commit comments

Comments
 (0)