-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix-staleness-dates.js
More file actions
57 lines (48 loc) · 1.66 KB
/
Copy pathfix-staleness-dates.js
File metadata and controls
57 lines (48 loc) · 1.66 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
47
48
49
50
51
52
53
54
55
56
57
const fs = require("fs");
const path = require("path");
const sixMonthsAgo = new Date();
sixMonthsAgo.setMonth(sixMonthsAgo.getMonth() - 6);
function findMarkdownFiles(dir = ".") {
const files = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory() && !entry.name.startsWith(".")) {
files.push(...findMarkdownFiles(path.join(dir, entry.name)));
} else if (entry.name.endsWith(".md") && entry.isFile()) {
files.push(path.join(dir, entry.name));
}
}
return files;
}
const files = findMarkdownFiles();
const changes = [];
const today = new Date().toISOString().split("T")[0];
files.forEach((file) => {
let content = fs.readFileSync(file, "utf-8");
const frontmatterMatch = content.match(/^---\n([^]*?)\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const lastUpdatedMatch = frontmatter.match(
/last_updated:\s*["']?([^"'\n]+)/,
);
if (lastUpdatedMatch) {
const lastUpdated = new Date(lastUpdatedMatch[1]);
if (lastUpdated < sixMonthsAgo) {
const newFrontmatter = frontmatter.replace(
/last_updated:\s*["']?[^"'\n]+/,
'last_updated: "' + today + '"',
);
const newContent = content.replace(
frontmatterMatch[0],
"---\n" + newFrontmatter + "\n---",
);
changes.push({ file, type: "staleness-update", lastUpdated });
if (process.env.DRY_RUN !== "true") {
fs.writeFileSync(file, newContent);
}
}
}
}
});
console.log("Staleness updates:", changes.length, "files");
console.log(JSON.stringify(changes, null, 2));