-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#72): provide a basic command line interface
- Loading branch information
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env node | ||
|
||
import { program } from 'commander' | ||
|
||
import PDFMerger from './index.js' | ||
import { parsePagesString } from './parsePagesString.js' | ||
|
||
import packageJson from './package.json' assert { type: 'json' }; | ||
|
||
async function main() { | ||
program | ||
.version(packageJson.version) | ||
.description(packageJson.description) | ||
.option('-o, --output <outputFile>', 'Merged PDF output file path') | ||
.arguments('<inputFiles...>') | ||
.action(async (inputFiles, cmd) => { | ||
const outputFile = cmd.output; | ||
|
||
if (!outputFile) { | ||
console.error('Please provide an output file using the --output flag'); | ||
return; | ||
} | ||
|
||
try { | ||
await mergePDFs(outputFile, inputFiles); | ||
console.log(`Merged PDFs successfully into ${outputFile}`); | ||
} catch (error) { | ||
console.error('An error occurred while merging PDFs:', error); | ||
} | ||
}); | ||
|
||
program.parse(process.argv); | ||
} | ||
|
||
async function mergePDFs(outputFile, inputFiles) { | ||
const merger = new PDFMerger(); | ||
|
||
for (const inputFile of inputFiles) { | ||
const [filePath, pageRange] = inputFile.split('#'); | ||
const pages = pageRange ? parsePagesString(pageRange) : null; | ||
await merger.add(filePath, pages); | ||
} | ||
await merger.save(outputFile); | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import pdfDiff from 'pdf-diff' | ||
import { exec } from 'child_process' | ||
import util from 'util' | ||
import { jest } from '@jest/globals' | ||
|
||
const asyncExec = util.promisify(exec) | ||
|
||
const __dirname = path.dirname(new URL(import.meta.url).pathname) | ||
const FIXTURES_DIR = path.join(__dirname, 'fixtures') | ||
const TMP_DIR = path.join(__dirname, 'tmp') | ||
|
||
jest.setTimeout(10000) | ||
|
||
async function mergePDFsCli (outputFile, inputFiles) { | ||
const { stdout, stderr } = await asyncExec(`node ./cli.js --output ${outputFile} ${inputFiles.join(' ')}`) | ||
return { stdout, stderr } | ||
} | ||
|
||
describe('issues', () => { | ||
beforeAll(async () => { | ||
await fs.ensureDir(TMP_DIR) | ||
}) | ||
|
||
test('should merge two pdfs', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, 'Testfile_AB.pdf'), [ | ||
path.join(FIXTURES_DIR, 'Testfile_A.pdf'), | ||
path.join(FIXTURES_DIR, 'Testfile_B.pdf') | ||
]) | ||
const diff1 = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff1).toBeFalsy() | ||
}) | ||
|
||
afterAll(async () => { | ||
await fs.remove(TMP_DIR) | ||
}) | ||
}) |