Skip to content

Commit

Permalink
Implemented tests, improved error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
vseventer committed Nov 5, 2015
1 parent 8f6f856 commit 84e9a45
Show file tree
Hide file tree
Showing 9 changed files with 590 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules/
test/fixtures/output.jpg
test/fixtures/output*.jpg
.DS_Store
npm_debug.log
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Changelog

## 0.1.0 (November 4, 2015)
## 0.1.0 (November 5, 2015)
Initial version.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ $ sharp --help
--blur [number] Blur the output image, optionally specifying the blur radius.
--compressionLevel <number> zlib compression level, between 0 and 9, of the lossless PNG output
format. Defaults to 6.
--crop [string] Crop the resized image, optionally specifying the gravity
(north, east, south, west, center). Defaults to center.
--crop <string> Crop the resized image based on the specified gravity (north, east,
south, west, center).
--embed Resize the image, then embed on a background.
--extractHeight <number> Height of the region to be extracted.
--extractLeft <number> Left offset of the region to be extracted.
--extractTop <number> Top offset of the region to be extracted.
--extractWidth <number> Width of the region to be extracted.
--extractHeight <number> Height of the region to be extracted. Use in conjunction with
--extractLeft, --extractTop, and --extractWidth.
--extractLeft <number> Left offset of the region to be extracted. Use in conjunction with
--extractHeight, --extractTop, and --extractWidth.
--extractTop <number> Top offset of the region to be extracted. Use in conjunction with
--extractHeight, --extractLeft, and --extractWidth.
--extractWidth <number> Width of the region to be extracted. Use in conjunction with
--extractHeight, --extractLeft, and --extractTop.
-f, --format <string> Output format (jpeg, png, webp, raw). Defaults to input format.
--flatten Merge alpha transparency channel, if any, with --background.
--flip Flip the image about the vertical Y axis.
Expand Down Expand Up @@ -69,14 +73,16 @@ $ sharp --help
--sequentialRead Switches the libvips access method to VIPS_ACCESS_SEQUENTIAL.
--sharpen [number] Sharpen the output image, optionally specifying the sharpen radius.
--sharpenFlat <number> Specify the level of sharpeness to apply to "flat" areas. Defaults to
1.0.
1.0. Use in conjunction with --sharpen.
--sharpenJagged <number> Specify the level of sharpeness to apply to "jagged" areas. Defaults
to 2.0.
to 2.0. Use in conjunction with --sharpen.
--tile [number] Applies square image pyramid tiles over the image, optionally
specifying a tile size between 1 and 8192. Defaults to 256 pixels.
--tileOverlap <number> Specifies the tile overlap, between 0 and 8192. Defaults to 0 pixels.
Use in conjunction with --tile.
--trellisQuantization, --trellesQuantisation
Apply the use of trellis quantization with JPEG output.
-v, --verbose Output image processing information.
-w, --width <number> Scale output to width.
--withoutAdaptiveFiltering Disable adaptive row filtering for the lossless PNG output format.
--withoutChromaSubsampling Disable the use of chroma subsampling with JPEG output (4:4:4).
Expand Down
24 changes: 17 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
'use strict';

// Standard lib.
var fs = require('fs');
var fs = require('fs'),
path = require('path');

// Package modules.
var glob = require('glob'),
Expand All @@ -38,9 +39,11 @@ var glob = require('glob'),
var runner = require('./lib/runner');

