-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathrefactor_reading_time.js
More file actions
46 lines (38 loc) · 1.6 KB
/
Copy pathrefactor_reading_time.js
File metadata and controls
46 lines (38 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const fs = require('fs');
const path = require('path');
const dir = 'e:/GSsoc contribution/openCSE/app';
function findFiles(dir, filter, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isDirectory()) {
findFiles(filePath, filter, fileList);
} else if (filter.test(filePath)) {
fileList.push(filePath);
}
});
return fileList;
}
const pageFiles = findFiles(dir, /page\.tsx$/);
pageFiles.forEach(file => {
let content = fs.readFileSync(file, 'utf8');
if (content.includes('<ReadingTime chapterKey={chapter.id} />')) {
// Check if it already has the new format to prevent double running
if (content.includes('gap-x-4')) {
return;
}
let newContent = content.replace(
/<div className="flex items-center justify-between">\s*<p className=\{`text-2xl mt-\[-8px\] \$\{righteous\.className\}`\}>\s*\{chapter\.title\}\s*<\/p>/g,
`<div className="flex flex-wrap items-start justify-between gap-y-2">\n <div className="flex flex-wrap items-center gap-x-4 gap-y-2 mt-[-8px]">\n <p className={\`text-2xl \${righteous.className}\`}>\n {chapter.title}\n </p>\n <ReadingTime chapterKey={chapter.id} />\n </div>`
);
newContent = newContent.replace(
/<\/div>\s*<ReadingTime chapterKey=\{chapter\.id\} \/>/g,
`</div>`
);
if (content !== newContent) {
fs.writeFileSync(file, newContent, 'utf8');
console.log('Updated:', file);
}
}
});