-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
35 lines (29 loc) · 1.14 KB
/
template.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
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts"
import { dirname } from "https://deno.land/[email protected]/path/mod.ts"
const regex = /<!--\s*{(?<path>.*)}\s*-->/g
const marp = /---\s*marp:\s*true\s*---/g
async function replace(match: RegExpMatchArray) {
const location = match[0]
const { path } = match.groups as { path: string }
const text = (await Deno.readTextFile(path)).replace(marp, "")
return { location, text }
}
async function main(input: string, output: string) {
const text = await Deno.readTextFile(input)
const replaces = await Promise.all([...text.matchAll(regex)].map(replace))
const result = replaces.reduce(
(acc, { location, text }) => acc.replace(location, text),
text,
)
await Deno.mkdir(dirname(output), { recursive: true })
await Deno.writeTextFile(output, result)
}
await new Command()
.name("template")
.description("Replace <!-- {path} --> with the content of the file at path.")
.option("-o --output <output:string>", "Output file.", { required: true })
.arguments("<input:string>")
.action(async ({ output }, input) => {
await main(input, output)
})
.parse(Deno.args)