-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·57 lines (46 loc) · 1.48 KB
/
index.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
import { pathToFileURL } from 'node:url';
import { convert } from './convert.js';
import { optimize } from './optimize.js';
import { SUPPORTED_FILE_TYPES } from './lib/constants.js';
import { findConfigFilePath } from './lib/find-config-file-path.js';
import { log } from './lib/log.js';
import { prepareFilePaths } from './lib/prepare-file-paths.js';
import { prepareOutputDirectoryPath } from './lib/prepare-output-directory-path.js';
import { programOptions } from './lib/program-options.js';
const MODE_NAME = {
CONVERT: 'convert',
OPTIMIZE: 'optimize',
};
export default async function optimizt({
inputPaths,
outputDirectoryPath,
configFilePath,
}) {
const {
isLossless,
shouldConvertToAvif,
shouldConvertToWebp,
} = programOptions;
const shouldConvert = shouldConvertToAvif || shouldConvertToWebp;
const currentMode = shouldConvert
? MODE_NAME.CONVERT
: MODE_NAME.OPTIMIZE;
const foundConfigFilePath = pathToFileURL(await findConfigFilePath(configFilePath));
const configData = await import(foundConfigFilePath);
const config = configData.default[currentMode.toLowerCase()];
const filePaths = await prepareFilePaths({
inputPaths,
outputDirectoryPath: await prepareOutputDirectoryPath(outputDirectoryPath),
extensions: SUPPORTED_FILE_TYPES[currentMode.toUpperCase()],
});
if (isLossless) {
log('Lossless optimization may take a long time');
}
const processFunction = shouldConvert
? convert
: optimize;
await processFunction({
filePaths,
config,
});
}