Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Remove stray jpegtran temp file in img task #64

Merged
merged 1 commit into from

2 participants

@robinpyon

New to node and OSS development :) Please let me know if there's a better way to go about this / if there's a contributor's guide.

tasks/img.js
((6 lines not shown))
grunt.helper('which', 'jpegtran', function(err, cmdpath) {
if(err) return grunt.helper('not installed', 'jpegtran', cb);
+
+ // Remove temp JPEGtran file when finished
+ function clean() {
+ if (fs.exists(tmpfile, function(exists) {
@mklabs Owner
mklabs added a note

Asynchronous function doesn't usually return value in a way that we can use it like this. The boolean is carried over by the exists value you get in the callback. Can you drop this if statement around fs.exists ?

@mklabs Owner
mklabs added a note

Related, fs.exists is a new API introduced in node 0.8.x, which unfortunately would break any 0.6.x based install. Can you, at the top of the file, do something like:

// other dependencies, next to the list of requires
var exists = fs.exists || path.exists;

and then use this exists reference instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tasks/img.js
((6 lines not shown))
grunt.helper('which', 'jpegtran', function(err, cmdpath) {
if(err) return grunt.helper('not installed', 'jpegtran', cb);
+
+ // Remove temp JPEGtran file when finished
+ function clean() {
+ if (fs.exists(tmpfile, function(exists) {
+ if (exists) {
@mklabs Owner
mklabs added a note

Early return pattern whenever possible, especially true for within all these callbacks. The else statement consists in just calling the callback. Can you turn it into something like:

if(!exists) return cb();

// rest of the code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tasks/img.js
((6 lines not shown))
grunt.helper('which', 'jpegtran', function(err, cmdpath) {
if(err) return grunt.helper('not installed', 'jpegtran', cb);
+
+ // Remove temp JPEGtran file when finished
+ function clean() {
+ if (fs.exists(tmpfile, function(exists) {
+ if (exists) {
+ grunt.log.subhead('** Removing: ' + tmpfile);
+ fs.unlink( tmpfile, function(err) {
+ if (err) {
+ grunt.log.error(err);
@mklabs Owner
mklabs added a note

missing return. Plus, the grunt callback should get false to indicate the failure. return cb(false); Ideally, it should be just return cb(err); but right now we're forced to log the error and call the callback with a false value. Will change in grunt 0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mklabs
Owner

Hi Robin,

Thanks for taking the time to look over the project to make it better. I added a few comments for you inline, I'll be happy to merge this once you've changed these few little things. Thanks!

@mklabs
Owner

Should close #49

@robinpyon

Thanks for your feedback and advice. I just added another commit and rebased it before pushing—not 100% sure if this is the correct procedure, let me know if I should be doing things differently.

@mklabs
Owner

Hmm, Github got confused with another Pull request merge. Discussion goes into "View outdated diff" view. It seems I can still automatically merge this though.

@mklabs
Owner

oh, we crossed messages. You did well, rebase + forced push is the right thing to do. Nice to see that GitHub handles it well.

Reviewing, but all seems super good. The very last thing that would be cool is to turn the clean function into a grunt helper, but I don't really mind. Great PR, thanks and merging.

@mklabs mklabs merged commit e55b5d0 into mklabs:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 29, 2012
  1. Remove stray jpegtran temp file in img task

    Robin Pyon authored
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 4 deletions.
  1. +24 −4 tasks/img.js
View
28 tasks/img.js
@@ -1,7 +1,9 @@
var fs = require('fs'),
path = require('path'),
- which = require('which');
+ which = require('which'),
+ exists = fs.exists || path.exists;
+
//
// This task takes care of img optimizations by running a set of `.png`
@@ -86,10 +88,28 @@ module.exports = function(grunt) {
cb = cb || function() {};
opts.args = opts.args ? opts.args : ['-copy', 'none', '-optimize', '-outfile', 'jpgtmp.jpg'];
+ var tmpfile = 'jpgtmp.jpg';
+
grunt.helper('which', 'jpegtran', function(err, cmdpath) {
if(err) return grunt.helper('not installed', 'jpegtran', cb);
+
+ // Remove temp JPEGtran file when finished
+ function clean() {
+ exists(tmpfile, function(exists) {
+ if (!exists) return cb();
+ grunt.log.subhead('** Removing: ' + tmpfile);
+ fs.unlink(tmpfile, function(err) {
+ if (err) {
+ grunt.log.error(err);
+ return cb(false);
+ }
+ return cb();
+ });
+ });
+ }
+
(function run(file) {
- if(!file) return cb();
+ if(!file) return clean();
grunt.log.subhead('** Processing: ' + file);
var jpegtran = grunt.utils.spawn({
cmd: cmdpath,
@@ -102,9 +122,9 @@ module.exports = function(grunt) {
jpegtran.on('exit', function(code) {
if(code) return grunt.warn('jpgtran exited unexpectedly with exit code ' + code + '.', code);
// output some size info about the file
- grunt.helper('min_max_stat', 'jpgtmp.jpg', file);
+ grunt.helper('min_max_stat', tmpfile, file);
// copy the temporary optimized jpg to original file
- fs.createReadStream('jpgtmp.jpg')
+ fs.createReadStream(tmpfile)
.pipe(fs.createWriteStream(file)).on('close', function() {
run(files.shift());
});
Something went wrong with that request. Please try again.