Skip to content

Commit 1113206

Browse files
committed
Add programOptions helper
1 parent e66ef40 commit 1113206

File tree

6 files changed

+50
-36
lines changed

6 files changed

+50
-36
lines changed

cli.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'node:url';
77
import { program } from 'commander';
88

99
import optimizt from './index.js';
10+
import { setProgramOptions } from './lib/program-options.js';
1011

1112
const dirname = path.dirname(fileURLToPath(import.meta.url));
1213
const packageJson = JSON.parse(await fs.readFile(path.join(dirname, 'package.json')));
@@ -29,9 +30,20 @@ program
2930
if (program.args.length === 0) {
3031
program.help();
3132
} else {
33+
const { avif, webp, force, lossless, verbose, config, output } = program.opts();
34+
35+
setProgramOptions({
36+
shouldConvertToAvif: Boolean(avif),
37+
shouldConvertToWebp: Boolean(webp),
38+
isForced: Boolean(force),
39+
isLossless: Boolean(lossless),
40+
isVerbose: Boolean(verbose),
41+
});
42+
3243
optimizt({
33-
paths: program.args,
34-
...program.opts(),
44+
inputPaths: program.args,
45+
outputDirectoryPath: output,
46+
configFilePath: config,
3547
});
3648
}
3749

index.js

+11-22
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ import { pathToFileURL } from 'node:url';
55
import { SUPPORTED_FILE_TYPES } from './lib/constants.js';
66
import convert from './lib/convert.js';
77
import { findConfig } from './lib/find-config.js';
8-
import { enableVerbose, log, logErrorAndExit } from './lib/log.js';
8+
import { log, logErrorAndExit } from './lib/log.js';
99
import optimize from './lib/optimize.js';
1010
import { prepareInputFilePaths } from './lib/prepare-input-file-paths.js';
1111
import { prepareOutputDirectoryPath } from './lib/prepare-output-directory-path.js';
12+
import { programOptions } from './lib/program-options.js';
1213

1314
const MODE_NAME = {
1415
CONVERT: 'convert',
1516
OPTIMIZE: 'optimize',
1617
};
1718

1819
export default async function optimizt({
19-
paths: inputPaths,
20-
output: outputDirectoryPath,
21-
config: configFilePath,
22-
23-
avif: shouldConvertToAvif,
24-
webp: shouldConvertToWebp,
25-
26-
force: isForced,
27-
lossless: isLossless,
28-
verbose: isVerbose,
20+
inputPaths,
21+
outputDirectoryPath,
22+
configFilePath,
2923
}) {
24+
const {
25+
isLossless,
26+
shouldConvertToAvif,
27+
shouldConvertToWebp,
28+
} = programOptions;
29+
3030
const shouldConvert = shouldConvertToAvif || shouldConvertToWebp;
3131

3232
const currentMode = shouldConvert
@@ -44,10 +44,6 @@ export default async function optimizt({
4444
const preparedInputFilePaths = await prepareInputFilePaths(inputPaths, SUPPORTED_FILE_TYPES[currentMode.toUpperCase()]);
4545
const preparedOutputDirectoryPath = await prepareOutputDirectoryPath(outputDirectoryPath);
4646

47-
if (isVerbose) {
48-
enableVerbose();
49-
}
50-
5147
if (isLossless) {
5248
log('Lossless optimization may take a long time');
5349
}
@@ -59,14 +55,7 @@ export default async function optimizt({
5955
await processFunction({
6056
inputFilePaths: preparedInputFilePaths,
6157
outputDirectoryPath: preparedOutputDirectoryPath,
62-
isLossless,
6358
config,
64-
65-
...shouldConvert && {
66-
shouldConvertToAvif,
67-
shouldConvertToWebp,
68-
isForced,
69-
},
7059
});
7160
}
7261

lib/convert.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@ import {
2323
import { optionsToArguments } from './options-to-arguments.js';
2424
import { parseImageMetadata } from './parse-image-metadata.js';
2525
import prepareWriteFilePath from './prepare-write-file-path.js';
26+
import { programOptions } from './program-options.js';
2627
import showTotal from './show-total.js';
2728

2829
export default async function convert({
2930
inputFilePaths,
3031
outputDirectoryPath,
31-
isLossless,
3232
config,
33-
shouldConvertToAvif,
34-
shouldConvertToWebp,
35-
isForced,
3633
}) {
34+
const {
35+
isForced,
36+
isLossless,
37+
shouldConvertToAvif,
38+
shouldConvertToWebp,
39+
} = programOptions;
40+
3741
const inputFilePathsCount = inputFilePaths.length;
3842

3943
if (!inputFilePathsCount) {

lib/log.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EOL } from 'node:os';
22

33
import { colorize } from './colorize.js';
4+
import { programOptions } from './program-options.js';
45

56
export const LOG_TYPES = {
67
INFO: 'info',
@@ -26,12 +27,6 @@ const symbols = {
2627
const isUnicodeSupported = process.platform !== 'win32' || process.env.TERM === 'xterm-256color';
2728
const symbolIndex = isUnicodeSupported ? 1 : 0;
2829

29-
let isVerbose = false;
30-
31-
export function enableVerbose() {
32-
isVerbose = true;
33-
}
34-
3530
function formatLogMessage(title, { type = LOG_TYPES.INFO, description } = {}) {
3631
if (!title) {
3732
throw new Error('Title is required');
@@ -73,7 +68,7 @@ export function logProgress(title, { type, description, progressBarContainer } =
7368
}
7469

7570
export function logProgressVerbose(title, { type, description, progressBarContainer } = {}) {
76-
if (isVerbose) {
71+
if (programOptions.isVerbose) {
7772
logProgress(title, { type, description, progressBarContainer });
7873
}
7974
}

lib/optimize.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ import {
2323
import { optionsToArguments } from './options-to-arguments.js';
2424
import { parseImageMetadata } from './parse-image-metadata.js';
2525
import prepareWriteFilePath from './prepare-write-file-path.js';
26+
import { programOptions } from './program-options.js';
2627
import showTotal from './show-total.js';
2728

2829
export default async function optimize({
2930
inputFilePaths,
3031
outputDirectoryPath,
31-
isLossless,
3232
config,
3333
}) {
34+
const { isLossless } = programOptions;
35+
3436
const inputFilePathsCount = inputFilePaths.length;
3537

3638
if (inputFilePathsCount <= 0) {

lib/program-options.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const programOptions = {
2+
shouldConvertToAvif: false,
3+
shouldConvertToWebp: false,
4+
isForced: false,
5+
isLossless: false,
6+
isVerbose: false,
7+
};
8+
9+
export function setProgramOptions(options) {
10+
Object.assign(programOptions, options);
11+
Object.freeze(programOptions);
12+
}

0 commit comments

Comments
 (0)