// Configure.
var cli = meow({
help: fs.readFileSync('./help.txt', 'utf8')
}, {
var helpFile = path.join(__dirname, './help.txt'),
help = fs.readFileSync(helpFile, 'utf8');

// Run.
var cli = meow(help, {
boolean: [
'embed',
'flatten',
Expand Down Expand Up @@ -94,6 +97,7 @@ var cli = meow({
h : 'height',
o : 'output',
q : 'quality',
v : 'verbose',
w : 'width',

// American vs. UK English.
Expand Down Expand Up @@ -128,14 +132,20 @@ else {
}, [ ]);

// Ensure output flag is set.
if(1 < src.length && !cli.flags.output) {
console.error('No output.');
if(1 < src.length && false === cli.flags.output) {
console.error('Batch operation requires the use of --output.');
process.exit(1); // Exit with failure.
}

// Invoke runner for each src.
var promises = src.map(function(file) {
return runner.run(file, cli.flags);
return runner.run(file, cli.flags).then(function(m) {
if(cli.flags.verbose) {
var size = Math.round(m.size / 1024);
console.log(' %s -> %s (%d×%d, %dkb)', file, m.src, m.width, m.height, size);
}
return m; // Continue.
});
});

// Run.
Expand Down
22 changes: 14 additions & 8 deletions help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ Options
--blur [number] Blur the output image, optionally specifying the blur radius.
--compressionLevel <number> zlib compression level, between 0 and 9, of the lossless PNG output
format. Defaults to 6.
--crop [string] Crop the resized image, optionally specifying the gravity
(north, east, south, west, center). Defaults to center.
--crop <string> Crop the resized image based on the specified gravity (north, east,
south, west, center).
--embed Resize the image, then embed on a background.
--extractHeight <number> Height of the region to be extracted.
--extractLeft <number> Left offset of the region to be extracted.
--extractTop <number> Top offset of the region to be extracted.
--extractWidth <number> Width of the region to be extracted.
--extractHeight <number> Height of the region to be extracted. Use in conjunction with
--extractLeft, --extractTop, and --extractWidth.
--extractLeft <number> Left offset of the region to be extracted. Use in conjunction with
--extractHeight, --extractTop, and --extractWidth.
--extractTop <number> Top offset of the region to be extracted. Use in conjunction with
--extractHeight, --extractLeft, and --extractWidth.
--extractWidth <number> Width of the region to be extracted. Use in conjunction with
--extractHeight, --extractLeft, and --extractTop.
-f, --format <string> Output format (jpeg, png, webp, raw). Defaults to input format.
--flatten Merge alpha transparency channel, if any, with --background.
--flip Flip the image about the vertical Y axis.
Expand Down Expand Up @@ -55,14 +59,16 @@ Options
--sequentialRead Switches the libvips access method to VIPS_ACCESS_SEQUENTIAL.
--sharpen [number] Sharpen the output image, optionally specifying the sharpen radius.
--sharpenFlat <number> Specify the level of sharpeness to apply to "flat" areas. Defaults to
1.0.
1.0. Use in conjunction with --sharpen.
--sharpenJagged <number> Specify the level of sharpeness to apply to "jagged" areas. Defaults
to 2.0.
to 2.0. Use in conjunction with --sharpen.
--tile [number] Applies square image pyramid tiles over the image, optionally
specifying a tile size between 1 and 8192. Defaults to 256 pixels.
--tileOverlap <number> Specifies the tile overlap, between 0 and 8192. Defaults to 0 pixels.
Use in conjunction with --tile.
--trellisQuantization, --trellesQuantisation
Apply the use of trellis quantization with JPEG output.
-v, --verbose Output image processing information.
-w, --width <number> Scale output to width.
--withoutAdaptiveFiltering Disable adaptive row filtering for the lossless PNG output format.
--withoutChromaSubsampling Disable the use of chroma subsampling with JPEG output (4:4:4).
Expand Down
47 changes: 29 additions & 18 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ var run = function(src, flags) {
var isStream = src instanceof stream.Readable,
image = isStream ? src.pipe(sharp()) : sharp(src);

// Add error listener.
image.on('error', function(err) {
console.error('%s', err);
process.exit(1); // Exit with failure.
});

// Input options.
// ==============

Expand All @@ -62,7 +56,8 @@ var run = function(src, flags) {

// @see http://sharp.dimens.io/en/stable/api/#limitinputpixelspixels
if(flags.limitInputPixels) {
image.limitInputPixels(flags.limitInputPixels);
var pixels = parseInt(flags.limitInputPixels, 10);
image.limitInputPixels(pixels);
}

// Resize options.
Expand Down Expand Up @@ -115,7 +110,7 @@ var run = function(src, flags) {
// ============

// @see http://sharp.dimens.io/en/stable/api/#extracttop-left-width-height
if(flags.extractTop && flags.extractLeft && flags.extractWidth && flags.extractHeight) {
if(flags.extractTop || flags.extractLeft || flags.extractWidth || flags.extractHeight) {
var top = parseInt(flags.extractTop, 10),
left = parseInt(flags.extractLeft, 10),
eWidth = parseInt(flags.extractWidth, 10),
Expand Down Expand Up @@ -150,21 +145,27 @@ var run = function(src, flags) {
}

// @see http://sharp.dimens.io/en/stable/api/#blursigma
if(flags.blur) {
if(true === flags.blur) {
image.blur();
}
else if(flags.blur) {
var sigma = parseFloat(flags.blur);
image.blur(sigma);
}

// @see http://sharp.dimens.io/en/stable/api/#sharpenradius-flat-jagged
if(flags.sharpen) {
var radius = parseInt(flags.sharpen, 10),
flat = parseInt(flags.sharpenFlat, 10),
jagged = parseInt(flags.sharpenJagged, 10);
if(flags.sharpen || flags.sharpenFlat || flags.sharpenJagged) {
var radius = true === flags.sharpen ? undefined : parseInt(flags.sharpen, 10),
flat = flags.sharpenFlat ? parseFloat(flags.sharpenFlat) : undefined,
jagged = flags.sharpenJagged ? parseFloat(flags.sharpenJagged) : undefined;
image.sharpen(radius, flat, jagged);
}

// @see http://sharp.dimens.io/en/stable/api/#gammagamma
if(flags.gamma) {
if(true === flags.gamma) {
image.gamma();
}
else if(flags.gamma) {
var gamma = parseFloat(flags.gamma);
image.gamma(gamma);
}
Expand Down Expand Up @@ -209,9 +210,9 @@ var run = function(src, flags) {
}

// @see http://sharp.dimens.io/en/stable/api/#tilesize-overlap
if(flags.tile) {
var tile = parseInt(flags.tile, 10),
overlap = parseInt(flags.tileOverlap, 10);
if(flags.tile || flags.tileOverlap) {
var tile = true === flags.tile ? undefined : parseInt(flags.tile, 10),
overlap = flags.tileOverlap ? parseInt(flags.tileOverlap, 10) : undefined;
image.tile(tile, overlap);
}

Expand Down Expand Up @@ -256,6 +257,9 @@ var run = function(src, flags) {
// Ensure the destination directory exists.
return mkdirSafe(dir).then(function() {
return image.toFile(dest);
}).then(function(metadata) {
metadata.src = dest;
return metadata;
});
}

Expand All @@ -267,4 +271,11 @@ var run = function(src, flags) {
};

// Exports.
module.exports = { run: run };
module.exports = {
run: function(src, flags) {
// Wrap in Promise so exceptions turn into proper promise rejections.
return new Promise(function(resolve) {
return resolve(run(src, flags));
});
}
};
Binary file added test/fixtures/input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
--check-leaks
--no-exit
--reporter spec
--slow 50
--timeout 100
--ui bdd
Loading

0 comments on commit 84e9a45

Please sign in to comment.