88const fs = require ( 'fs' ) ;
99const path = require ( 'path' ) ;
1010const { output } = require ( '../core/formatting' ) ;
11+ const { loadJournalEntries, saveJournalEntry, getCurrentSession } = require ( '../utils/journal-utils' ) ;
1112
1213/**
1314 * Initialize paths required by the PRD command
@@ -452,9 +453,11 @@ function buildPRDContext(prd) {
452453 * Generate initial journal entries from PRD
453454 */
454455function generateJournalFromPRD ( prd ) {
456+ const baseTimestamp = Date . now ( ) ; // Capture base timestamp
457+
455458 // Add PRD context entry
456459 const contextEntry = {
457- id : Date . now ( ) ,
460+ id : baseTimestamp , // Use base
458461 timestamp : new Date ( ) . toISOString ( ) ,
459462 type : 'context' ,
460463 content : `Project initialized: ${ prd . title } . Goals: ${ prd . goals . length } , Features: ${ prd . features . length } , Requirements: ${ prd . requirements . length } ` ,
@@ -464,57 +467,29 @@ function generateJournalFromPRD(prd) {
464467 } ;
465468
466469 // Add goal entries
470+ const goalEntries = [ ] ;
467471 prd . goals . forEach ( ( goal , i ) => {
468472 const goalEntry = {
469- id : Date . now ( ) + i + 1 ,
473+ id : baseTimestamp + 1000 + i , // Offset significantly to avoid collision with contextEntry and other goal entries
470474 timestamp : new Date ( ) . toISOString ( ) ,
471475 type : 'idea' ,
472476 content : `Project Goal: ${ goal } ` ,
473477 tags : [ 'prd' , 'goal' ] ,
474478 files : [ ] ,
475479 session : getCurrentSession ( )
476480 } ;
477- saveJournalEntry ( goalEntry ) ;
481+ goalEntries . push ( goalEntry ) ; // Collect goal entries before saving
478482 } ) ;
479483
484+ // Save entries - goals first, then context. Order can be debated but this is fine.
485+ goalEntries . forEach ( entry => saveJournalEntry ( entry ) ) ;
480486 saveJournalEntry ( contextEntry ) ;
481487}
482488
483- /**
484- * Get current session identifier
485- */
486- function getCurrentSession ( ) {
487- const now = new Date ( ) ;
488- return `${ now . getFullYear ( ) } -${ now . getMonth ( ) + 1 } -${ now . getDate ( ) } -${ now . getHours ( ) } ` ;
489- }
490-
491- /**
492- * Save a journal entry (helper function)
493- */
494- function saveJournalEntry ( entry ) {
495- const journalDir = path . join ( process . cwd ( ) , '.tasktracker' , 'journal' ) ;
496- if ( ! fs . existsSync ( journalDir ) ) {
497- fs . mkdirSync ( journalDir , { recursive : true } ) ;
498- }
499-
500- const journalFile = path . join ( journalDir , 'entries.json' ) ;
501- let entries = [ ] ;
502-
503- if ( fs . existsSync ( journalFile ) ) {
504- try {
505- entries = JSON . parse ( fs . readFileSync ( journalFile , 'utf8' ) ) ;
506- } catch ( e ) {
507- entries = [ ] ;
508- }
509- }
510-
511- entries . push ( entry ) ;
512- fs . writeFileSync ( journalFile , JSON . stringify ( entries , null , 2 ) ) ;
513- }
514-
515489module . exports = {
516490 initPaths,
517491 parsePRD,
518492 showPRD,
519- generatePRDContext
493+ generatePRDContext,
494+ loadPRD
520495} ;
0 commit comments