-
Notifications
You must be signed in to change notification settings - Fork 7
/
pack-spritesheets.js
59 lines (56 loc) · 1.67 KB
/
pack-spritesheets.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
/* global require */
/* eslint-disable no-console */
const fs = require("fs");
const packer = require("gamefroot-texture-packer");
let waiting = 0;
for (const name of ["backgrounds", "menus", "sprites"]) {
const subFolders = ["", "Undercover Exclusive/"];
if (name === "sprites") {
subFolders.push("custom/");
}
for (const subfolderName of subFolders) {
const inFolderPath = `images/${name}/${subfolderName}`;
const outFolderPath = `images/spritesheets/${subfolderName}`;
waiting += 1;
console.log(`Packing ${inFolderPath} to ${outFolderPath}...`);
packer(`${inFolderPath}*.png`, {
format: "easel.js",
name,
path: outFolderPath,
// eslint-disable-next-line no-loop-func
}, (err) => {
if (err) {
throw err;
}
console.log(`${name} spritesheet successfully generated`);
fs.readFile(`${outFolderPath}${name}-1.json`, "utf8", (err, json) => {
if (err) {
throw err;
}
json = json.replace(/\s*\/\/.*/g, ""); // remove line comments
json = json.replace(/:\[/g, ": ["); // add whitespace
json = json.replace(/\t\t\t\t?/g, "\t\t"); // fix indentation
json = json.replace(/"images": \["[^"]*"\]/g, `"images": ["${name}.png"]`); // update filename reference
fs.writeFile(`${outFolderPath}${name}.json`, json, (err) => {
if (err) {
throw err;
}
fs.unlink(`${outFolderPath}${name}-1.json`, (err) => {
if (err) {
throw err;
}
fs.rename(`${outFolderPath}${name}-1.png`, `${outFolderPath}${name}.png`, (err) => {
if (err) {
throw err;
}
waiting -= 1;
if (waiting === 0) {
console.log("All done!");
}
});
});
});
});
});
}
}