Files
SkillMate/backend/scripts/seed-skills-from-frontend.js
Claude Project Manager 4d509d255f Update changes
2025-10-16 08:24:01 +02:00

75 Zeilen
2.4 KiB
JavaScript

// Seed skills table from frontend's SKILL_HIERARCHY definition
// Usage: from backend dir: npm run seed-skills
const fs = require('fs')
const path = require('path')
const Database = require('better-sqlite3')
function loadHierarchy() {
const sharedPath = path.join(process.cwd(), '..', 'shared', 'skills.js')
if (fs.existsSync(sharedPath)) {
const sharedModule = require(sharedPath)
if (Array.isArray(sharedModule?.SKILL_HIERARCHY)) {
return sharedModule.SKILL_HIERARCHY
}
throw new Error('SKILL_HIERARCHY missing or invalid in shared/skills.js')
}
const tsPath = path.join(process.cwd(), '..', 'frontend', 'src', 'data', 'skillCategories.ts')
if (!fs.existsSync(tsPath)) {
throw new Error('No skill hierarchy definition found in shared/skills.js or frontend/src/data/skillCategories.ts')
}
const src = fs.readFileSync(tsPath, 'utf8')
let code = src
.replace(/export interface[\s\S]*?\n\}/g, '')
.replace(/export const LANGUAGE_LEVELS[\s\S]*?\n\n/, '')
.replace(/export const SKILL_HIERARCHY:[^=]*=/, 'module.exports =')
const sandbox = { module: {}, exports: {} }
require('vm').runInNewContext(code, sandbox)
const hierarchy = sandbox.module?.exports || sandbox.exports
if (!Array.isArray(hierarchy)) {
throw new Error('Parsed hierarchy is not an array')
}
return hierarchy
}
function main() {
const dbPath = path.join(process.cwd(), 'skillmate.dev.encrypted.db')
const db = new Database(dbPath)
try {
const hierarchy = loadHierarchy()
const insert = db.prepare(`
INSERT OR IGNORE INTO skills (id, name, category, description, expires_after)
VALUES (?, ?, ?, ?, ?)
`)
let count = 0
for (const cat of hierarchy) {
for (const sub of (cat.subcategories || [])) {
const categoryKey = `${cat.id}.${sub.id}`
for (const sk of (sub.skills || [])) {
const id = `${categoryKey}.${sk.id}`
const name = sk.name
const description = null
const expires = cat.id === 'certifications' ? 36 : null
const res = insert.run(id, name, categoryKey, description, expires)
if (res.changes > 0) count++
}
}
}
console.log(`✅ Seed abgeschlossen. Neue Skills eingefügt: ${count}`)
} catch (err) {
console.error('❌ Seed fehlgeschlagen:', err)
process.exitCode = 1
} finally {
db.close()
}
}
main()