-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
42 lines (34 loc) · 1.02 KB
/
cli.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
#!/usr/bin/env node
import { compile } from './compiler'
import * as fs from 'fs'
import { minify } from 'html-minifier'
function main() {
if (process.argv.length < 2) {
console.log("usage: compiler.js [input file] [output file]")
process.exit(1)
}
let inputFile = process.argv[2]
let outputFile = process.argv[3]
fs.readFile(inputFile, 'utf8', (err,data) => {
if (err) throw err
let output = compile(data)
fs.readFile("template.html", 'utf8', (err,template) => {
if (err) throw err
writeOutput(minifyOutput(template.replace("%%content%%", output.content).replace('%%title%%', output.title)), outputFile)
})
})
}
function minifyOutput(output) {
return minify(output, {
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
removeEmptyAttributes: true
})
}
function writeOutput(output, outputFile) {
fs.writeFile(outputFile, output, 'utf8', (err) => {
if (err) throw err
})
}
main()