forked from newrelic/docs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocsAndLastDateEdited.mjs
More file actions
executable file
·106 lines (89 loc) · 3.08 KB
/
Copy pathdocsAndLastDateEdited.mjs
File metadata and controls
executable file
·106 lines (89 loc) · 3.08 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env node
import { execSync } from 'child_process';
import { glob } from 'glob10';
import { writeFile, readFileSync } from 'fs';
import { stringify } from 'csv-stringify';
import cliProgress from 'cli-progress';
console.log('Building CSV file...');
console.time('csvCreation');
const allDocs = await glob('src/content/docs/**/*.mdx', {
ignore: [
'**/index.mdx',
'src/content/docs/release-notes/**/*.mdx',
'src/content/docs/style-guide/**/*.mdx',
'src/content/whats-new/**/*.mdx',
'src/content/docs/security/new-relic-security/security-bulletins/**/*.mdx',
],
});
const freshnessFrontmatterKey = 'freshnessValidatedDate: ';
/**
* Reads a doc file and extracts the `freshnessValidatedDate` value from frontmatter
* @param {String} doc - doc file path
* @returns {String} freshnessDate - date string | `never` | null
**/
const getFreshnessFrontmatter = (doc) => {
// split the doc using the frontmatter delimiter and keep the frontmatter data
const frontmatter = readFileSync(doc, 'utf-8').split('---')[1];
let freshnessDate;
if (frontmatter?.includes(freshnessFrontmatterKey)) {
freshnessDate = frontmatter
.split('\n')
// find the line with the freshnessDate, there should always be one, and remove the key
.filter((string) => string.includes(freshnessFrontmatterKey))[0]
.replace(freshnessFrontmatterKey, '');
} else {
freshnessDate = null;
}
return freshnessDate?.includes('never') | (freshnessDate === null)
? freshnessDate
: new Date(freshnessDate).toDateString();
};
const progressBar = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_classic
);
progressBar.start(allDocs.length, 0);
const allDocsAndDates = allDocs.map((doc, i) => {
// get date of most recent commit
const gitLogModifiedDate = execSync(
`git log -1 --pretty=format:%aI ${doc}`
).toString();
const dateModified = new Date(gitLogModifiedDate).toDateString();
const freshnessDate = getFreshnessFrontmatter(doc);
// get author and date for each commit in last 180 days. split into an array
const authorData = execSync(
`git log --format=%an,%aI --since=180.days ${doc}`
)
.toString()
.split('\n');
// remove empty element from trailing new line
authorData.pop();
// shorten date to YYYY-MM-DD and return author data formatted for readability
const authorsAndDates = authorData.map((author) => {
const [name, date] = author.split(',');
return `[${name} - ${date?.slice(0, 10)}]`;
});
progressBar.update(i + 1);
return [doc, freshnessDate, dateModified, authorsAndDates.toString()];
});
const headers = [
'Doc',
'Freshness Date',
'Last Modified Date',
'Recent Authors',
];
allDocsAndDates.unshift(headers);
stringify(allDocsAndDates, function (err, output) {
writeFile('docsLastModifiedDate.csv', output, 'utf8', function (err) {
if (err) {
console.log(
'Some error occurred - file either not saved or corrupted file saved.'
);
} else {
console.log('\n');
console.timeEnd('csvCreation');
console.log('CSV Created!');
progressBar.stop();
}
});
});