forked from janl/mustache.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jakefile
125 lines (104 loc) · 2.72 KB
/
Jakefile
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
var fs = require('fs'),
util = require('util'),
jshint = require('jshint'),
uglify = require('uglify-js');
function copyFile(src, dst, cb) {
function copy(err) {
var is, os;
if (!err) {
return cb(new Error("File " + dst + " exists."));
}
fs.stat(src, function (err) {
if (err) {
return cb(err);
}
is = fs.createReadStream(src);
os = fs.createWriteStream(dst);
util.pump(is, os, cb);
});
}
fs.stat(dst, copy);
}
function makeDirectoryIfNotExists(path) {
try {
var stats = fs.statSync(path);
if (!stats.isDirectory()) {
fs.mkdirSync(path, 0);
}
} catch (e) {
fs.mkdirSync(path, 0);
}
}
desc('Code Lint');
task('lint', function() {
var all = fs.readFileSync('mustache.js').toString();
var result = jshint.JSHINT(all, {}, {});
if (!result) {
for (var i = 0; i < jshint.JSHINT.errors.length; i++) {
var error = jshint.JSHINT.errors[i];
console.error('line: ' + error.line + ', char ' + error.character + ': ' + error.reason);
}
}
});
desc('Obfuscation and Compression');
task('minify', ['lint'], function() {
var all = fs.readFileSync('mustache.js').toString(),
out = fs.openSync('mustache.min.js', 'w+'),
ast = uglify.parser.parse(all);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
fs.writeSync(out, uglify.uglify.gen_code(ast));
});
task('package', function() {
function package(id, location) {
var files = [
, 'mustache.js'
];
files.unshift('mustache-' + id + '/mustache.js.tpl.pre');
files.push('mustache-' + id + '/mustache.js.tpl.post');
var all = '';
files.forEach(function(file, i) {
all += fs.readFileSync(file).toString();
all += '\n';
});
var outPath;
if (location) {
makeDirectoryIfNotExists('packages/' + id);
if (location === true) {
outPath = 'packages/' + id + '/mustache.js';
} else {
outPath = 'packages/' + id + '/' + location + '/mustache.js';
makeDirectoryIfNotExists('packages/' + id + '/' + location);
}
} else {
outPath = 'packages/' + id + '.mustache.js';
}
var out = fs.openSync(outPath, 'w+');
fs.writeSync(out, all);
}
var params = Array.prototype.slice.call(arguments);
makeDirectoryIfNotExists('packages');
for (var i = 0, n = params.length; i<n; ++i) {
switch (params[i].toLowerCase()) {
case 'jquery':
package('jquery');
break;
case 'commonjs':
package('commonjs', true);
copyFile('mustache-commonjs/package.json', 'packages/commonjs/package.json');
break;
case 'dojox':
package('dojox', 'string');
break;
case 'yui3':
package('yui3', 'mustache');
break;
case 'requirejs':
package('requirejs');
break;
default:
break;
}
}
});
task('default', ['minify'], function() {});