-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
165 lines (141 loc) Β· 4.03 KB
/
bin.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
const yargs = require('yargs')
.command(
'$0',
'Convert HEIC image to JPEG or PNG',
yargs => yargs
.option('format', {
alias: 'f',
describe: 'The output format',
choices: ['jpg', 'png'],
default: 'jpg',
coerce: val => {
if (val.toLowerCase() === 'jpeg') {
return 'jpg';
}
return val.toLowerCase();
}
})
.option('input', {
alias: 'i',
describe: 'The input file to convert, - for stdin',
default: '-'
})
.option('output', {
alias: 'o',
describe: 'The output file to create, - for stdout',
default: '-'
})
.option('images', {
alias: 'm',
type: 'array',
describe: 'Which images to decode, -1 for all',
default: [0]
}),
async ({ input, output, format, images }) => {
const all = images.length === 1 && images[0] === -1;
const single = images.length === 1;
try {
await new Promise(r => setTimeout(() => r(), 0));
const results = await prep({ input, format });
results.forEach((img, i) => {
img.idx = i;
});
if (all) {
return await outputAllImages({ images: results, output });
}
for (let i of images) {
if (!results[i]) {
throw new RangeError(`no image at index ${i}, images in file: ${results.length}`);
}
}
if (single) {
return await outputImage({ image: results[images[0]], output });
}
return outputAllImages({ images: results.filter((r, i) => images.includes(i)), output });
} catch (err) {
onError(err);
}
}
)
.command(
'info',
'See minimum info about each image in the file',
(yargs) => yargs
.option('input', {
alias: 'i',
describe: 'The input file to convert, - for stdin',
default: '-'
})
.option('count', {
alias: 'c',
describe: 'Print only the amount of images in the file as a number',
type: 'boolean',
default: false
}),
async ({ input, count }) => {
try {
await new Promise(r => setTimeout(() => r(), 0));
const images = await prep({ input });
// eslint-disable-next-line no-console
console.log(count ? `${images.length}` : `images in file: ${images.length}`);
} catch (err) {
onError(err);
}
}
)
.help();
yargs.argv;
const { promisify } = require('util');
const fs = require('fs');
const path = require('path');
const convert = require('heic-convert');
const FORMAT = {
png: 'PNG',
jpg: 'JPEG'
};
const readStdin = () => new Promise(resolve => {
const result = [];
process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read())) {
result.push(chunk);
}
});
process.stdin.on('end', () => {
resolve(Buffer.concat(result));
});
});
const prep = async ({ input, format = 'jpg' }) => {
const buffer = input === '-' ?
await readStdin() :
await promisify(fs.readFile)(path.resolve('.', input));
const results = await convert.all({ buffer, format: FORMAT[format], quality: 1 });
return results;
};
const outputImage = async ({ image, output }) => {
const result = await image.convert();
if (output === '-') {
process.stdout.write(result);
} else {
await promisify(fs.writeFile)(path.resolve('.', output), result);
}
};
const outputAllImages = async ({ images, output }) => {
if (output === '-') {
throw new Error('cannot write all images to standard out, use --output to provide filename template');
}
for (let image of images) {
let name = output.replace(/%s/g, image.idx);
if (output === name) {
name = `${image.idx}-${output}`;
}
await outputImage({ image, output: name });
}
};
const onError = err => {
console.error(err); // eslint-disable-line no-console
console.error(''); // eslint-disable-line no-console
yargs.showHelp();
process.exitCode = 1;
};