forked from Khan/live-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
283 lines (237 loc) · 8.87 KB
/
gulpfile.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var http = require("http");
var zlib = require("zlib");
var fs = require("fs");
var path = require("path");
var gulp = require("gulp");
var concat = require("gulp-concat");
var uglify = require("gulp-uglify");
var newer = require("gulp-newer");
var changed = require("gulp-changed");
var handlebars = require("gulp-handlebars");
var defineModule = require("gulp-define-module");
var declare = require("gulp-declare");
var runSequence = require("run-sequence");
var mochaPhantomJS = require("gulp-mocha-phantomjs");
var staticServe = require("node-static");
var request = require("request");
var gutil = require("gulp-util");
var babel = require("./babel-plugin.js");
var gulpIf = require("gulp-if");
var chmod = require("gulp-chmod");
var eol = require("gulp-eol");
var mochaRunner = require("./testutil/gulp-mocha-runner.js");
var check = require("./check.js");
var paths = require("./build-paths.json");
gulp.task("templates", function() {
gulp.src(paths.templates)
.pipe(changed("build/tmpl", {extension: ".js"}))
.pipe(handlebars({
handlebars: require("handlebars")
}))
.pipe(defineModule("plain"))
.pipe(declare({
namespace: "Handlebars.templates"
}))
.pipe(gulp.dest("build/tmpl"));
});
var firstBuild = true;
var scriptTypes = Object.keys(paths.scripts);
scriptTypes.forEach(function(type) {
gulp.task("script_" + type, ["templates"], function() {
var outputFileName = "live-editor." + type + ".js";
var srcPath = path.join(__dirname, "js");
return gulp.src(paths.scripts[type])
.pipe(firstBuild ? gutil.noop() : newer("build/js/" + outputFileName))
.pipe(gulpIf(function (file) {
// transform source files but not dependencies
return file.path.indexOf(srcPath) === 0;
}, babel({ blacklist: ["strict"] })))
.pipe(concat(outputFileName))
.pipe(chmod(644))
.pipe(eol("\n"))
.pipe(gulp.dest("build/js"));
});
gulp.task("script_" + type + "_min", ["script_" + type], function() {
var outputFileName = "live-editor." + type + ".min.js";
return gulp.src(["build/js/live-editor." + type + ".js"])
.pipe(firstBuild ? gutil.noop() : newer("build/js/" + outputFileName))
.pipe(uglify())
.pipe(concat(outputFileName))
.pipe(chmod(644))
.pipe(eol("\n"))
.pipe(gulp.dest("build/js"));
});
});
gulp.task("scripts", scriptTypes.map(function(type) {
return "script_" + type;
}));
gulp.task("scripts_min", scriptTypes.map(function(type) {
return "script_" + type + "_min";
}));
gulp.task("workers", function() {
gulp.src(paths.workers_webpage)
.pipe(gulp.dest("build/workers/webpage"));
gulp.src(paths.workers_pjs)
.pipe(gulp.dest("build/workers/pjs"));
gulp.src(paths.workers_shared)
.pipe(gulp.dest("build/workers/shared"));
});
gulp.task("externals", function() {
gulp.src(paths.externals, {base: "./"})
.pipe(gulp.dest("build/"));
});
var styleTypes = Object.keys(paths.styles);
styleTypes.forEach(function(type) {
gulp.task("style_" + type, function() {
var outputFileName = "live-editor." + type + ".css";
return gulp.src(paths.styles[type])
.pipe(firstBuild ? gutil.noop() : newer("build/css/" + outputFileName))
.pipe(concat(outputFileName))
.pipe(gulp.dest("build/css"));
});
});
gulp.task("styles", styleTypes.map(function(type) {
return "style_" + type;
}));
gulp.task("images", function() {
gulp.src(paths.images)
.pipe(gulp.dest("build/images"));
});
gulp.task("watch", function() {
scriptTypes.forEach(function(type) {
gulp.watch(paths.scripts[type], ["script_" + type]);
});
styleTypes.forEach(function(type) {
gulp.watch(paths.styles[type], ["style_" + type]);
});
gulp.watch(paths.templates, ["templates"]);
gulp.watch(paths.workers_pjs.concat(paths.workers_webpage)
.concat(paths.workers_shared), ["workers"]);
gulp.watch(paths.images, ["images"]);
});
var runTest = function(fileName) {
return function() {
// We need to set up a server to host the content
// Unfortunately we can't just run it from a file:// url
// as web workers don't like working in that way.
var fileServer = new staticServe.Server("./");
var server = http.createServer(function(req, res) {
req.addListener("end", function() {
fileServer.serve(req, res);
}).resume();
});
server.listen(11537);
// We then run the Mocha tests in a headless PhantomJS
var stream = mochaPhantomJS();
stream.write({
path: "http://localhost:11537/tests/" + fileName
});
stream.end();
stream.on("finish", function() {
server.close();
});
// Returning the stream lets Gulp know when the tests have
// finished running.
return stream;
};
};
var failureCount = 0;
// We run tests in groups so that we don't require as much memory to run them
// in Travis-CI.
var pjs_tests = ["jshint", "output", "assert", "ast_transform"];
pjs_tests.forEach(function(test) {
gulp.task("test_output_pjs_" + test, ["script_output_pjs"], function() {
return gulp.src("tests/output/pjs/index.html")
.pipe(mochaRunner({ test: test + "_test.js" }))
.on('error', function (err) {
failureCount += parseInt(err.message);
this.emit('end');
});
});
});
gulp.task("test_output_pjs", function(callback) {
var sequence = pjs_tests.map(function(test) {
return "test_output_pjs_" + test;
});
sequence.push(callback);
runSequence.apply(null, sequence);
});
// TODO(kevinb) add this to test_output_pjs after adding try/catch to event handlers
gulp.task("test_output_pjs_async", ["script_output_pjs"], function() {
return gulp.src("tests/output/pjs/index.html")
.pipe(mochaRunner({ test: "async_test.js" }))
.on('error', function (err) {
failureCount += parseInt(err.message);
this.emit('end');
});
});
var webpage_tests = ["assert", "output", "transform"];
webpage_tests.forEach(function(test) {
gulp.task("test_output_webpage_" + test, ["script_output_pjs"], function() {
return gulp.src("tests/output/webpage/index.html")
.pipe(mochaRunner({ test: test + "_test.js" }))
.on('error', function (err) {
failureCount += parseInt(err.message);
this.emit('end');
});
});
});
gulp.task("test_output_webpage", ["script_output_webpage"], function(callback) {
var sequence = webpage_tests.map(function(test) {
return "test_output_webpage_" + test;
});
sequence.push(callback);
runSequence.apply(null, sequence);
});
// TODO(kevinb): Uncomment when phantomJS has been upgraded to 2.0
//gulp.task("test_output_sql", ["script_output_sql"], function() {
// return gulp.src("tests/output/sql/index.html")
// .pipe(mochaRunner());
//});
gulp.task("test_tooltips", ["script_tooltips"], function() {
return gulp.src("tests/tooltips/index.html")
.pipe(mochaRunner());
});
gulp.task("check_errors", [], function() {
process.exit(failureCount);
});
// TODO(kevinb7): Add task for debugger tests once ES5 is supported
// NOTE(jeresig): We don't bundle this data as it's kind of big. Better to
// download it dynamically, when we need it.
var recordDataURL = "https://s3.amazonaws.com/ka-cs-scratchpad-audio/" +
"tests/recording-data.json.gz";
var recordDataFile = "tests/record/recording-data.json";
gulp.task("test_record_data", function(done) {
if (fs.existsSync(recordDataFile)) {
return done();
}
return request(recordDataURL)
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream(recordDataFile));
});
// NOTE(jeresig): Not included in the main tests yet, as they take a
// long time to run.
// TODO(kevinb) find out about recording-data.json
gulp.task("test_record", ["test_record_data"],
runTest("record/index.html"));
gulp.task("test", function(callback) {
// test_output_sql is intentionally left out for now until
// phantomJS has support for typed arrays
runSequence("test_output_pjs", "test_output_webpage", "test_tooltips", "check_errors", callback);
});
// Check to make sure all source files and dependencies exist before building.
gulp.task("check", function() {
var missing = check();
if (missing.length > 0) {
console.log("Aborting build");
process.exit();
} else {
console.log("all files exist");
}
});
gulp.task("build",
["check", "templates", "scripts", "workers", "styles", "images", "externals"],
function() {
firstBuild = false;
});
gulp.task("default", ["watch", "build"]);