mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 03:58:27 +00:00
feat(npm): auto deploy database migrations on package start
This commit is contained in:
+69
-20
@@ -1,7 +1,7 @@
|
||||
import { rm, mkdir, cp, writeFile, readdir } from 'node:fs/promises'
|
||||
import { existsSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import tailwindPlugin from 'bun-plugin-tailwind'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { cp, mkdir, readdir, rm, writeFile } from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
import { temporalWorkflow } from '../packages/workflow-core/src/bun-temporal-plugin'
|
||||
|
||||
console.log('\n🚀 Starting NPM packaging build process for all applications...\n')
|
||||
@@ -286,22 +286,56 @@ if (!binaryPath) {
|
||||
|
||||
const appJsPath = join(__dirname, '${appName}-app.js')
|
||||
|
||||
const child = spawn(binaryPath, ['run', appJsPath, ...process.argv.slice(2)], {
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
BUN_BE_BUN: '1',
|
||||
},
|
||||
})
|
||||
function startApp() {
|
||||
const child = spawn(binaryPath, ['run', appJsPath, ...process.argv.slice(2)], {
|
||||
stdio: 'inherit',
|
||||
env: {
|
||||
...process.env,
|
||||
BUN_BE_BUN: '1',
|
||||
},
|
||||
})
|
||||
|
||||
child.on('close', (code) => {
|
||||
process.exit(code ?? 0)
|
||||
})
|
||||
child.on('close', (code) => {
|
||||
process.exit(code ?? 0)
|
||||
})
|
||||
|
||||
child.on('error', (err) => {
|
||||
console.error(\`Failed to start child process:\`, err)
|
||||
process.exit(1)
|
||||
})
|
||||
child.on('error', (err) => {
|
||||
console.error(\`Failed to start child process:\`, err)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
|
||||
const prismaSchemaPath = join(__dirname, '..', 'prisma', 'schema.prisma')
|
||||
if (existsSync(prismaSchemaPath)) {
|
||||
console.log('🔄 Running database migrations...')
|
||||
const migrationProcess = spawn(
|
||||
binaryPath,
|
||||
['run', 'prisma', 'migrate', 'deploy', '--schema', './prisma/schema.prisma'],
|
||||
{
|
||||
stdio: 'inherit',
|
||||
cwd: join(__dirname, '..'),
|
||||
env: {
|
||||
...process.env,
|
||||
BUN_BE_BUN: '1',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
migrationProcess.on('close', (code) => {
|
||||
if (code !== 0) {
|
||||
console.error(\`❌ Database migration failed with code \${code}. Exiting.\`)
|
||||
process.exit(code ?? 1)
|
||||
}
|
||||
startApp()
|
||||
})
|
||||
|
||||
migrationProcess.on('error', (err) => {
|
||||
console.error('❌ Failed to run database migrations:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
} else {
|
||||
startApp()
|
||||
}
|
||||
`
|
||||
|
||||
const wrapperPath = path.join(binOutDir, `${appName}.js`)
|
||||
@@ -318,6 +352,21 @@ child.on('error', (err) => {
|
||||
recursive: true,
|
||||
})
|
||||
}
|
||||
|
||||
// Generate prisma.config.ts in the package root
|
||||
const localConfigContent = `import { defineConfig } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: './prisma/schema.prisma',
|
||||
migrations: {
|
||||
path: './prisma/migrations',
|
||||
},
|
||||
datasource: {
|
||||
url: process.env.DATABASE_URL,
|
||||
},
|
||||
})
|
||||
`
|
||||
await writeFile(path.join(mainOutDir, 'prisma.config.ts'), localConfigContent, 'utf-8')
|
||||
}
|
||||
|
||||
// Generate package.json
|
||||
@@ -338,7 +387,7 @@ child.on('error', (err) => {
|
||||
bin: {
|
||||
[appName]: `./bin/${appName}.js`,
|
||||
},
|
||||
files: ['bin', ...(hasPrisma ? ['prisma'] : [])],
|
||||
files: ['bin', ...(hasPrisma ? ['prisma', 'prisma.config.ts'] : [])],
|
||||
dependencies,
|
||||
optionalDependencies,
|
||||
repository: rootPackageJson.repository,
|
||||
@@ -377,7 +426,7 @@ await buildPlatformPackages('shumai-agent', dummyRunnerPath)
|
||||
await buildMainPackage({
|
||||
appName: 'shumai-agent',
|
||||
description: 'AI worker agent for the shumai media workspace',
|
||||
hasPrisma: false,
|
||||
hasPrisma: true,
|
||||
entrypoint: './apps/agent/src/index.ts',
|
||||
plugins: agentPlugins,
|
||||
})
|
||||
@@ -392,7 +441,7 @@ await buildPlatformPackages('shumai-transcode', dummyRunnerPath)
|
||||
await buildMainPackage({
|
||||
appName: 'shumai-transcode',
|
||||
description: 'Media transcoding worker for the shumai media workspace',
|
||||
hasPrisma: false,
|
||||
hasPrisma: true,
|
||||
entrypoint: './apps/transcode/src/index.ts',
|
||||
plugins: transcodePlugins,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user