UNPKG

markdown-proofing

Version:

A markdown proofing platform for individuals, teams, and organizations.

156 lines (119 loc) 5.15 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 _chalk = require('chalk'); var _chalk2 = _interopRequireDefault(_chalk); var _glob = require('glob'); var _glob2 = _interopRequireDefault(_glob); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _markdownProofing = require('./markdownProofing'); var _markdownProofing2 = _interopRequireDefault(_markdownProofing); 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"); } } var Main = function () { function Main(cli, configurationProvider, logger) { _classCallCheck(this, Main); this.cli = cli; this.configurationProvider = configurationProvider; this.logger = logger; } _createClass(Main, [{ key: 'run', value: function run() { var items = this._getItemsFromInput(this.cli.input); if (this.cli.input.length === 0 && items.length === 0) { this.logger.log(this.cli.help); } return this._processItems(items); } }, { key: '_getItemsFromInput', value: function _getItemsFromInput(input) { var items = []; input.forEach(function (x) { var files = _glob2.default.sync(x, {}); files.forEach(function (file) { var text = _fs2.default.readFileSync(file, 'utf-8'); items.push({ info: file, text: text }); }); }); return items; } }, { key: '_processItems', value: function _processItems(items) { var _this = this; var configuration = this.configurationProvider.getConfiguration(); var markdownProofing = _markdownProofing2.default.createUsingConfiguration(configuration); var promises = []; var errorsCount = 0; items.forEach(function (item) { var promise = markdownProofing.proof(item.text).then(function (results) { errorsCount += _this._displayResults(item.info, markdownProofing.rules, results); }); promises.push(promise); }); return Promise.all(promises).then(function () { if (_this.cli.flags.throw && errorsCount > 0) { throw new Error(errorsCount + ' ' + (errorsCount > 1 ? 'errors were' : 'error was') + ' encountered while proofing.'); } }); } }, { key: '_displayResults', value: function _displayResults(info, rules, results) { var _this2 = this; var line = new Array(info.length + 1).join('-'); this.logger.log('\n' + line + '\n' + info + '\n' + line + '\n'); var errorsCount = 0; results.messages.forEach(function (message) { var location = message.line !== undefined && message.column !== undefined ? // eslint-disable-line no-undefined ' (' + message.line + ':' + message.column + ')' : ''; var applicableRules = rules.filter(function (rule) { return rule.messageType === message.type && rule.matchesCondition(message); }); // Use startsWith when determining the condition to display // as a condition could be: // error < 5 var ruleConditionToApply = void 0; if (applicableRules.some(function (x) { return x.condition.startsWith('error'); })) { ruleConditionToApply = 'error'; errorsCount++; } else if (applicableRules.some(function (x) { return x.condition.startsWith('warning'); })) { ruleConditionToApply = 'warning'; } else if (applicableRules.some(function (x) { return x.condition.startsWith('info'); })) { ruleConditionToApply = 'info'; } else { throw new Error('An unexpected error occurred: ' + 'The applicableRules did not match any of the handled conditions.'); } var messageTemplate = '[' + ruleConditionToApply + '] ' + message.type + location + ' : ' + message.text; if (_this2.cli.flags.color) { var colorsLookup = { info: _chalk2.default.blue, warning: _chalk2.default.yellow, error: _chalk2.default.red }; _this2.logger.log(colorsLookup[ruleConditionToApply](messageTemplate)); } else { _this2.logger.log(messageTemplate); } }); return errorsCount; } }]); return Main; }(); exports.default = Main; module.exports = exports['default'];