Files
dashboard-cpsp-executive/backend/dev.mjs
T
2026-09-08 15:21:23 +07:00

57 lines
1.4 KiB
JavaScript

/**
* Run Django management commands from this backend folder.
* Usage:
* node dev.mjs # runserver 0.0.0.0:8001
* 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:8001']
: ['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);
});