Skip to content

Commit

Permalink
Allow chunking of imagemagick commands to prevent command line list t…
Browse files Browse the repository at this point in the history
…oo long exceptions from child process
  • Loading branch information
freshly-pressed-trousers committed Mar 2, 2020
1 parent e73ee95 commit 4513ed3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
49 changes: 39 additions & 10 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ var async = require('async');
var os = require('os');
var path = require('path');
var crypto = require("crypto");
var util = require('util');
var _ = require('underscore');

var packing = require('./packing/packing.js');
var sorter = require('./sorter/sorter.js');

var BATCH_SIZE = 10;

/**
* Generate temporary trimmed image files
* @param {string[]} files
Expand Down Expand Up @@ -114,6 +118,23 @@ exports.determineCanvasSize = function (files, options, callback) {
callback(null, options);
};

function makeCommands(fileList, options, index) {
return [
`convert -define png:exclude-chunks=date -quality 0% -size ${options.width}x${options.height} xc:none`,
index !== 0 &&
`"${options.path}/${options.name}.png" -composite`,
...fileList.map(
file =>
`"${file.path}" -geometry +${
(file.x + options.padding)}+${
(file.y + options.padding)} -composite`
),
`"${options.path}/${options.name}.png"`,
]
.filter(Boolean)
.join(" ");
}

/**
* generates texture data file
* @param {object[]} files
Expand All @@ -122,17 +143,25 @@ exports.determineCanvasSize = function (files, options, callback) {
* @param {function} callback
*/
exports.generateImage = function (files, options, callback) {
var command = ['convert -define png:exclude-chunks=date -quality 0% -size ' + options.width + 'x' + options.height + ' xc:none'];
files.forEach(function (file) {
command.push('"' + file.path + '" -geometry +' + (file.x + options.padding) + '+' + (file.y + options.padding) + ' -composite');
});
command.push('"' + options.path + '/' + options.name + '.png"');
exec(command.join(' '), function (err) {
if (err) return callback(err);
var fileBatches = _.chunk(files, BATCH_SIZE);
var commands = fileBatches.map((fileList, index) =>
makeCommands(fileList, options, index)
);

unlinkTempFiles(files);
callback(null);
});
var execPromise = util.promisify(exec);

var runAllCommands = async () => {
for (command of commands) {
await execPromise(command);
}
}

runAllCommands()
.then(() => {
unlinkTempFiles(files);
callback(null);
})
.catch(err => callback(err));
};

function unlinkTempFiles(files) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spritesheet-js",
"version": "1.2.6",
"version": "1.2.7",
"description": "Spritesheet generator",
"main": "index.js",
"directories": {
Expand All @@ -17,7 +17,7 @@
"mustache": "~0.7.2",
"optimist": "~0.6.0",
"platform-command": "git+https://gitlab.com/gitlabdev/platform-command.git",
"underscore": "~1.5.2"
"underscore": "1.9.2"
},
"devDependencies": {
"mocha": "*",
Expand Down

0 comments on commit 4513ed3

Please sign in to comment.