-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-updated.ts
executable file
·166 lines (154 loc) · 4.92 KB
/
update-updated.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bun
const { ArgumentParser } = require("argparse");
import async from "async";
import { statSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { inspect, isDeepStrictEqual } from "node:util";
import { read } from "to-vfile";
import { VFile } from "vfile";
import { matter } from "vfile-matter";
import yaml from "yaml";
import { filelist } from "./lib/filelist.ts";
import { normalize_frontmatter } from "./lib/normalize_frontmatter.ts";
import { PostFrontmatter } from "./lib/schema.ts";
import { FileListItem } from "./lib/types.ts";
function padZero(num: number, targetLength: number) {
return num.toString().padStart(targetLength, "0");
}
function toISODateString(date: Date) {
return (
date.getFullYear().toString() +
"-" +
padZero(date.getMonth() + 1, 2) +
"-" +
padZero(date.getDate(), 2)
);
}
const parser = new ArgumentParser({
description: "Batch update frontmatter `updated` field in posts.",
});
parser.add_argument("-w", "--write", {
default: false,
action: "store_true",
help: "whether to write the `md` files back to a file",
});
parser.add_argument("-o", "--out", {
metavar: "FOLDER",
default: "./out",
nargs: "?",
help: "if `write` is specified, output a copy of files in `FOLDER`; set folder as `-` to overwrite the input files",
});
parser.add_argument("in", {
metavar: "INPUT",
help: "input file or folder",
});
let args = parser.parse_args();
console.log(inspect(args));
// process.exit(0);
const files = await filelist(args.in, {
filter: (name: string) => name.endsWith(".md") || name.endsWith(".mdx"),
});
// console.log(files);
async.mapLimit(
files,
10,
async (item: FileListItem) => {
// console.log(item);
return read(item.path)
.then((vfile) => {
// file reader
matter(vfile, { strip: true });
vfile.data.skip = isDeepStrictEqual(vfile.data.matter, {});
vfile.data.write = args.write;
vfile.data.modified = statSync(item.path).mtime;
// make a copy of the original frontmatter
vfile.data.orig = Object.assign({}, vfile.data.matter);
return vfile;
})
.then((vfile) => {
// transformer
if (vfile.data.skip) {
return vfile;
}
let frontmatter = vfile.data.matter as PostFrontmatter;
const orig_isodate = frontmatter.updated || frontmatter.created;
const file_isodate = toISODateString(vfile.data.modified as Date);
if (orig_isodate < file_isodate) {
console.log(orig_isodate, "=>", file_isodate);
frontmatter.updated = file_isodate;
} else vfile.data.skip = true;
vfile.data.matter = normalize_frontmatter(frontmatter);
// whether we need to write the file back to disk
// this is not good enough
// the parsed yaml is already different from the yaml frontmatter
// which may yield a false positive
// https://github.com/vfile/vfile-matter/issues/5
// vfile.data.write = vfile.data.write && !isDeepStrictEqual(
// vfile.data.orig,
// vfile.data.matter
// );
return vfile;
})
.then((vfile) => {
// debug printer
if (vfile.data.skip) {
return vfile;
}
const { orig, matter } = vfile.data;
console.log(`${inspect(orig)}\n=> ${inspect(matter)}`);
console.log("=======================");
// console.log(yaml.stringify(vfile.data.matter));
// console.log("=======================");
return vfile;
})
.then(async (vfile) => {
// writer
if (vfile.data.skip || !vfile.data.write) {
return vfile;
}
// write vfile to disk
let out_path = vfile.path;
if (args.out != "-") {
await mkdir(args.out, { recursive: true });
out_path = resolve(args.out, vfile.basename!);
}
await writeFile(
out_path,
"---\n" +
yaml.stringify(vfile.data.matter, {
lineWidth: 0,
}) +
"---\n" +
vfile.toString()
);
return vfile;
})
.catch((err) => {
console.error(`file: ${item.path}`);
console.error(err);
process.exit(1);
});
},
(err, vfiles) => {
if (err) throw err;
// filter out nulls
const vfiles_processed = (vfiles as VFile[]).filter(
(vfile) => vfile && !vfile.data.skip
);
const vfiles_written = vfiles_processed.filter(
(vfile) => vfile && vfile.data.write
);
if (args.write) {
console.log(
args.out != "-"
? `output folder: [${args.out}]`
: `output folder: (same as input)`
);
}
console.log(
`files: ${files.length}, processed: ${vfiles_processed.length}, written: ${vfiles_written.length}`
);
// console.log(contents.map((vfile) => vfile!.data.name).sort());
}
);