60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const rootDir = path.resolve(__dirname, '..');
|
|
const backupDir = path.join(rootDir, 'src', 'db', 'pb_data_backup');
|
|
const targetDir = path.join(rootDir, 'src', 'db', 'pb_data');
|
|
const forceReset = process.argv.includes('--reset');
|
|
|
|
const filesToCopy = ['data.db', 'logs.db'];
|
|
const transientFiles = ['data.db-shm', 'data.db-wal', 'logs.db-shm', 'logs.db-wal'];
|
|
|
|
function exists(filePath) {
|
|
return fs.existsSync(filePath);
|
|
}
|
|
|
|
function ensureDir(dirPath) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
}
|
|
|
|
function copyFile(source, target) {
|
|
fs.copyFileSync(source, target);
|
|
}
|
|
|
|
function removeIfExists(filePath) {
|
|
if (exists(filePath)) fs.unlinkSync(filePath);
|
|
}
|
|
|
|
function bootstrap() {
|
|
ensureDir(targetDir);
|
|
|
|
if (forceReset) {
|
|
filesToCopy.concat(transientFiles).forEach((name) => removeIfExists(path.join(targetDir, name)));
|
|
}
|
|
|
|
filesToCopy.forEach((fileName) => {
|
|
const sourcePath = path.join(backupDir, fileName);
|
|
const targetPath = path.join(targetDir, fileName);
|
|
|
|
if (!exists(sourcePath)) {
|
|
throw new Error(`Backup manquant: ${sourcePath}`);
|
|
}
|
|
|
|
if (!exists(targetPath) || forceReset) {
|
|
copyFile(sourcePath, targetPath);
|
|
process.stdout.write(`copied ${fileName}\n`);
|
|
} else {
|
|
process.stdout.write(`kept ${fileName}\n`);
|
|
}
|
|
});
|
|
|
|
process.stdout.write(`PocketBase bootstrap terminé (${forceReset ? 'reset' : 'safe'})\n`);
|
|
}
|
|
|
|
try {
|
|
bootstrap();
|
|
} catch (error) {
|
|
process.stderr.write(`db-bootstrap-local error: ${error.message}\n`);
|
|
process.exit(1);
|
|
}
|