Skip to content

Commit

Permalink
4.1.0 - show error (#3)
Browse files Browse the repository at this point in the history
* show error
* 4.1.0
  • Loading branch information
A-312 committed Aug 9, 2019
1 parent 74aa62f commit 562800b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use strict";

const through2 = require("through2"),
assign = require("object-assign"),
PluginError = require("plugin-error"),
Expand Down Expand Up @@ -39,6 +37,7 @@ module.exports = function (options) {
if (res.error) {
res.error.filePath = file.path;
res.error.fileContent = (typeof build === "string") ? build : build[res.error.filename];
printError(res.error);
return next(new PluginError('gulp-terser', res.error));
}

Expand All @@ -51,3 +50,34 @@ module.exports = function (options) {
return next(null, file);
});
};

function alignLine(num, text, longer, maxsize) {
let maxlength = process.stdout.columns - 1;
num = num + Array(longer - (num + "").length).join(" ");
maxlength -= num.length + 3;
console.log("\033[36m" + num + " |\033[00m " + text.slice(0, (maxsize < maxlength) ? maxlength : maxsize));
}

function printError(ev) {
if (!ev.fileContent || (ev.line === undefined && ev.col === undefined))
return console.error(ev.stack || ev);

const fileContent = ev.fileContent;
const lines = fileContent.replace(/\t/g, " ").split("\n"),
more = (ev.line + " ").length,
col = ev.col + 1,
pos = (more + 2) + fileContent.split("\n")[ev.line - 1].replace(/[^\t]/g, "").length * 3 + parseInt(col);

console.log(`\nError with ${ev.plugin} :`);
for (let i = ev.line - 5; i < ev.line; i++) {
if (lines[i]) {
alignLine(i + 1, lines[i], more, pos);
}
}

console.log(Array(pos).join(" ") + "\033[91m^\033[00m");
console.log("(" + ev.name + ") " + ev.message + " (line : \033[96m" + ev.line + "\033[00m, col : \033[96m" + col + "\033[00m).");
console.log(`in : ${ev.filePath}\n`);

return this.emit('end');
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-terser-js",
"version": "4.0.2",
"version": "4.1.0",
"description": "A Terser-js plugin for Gulp",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 562800b

Please sign in to comment.