-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhtml-lint.js
executable file
·127 lines (103 loc) · 3.35 KB
/
html-lint.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#! /usr/bin/env node
/* jslint node: true */
'use strict';
// usage: `node html-lint http://s.codepen.io/curtisj44/debug/xbQXbV`
// usage: `node html-lint http://s.codepen.io/curtisj44/debug/xbQXbV --verbose`
var
// node
fs = require('fs'),
spawn = require('child_process').spawn,
// dependencies
chalk = require('chalk'),
cheerio = require('cheerio'),
// variables
$,
currentErrors,
errors = 0,
passing = 0,
ERROR_LINTING_FAILURE = 1,
ERROR_HTML_FILE_NOT_FOUND = 127,
isStrict = false,
isVerbose = false,
strictFlag = '--strict',
verboseFlag = '--verbose',
output = '',
tests = require('./lib/tests').tests,
url = process.argv[2],
saveTo = (process.argv[3] && process.argv[3] !== verboseFlag) ? process.argv[3] : 'saved',
savedPath = 'temp/' + saveTo,
saveHtml = spawn('phantomjs', [__dirname + '/src/cli/save-html.js', url, saveTo]),
// methods
init = function () {
$ = cheerio.load(fs.readFileSync(savedPath + '.html'));
detectFlags();
if ($('html').length !== 1) {
console.log(chalk.yellow('Error: Something went wrong. Check the URL.'));
if (isStrict) process.exit(ERROR_HTML_FILE_NOT_FOUND);
return;
}
runTests();
summarize();
},
addSelectorToOutput = function (index, value) {
var
postFormatting = '',
preFormatting = chalk.cyan(' ├─ '),
oneBasedIndex = index + 1;
if (oneBasedIndex === currentErrors) {
postFormatting = '\n';
preFormatting = chalk.cyan(' └─ ');
}
output += chalk.dim(preFormatting + chalk.cyan(oneBasedIndex) + ' ' + $(value) + '\n' + postFormatting);
},
// TODO - this needs a better name
runTests = function () {
console.log(chalk.cyan('Running tests...\n'));
for (var test in tests) {
// console.log(chalk.dim('test: ' + test));
// console.log(chalk.dim('label: ' + tests[test].label));
currentErrors = $(test).length;
if (currentErrors > 0) {
errors += currentErrors;
output += ' ' + chalk.bold.red(currentErrors) + ' ' + tests[test].label + '\n';
if (isVerbose) $(test).each(addSelectorToOutput);
} else {
passing++;
}
}
},
detectFlags = function () {
process.argv.forEach(function (value, index, array) {
if (!isStrict) {
isStrict = value === strictFlag;
}
if (!isVerbose) {
isVerbose = value === verboseFlag;
}
});
},
summarize = function () {
console.log(output);
console.log(chalk.dim(' ' + passing + ' tests passing \n'));
console.log(chalk[errors === 0 ? 'bgGreen' : 'bgRed'](' HTML-Lint found' + chalk.bold(' ' + errors + ' errors') + ' on ' + url + ' '));
console.log('\n');
// Exit immediately with a non-zero return code
if (isStrict && errors > 0) process.exit(ERROR_LINTING_FAILURE);
};
if (!url) {
console.log(chalk.yellow('Error: No URL provided'));
} else {
saveHtml.stdout.on('data', function (data) {
console.log(chalk.yellow(data.toString().replace(/[\n\r]/g, '')));
});
saveHtml.stderr.on('data', function (data) {
console.log(chalk.red('stderr: ' + data));
});
saveHtml.on('exit', function (code) {
// console.log('child process exited with code ' + code);
// if (err) console.log(chalk.yellow('Error: Child process failed with [' + err.code + ']'));
console.log(chalk.yellow('Saved HTML' + ' to: ' + chalk.bold(__dirname + '/' + savedPath + '.html')));
console.log(chalk.yellow('Saved PNG' + ' to: ' + chalk.bold(__dirname + '/' + savedPath + '.png')));
init();
});
}