99 lines
2.1 KiB
JavaScript
99 lines
2.1 KiB
JavaScript
const { spawn, spawnSync } = require('child_process');
|
|
|
|
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(' ');
|
|
}
|
|
|
|
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 = {}) {
|
|
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,
|
|
});
|
|
|
|
if (result.error) {
|
|
throw result.error;
|
|
}
|
|
|
|
if (typeof result.status === 'number' && result.status !== 0) {
|
|
process.exit(result.status);
|
|
}
|
|
}
|
|
|
|
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,
|
|
});
|
|
}
|
|
|
|
function main() {
|
|
if (!process.env.DATABASE_URL) {
|
|
process.env.DATABASE_URL = 'file:./dev.db';
|
|
}
|
|
|
|
normalizeWrappedEnv('DATABASE_URL');
|
|
run('npx', ['prisma', 'migrate', 'deploy']);
|
|
|
|
const args = process.argv.slice(2);
|
|
if (args.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const child = launch(args[0], args.slice(1));
|
|
|
|
child.on('exit', (code, signal) => {
|
|
if (signal) {
|
|
process.kill(process.pid, signal);
|
|
return;
|
|
}
|
|
|
|
process.exit(code ?? 0);
|
|
});
|
|
}
|
|
|
|
main();
|