-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathgenerate_last_updated.js
37 lines (29 loc) · 1.17 KB
/
generate_last_updated.js
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
const fs = require('fs-extra');
const { execSync } = require('child_process');
const { find_files } = require('./shared');
const generate_last_updated = async () => {
for (const file of await find_files()) {
console.log(`\n Setting the last_updated field on ${file}`);
// Fetch the last modified date, according to the git log.
const date = get_last_updated_date(file);
console.log(` last_updated: ${date}`);
// Read the content of the file
let content = await fs.readFile(file, 'utf-8');
// Replace the frontmatter last_updated field. This is not
// greedy, so it will match the first instance (which must be
// in the frontmatter).
content = content.replace(/last_updated:\s*(\S*)/, `last_updated: ${date}`);
// Overwrite the file with the updated date.
await fs.outputFile(file, content, 'utf8');
}
};
const get_last_updated_date = (path) => {
const command = `git log -1 --date=iso-strict-local ${path} | cat`;
const stdout = execSync(command).toString();
const date_string = /Date:\s+(\S*)/g.exec(stdout)[1];
const date = new Date(date_string);
return date.toISOString();
};
module.exports = {
generate_last_updated
};