Return non-zero exit code when given images differ (#54)

* Return non-zero exit code when given images differ

So that caller of this script can decide what to do based on diff result.

* Fix typo

* Disable no-process-exit eslint rule in bin/pixelmatch

* Wait for write operation to finish before exiting

Otherwise resulting file may get corrupted.

* Add missing semicolon
This commit is contained in:
Marian Schubert 2019-02-15 20:24:32 +01:00 committed by Vladimir Agafonkin
parent 01af4e08e2
commit 8a5683b7e8

View File

@ -1,4 +1,6 @@
#!/usr/bin/env node
/* eslint-disable no-process-exit */
'use strict';
var PNG = require('pngjs').PNG,
@ -7,7 +9,7 @@ var PNG = require('pngjs').PNG,
if (process.argv.length < 5) {
console.log('Usage: pixelmatch image1.png image2.png output.png [threshold=0.005] [includeAA=false]');
return;
process.exit(64);
}
var threshold = isNaN(+process.argv[5]) ? undefined : +process.argv[5],
@ -22,7 +24,7 @@ function doneReading() {
if (img1.width !== img2.width || img1.height !== img2.height) {
console.log('Image dimensions do not match: %dx%d vs %dx%d',
img1.width, img1.height, img2.width, img2.height);
return;
process.exit(65);
}
var diff = new PNG({width: img1.width, height: img1.height});
@ -34,8 +36,12 @@ function doneReading() {
});
console.timeEnd('match');
diff.pack().pipe(fs.createWriteStream(process.argv[4]));
var writeStream = diff.pack().pipe(fs.createWriteStream(process.argv[4]));
console.log('different pixels: ' + diffs);
console.log('error: ' + (Math.round(100 * 100 * diffs / (diff.width * diff.height)) / 100) + '%');
writeStream.on('close', function () {
process.exit(diffs ? 66 : 0);
});
}