forked from simonhaenisch/md-to-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·130 lines (107 loc) · 3.07 KB
/
cli.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
// --
// Packages
const path = require('path');
const arg = require('arg');
const chalk = require('chalk').default;
const Listr = require('listr');
const getPort = require('get-port');
const { watch } = require('chokidar');
// --
// Utils
const help = require('./util/help');
const getMdFilesInDir = require('./util/get-md-files-in-dir');
const serveDirectory = require('./util/serve-dir');
const config = require('./util/config');
const { getDir } = require('./util/helpers');
const mdToPdf = require('./util/md-to-pdf');
// --
// Configure CLI Arguments
const args = arg({
'--help': Boolean,
'--version': Boolean,
'--watch': Boolean,
'--stylesheet': [String],
'--css': String,
'--body-class': [String],
'--highlight-style': String,
'--marked-options': String,
'--html-pdf-options': String,
'--pdf-options': String,
'--launch-options': String,
'--md-file-encoding': String,
'--stylesheet-encoding': String,
'--config-file': String,
'--devtools': Boolean,
'--debug': Boolean,
// aliases
'-h': '--help',
'-v': '--version',
'-w': '--watch',
});
// --
// Main
async function main(args, config) {
const input = args._[0];
const dest = args._[1];
if (args['--version']) {
return console.log(require('./package').version);
}
if (args['--help']) {
return help();
}
/**
* throw warning when using --html-pdf-options flag
* @todo remove in a future version
*/
if (args['--html-pdf-options']) {
console.warn(
[
chalk.red(`--html-pdf-options is not a valid argument anymore. Use --pdf-options instead.`),
chalk.gray(`valid options: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions`),
].join('\n'),
);
}
const mdFiles = input ? [input] : await getMdFilesInDir('.');
if (mdFiles.length === 0) {
return help();
}
if (dest) {
config.dest = dest;
}
// merge config from config file
if (args['--config-file']) {
try {
config = { ...config, ...require(path.resolve(args['--config-file'])) };
} catch (error) {
console.warn(chalk.red(`Warning: couldn't read config file: ${args['--config-file']}`));
if (args['--debug']) {
console.error(error);
}
}
}
// serve directory of first file because all files will be in the same dir
const port = await getPort();
const server = await serveDirectory(getDir(mdFiles[0]), port);
const getListrTask = mdFile => ({
title: `generating PDF from ${chalk.underline(mdFile)}`,
task: () => mdToPdf(mdFile, config, port, args),
});
// create list of tasks and run concurrently
await new Listr(mdFiles.map(getListrTask), { concurrent: true, exitOnError: false })
.run()
.then(() => {
if (args['--watch']) {
console.log(chalk.bgBlue('\n watching for changes \n'));
watch(mdFiles).on('change', async mdFile => {
await new Listr([getListrTask(mdFile)]).run().catch(error => args['--debug'] && console.error(error));
});
} else {
server.close();
}
})
.catch(error => (args['--debug'] && console.error(error)) || process.exit(1));
}
// --
// Run
main(args, config).catch(error => console.error(error) || process.exit(1));