-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
46 lines (40 loc) · 1.43 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
import * as color from 'colors';
import { CWebp } from 'cwebp';
import * as PATH from 'path';
import { createDirectoriesForPath, getFilesFromDirectory } from './directoryReader';
const inputDirectory = 'input';
const outputDirectory = 'output';
// Get list of file from <variable>inputDirectory</variable>.
getFilesFromDirectory(inputDirectory).then((fileList: string[]) => {
const validFilePaths = filterInvalidFiles(fileList);
const fileListWithRelativePath = ConvertAbsolutePathsToRelativePaths(
validFilePaths
);
fileListWithRelativePath.forEach(file => processFile(file));
});
function filterInvalidFiles(filePaths: string[]) {
return filePaths.filter(
path =>
path.split('.').pop() === 'png' || path.split('.').pop() === 'jpg'
);
}
function ConvertAbsolutePathsToRelativePaths(list: string[]) {
return list.map(pth => PATH.relative(process.cwd(), pth));
}
function processFile(file: string) {
const encoder = CWebp(file);
encoder.quality(80);
const outputPath = PATH.join(outputDirectory, file).replace(
/\..+/g,
'.webp'
);
createDirectoriesForPath(outputPath);
encoder
.write(outputPath)
.then(res => {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(color.green(`${file} success.`));
})
.catch(err => console.error(color.red(`${file} failed.`), err));
}