Skip to content

Commit

Permalink
Merge pull request #789 from peppergrayxyz/png2src_proc_dir
Browse files Browse the repository at this point in the history
process directories
  • Loading branch information
aduros authored Dec 15, 2024
2 parents 92490f2 + 9d2f0c4 commit 6c8c8dc
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
20 changes: 13 additions & 7 deletions cli/lib/png2src.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,22 @@ function runAll(files, opts) {

let output = { "sprites": [] };
for (let ii = 0; ii < files.length; ++ii) {
const file = files[ii];

const filePath = files[ii];
try {
if (ii > 0) {
console.log();
const filePaths = !fs.statSync(filePath).isDirectory() ? [filePath] :
fs.readdirSync(filePath)
.filter(file => path.extname(file).toLowerCase() === '.png')
.map(file => path.join(filePath, file));

for (let iii = 0; iii < filePaths.length; ++iii) {
const pngFile = filePaths[iii];
if (ii | iii > 0) {
console.log();
}
output.sprites.push(run(pngFile));
}

output.sprites.push(run(file));
} catch (error) {
throw new Error("Error processing " + file + ": " + error.message);
throw new Error("Error processing " + filePath + ": " + error.message);
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/lib/test/data_ok/smile_7x7_1BPP.png
1 change: 1 addition & 0 deletions cli/lib/test/data_ok/smile_7x7_2BPP.png
1 change: 1 addition & 0 deletions cli/lib/test/data_ok/smile_8x8_1BPP.png
1 change: 1 addition & 0 deletions cli/lib/test/data_ok/smile_8x8_2BPP.png
17 changes: 16 additions & 1 deletion cli/lib/test/png2src.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe("PNG2SRC", () => {
runAll(["/invalid/path"], {
lang: "rust",
output: "-",
})).toThrowError(/Error processing \/invalid\/path: ENOENT: no such file or directory, open '\/invalid\/path'/);
})).toThrowError(/Error processing \/invalid\/path: ENOENT: no such file or directory, stat '\/invalid\/path'/);
});

it("should console log one sprite", () => {
Expand Down Expand Up @@ -218,5 +218,20 @@ describe("PNG2SRC", () => {
expect(console.log).toHaveBeenCalledTimes(3);
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
});

it("should process directories", () => {
runAll(
[
__dirname + "/data_ok",
],
{
lang: "c",
output: "output.h",
}
);

expect(console.log).toHaveBeenCalledTimes(3);
expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 6c8c8dc

Please sign in to comment.