Skip to content

Commit 51d87d7

Browse files
committed
examples(book-mode-conference): add progress/resume support
- Introduce a simple progress manager inside the example - Support RESUME_MODE env or --resume flag to continue - Persist per-session note creation and restore URLs on resume - Save progress after each note; include book URL on success
1 parent 8a17d11 commit 51d87d7

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

examples/book-mode-conference/index.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,57 @@ ${bookContent}
319319
// MAIN EXECUTION LOGIC
320320
// ==========================================
321321

322+
// Simple reusable progress manager
323+
type ProgressState = {
324+
completedSessions: string[]
325+
sessionNotes: Record<string, string>
326+
mainBookCreated?: boolean
327+
mainBookUrl?: string
328+
startedAt?: string
329+
completedAt?: string
330+
}
331+
332+
function createProgressManager(progressFilePath: string) {
333+
const resolvedPath = path.resolve(progressFilePath)
334+
335+
function load(): ProgressState | null {
336+
if (!fs.existsSync(resolvedPath)) return null
337+
try {
338+
const data = JSON.parse(fs.readFileSync(resolvedPath, 'utf8'))
339+
return data
340+
} catch (e: any) {
341+
console.warn(`⚠️ Failed to load progress: ${e.message}`)
342+
return null
343+
}
344+
}
345+
346+
function initFresh(): ProgressState {
347+
if (fs.existsSync(resolvedPath)) {
348+
try { fs.unlinkSync(resolvedPath) } catch {}
349+
}
350+
return {
351+
completedSessions: [],
352+
sessionNotes: {},
353+
startedAt: new Date().toISOString(),
354+
}
355+
}
356+
357+
function save(progress: ProgressState) {
358+
try { fs.writeFileSync(resolvedPath, JSON.stringify(progress, null, 2)) } catch {}
359+
}
360+
361+
function isSessionDone(id: string, p: ProgressState) {
362+
return p.completedSessions.includes(id)
363+
}
364+
365+
function markSessionDone(id: string, noteUrl: string, p: ProgressState) {
366+
if (!p.completedSessions.includes(id)) p.completedSessions.push(id)
367+
p.sessionNotes[id] = noteUrl
368+
}
369+
370+
return { load, initFresh, save, isSessionDone, markSessionDone, progressFilePath: resolvedPath }
371+
}
372+
322373
/**
323374
* Main function that orchestrates the entire book mode note creation process
324375
*/
@@ -340,9 +391,32 @@ async function main(): Promise<void> {
340391
const sessionList = loadAndProcessSessions()
341392
console.log(`Processing ${sessionList.length} sessions...`)
342393

394+
// Progress/resume support
395+
const pm = createProgressManager(path.join(__dirname, 'progress.json'))
396+
const RESUME_MODE = process.env.RESUME_MODE === 'true' || process.argv.includes('--resume')
397+
let progress: ProgressState | null = null
398+
if (RESUME_MODE) {
399+
progress = pm.load()
400+
if (!progress) {
401+
console.error('No progress.json found. Start without --resume to create it.')
402+
process.exit(1)
403+
}
404+
console.log(`🔄 Resume mode: ${progress.completedSessions.length} sessions already created`)
405+
} else {
406+
progress = pm.initFresh()
407+
console.log('🚀 Fresh run: progress initialized')
408+
}
409+
343410
// Create individual session notes
344411
console.log('\n=== Creating Individual Session Notes ===')
345412
for (let data of sessionList) {
413+
if (pm.isSessionDone(data.id, progress!)) {
414+
// restore URL
415+
if (progress!.sessionNotes[data.id]) data.noteUrl = progress!.sessionNotes[data.id].replace(`${getHackMDHost()}/`, '')
416+
console.log(`⏭️ Skip existing: ${data.title}`)
417+
continue
418+
}
419+
346420
const noteContent = generateSessionNoteContent(data)
347421

348422
const noteData = {
@@ -355,6 +429,8 @@ async function main(): Promise<void> {
355429
try {
356430
const note = await api.createTeamNote(TEAM_PATH, noteData)
357431
data.noteUrl = note.shortId
432+
pm.markSessionDone(data.id, `${getHackMDHost()}/${note.shortId}`, progress!)
433+
pm.save(progress!)
358434
console.log(`✓ Created note for: ${data.title}`)
359435
} catch (error: any) {
360436
console.error(`✗ Failed to create note for ${data.title}:`, error.message)
@@ -395,6 +471,12 @@ async function main(): Promise<void> {
395471
console.log(`✓ Book URL: ${hackmdHost}/${mainBook.shortId}`)
396472
console.log('\n🎉 Book mode conference notes created successfully!')
397473
console.log(`📚 Main book contains links to ${sessionUrls.length} session notes`)
474+
if (progress) {
475+
progress.mainBookCreated = true
476+
progress.mainBookUrl = `${hackmdHost}/${mainBook.shortId}`
477+
progress.completedAt = new Date().toISOString()
478+
pm.save(progress)
479+
}
398480
} catch (error: any) {
399481
console.error('✗ Failed to create main book:', error.message)
400482
}

0 commit comments

Comments
 (0)