1
1
const fs = require ( 'fs' ) ;
2
2
3
+ // Helper function to format a date into YYYY-MM-DD format
4
+ function formatDate ( date ) {
5
+ const d = new Date ( date ) ;
6
+ if ( isNaN ( d ) ) {
7
+ return null ; // Invalid date
8
+ }
9
+ const year = d . getFullYear ( ) ;
10
+ const month = String ( d . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
11
+ const day = String ( d . getDate ( ) ) . padStart ( 2 , '0' ) ;
12
+ return `${ year } -${ month } -${ day } ` ;
13
+ }
14
+
3
15
const path = 'README.md' ;
4
16
const content = fs . readFileSync ( path , 'utf8' ) ;
5
17
const lines = content . split ( '\n' ) ;
6
18
7
- const currentDate = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ; // e.g., 2023-10-15
19
+ const currentDate = formatDate ( new Date ( ) ) ; // Get current date in YYYY-MM-DD format
8
20
const updatedLines = [ ] ;
9
21
let changesCount = 0 ;
10
22
@@ -41,7 +53,7 @@ for (let i = 0; i < lines.length; i++) {
41
53
headerLineCount ++ ;
42
54
}
43
55
44
- const parts = line . split ( '|' ) ;
56
+ const parts = line . split ( '|' ) . map ( part => part . trim ( ) ) ;
45
57
46
58
// Skip header and separator rows
47
59
if ( headerLineCount <= 2 ) {
@@ -51,8 +63,14 @@ for (let i = 0; i < lines.length; i++) {
51
63
52
64
// Handle data rows
53
65
if ( tableHasDateColumn && dateColumnIndex > 0 ) {
54
- // Check if this row needs a date
55
- const dateValue = parts [ dateColumnIndex ] . trim ( ) ;
66
+ // Ensure the row has enough columns
67
+ if ( parts . length <= dateColumnIndex ) {
68
+ console . warn ( `Skipping malformed row (not enough columns): ${ line } ` ) ;
69
+ updatedLines . push ( line ) ; // Skipping malformed row
70
+ continue ;
71
+ }
72
+
73
+ let dateValue = parts [ dateColumnIndex ] . trim ( ) ;
56
74
57
75
if ( ! dateValue || dateValue === '' ) {
58
76
// This row needs a date
@@ -63,8 +81,20 @@ for (let i = 0; i < lines.length; i++) {
63
81
changesCount ++ ;
64
82
console . log ( `Added date to row: ${ parts [ 1 ] . trim ( ) } ` ) ;
65
83
} else {
66
- // Row already has a date
67
- updatedLines . push ( line ) ;
84
+ // Check if the existing date is valid and in the correct format
85
+ const formattedDate = formatDate ( dateValue ) ;
86
+ if ( formattedDate !== dateValue ) {
87
+ // The date is not in the correct format, so we update it
88
+ parts [ dateColumnIndex ] = ` ${ formattedDate } ` ;
89
+ let newLine = parts . join ( '|' ) . trimEnd ( ) ;
90
+ if ( ! newLine . endsWith ( '|' ) ) newLine += ' |' ;
91
+ updatedLines . push ( newLine ) ;
92
+ changesCount ++ ;
93
+ console . log ( `Updated date for row: ${ parts [ 1 ] . trim ( ) } to ${ formattedDate } ` ) ;
94
+ } else {
95
+ // Row already has a valid date
96
+ updatedLines . push ( line ) ;
97
+ }
68
98
}
69
99
} else {
70
100
// No date column in this table
0 commit comments