-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
65 lines (59 loc) · 1.66 KB
/
build.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
58
59
60
61
62
63
64
65
const path = require("path");
const sharp = require("sharp");
const glob = require("glob");
const fs = require('fs');
/**
* Images
*/
const inputDir = "assets/img/";
const outputDir = "public/img/";
var allImages = glob.sync(`${inputDir}**/*.png`, {
nodir: true,
absolute: false
});
const widths = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
const buildImages = () => {
widths.map((width, i) => {
console.log(`Resizing to ${width}`)
allImages.map(async (image) => {
try {
const imgFullName = path.basename(image);
const imgExt = image.substr(image.lastIndexOf("."));
const imgName = imgFullName.replace(imgExt, "");
const outputName = imgName + "_" + width + "w" + imgExt;
const destination = image
.replace(inputDir, outputDir)
.replace(imgFullName, outputName);
await sharp(image)
.resize(width)
.toFile(destination);
} catch (error) {
console.log(`${image} error`, error);
}
});
});
};
var allSvgs = glob.sync(`${inputDir}**/*.svg`, {
nodir: true,
absolute: false
});
const moveSvgs = () => {
allSvgs.map(async image => {
try {
const imgFullName = path.basename(image);
const imgExt = image.substr(image.lastIndexOf("."));
const imgName = imgFullName.replace(imgExt, "");
const outputName = imgName + imgExt;
const destination = image
.replace(inputDir, outputDir)
.replace(imgFullName, outputName);
fs.copyFile(image, destination, (err) => {
if (err) throw err
});
} catch (error) {
console.log(`${image} error`, error);
}
});
};
buildImages();
moveSvgs();