This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
texture-optimiser.js
125 lines (109 loc) · 3.61 KB
/
texture-optimiser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const fs = require('fs');
const shell = require('shelljs');
const sharp = require('sharp');
const sizeOf = require('image-size');
const fileExtension = require('file-extension');
const configTemplate = require('./config').textures;
/*
* Resize and copy textures from
* the source to destination directory
*
* Uses sharp for the image conversion: https://www.npmjs.com/package/sharp
*/
module.exports = class TextureOptimiser {
constructor() {
this.files = [];
}
add(directory, name, extension) {
const fileName = `${name}.${extension}`;
const filePath = `${directory}/${fileName}`;
this.files.push({ filePath, fileName, name, extension });
}
includes(file) {
return /(jpg|png)$/i.test(file);
}
copy(file, fileDest) {
return new Promise((resolve, reject) => {
sharp(file).toFile(fileDest, (error, info) => {
if (error) {
reject(error);
return;
}
resolve(fileDest);
});
});
}
resize(file, fileDest, size) {
return new Promise((resolve, reject) => {
const dimensions = sizeOf(file);
const scale = dimensions.height / dimensions.width;
sharp(file)
.resize(size, Math.floor(size * scale))
.toFile(fileDest, (error, info) => {
if (error) {
reject(error);
return;
}
resolve(fileDest);
});
});
}
validateConfig(configFile) {
return configFile.models !== undefined;
}
process(folderName, srcDirectory, destDirectory) {
return new Promise((resolve, reject) => {
try {
if (this.files.length === 0) {
resolve();
return;
}
const tmpDirectory = `${srcDirectory}/tmp-textures`;
shell.mkdir('-p', tmpDirectory);
// Check of the current directory includes a config file
let config = {};
try {
const file = Object.assign(config, JSON.parse(fs.readFileSync(`${srcDirectory}/config.json`)));
if (this.validateConfig(file)) config = file.textures;
} catch (error) {}
const queue = [];
this.files.forEach(data => {
const fileConfig = config[data.fileName] || configTemplate;
// Only resize textures if a size is specified and the resize flag is true
if (fileConfig.sizes.length > 0 && fileConfig.resize) {
fileConfig.sizes.forEach(size => {
const fileDest = `${tmpDirectory}/${data.name}-${size}.${data.extension}`;
queue.push(this.resize(data.filePath, fileDest, size));
});
} else {
const fileDest = `${tmpDirectory}/${data.fileName}`;
queue.push(this.copy(data.filePath, fileDest));
}
});
Promise.all(queue)
.then(() => {
// Create destination path
const destinationDirectory = `${destDirectory}/${folderName}`;
// Remove old files
shell.ls(destinationDirectory).forEach(file => {
const extension = fileExtension(file);
if (this.includes(extension)) {
const fullPath = `${destinationDirectory}/${file}`;
shell.rm(fullPath);
}
});
// Make directory if it doesn't exist
shell.mkdir('-p', destinationDirectory);
// Copy new files to destination directory
shell.cp(`${tmpDirectory}/*`, destinationDirectory);
// Cleanup tmp directory
shell.rm('-rf', tmpDirectory);
resolve();
})
.catch(reject);
} catch (error) {
reject(error);
}
});
}
};