Files
tools-show/server/scripts/start-with-migrate.js

99 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2026-04-11 20:46:55 +08:00
const { spawn, spawnSync } = require('child_process');
2026-04-15 13:47:39 +08:00
const IS_WINDOWS = process.platform === 'win32';
function quoteWindowsArg(value) {
const text = String(value ?? '');
if (text.length === 0) {
return '""';
}
if (!/[\s"&()^<>|]/.test(text)) {
return text;
}
return `"${text
.replace(/(\\*)"/g, '$1$1\\"')
.replace(/(\\+)$/g, '$1$1')}"`;
}
function buildWindowsCommand(command, args) {
return [command, ...args].map(quoteWindowsArg).join(' ');
}
2026-04-11 20:46:55 +08:00
function normalizeWrappedEnv(name) {
const value = process.env[name];
if (!value || value.length < 2) {
return;
}
const first = value[0];
const last = value[value.length - 1];
if ((first === '"' && last === '"') || (first === "'" && last === "'")) {
process.env[name] = value.slice(1, -1);
}
}
function run(command, args, options = {}) {
2026-04-15 13:47:39 +08:00
const result = IS_WINDOWS
? spawnSync('cmd.exe', ['/d', '/s', '/c', buildWindowsCommand(command, args)], {
stdio: 'inherit',
env: process.env,
...options,
})
: spawnSync(command, args, {
stdio: 'inherit',
env: process.env,
...options,
});
2026-04-11 20:46:55 +08:00
if (result.error) {
throw result.error;
}
if (typeof result.status === 'number' && result.status !== 0) {
process.exit(result.status);
}
}
2026-04-15 13:47:39 +08:00
function launch(command, args, options = {}) {
return IS_WINDOWS
? spawn('cmd.exe', ['/d', '/s', '/c', buildWindowsCommand(command, args)], {
stdio: 'inherit',
env: process.env,
...options,
})
: spawn(command, args, {
stdio: 'inherit',
env: process.env,
...options,
});
}
2026-04-11 20:46:55 +08:00
function main() {
if (!process.env.DATABASE_URL) {
process.env.DATABASE_URL = 'file:./dev.db';
}
normalizeWrappedEnv('DATABASE_URL');
2026-04-15 13:47:39 +08:00
run('npx', ['prisma', 'migrate', 'deploy']);
2026-04-11 20:46:55 +08:00
const args = process.argv.slice(2);
if (args.length === 0) {
return;
}
2026-04-15 13:47:39 +08:00
const child = launch(args[0], args.slice(1));
2026-04-11 20:46:55 +08:00
child.on('exit', (code, signal) => {
if (signal) {
process.kill(process.pid, signal);
return;
}
process.exit(code ?? 0);
});
}
main();