-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
50 lines (44 loc) · 1.46 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Copyright Since 2019 YuYanDev
* MIT License
*/
var fs = require('fs')
var inquirer = require('inquirer');
var markdownpdf = require("markdown-pdf")
var commander = require('commander')
/* Commander set*/
commander
.version('0.0.1')
.description('DMCA-Generator')
.option('-t, --template <template>', 'template name', 'default')
.option('-o, --output <output>', 'output name', 'DMCA')
.option('-p, --outputpath <outputpath>', 'output path', './')
commander.parse(process.argv)
/* Path init */
var templateJsonUrl = './templates/' + commander.template+ '.json'
var templateUrl = './templates/' + commander.template+ '.md'
var outputPath = commander.outputpath + commander.output + '.pdf'
var templateJson = JSON.parse(fs.readFileSync( templateJsonUrl ));
var template = fs.readFileSync( templateUrl ,'utf-8')
/**
* Template Render Function
* @param {String} template The template markdown string
* @param {Object} answers The inquirer answers object
*/
function render(template,answers){
Object.keys(answers).map((item)=>{
let reg = new RegExp('\{\{ +'+item+' \}\}')
template = template.replace(reg,answers[item])
})
return template;
}
/* Inquirer ask */
inquirer
.prompt(templateJson)
.then((answers) => {
template = render(template,answers)
/* export PDF */
markdownpdf().from.string(template).to(outputPath, function () {
console.log("Created", outputPath, 'success!')
})
})