-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (77 loc) · 3.33 KB
/
index.js
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
/**
* Created by mewtwo on 2017/3/13.
*/
var _ = require('lodash')
var formatter = require('eslint-friendly-formatter')
var standard = require('eslint-config-standard')
var config = require('./config.json')
var cheerio = require('cheerio')
var fs = require('fs')
/**
* @description html实体转换为javascript字符串
* @param {string} str 需要格式化的html字符串
*/
function html2js(str) {
var htmlArr = str.replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").replace(/\"/g, "\\\"").split('\n')
var len = htmlArr.length
var outArr = []
//
_.forEach(htmlArr, function (value,index) {
if (value !== "") {
if (index === len - 1) {
outArr.push("\"" + value + "\"")
} else {
outArr.push("\"" + value + "\\n"+"\"+\n")
}
}
})
return outArr.join("")
}
module.exports = function (content, file, conf) {
// 合并standard的rules和plugins
_.merge(config.plugins, standard.plugins)
_.merge(config.rules, standard.rules)
// 合并传入参数
_.merge(config,conf)
// 调用eslint node api
var CLIEngine = require("eslint").CLIEngine
// 实例化并传入配置信息
var cli = new CLIEngine(config)
// 返回eslint检测结果
var report = cli.executeOnText(content)
// 判断是否有错误信息
if (report.errorCount || report.warningCount) {
// 格式化错误信息
var output = formatter(report.results)
// 读取belss字符
fs.readFile(__dirname+'/bless.txt', {flag: 'r'}, function (err,bless) {
var jsStr = 'console.error('+ html2js(output) +');\n'
var idStr = 'console.error("-------------------- '+ file.id +' --------------------");\n'
var htmlStr = 'var errorDom=document.getElementById("errorDom");if(errorDom){errorDom.innerHTML='+bless+'}else{document.write("<div id=errorDom style=\'background: rgba(0,0,0,0.8);display: flex;position:absolute;top:0;width:100%;height:100%;color:red;font-size: .2rem;justify-content:center;align-items:Center;\'>\"+'+bless+'+\"</div>")};'
var outStr = htmlStr+idStr+jsStr
fs.unlink(fis.project.getTempRoot() + '/www/debug-lint.js', function () {
fs.writeFile(fis.project.getTempRoot() + '/www/debug-lint.js', outStr, {flag: 'a'}, function (err) {
if(err) {
console.error(err)
} else {
}
})
})
})
// 在控制台展示错误信息
fis.log.info("---------------------------------------- "+file.id+" ----------------------------------------\n\n\n"+output)
// return
// 判断没有错误
}else{
// 删除html中的错误提示
var htmlStr = 'var errorDom=document.getElementById("errorDom");if(errorDom){document.getElementsByTagName("body")[0].removeChild(errorDom)};'
fs.unlink(fis.project.getTempRoot() + '/www/debug-lint.js', function () {
fs.writeFile(fis.project.getTempRoot() + '/www/debug-lint.js', htmlStr, {flag: 'a'}, function (err) {
if(err) {
console.error(err)
} else {
}
})
})
}
}