-
Notifications
You must be signed in to change notification settings - Fork 1
/
pug.js
36 lines (32 loc) · 1.12 KB
/
pug.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
import { watch, readFile, writeFile } from 'fs/promises'
import { compile } from 'pug'
import { Command } from 'commander'
const program = new Command()
program
.argument('<filename>', 'pug file to convert')
.option('-c, --client', 'export as js template')
.option('-E, --extension <ext>', 'force output extension')
.option('-w, --watch', 'watch mode')
program.parse(process.argv)
program
.action(async (filename, options) => {
const ext = options.extension || (options.client ? 'js' : 'html')
const outFile = filename.replace(/\.\w+$/, `.${ext}`)
const watcher = watch(filename)
await build(filename, outFile, options)
if(!options.watch) return
let i =0;
for await (const event of watcher){
await build(filename, outFile, options)
}
})
.parse()
async function build(filename, outFile, options) {
const source = await readFile(filename, 'utf-8')
let outSource = compile(source)()
if (options.client) {
outSource = `export default ${JSON.stringify(outSource)}`
}
writeFile(outFile, outSource)
console.info(`\x1b[32m\x1b[1m${filename}\x1b[10m → \x1b[1m${outFile}\x1b[0m`)
}