|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +const sourceDir = path.resolve(__dirname, '../src/database') |
| 7 | +const outputDir = process.env.MOCK_FILE_DB_PATH || path.resolve(__dirname, '../data') |
| 8 | +const forceOverwrite = process.argv.includes('--force') |
| 9 | + |
| 10 | +const collectionNamingFields = { |
| 11 | + pages: ['name'], |
| 12 | + apps: ['name'], |
| 13 | + blocks: ['label', 'name'], |
| 14 | + blockGroups: ['name'], |
| 15 | + blockCategories: ['name'] |
| 16 | +} |
| 17 | + |
| 18 | +function sanitizeFileName (name) { |
| 19 | + if (!name) { |
| 20 | + return '' |
| 21 | + } |
| 22 | + |
| 23 | + const normalized = String(name) |
| 24 | + .trim() |
| 25 | + .replace(/\s+/g, '-') |
| 26 | + .replace(/[<>:"/\\|?*]/g, '-') |
| 27 | + .replace(/-+/g, '-') |
| 28 | + .replace(/^\.+/, '') |
| 29 | + .replace(/\.+$/, '') |
| 30 | + .slice(0, 120) |
| 31 | + |
| 32 | + if (!normalized || normalized === '.' || normalized === '..') { |
| 33 | + return '' |
| 34 | + } |
| 35 | + |
| 36 | + return normalized |
| 37 | +} |
| 38 | + |
| 39 | +function resolveFileBaseName (doc, collectionName, usedNames) { |
| 40 | + const namingFields = collectionNamingFields[collectionName] || ['name', 'label'] |
| 41 | + let preferred = '' |
| 42 | + |
| 43 | + for (const field of namingFields) { |
| 44 | + const value = sanitizeFileName(doc[field]) |
| 45 | + if (value) { |
| 46 | + preferred = value |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (!preferred) { |
| 52 | + preferred = sanitizeFileName(doc.id) || sanitizeFileName(doc._id) || 'record' |
| 53 | + } |
| 54 | + |
| 55 | + const idSuffix = String(doc._id || doc.id || 'item').slice(0, 6) |
| 56 | + const normalizedPreferred = preferred.toLowerCase() |
| 57 | + if (!usedNames.has(normalizedPreferred)) { |
| 58 | + usedNames.add(normalizedPreferred) |
| 59 | + return preferred |
| 60 | + } |
| 61 | + |
| 62 | + let attempt = `${preferred}-${idSuffix}` |
| 63 | + let seq = 2 |
| 64 | + |
| 65 | + while (usedNames.has(attempt.toLowerCase())) { |
| 66 | + attempt = `${preferred}-${idSuffix}-${seq}` |
| 67 | + seq += 1 |
| 68 | + } |
| 69 | + |
| 70 | + usedNames.add(attempt.toLowerCase()) |
| 71 | + return attempt |
| 72 | +} |
| 73 | + |
| 74 | +function parseDbFile (dbPath) { |
| 75 | + const content = fs.readFileSync(dbPath, 'utf8') |
| 76 | + return content |
| 77 | + .split('\n') |
| 78 | + .map((line) => line.trim()) |
| 79 | + .filter(Boolean) |
| 80 | + .map((line, index) => { |
| 81 | + try { |
| 82 | + return JSON.parse(line) |
| 83 | + } catch (error) { |
| 84 | + throw new Error(`Failed to parse ${path.basename(dbPath)} line ${index + 1}: ${error.message}`) |
| 85 | + } |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +function ensureDirectory (dirPath) { |
| 90 | + fs.mkdirSync(dirPath, { recursive: true }) |
| 91 | +} |
| 92 | + |
| 93 | +function cleanCollectionDirectory (collectionPath) { |
| 94 | + if (!fs.existsSync(collectionPath)) { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + const files = fs.readdirSync(collectionPath) |
| 99 | + for (const fileName of files) { |
| 100 | + if (fileName.endsWith('.json')) { |
| 101 | + fs.unlinkSync(path.join(collectionPath, fileName)) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function exportCollection (dbFile) { |
| 107 | + const collectionName = path.basename(dbFile, '.db') |
| 108 | + const dbPath = path.join(sourceDir, dbFile) |
| 109 | + const collectionPath = path.join(outputDir, collectionName) |
| 110 | + const docs = parseDbFile(dbPath) |
| 111 | + |
| 112 | + ensureDirectory(collectionPath) |
| 113 | + if (forceOverwrite) { |
| 114 | + cleanCollectionDirectory(collectionPath) |
| 115 | + } |
| 116 | + |
| 117 | + let written = 0 |
| 118 | + let skipped = 0 |
| 119 | + const usedNames = new Set() |
| 120 | + |
| 121 | + for (const doc of docs) { |
| 122 | + if (!doc._id && doc.id !== undefined) { |
| 123 | + doc._id = String(doc.id) |
| 124 | + } |
| 125 | + |
| 126 | + const fileBaseName = resolveFileBaseName(doc, collectionName, usedNames) |
| 127 | + const filePath = path.join(collectionPath, `${fileBaseName}.json`) |
| 128 | + |
| 129 | + if (!forceOverwrite && fs.existsSync(filePath)) { |
| 130 | + skipped += 1 |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + fs.writeFileSync(filePath, JSON.stringify(doc, null, 2), 'utf8') |
| 135 | + written += 1 |
| 136 | + } |
| 137 | + |
| 138 | + return { collectionName, total: docs.length, written, skipped } |
| 139 | +} |
| 140 | + |
| 141 | +function run () { |
| 142 | + if (!fs.existsSync(sourceDir)) { |
| 143 | + throw new Error(`Database directory not found: ${sourceDir}`) |
| 144 | + } |
| 145 | + |
| 146 | + ensureDirectory(outputDir) |
| 147 | + |
| 148 | + const dbFiles = fs.readdirSync(sourceDir).filter((fileName) => fileName.endsWith('.db')) |
| 149 | + |
| 150 | + if (dbFiles.length === 0) { |
| 151 | + console.log('No .db files found, nothing to export.') |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + const summary = dbFiles.map(exportCollection) |
| 156 | + |
| 157 | + console.log(`Export complete. Output: ${outputDir}`) |
| 158 | + summary.forEach((item) => { |
| 159 | + console.log( |
| 160 | + `- ${item.collectionName}: total=${item.total}, written=${item.written}, skipped=${item.skipped}` |
| 161 | + ) |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +try { |
| 166 | + run() |
| 167 | +} catch (error) { |
| 168 | + console.error(error.message) |
| 169 | + process.exit(1) |
| 170 | +} |
0 commit comments