Files
kilo-cv/scripts/sync-profile.mjs
Tuan-Dat Tran e765551709
Some checks failed
Release / Release (push) Has been cancelled
Release / Build & Push Docker Image (push) Has been cancelled
feat: add sync-profile script to populate default CV from persons repo
2026-07-08 00:27:56 +02:00

61 lines
1.8 KiB
JavaScript

#!/usr/bin/env node
/**
* Reads cv-data.json from the persons profile directory and writes it into:
* - src/lib/cv.json (frontend default / compile-time fallback)
* - backend/db/init.js (backend DB seed on first boot)
*
* Usage:
* node scripts/sync-profile.mjs [path/to/cv-data.json]
*
* Default source path: ~/workspace/persons/tuan-dat.tran/cv-data.json
*/
import { readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { homedir } from 'os';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const root = resolve(__dirname, '..');
const sourcePath = process.argv[2]
?? resolve(homedir(), 'workspace/persons/tuan-dat.tran/cv-data.json');
let data;
try {
data = JSON.parse(readFileSync(sourcePath, 'utf8'));
} catch (e) {
console.error(`Failed to read ${sourcePath}: ${e.message}`);
process.exit(1);
}
// 1. Write src/lib/cv.json
const cvJsonPath = resolve(root, 'src/lib/cv.json');
writeFileSync(cvJsonPath, JSON.stringify(data, null, 2) + '\n');
console.log(`wrote ${cvJsonPath}`);
// 2. Rewrite the seed block in backend/db/init.js
const initPath = resolve(root, 'backend/db/init.js');
const initSrc = readFileSync(initPath, 'utf8');
const seedJson = JSON.stringify(data, null, 8)
.split('\n')
.map((line, i) => i === 0 ? line : ' ' + line)
.join('\n');
const updated = initSrc.replace(
/data: JSON\.stringify\([\s\S]*?\)\s*\}\);/,
`data: JSON.stringify(${seedJson})\n });`
);
if (updated === initSrc) {
console.error('Could not find seed block in backend/db/init.js — pattern mismatch');
process.exit(1);
}
writeFileSync(initPath, updated);
console.log(`wrote ${initPath}`);
console.log('done — rebuild Docker images and restart with a fresh volume to apply');