57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
/**
|
|
* Run Django management commands from this backend folder.
|
|
* Usage:
|
|
* node dev.mjs # runserver 0.0.0.0:8000
|
|
* node dev.mjs migrate
|
|
* node dev.mjs seed_demo
|
|
*/
|
|
import { spawn } from 'node:child_process';
|
|
import { existsSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const djangoRoot = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
if (!existsSync(path.join(djangoRoot, 'manage.py'))) {
|
|
console.error('[backend] manage.py not found.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const extra = process.argv.slice(2);
|
|
const manageArgs =
|
|
extra.length === 0
|
|
? ['manage.py', 'runserver', '0.0.0.0:8000']
|
|
: ['manage.py', ...extra];
|
|
|
|
function resolvePython() {
|
|
const isWin = process.platform === 'win32';
|
|
const venvPython = isWin
|
|
? path.join(djangoRoot, '.venv', 'Scripts', 'python.exe')
|
|
: path.join(djangoRoot, '.venv', 'bin', 'python');
|
|
|
|
if (existsSync(venvPython)) {
|
|
return venvPython;
|
|
}
|
|
|
|
return isWin ? 'python' : 'python3';
|
|
}
|
|
|
|
const python = resolvePython();
|
|
const child = spawn(python, manageArgs, {
|
|
cwd: djangoRoot,
|
|
stdio: 'inherit',
|
|
env: process.env,
|
|
});
|
|
|
|
child.on('error', (err) => {
|
|
console.error(`[backend] Failed to start ${python}:`, err.message);
|
|
process.exit(1);
|
|
});
|
|
|
|
child.on('exit', (code, signal) => {
|
|
if (signal) {
|
|
process.exit(1);
|
|
}
|
|
process.exit(code ?? 0);
|
|
});
|