From afc581c6f159d64852f850f71c423ce4b2670312 Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Wed, 10 Apr 2019 15:17:08 +0200 Subject: [PATCH] (refactor) Refactored result handler function to named function To reduce nesting. --- tasks/postcss.js | 79 +++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/tasks/postcss.js b/tasks/postcss.js index 9961b65..ce00967 100644 --- a/tasks/postcss.js +++ b/tasks/postcss.js @@ -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 */ @@ -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)); })); });