all files / lib/ index.js

100% Statements 39/39
88.24% Branches 15/17
100% Functions 1/1
100% Lines 39/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                                                                                                                                             
/**
 * gulp-scss-lint-stylish | lib/index.js
 *
 * Copyright (c) 2015-2016 Roel Schut (roelschut.nl)
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
'use strict';
 
var _          = require('underscore');
var Chalk      = require('gulp-util').colors;
var LogSymbols = require('log-symbols');
var Path       = require('path');
var TextTable  = require('text-table');
 
var colorize  = require('./utils/colorize');
var pluralize = require('./utils/pluralize');
 
// // // // // // // // // // // // // // // // // // // // // // // // // // //
 
var IS_WIN32 = (process.platform === 'win32');
 
// -----------------------------------------------------------------------------
 
/**
 * @param {object} $file - Vinyl object with scsslint key in it.
 * @return {boolean|string} Returns the displayed output, or false on empty.
 */
module.exports = function GulpScssLintStylish($file)
{
    var $result       = false;
    var $report       = $file.scsslint;
    var $errorCount   = 0;
    var $warningCount = 0;
    var $log          = [];
 
    // no output needed on success or empty report
    if (_.isUndefined($report) || _.isEmpty($report) ||
        $report.success === true)
    {
        return $result;
    }
 
    // loop through all issues in the report
    for (var $i = 0, $iL = $report.issues.length; $i < $iL; $i++)
    {
        var $issue  = $report.issues[$i];
        var $reason = $issue.reason;
 
        if ($issue.severity === 'error')
        {
            $errorCount++;
            $reason = Chalk.red($reason);
        }
        else
        {
            $warningCount++;
            $reason = (IS_WIN32 ?
                colorize($reason, Chalk.cyan, Chalk.blue) :
                colorize($reason, Chalk.blue, Chalk.cyan));
        }
 
        if (!_.isEmpty($issue.linter))
        {
            $reason = $issue.linter + ': ' + $reason;
        }
 
        // this is actually a with columns wich are passed to TextTable
        $log.push([
            '',
            Chalk.gray('line ' + $issue.line),
            Chalk.gray('col ' + $issue.column),
            $reason
        ]);
    }
 
    // at the end of the output log, display the amount of warnings/errors
    Eif ($log.length >= 1)
    {
        var $filePath = '.' + Path.sep +
                        Path.relative(process.cwd(), $file.path);
 
        $result = [
            Chalk.underline($filePath),
            TextTable($log) + '\n'
        ];
 
        if ($errorCount > 0)
        {
            $errorCount += pluralize(' error', $errorCount);
            $result.push('  ' + LogSymbols.error + '  ' + $errorCount);
        }
 
        if ($warningCount > 0)
        {
            $warningCount += pluralize(' warning', $warningCount);
            $result.push('  ' + LogSymbols.warning + '  ' + $warningCount);
        }
 
        $result = '\n' + $result.join('\n') + '\n';
        console.log($result);
    }
 
    return $result;
};