-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulp-tenon.js
78 lines (72 loc) · 2.24 KB
/
gulp-tenon.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
'use strict';
var clone = require('clone'),
through = require('through2'),
merge = require('merge'),
chalk = require('chalk'),
fs = require('fs'),
tenon = require('tenon-api-client')
;
var PLUGIN_NAME = 'gulp-tenon';
// console.log(PLUGIN_NAME);
module.exports = function(opts) {
var options = merge({config: '.tenonrc'}, opts),
failed = 0,
snippet = options.snippet,
writePath = options.saveOutputIn,
allOut = {}
;
delete options.snippet;
delete options.saveOutputIn;
function fmtFilename(file) {
var fp = file.path,
lix;
if (file.cwd && fp.indexOf(file.cwd) === 0) {
fp = fp.slice(file.cwd.length + 1);
}
fp = fp.replace(/\\/g, '/');
lix = fp.lastIndexOf('/');
if (lix < 0) {
return chalk.bold(fp);
}
return fp.slice(0, lix+1) + chalk.bold(fp.slice(lix+1));
}
return through.obj(function(file, enc, cb) {
var fopts = clone(options, false);
fopts.url = file.path;
tenon(fopts, function(err, data) {
var fname = fmtFilename(file);
if (err) {
console.log('\n' + fname + '\n' + chalk.red.bgWhite('Tenon error:') + ' ' + err.slice(0,500));
} else {
if (data.resultSetFiltered.length > 0) {
failed += 1;
console.log('\n' + fname);
data.resultSetFiltered.forEach(function(itm) {
console.log(chalk.red('>>') + ' tID: ' + itm.tID);
console.log(' bpID: ' + itm.bpID);
console.log(' Title: ' + itm.errorTitle);
console.log(' Xpath: ' + itm.xpath);
if (snippet) {
console.log(' Snippit:');
console.log(chalk.gray(itm.errorSnippet.replace(/</g, '<').replace(/>/g, '>')));
}
});
} else {
console.log('\n' + fname + chalk.green(' >> OK'));
}
if (writePath) {
delete data.resultSetFiltered;
allOut[file.path] = data;
}
}
cb();
});
}, function() {
if (writePath && Object.keys(allOut).length > 0) {
fs.writeFileSync(writePath, JSON.stringify(allOut, null, ' '), {encoding: 'utf8'});
}
if (failed) {
console.log(chalk.yellow('\nFiles with errors: ' + failed + '\n'));
}
});
};