-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-cleanup.ts
executable file
·141 lines (131 loc) · 4.06 KB
/
batch-cleanup.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
#!/usr/bin/env bun
const { ArgumentParser } = require("argparse");
import async from "async";
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";
const parser = new ArgumentParser({
description: "Batch clean up frontmatters in posts.",
});
parser.add_argument("-w", "--write", {
default: false,
action: "store_true",
help: "whether to write the `md` files with 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;
// 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;
vfile.data.matter = normalize_frontmatter(frontmatter);
// whether we need to write the file back to disk
vfile.data.skip = isDeepStrictEqual(
normalize_frontmatter(vfile.data.orig as PostFrontmatter),
vfile.data.matter
);
return vfile;
})
.then((vfile) => {
// debug printer
if (vfile.data.skip) {
console.log(`${vfile.basename} skipped`);
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());
}
);