-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathJakefile.js
148 lines (108 loc) · 3.07 KB
/
Jakefile.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
var // Program refs
fs = require("fs"),
sys = require("util"),
uglify = require("uglify-js"),
jshint = require("jshint").JSHINT,
print = require("sys").print,
util = require("util"),
exec = require("child_process").exec,
child;
const FILES = {
src: [
"popcorn.capture.js"
]
},
SRC_DIR = "src/",
DIST_DIR = "dist/",
PROJECT = "popcorn.capture";
desc("Uglify JS");
task("minify", [ "hint" ], function( params ) {
print( "\nUglifying..." );
var ast, out,
files, type;
for ( type in FILES ) {
all = "";
// Concatenate JavaScript resources
FILES[ type ].forEach(function( file, i ) {
if ( file.match(/^.*js$/) && file ) {
all += fs.readFileSync( SRC_DIR + file ).toString();
}
});
// Outout concatenated
out = fs.openSync( DIST_DIR + PROJECT + ".js", "w+" );
fs.writeSync( out, all );
// Create AST from concatenated sources
ast = uglify.parser.parse( all );
// Open output stream
out = fs.openSync( DIST_DIR + PROJECT + ".min.js", "w+" );
// Compress AST
ast = uglify.uglify.ast_mangle( ast );
ast = uglify.uglify.ast_squeeze( ast );
// Output regenerated, compressed code
fs.writeSync( out, uglify.uglify.gen_code( ast ) );
}
print( "Success!\n" );
});
desc("JSHint");
task("hint", [], function( params ) {
print( "\nHinting...\n" );
function hintFile( file ) {
var src = fs.readFileSync( SRC_DIR + file, "utf8"),
ok = {
// warning.reason
"Expected an identifier and instead saw 'undefined' (a reserved word).": true,
"Use '===' to compare with 'null'.": true,
"Use '!==' to compare with 'null'.": true,
"Expected an assignment or function call and instead saw an expression.": true,
"Expected a 'break' statement before 'case'.": true,
"'e' is already defined.": true,
// warning.raw
"Expected an identifier and instead saw \'{a}\' (a reserved word).": true
},
found = 0, errors, warning;
jshint( src, { evil: true, forin: false, maxerr: 100 });
errors = jshint.errors;
for ( var i = 0; i < errors.length; i++ ) {
warning = errors[i];
if ( warning ) {
//print( w );
if ( !ok[ warning.reason ] && !ok[ warning.raw ] ) {
found++;
print( "\n" + file + " at L" + warning.line + " C" + warning.character + ": " + warning.reason );
print( "\n" + warning.evidence + "\n");
}
}
}
if ( found > 0 ) {
print( "\n\n" + found + " Error(s) found.\n" );
} else {
print( " " + file + " => Success!\n" );
}
}
FILES.src.forEach(function( file, i ) {
hintFile( file );
})
});
task("clean", [], function( params ) {
print( "\nCleaning...\n\n" );
FILES.src.forEach(function( file, i ) {
exec("rm " + DIST_DIR + file,
function( error, stdout, stderr ) {
if ( error !== null && !/No such file/.test( error ) ) {
console.log( error );
} else {
// no such file errors will be allowed through, just ignore them
if ( error !== null ) {
console.log(" deleted: " + file );
}
}
}
);
if ( files.length - 1 === i ) {
print("Completed.\n");
}
});
});
task("default", [ "hint", "minify" ], function( params ) {
print( "\n" );
});