-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
86 lines (76 loc) · 2.03 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
85
86
import {Args, Flags} from '@oclif/core';
import {
addExtension,
removeExtension,
// Removing the extension will make the built cli crash
} from '../../utils.js';
import {BaseCommand} from '../../base-command.js';
export default class Split extends BaseCommand {
static aliases = ['s'];
static description = `Split each page into many tiles [mutool]`;
static examples = [
'<%= config.bin %> <%= command.id %> input.pdf',
'<%= config.bin %> <%= command.id %> input.pdf -x 2',
'<%= config.bin %> <%= command.id %> input.pdf -o splitted.pdf -y 2',
'<%= config.bin %> <%= command.id %> input.pdf -x 2 -y 3 -r',
];
static args = {
input: Args.string({
description: `Input PDF file`,
required: true,
}),
};
static flags = {
output: Flags.string({
char: 'o',
description: 'Output file',
}),
x: Flags.integer({
char: 'x',
description:
'Pieces to horizontally divide each page into. (Uses default only if --y is NOT used)',
async default(context) {
if (!context.flags.y) {
return 2;
}
},
}),
y: Flags.integer({
char: 'y',
description: 'Pieces to vertically divide each page into.',
}),
r: Flags.boolean({
char: 'r',
description:
'Split horizontally from right to left (default splits from left to right). (v1.23.0+)',
}),
};
async run(): Promise<void> {
const {args, flags} = await this.parse(Split);
const {input} = args;
const {x, y, r, output, 'dry-run': dryRun, silent} = flags;
let finalOutput: string;
if (output) {
finalOutput = removeExtension(output);
} else {
finalOutput = removeExtension(input);
finalOutput = `${finalOutput}-splitted`;
}
finalOutput = addExtension(finalOutput);
await this.ensureDirExists(finalOutput);
this.logger(`Creating ${finalOutput}...`, silent);
const args2 = ['poster'];
if (x) {
args2.push('-x', `${x}`);
}
if (y) {
args2.push('-y', `${y}`);
}
if (r) {
args2.push('-r');
}
args2.push(input, finalOutput);
await this.execute('mutool', args2, dryRun);
this.logger('Done.', silent);
}
}