-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (56 loc) · 1.43 KB
/
index.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
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
#!/usr/bin/env node
import { stringify } from "yaml";
import { matter } from "vfile-matter";
import { read, write } from "to-vfile";
import { globby } from "globby";
import { exec } from "child_process";
import { promisify } from "util";
import * as Fs from "node:fs/promises";
import * as Path from "node:path";
import meow from "meow";
const cli = meow(
`
Usage
$ add-last-updated <input>
Examples
$ add-last-updated starlight-content-dir
`,
{
importMeta: import.meta,
flags: {},
},
);
const INPUT_DIR = cli.input.at(0);
if (!INPUT_DIR) {
console.error("Input dir missing");
cli.showHelp();
}
const files = await globby([`${INPUT_DIR}/**/*.(md|mdx)`]);
console.log("Transforming documents…");
for (let path of files) {
const file = await read(path, "utf8");
matter(file, { strip: true });
if (!file.data.matter.lastUpdated) {
const stats = await Fs.stat(file.path);
const changeTime = stats.mtime || status.birthtime;
const mod = await promisify(exec)(
`git log -1 --format="%as" "${Path.relative(INPUT_DIR, file.path)}"`,
{
cwd: INPUT_DIR,
},
);
const time = mod.stdout.trim();
if (time) {
file.data.matter.lastUpdated = time;
}
file.value =
"---\n" +
stringify(file.data.matter) +
"---\n" +
file.value.toString("utf8");
console.log(`✅ ${file.path}`);
write(file);
} else {
console.log(`🆗 ${file.path}`);
}
}