-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
145 lines (123 loc) · 3.7 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { showHelp } from "./showHelp.ts";
import { parseNumberOrFraction } from "./helpers.ts";
import { enlargeImage } from "./enlarge.ts";
import { mergeImages } from "./merge.ts";
import { parse } from "https://deno.land/std@0.117.0/flags/mod.ts";
import { basename } from "https://deno.land/std@0.214.0/path/mod.ts";
const command = Deno.args[0];
if (command === "-h") {
// show help if no command is given but only -h
showHelp();
} else if (command === "enlarge") {
await runCommandEnlarge();
} else if (command === "merge") {
await runCommandMerge();
} else {
console.error(command ? `Unknown command: ${command}` : "No command given.");
showHelp();
Deno.exit(1);
}
async function runCommandEnlarge() {
const args = parse(Deno.args.slice(1), {
string: [
"blur",
"ratio", // -r or --ratio will be treated as a string
"rotate",
],
alias: {
b: "blur",
r: "ratio", // alias -r to --ratio
h: "help", // alias -h to --help
},
default: {
blur: 1,
ratio: 1.5,
rotate: "auto",
help: false, // default value for --help is false
},
unknown: (option) => {
if (option.startsWith("-")) {
console.error(`Unknown option: ${option}`);
showHelp();
Deno.exit(1);
}
return true; // all non-options (file names) are allowed
},
});
if (args.help) {
showHelp();
Deno.exit(0);
}
const aspectRatio = parseNumberOrFraction(args.ratio);
if (!aspectRatio) {
console.error(
"Invalid aspect ratio, expected number (1.5) or fraction (3/2)",
);
Deno.exit(1);
}
const blur = parseFloat(args.blur);
if (isNaN(blur)) {
console.error(
"Invalid blur factor, expected number",
);
Deno.exit(1);
}
const rotate = args.rotate;
if (rotate !== "auto" && rotate !== "on" && rotate !== "off") {
console.error(
"Invalid rotate option, allowed: on, off and auto",
);
Deno.exit(1);
}
for (const inputFile of args._.map(String)) {
console.debug(`[DEBUG] Transforming ${inputFile}...`);
const fileNameParts = /^(.+?)(\.[^.]*$|$)/.exec(inputFile);
if (!fileNameParts) {
console.error("Invalid file name format.");
Deno.exit(1);
}
const [, name, extension] = fileNameParts;
const outputFile = `${name}-converted${extension}`;
await enlargeImage(inputFile, rotate, aspectRatio, blur, outputFile);
console.log(`Written ${outputFile}.`);
}
}
async function runCommandMerge() {
const args = parse(Deno.args.slice(1), {
alias: {
h: "help", // alias -h to --help
},
default: {
help: false, // default value for --help is false
},
unknown: (option) => {
if (option.startsWith("-")) {
console.error(`Unknown option: ${option}`);
showHelp();
Deno.exit(1);
}
return true; // all non-options (file names) are allowed
},
});
if (args.help) {
showHelp();
Deno.exit(0);
}
if (args._.length !== 4) {
console.error("Expected exactly four input files.");
Deno.exit(1);
}
const inputFilePaths: [string, string, string, string] = [
String(args._[0]),
String(args._[1]),
String(args._[2]),
String(args._[3]),
];
console.debug(`[DEBUG] Merging ${inputFilePaths}...`);
// Get filename from path
const inputFiles = inputFilePaths.map((inputFile) => basename(inputFile));
const outputFile = `${inputFiles[0]}-${inputFiles[1]}-` +
`${inputFiles[2]}-${inputFiles[3]}-merged.jpg`;
await mergeImages(inputFilePaths, outputFile);
console.log(`Written ${outputFile}.`);
}