This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
forked from quirkey/sammy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJakefile.js
209 lines (173 loc) · 6.08 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
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
var fs = require('fs'),
path = require('path'),
child_process = require('child_process'),
rexp_minified = new RegExp("\\.min\\.js$"),
rexp_src = new RegExp('\\.js$'),
version;
// Modified from lukebayes: http://gist.github.com/814063
function forEachFile(root, cbFile, cbDone) {
var count = 0;
function scan(name) {
++count;
fs.stat(name, function (err, stats) {
if (err) cbFile(err);
if (stats.isDirectory()) {
fs.readdir(name, function (err, files) {
if (err) cbFile(err);
files.forEach(function (file) {
scan(path.join(name, file));
});
done();
});
} else if (stats.isFile()) {
cbFile(null, name, stats, done);
} else {
done();
}
});
}
function done() {
--count;
if (count === 0 && cbDone) cbDone();
}
scan(root);
}
desc('Pulls the current version from lib/sammy.js');
task('version', function () {
var sammyjs = fs.readFileSync('lib/sammy.js'),
regex_version = new RegExp("Sammy.VERSION = '([\\d\\w\\.]+)'");
version = regex_version.exec(sammyjs)[1];
console.log("VERSION: " + version);
});
desc('Uses Uglify-js to minify lib/sammy.js and all plugins');
task({ 'minify': [ 'version' ] }, function () {
forEachFile('lib', function (err, file, stats, cbDone) {
if (err) throw err;
if (!rexp_minified.test(file)) {
cbDone();
} else {
fs.unlink(file, function (err) {
if (err) throw err;
cbDone();
});
}
}, function() {
var rexp_base_name = new RegExp("^lib/(.+)\\.js$"),
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify,
dateformatjs = require('dateformatjs'),
dateformat = new dateformatjs.DateFormat(dateformatjs.DateFormat.ISO8601);
forEachFile('lib', function (err, file, stats, cbDone) {
if (err) throw err;
var m = rexp_base_name.exec(file);
if (!m) {
cbDone();
} else {
fs.readFile(file, 'utf8', function (err, src) {
if (err) throw err;
var minified = [
"// -- Sammy.js -- " + m[1] + ".js",
"// http://sammyjs.org",
"// Version: " + version,
"// Built: " + dateformat.format(new Date()) + "\n"
].join("\n") + pro.gen_code(pro.ast_squeeze(pro.ast_mangle(jsp.parse(src)))),
dir = 'lib/min';
fs.writeFile(path.join(dir, m[1] + "-" + version + ".min.js"), minified, function (err) {
if (err) throw err;
fs.writeFile(path.join(dir, m[1] + "-latest.min.js"), minified, function (err) {
if (err) throw err;
console.log("Minified " + file);
cbDone();
});
});
});
}
}, function() {
console.log('Done.');
complete();
});
});
}, true);
function simple_exec(cmd, cbDone) {
child_process.exec(cmd, function (err, stdout, stderr) {
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
if (err) throw err;
if (cbDone) cbDone();
});
}
function run_tests() {
var cmd = process.env.TEST_CMD,
test = process.env.TEST || path.join(__dirname, 'test', 'index.html');
if (!cmd) cmd = '/usr/bin/gnome-open';
if (fs.statSync(cmd).isFile()) {
simple_exec(cmd + ' ' + test);
} else {
simple_exec('open ' + test);
}
}
// Modified from peterc: http://gist.github.com/113226
desc("Automatically run something when code is changed");
task('autotest', function () {
var files = {};
function run() {
var changed = false;
forEachFile(__dirname, function (err, file, stats, cbDone) {
if (err) throw err;
var ctime = stats.ctime.toString();
if (rexp_src.test(file) && ctime !== files[file]) {
files[file] = ctime;
if (!changed) {
changed = true;
console.log("Running at " + new Date());
run_tests();
}
}
cbDone();
}, function () {
if (changed) console.log("\nWaiting for a *.js change.");
setTimeout(run, 1000);
});
}
run();
});
desc('launch the test file in the browser');
task('test', function () {
run_tests();
});
desc('Tag with the current version');
task({ 'tag': [ 'version' ] }, function () {
simple_exec([
"git add .",
"git commit -a -m'Pushing version " + version + "'",
"git tag v" + version,
"git push --tags"
].join(' && '));
});
desc('Prepare for release.');
task({ 'release': [ 'minify' ] }, function () {
jake.Task.tag.invoke();
});
desc('Generate the docs for the current version to DIR');
task({ 'docs': [ 'version' ] }, function () {
if (process.env.VERSION) version = process.env.VERSION;
simple_exec("ruby vendor/jsdoc/jsdoc.rb '" + process.env.DIR + "' '" + version + "' lib/ lib/plugins/");
});
desc('Check sources with JSHint.');
task('lint', function () {
var lint = [ './node_modules/.bin/jshint', 'Jakefile.js' ];
forEachFile('lib', function (err, file, stats, cbDone) {
if (err) throw err;
if (!rexp_minified.test(file) && rexp_src.test(file)) {
lint.push(file);
}
cbDone();
}, function() {
child_process.exec(lint.join(' '), function (err, stdout, stderr) {
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
if (err && err.code !== 1) throw err;
complete();
});
});
}, true);