This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
make.js
executable file
·267 lines (231 loc) · 7.16 KB
/
make.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*global cat,echo,exec,exit,find,target */
var async = require( "async" ),
path = require( "path" ),
normalize = function( p ){ return path.normalize( p ); },
// Make Windows happy, use `node <path>`
nodeExec = function( p ){ return 'node "' + p + '"'; },
JSLINT = nodeExec( normalize( "./node_modules/jshint/bin/jshint" ) ),
html5lint = require( "html5-lint" ),
// Global var for exit code
passed = true;
require("shelljs/make");
function lessToCSS( lessFile, callback ) {
var less = require( "less" );
var fileContents = cat( lessFile );
var parser = new less.Parser({
filename: lessFile,
paths: [ path.dirname( lessFile ) ]
});
parser.parse( fileContents, function( err, tree ) {
if ( err ) {
callback( err );
return;
}
try {
var css = tree.toCSS();
callback( null, css );
} catch ( ex ) {
callback( ex );
}
});
}
function checkCSSFile( cssFile, filename ) {
var csslint = require( "csslint" ).CSSLint;
// 0 = disabled, 1 = warning, 2 = error
// run csslint --list-rules to see more
var rules = {
//"important": 2,
//"adjoining-classes": 2,
"known-properties": 2,
//"box-sizing": 2,
//"box-model": 2,
//"overqualified-elements": 2,
"display-property-grouping": 2,
//"bulletproof-font-face": 2,
//"compatible-vendor-prefixes": 2,
//"regex-selectors": 2,
"errors": 2,
//"duplicate-background-images": 2,
"duplicate-properties": 2,
"empty-rules": 2,
"selector-max-approaching": 2,
"gradients": 2,
//"fallback-colors": 2,
//"font-sizes": 2,
"font-faces": 2,
//"floats": 2,
//"star-property-hack": 2,
//"outline-none": 2,
"import": 2,
//"ids": 2,
"underscore-property-hack": 2,
"rules-count": 2,
//"qualified-headings": 2,
"selector-max": 2,
"shorthand": 2,
"text-indent": 2,
//"unique-headings": 2,
//"universal-selector": 2,
//"unqualified-attributes": 2,
"vendor-prefix": 2,
"zero-units": 2
};
var result = csslint.verify( cssFile, rules );
result.messages.filter( function( msg ) {
// If the evidence string contains 'csslint-ignore' then skip it
return !msg.evidence.match( /csslint-ignore/ );
}).forEach( function( msg ) {
passed = false;
console.error( "%s: line %d, col %d, Error - %s",
filename, msg.line, msg.col, msg.message );
});
}
function checkCSS( callback ) {
echo( "" );
echo( "# Linting CSS and LESS files" );
var files = [
"public/css/butter.ui.less",
"public/css/transitions.less",
"public/css/embed.less",
"public/css/embed-shell.less",
"public/templates/assets/css/jquery-ui/jquery.ui.butter.less",
"public/templates/assets/plugins/twitter/popcorn.twitter.less",
"public/templates/assets/plugins/wikipedia/popcorn.wikipedia.less",
"public/templates/basic/style.less"
];
files = files.concat( find( "public/" ).filter( function( filename ) {
return filename.match( /\.css$/ ) && !filename.match( /^public\/test/ ) &&
!filename.match( /^public\/external/ );
})).sort();
var q = async.queue( function( lessFile, cb ) {
lessToCSS( lessFile, function( err, cssFile ) {
echo( "## " + lessFile );
if ( err ) {
passed = false;
console.error( "%sError: %s in %s:%d:%d",
err.type, err.message, err.filename, err.line, err.column );
cb();
return;
}
checkCSSFile( cssFile, lessFile );
cb();
});
}, 1);
q.drain = function() {
callback();
};
q.push( files );
}
function checkJS() {
var dirs = [ "*.js", "lib/", "public/src/", "public/templates/", "routes/", "test/" ];
echo( "# Linting JS files" );
dirs.forEach( function( value ) {
echo( "## `" + value + "`" );
});
// Get all js and json files in dirs
var files = dirs.join( " " );
passed = !exec( JSLINT + " " + files + " --extra-ext json" ).code && passed;
}
var desc = {
check: "Lint CSS, HTML, and JS",
server: "Run the development server"
};
target.all = function() {
echo("Please specify a target. Available targets:");
Object.keys(target).sort().filter(function(t) {
return t !== "all";
}).forEach(function(t) {
echo(" " + t + " - " + desc[t]);
});
};
function checkHTML( callback ) {
// Poor-man's HTML Doc vs. Fragment check
function isHTMLFragment( filename ) {
return !( /<html[^>]*\>/m ).test( cat( filename ) );
}
// List of errors/warnings to ignore, some with a conditional
// to only ignore when some condition is true.
var ignoreList = [
{
// Don't warn on valid docs
text: "The document is valid HTML5 + ARIA + SVG 1.1 + MathML 2.0 (subject to the utter previewness of this service)."
},
{
text: "Start tag seen without seeing a doctype first. Expected “<!DOCTYPE html>”.",
when: isHTMLFragment
},
{
text: "Element “head” is missing a required instance of child element “title”.",
when: isHTMLFragment
},
{
text: "Bad value “X-UA-Compatible” for attribute “http-equiv” on element “meta”."
},
{
text: "Warning: The character encoding of the document was not declared."
},
{
text: "Attribute “mozallowfullscreen” not allowed on element “iframe” at this point."
},
{
text: "Attribute “webkitallowfullscreen” not allowed on element “iframe” at this point."
},
{
text: "Attribute “allowfullscreen” not allowed on element “iframe” at this point."
},
{
// Let <style> be in fragments.
text: "Element “style” not allowed as child of element “body” in this context. (Suppressing further errors from this subtree.)",
when: isHTMLFragment
},
{
// Let <li> be in fragments.
text: "Element “li” not allowed as child of element “body” in this context. (Suppressing further errors from this subtree.)",
when: isHTMLFragment
}
];
echo( "" );
echo( "# Linting HTML Files" );
var q = async.queue( function( htmlFile, cb ) {
html5lint( cat( htmlFile ), function( err, messages ) {
echo( "## " + htmlFile );
messages.messages.forEach( function( msg ) {
var ignored = ignoreList.some( function( ignore ) {
return ignore.text === msg.message && ( !ignore.when || ( ignore.when && ignore.when( htmlFile ) ) );
});
if ( !ignored ) {
console.log( "Error: %s in %s:%d:%d", msg.message, htmlFile, msg.lastLine, msg.lastColumn );
}
});
cb();
});
});
q.drain = function() {
callback();
};
q.push( find( "public/" ).filter( function( file ) {
return file.match( /\.html$/ ) && !file.match( /^public\/test/ ) && !file.match( /^public\/external/ );
}));
}
target.check = function() {
async.series([
function( callback ) {
checkJS();
callback();
},
function( callback ) {
checkCSS( callback );
},
function( callback ) {
checkHTML( callback );
},
function( callback ) {
exit( passed ? 0 : 1 );
callback();
}
]);
};
target.server = function() {
echo("### Serving butter");
require( "./server.js" );
};