-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·53 lines (47 loc) · 2.27 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
#! env node
const path = require('path');
const { program } = require('commander');
const pkg = require(`${__dirname}/package.json`);
const { BaseConfig, CliConfig, PluginConfig } = require('./lib/PluginConfig');
const cliAdapter = require('./lib/cliAdapter');
program
.version(pkg.version, '-v, --version')
.argument('[input]', 'file or directory path to markdown files for conversion', '.')
.option('-c, --collate', 'collate multiple files from the input path into a single PDF')
.option('-n, --name <name>', 'when collating, the name of the output PDF (defaults to "collate.pdf")')
.option('-f, --config <path>', 'relative path to .d11ty.json file containing config')
.option('-x, --explicit', 'only print files to PDF that explicitly include the {% d11ty %} shortcode')
.action(async function(input, flags, cmd){
let { config, collate, name, output, explicit } = flags;
if(!output) output = path.dirname(input);
try{
let baseConfig = new BaseConfig();
// if config file, apply its values
if(config){
if(!config.trim().endsWith('.d11ty.js')){
throw new Error('Config file must be named ".d11ty.js" and export a single object or function');
}
let absPath = path.resolve(process.cwd(), path.normalize(config));
let d11tyConfig = require(absPath),
configObj = await (async ()=>{
if(typeof d11tyConfig === 'object') return d11tyConfig;
if(typeof d11tyConfig === 'function') return await d11tyConfig();
})();
let { pdfOptions, serverOptions } = configObj;
if(pdfOptions) Object.assign(baseConfig.pdfOptions, pdfOptions);
if(serverOptions) Object.assign(baseConfig.serverOptions, serverOptions);
}
const pluginConfig = new CliConfig({
output,
explicit,
collate,
collateName: name
});
await cliAdapter(input, new PluginConfig(baseConfig, pluginConfig));
process.exit(0);
} catch(err){
console.error(err);
process.exit(1);
}
});
program.parse();