-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·79 lines (63 loc) · 2.57 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
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
const fs = require('fs')
const path = require('path')
const Markdown2Pdf = require('./src/Markdown2Pdf')
const getConfig = require('./src/getConfig')
const getHelp = require('./src/getHelp')
module.exports = (opts) => {
// Should help be shown?
if(opts.hasOwnProperty('help')) {
console.log(getHelp())
process.exit(0)
}
// Should the default CSS be exported?
if (opts.cssToPath) {
const dir = (opts.cssToPath === '.' || opts.cssToPath === '..') ? path.resolve(process.cwd(), opts.cssToPath) : opts.cssToPath
if (!fs.existsSync(dir)) {
console.error(`\nError: Unable to create pdfMd.css'. Target directory '${dir}' is not accessible.`)
process.exit(1)
}
const buffer = fs.readFileSync(`${__dirname}/style.css`)
fs.writeFileSync(`${dir}/pdfMd.css`, buffer);
console.log(`CSS styling file was created as '${dir}/pdfMd.css'\n`);
process.exit(0)
}
// Should the default config be exported?
if (opts.cfgToPath) {
const dir = (opts.cfgToPath === '.' || opts.cfgToPath === '..') ? path.resolve(process.cwd(), opts.cfgToPath) : opts.cfgToPath
if (!fs.existsSync(dir)) {
console.error(`\nError: Unable to create pdfMd.json. Target directory '${dir}' is not accessible.`)
process.exit(1)
}
const jsonBeautify = require('json-beautify')
const output = JSON.stringify(getConfig({}), null, 2)
fs.writeFileSync(`${dir}/pdfMd.json`, output)
console.log(`JSON configuration file was created as '${dir}/pdfMd.json'\n`);
process.exit(0)
}
// Parse commandline and config file options
const config = getConfig(opts)
// Are we in verbose mode?
const verbose = opts.hasOwnProperty('verbose')
// Get a list of files from the input directory
const unfilteredList = fs.readdirSync(config.input)
// Filter out non markdown files
const fileList = unfilteredList
.sort()
.map( fileName => {
if (fileName && fileName.indexOf('.md') > -1) {
return `${config.input}/${fileName}`
}
})
.filter(val => typeof val !== 'undefined')
// Create the converter and generate the PDF
const converter = new Markdown2Pdf()
if (verbose) {
console.log('Conversion configuration:\n', config, '\n')
console.log('File list: \n', fileList.join('\n'), '\n')
}
converter.generate(
fileList.filter(val => typeof val !== 'undefined'),
`${config.output}/${config.name}.pdf`,
config
)
}