-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
84 lines (75 loc) · 2.19 KB
/
index.ts
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
import {Args, Flags} from '@oclif/core';
import {globby} from 'globby';
import {
addExtension,
removeExtension,
// Removing the extension will make the built cli crash
} from '../../utils.js';
import {BaseCommandWithCompression} from '../../base-command-with-compression.js';
export default class Merge extends BaseCommandWithCompression {
static aliases = ['m', 'join', 'j'];
static description = 'Merge PDFs';
static examples = [
{
description: 'Merge all .pdf files',
command: '<%= config.bin %> <%= command.id %> *.pdf',
},
{
description: 'Merge all .pdf files',
command: '<%= config.bin %> <%= command.id %> *.pdf -o output.pdf',
},
{
description:
'Merge all .pdf files that start with input- & compress the output',
command: `<%= config.bin %> <%= command.id %> input-*.pdf -o output.pdf -c`,
},
{
description:
'Merge cover.pdf with all .pdf files that start with input-, and notes.pdf',
command: `<%= config.bin %> <%= command.id %> cover.pdf input-*.pdf notes.pdf -o output.pdf`,
},
];
// Todo: revise
// For variable length arguments,
// disable argument validation
static strict = false;
static args = {
input: Args.string({
required: true,
// multiple: true,
description: `Input files (e.g. cover.pdf part-*.pdf)`,
}),
};
// https://oclif.io/docs/flags
static flags = {
output: Flags.string({
char: 'o',
description: 'Output file',
default: 'merged.pdf',
}),
keep: Flags.boolean({
char: 'k',
description: `Keep output's name`,
}),
};
async run(): Promise<void> {
// Todo: revise
const {argv, flags} = await this.parse(Merge);
const input = argv as string[];
const {output, compress, keep, 'dry-run': dryRun, silent} = flags;
let finalOutput = removeExtension(output);
await this.ensureDirExists(finalOutput);
if (compress && !keep) {
finalOutput = `${finalOutput}-compressed`;
}
finalOutput = addExtension(finalOutput);
const files = await globby(input);
const args = [...files, 'cat', 'output', finalOutput];
if (compress) {
args.push('compress');
}
this.logger(`Creating ${finalOutput}...`, silent);
await this.execute('pdftk', args, dryRun);
this.logger('Done.', silent);
}
}