-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·52 lines (42 loc) · 1.47 KB
/
cli.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
#!/usr/bin/env node
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { program } from 'commander';
import optimizt from './index.js';
import { setProgramOptions } from './lib/program-options.js';
const dirname = path.dirname(fileURLToPath(import.meta.url));
const packageJson = JSON.parse(await fs.readFile(path.join(dirname, 'package.json')));
program
.option('--avif', 'create AVIF and exit')
.option('--webp', 'create WebP and exit')
.option('-f, --force', 'force create AVIF and WebP')
.option('-l, --lossless', 'perform lossless optimizations')
.option('-v, --verbose', 'be verbose')
.option('-c, --config <path>', 'use this configuration, overriding default config options if present')
.option('-o, --output <path>', 'write output to directory');
program
.usage('[options] <dir> <file ...>')
.version(packageJson.version, '-V, --version')
.description(packageJson.description)
.parse(process.argv);
if (program.args.length === 0) {
program.help();
} else {
const { avif, webp, force, lossless, verbose, config, output } = program.opts();
setProgramOptions({
shouldConvertToAvif: Boolean(avif),
shouldConvertToWebp: Boolean(webp),
isForced: Boolean(force),
isLossless: Boolean(lossless),
isVerbose: Boolean(verbose),
});
optimizt({
inputPaths: program.args,
outputDirectoryPath: output,
configFilePath: config,
});
}
process.on('unhandledRejection', error => {
console.error(error);
});