todo.js (1575B)
1 const fs = require('fs'); 2 const path = require('path'); 3 4 const DATA_REPO = path.join(__dirname, '..', 'data', 'repo'); 5 const TODAY_PATH = path.join(DATA_REPO, 'today.md'); 6 const BACKLOG_PATH = path.join(DATA_REPO, 'backlog.md'); 7 const ARCHIVE_DIR = path.join(DATA_REPO, 'archive'); 8 9 function todayDate() { 10 return new Date().toISOString().slice(0, 10); 11 } 12 13 function readToday() { 14 if (!fs.existsSync(TODAY_PATH)) return ''; 15 return fs.readFileSync(TODAY_PATH, 'utf8'); 16 } 17 18 function readBacklog() { 19 if (!fs.existsSync(BACKLOG_PATH)) return ''; 20 return fs.readFileSync(BACKLOG_PATH, 'utf8'); 21 } 22 23 function writeToday(content) { 24 fs.writeFileSync(TODAY_PATH, content, 'utf8'); 25 } 26 27 function writeBacklog(content) { 28 fs.writeFileSync(BACKLOG_PATH, content, 'utf8'); 29 } 30 31 // Archives today.md under archive/YYYY-MM-DD.md and resets it for today. 32 // Returns true if a rotation occurred, false if today.md is already current. 33 function rotateTodayIfStale() { 34 if (!fs.existsSync(TODAY_PATH)) return false; 35 const content = fs.readFileSync(TODAY_PATH, 'utf8'); 36 const match = content.match(/^# Todo — (d{4}-d{2}-d{2})/); 37 if (!match) return false; 38 39 const fileDate = match[1]; 40 const today = todayDate(); 41 if (fileDate === today) return false; 42 43 fs.mkdirSync(ARCHIVE_DIR, { recursive: true }); 44 fs.writeFileSync(path.join(ARCHIVE_DIR, `${fileDate}.md`), content, 'utf8'); 45 fs.writeFileSync(TODAY_PATH, `# Todo — ${today}nn## Tasksnn## Donen`, 'utf8'); 46 47 return true; 48 } 49 50 module.exports = { readToday, readBacklog, writeToday, writeBacklog, rotateTodayIfStale };