UNPKG

broccoli-leasot

Version:

A broccoli plugin which wraps around the leasot module to parse and output TODOs and FIXMEs from comments in your files

152 lines (116 loc) 6.42 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var _broccoliFilter = require('broccoli-filter'); var _broccoliFilter2 = _interopRequireDefault(_broccoliFilter); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _leasot = require('leasot'); var _leasot2 = _interopRequireDefault(_leasot); var _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _lodashCollection = require('lodash/collection'); var _lodashObject = require('lodash/object'); var DEFAULT_SUPPORTED_FILE_EXTENSIONS = ['js', 'css', 'less', 'scss', 'hbs', 'handlebars']; var DEFAULT_MARKERS = ['TODO', 'FIXME']; var printExceptions = function printExceptions(stdout, exceptions) { var _exceptions = exceptions; if (_exceptions.length > 0) { _exceptions = _exceptions.filter(function (exception, i) { return _exceptions.indexOf(exception) === i; }); var header = _chalk2['default'].red('Leasot module doesn\'t support ' + _exceptions.join(', ')); var linkToSupportedExtensions = _chalk2['default'].blue.underline('https://github.com/pgilad/leasot#supported-languages'); stdout.log('\n' + header + ' \n' + linkToSupportedExtensions); } }; var getMessageToPrint = function getMessageToPrint(marker, groupByEntity, groupByCriteria) { var inlineGroup = 'kind'; if (groupByCriteria === 'kind') { inlineGroup = 'file'; } var textToAdd = ''; marker.forEach(function (m) { textToAdd += _chalk2['default'].gray('line ' + m.line) + ' ' + _chalk2['default'].green(m[inlineGroup]) + ' ' + _chalk2['default'].cyan(m.text) + '\n'; }); return _chalk2['default'].underline(groupByEntity) + '\n' + textToAdd; }; var printMarkers = function printMarkers(stdout, markers, groupBy, treeInternalName) { var _markers = [].concat.apply([], markers); var noOfMarkers = _markers.length; _markers = (0, _lodashCollection.groupBy)(_markers, groupBy); (0, _lodashCollection.forEach)(_markers, function (marker, groupByEntity) { stdout.log(getMessageToPrint(marker, groupByEntity, groupBy)); }); var messageToShow = _chalk2['default'].red.bold('✘') + (' ' + noOfMarkers + ' markers found in ' + treeInternalName); if (noOfMarkers === 0) { messageToShow = _chalk2['default'].green.bold('✔') + ' No markers found in ${treeInternalName}'; } stdout.log('\n ' + messageToShow + ' \n'); }; var BroccoliLeasotFilter = (function (_Filter) { _inherits(BroccoliLeasotFilter, _Filter); function BroccoliLeasotFilter(inputTree, options, treeInternalName) { _classCallCheck(this, BroccoliLeasotFilter); _get(Object.getPrototypeOf(BroccoliLeasotFilter.prototype), 'constructor', this).call(this, inputTree, options); /* defaults */ this.enabled = false; this.kinds = DEFAULT_MARKERS; this.extensions = DEFAULT_SUPPORTED_FILE_EXTENSIONS; this.groupBy = ''; /* internals */ this.inputTree = inputTree; this._markers = []; this._exceptions = []; this.treeInternalName = treeInternalName; this.console = options && options['console'] || console; var context = this; (0, _lodashObject.forOwn)(options, function (value, key) { context[key] = value; }); if (['file', 'kind'].indexOf(this.groupBy.toLowerCase()) === -1) { this.groupBy = 'file'; } } _createClass(BroccoliLeasotFilter, [{ key: 'build', value: function build(readTree, destDir) { var self = this; return _get(Object.getPrototypeOf(BroccoliLeasotFilter.prototype), 'build', this).call(this, readTree, destDir).then(function () { if (self.enabled) { printMarkers(self.console, self._markers, self.groupBy, self.treeInternalName); printExceptions(self.console, self._exceptions); } }); } }, { key: 'processString', value: function processString(content, relativePath) { if (this.enabled) { var fileExtension = _path2['default'].extname(relativePath); if (_leasot2['default'].isExtSupported(fileExtension)) { this._markers.push(_leasot2['default'].parse({ ext: fileExtension, content: content, fileName: relativePath, customTags: this.kinds })); } else { this._exceptions.push(fileExtension); } } return content; } }]); return BroccoliLeasotFilter; })(_broccoliFilter2['default']); exports['default'] = function (inputTree, options, treeInternalName) { return new BroccoliLeasotFilter(inputTree, options, treeInternalName); }; module.exports = exports['default'];