UNPKG

fork-ts-checker-notifier-webpack-plugin

Version:
77 lines (76 loc) 3.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const path_1 = __importDefault(require("path")); const node_notifier_1 = __importDefault(require("node-notifier")); const util_1 = __importDefault(require("util")); const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin")); class ForkTsCheckerNotifierWebpackPlugin { constructor(options) { this.compilationDone = (issues) => { const notification = this.buildNotification(issues); if (notification) { node_notifier_1.default.notify(notification); } return issues; }; this.options = options !== null && options !== void 0 ? options : {}; this.lastBuildSucceeded = false; this.isFirstBuild = true; this.titlePrefix = this.options.title ? this.options.title + ' - ' : ''; } buildNotification(issues) { var _a, _b; if (this.isFirstBuild) { this.isFirstBuild = false; if (this.options.skipFirstNotification) { return undefined; } } const firstError = issues.find((issue) => issue.severity === 'error'); const firstWarning = issues.find((issue) => issue.severity === 'warning'); if (firstError) { this.lastBuildSucceeded = false; return { title: util_1.default.format('%s%s', this.titlePrefix, 'Error in ' + path_1.default.basename((_a = firstError.file) !== null && _a !== void 0 ? _a : '')), message: firstError.message, icon: path_1.default.join(__dirname, 'images/error.png'), }; } if (firstWarning && !this.options.excludeWarnings) { this.lastBuildSucceeded = false; return { title: util_1.default.format('%s%s', this.titlePrefix, 'Warning in ' + path_1.default.basename((_b = firstWarning.file) !== null && _b !== void 0 ? _b : '')), message: firstWarning.message, icon: path_1.default.join(__dirname, 'images/warning.png'), }; } if (!this.options.skipSuccessful && (!this.lastBuildSucceeded || this.options.alwaysNotify)) { this.lastBuildSucceeded = true; return { title: util_1.default.format('%sType check succeeded', this.titlePrefix), message: util_1.default.format('%s%s', 'No type errors!', firstWarning ? ' See warning(s) in console!' : ''), icon: path_1.default.join(__dirname, 'images/built.png'), }; } } apply(compiler) { // webpack 4+ try { fork_ts_checker_webpack_plugin_1.default .getCompilerHooks(compiler) .issues.tap('fork-ts-checker-notifier-webpack-plugin', this.compilationDone); } catch (error) { console.error(` Something went wrong in accessing the hooks. Most likely the order of plugins is wrong.\n Check the documentation for "fork-ts-checker-notifier-webpack-plugin"\n `); throw Error(`Error: ${error}`); } } } module.exports = ForkTsCheckerNotifierWebpackPlugin;