-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (42 loc) · 1.42 KB
/
index.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
47
48
49
50
// Config
import config from './config';
// Packages
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import sharp from 'sharp';
// Interfaces/Types
import ISize from './definitions/ISize';
import { TFormat } from './definitions/Types';
// Get all files from config input
const files = glob.sync(`${config.input}*.*`);
config.sizes.forEach((size: ISize) => {
// Generates the output path according to the output and size name config
const outputPath = `${config.output}${size.name}`
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
files.forEach(file => {
const filename = path.basename(file);
const image = sharp(file);
const sizeConfig = {width: size.width, height: size.height, fit: config.fit};
// Generates the image according to the size config
image
.resize(sizeConfig)
.toFile(`${outputPath}/${filename}`)
.catch(error => {
console.log(error);
});
config.formats.forEach((format: TFormat) => {
// The current image format
const filesplit = filename.split('.');
const defaultFormat = filesplit[filesplit.length-1];
// Generates the image according to the format config
image
.toFormat(format)
.resize(sizeConfig)
.toFile(`${outputPath}/${filename.replace(defaultFormat, format)}`)
.catch(error => {
console.log(error);
});
});
});
});