Skip to content

Commit 3adeeb4

Browse files
committed
Add postinstall to run db migrations on npm install
1 parent d3e80a9 commit 3adeeb4

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
@@ -8,7 +8,8 @@
88
"dtos": "npx get-dtos ts",
99
"build": "tsc && vite build",
1010
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
11-
"preview": "vite preview"
11+
"preview": "vite preview",
12+
"postinstall": "node ./postinstall.mjs"
1213
},
1314
"dependencies": {
1415
"@radix-ui/react-dialog": "^1.1.13",

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)