-
Notifications
You must be signed in to change notification settings - Fork 55
/
cli.js
executable file
·68 lines (56 loc) · 2.03 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
#!/usr/bin/env node
import fs from 'fs'
import { program } from 'commander'
import PDFMerger from './index.js'
import { parsePagesString } from './parsePagesString.js'
function main (packageJson) {
program
.version(packageJson.version)
.description(packageJson.description)
.option('-o, --output <outputFile>', 'Merged PDF output file path')
.option('-v, --verbose', 'Print verbose output')
.option('-s, --silent', 'do not print any output to stdout. Overwrites --verbose')
.arguments('<inputFiles...>')
.action(async (inputFiles, cmd) => {
const outputFile = cmd.output
const verbose = cmd.verbose && !cmd.silent
const silent = cmd.silent
if (!outputFile) {
console.error('Please provide an output file using the --output flag')
return
}
if (!inputFiles || !inputFiles.length) {
console.error('Please provide at least one input file')
return
}
try {
const merger = new PDFMerger()
for (const inputFile of inputFiles) {
const [filePath, pagesString] = inputFile.split('#')
const pages = pagesString ? parsePagesString(pagesString) : null
if (verbose) {
if (pages && pages.length) {
console.log(`adding page${pages.length > 1 ? 's' : ''} ${pages.join(',')} from ${filePath} to output...`)
} else {
console.log(`adding all pages from ${filePath} to output...`)
}
}
await merger.add(filePath, pages)
}
if (verbose) {
console.log(`Saving merged output to ${outputFile}...`)
}
await merger.save(outputFile)
if (!silent) {
console.log(`Merged pages successfully into ${outputFile}`)
}
} catch (error) {
console.error('An error occurred while merging the PDFs:', error)
}
})
program.parse(process.argv)
}
(() => {
const packageJson = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf-8'))
main(packageJson)
})()