Skip to content
This repository has been archived by the owner on Sep 25, 2020. It is now read-only.

Commit

Permalink
(refactor) Refactored result handler function to named function
Browse files Browse the repository at this point in the history
To reduce nesting.
  • Loading branch information
tholewebgods committed Apr 10, 2019
1 parent 567a206 commit afc581c
Showing 1 changed file with 41 additions and 38 deletions.
79 changes: 41 additions & 38 deletions tasks/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,45 @@ module.exports = function(grunt) {
});
}

function writeResult(tally, input, dest, result) {
var warnings = result.warnings();

tally.issues += warnings.length;

warnings.forEach(function(msg) {
grunt.log.error(msg.toString());
});

if (options.writeDest) {
grunt.file.write(dest, result.css);
log('File ' + chalk.cyan(dest) + ' created.');
}

tally.sheets += 1;

if (result.map) {
var mapDest = dest + '.map';

if (typeof options.map.annotation === 'string') {
mapDest = getSourcemapPath(dest);
}

grunt.file.write(mapDest, result.map.toString());
log('File ' + chalk.cyan(dest + '.map') + ' created (source map).');

tally.maps += 1;
}

if (options.diff) {
var diffPath = (typeof options.diff === 'string') ? options.diff : dest + '.diff';

grunt.file.write(diffPath, diff.createPatch(dest, input, result.css));
log('File ' + chalk.cyan(diffPath) + ' created (diff).');

tally.diffs += 1;
}
}

/**
* @param {string} msg Log message
*/
Expand Down Expand Up @@ -131,44 +170,8 @@ module.exports = function(grunt) {
var dest = f.dest || filepath;
var input = grunt.file.read(filepath);

return process(input, filepath, dest).then(function(result) {
var warnings = result.warnings();

tally.issues += warnings.length;

warnings.forEach(function(msg) {
grunt.log.error(msg.toString());
});

if (options.writeDest) {
grunt.file.write(dest, result.css);
log('File ' + chalk.cyan(dest) + ' created.');
}

tally.sheets += 1;

if (result.map) {
var mapDest = dest + '.map';

if (typeof options.map.annotation === 'string') {
mapDest = getSourcemapPath(dest);
}

grunt.file.write(mapDest, result.map.toString());
log('File ' + chalk.cyan(dest + '.map') + ' created (source map).');

tally.maps += 1;
}

if (options.diff) {
var diffPath = (typeof options.diff === 'string') ? options.diff : dest + '.diff';

grunt.file.write(diffPath, diff.createPatch(dest, input, result.css));
log('File ' + chalk.cyan(diffPath) + ' created (diff).');

tally.diffs += 1;
}
});
return process(input, filepath, dest)
.then(writeResult.bind(null, tally, input, dest));
}));
});

Expand Down

0 comments on commit afc581c

Please sign in to comment.