48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs');
|
|
const { spawn } = require('child_process');
|
|
|
|
const rootDir = path.resolve(__dirname, '..');
|
|
const dbDir = path.join(rootDir, 'src', 'db');
|
|
const pbDataDir = path.join(dbDir, 'pb_data');
|
|
|
|
const fallbackUrl = 'http://127.0.0.1:8091/';
|
|
const dbUrl = process.env.DB_URL || fallbackUrl;
|
|
const url = (() => {
|
|
try {
|
|
return new URL(dbUrl);
|
|
} catch (_error) {
|
|
return new URL(fallbackUrl);
|
|
}
|
|
})();
|
|
const host = `${url.hostname}:${url.port || '8091'}`;
|
|
|
|
const binaryPath = process.platform === 'win32'
|
|
? path.join(dbDir, 'Pocketbase_0.7.5.exe')
|
|
: path.join(dbDir, 'pocketbase');
|
|
|
|
if (!fs.existsSync(binaryPath)) {
|
|
process.stderr.write(`PocketBase binaire introuvable: ${binaryPath}\n`);
|
|
process.exit(1);
|
|
}
|
|
|
|
if (!fs.existsSync(pbDataDir)) {
|
|
process.stderr.write(`Répertoire pb_data introuvable: ${pbDataDir}\n`);
|
|
process.stderr.write('Lance d\'abord: npm run db:bootstrap\n');
|
|
process.exit(1);
|
|
}
|
|
|
|
const child = spawn(binaryPath, ['serve', '--http', host, '--dir', pbDataDir], {
|
|
cwd: dbDir,
|
|
stdio: 'inherit'
|
|
});
|
|
|
|
child.on('exit', (code) => {
|
|
process.exit(code || 0);
|
|
});
|
|
|
|
child.on('error', (error) => {
|
|
process.stderr.write(`Erreur lancement PocketBase: ${error.message}\n`);
|
|
process.exit(1);
|
|
});
|