-
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 command line interface
- Loading branch information
Showing
8 changed files
with
223 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,68 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'fs-extra' | ||
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 = fs.readJsonSync(new URL('./package.json', import.meta.url)) | ||
main(packageJson) | ||
})() |
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,109 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import util from 'util' | ||
import { exec } from 'child_process' | ||
import { jest } from '@jest/globals' | ||
import pdfDiff from 'pdf-diff' | ||
|
||
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 diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('should merge two pdfs from URL', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, 'Testfile_AB.pdf'), [ | ||
'https://github.com/nbesli/pdf-merger-js/raw/master/test/fixtures/Testfile_A.pdf', | ||
'https://github.com/nbesli/pdf-merger-js/raw/master/test/fixtures/Testfile_B.pdf' | ||
]) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'), | ||
path.join(TMP_DIR, 'Testfile_AB.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine single pages from multiple pdfs', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, '2468.pdf'), [ | ||
path.join(FIXTURES_DIR, '123456789.pdf#2'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#4'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#6'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#8') | ||
]) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, '2468.pdf'), | ||
path.join(TMP_DIR, '2468.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multiple pdfs (list)', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, '2468.pdf'), [ | ||
path.join(FIXTURES_DIR, '123456789.pdf#2,4'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#6,8') | ||
]) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, '2468.pdf'), | ||
path.join(TMP_DIR, '2468.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multipel pdfs (rang)', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, '123456789.pdf'), [ | ||
path.join(FIXTURES_DIR, '123456789.pdf#1-5'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#6-9') | ||
]) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, '123456789.pdf'), | ||
path.join(TMP_DIR, '123456789.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
test('combine pages from multipel pdfs (combined list and range)', async () => { | ||
await mergePDFsCli(path.join(TMP_DIR, '123456789.pdf'), [ | ||
path.join(FIXTURES_DIR, '123456789.pdf#1,2,3-4'), | ||
path.join(FIXTURES_DIR, '123456789.pdf#5-7,8to9') | ||
]) | ||
const diff = await pdfDiff( | ||
path.join(FIXTURES_DIR, '123456789.pdf'), | ||
path.join(TMP_DIR, '123456789.pdf') | ||
) | ||
expect(diff).toBeFalsy() | ||
}) | ||
|
||
afterEach(async () => { | ||
for (const file of await fs.readdir(TMP_DIR)) { | ||
await fs.unlink(path.join(TMP_DIR, file)) | ||
} | ||
}) | ||
|
||
afterAll(async () => { | ||
await fs.remove(TMP_DIR) | ||
}) | ||
}) |
Binary file not shown.
Binary file not shown.
Binary file not shown.