/
gulpfile.js
583 lines (495 loc) · 21 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
'use strict';
/////// SETTINGS ////////////
//var argv = require('minimist')(process.argv.slice(2));
//console.dir(argv);
//var proEnv = (argv.proEnv == undefined) ? false : true;
var configparam = require('./configparam.json');
var syncfrontedsftp = (configparam === undefined) ? false : ((configparam.sftp.host === undefined) ? false : true);
var syncfrontedlocal = (configparam === undefined) ? false : ((configparam.localserver.localPath === undefined) ? false : true);
var proEnv = (configparam === undefined) ? false : ((configparam.proEnv === undefined) ? false : configparam.proEnv);
if (configparam == undefined) {
syncfrontedsftp = false
} else if (configparam.sftp.enabled == undefined) {
syncfrontedsftp = false
} else {
syncfrontedsftp = configparam.sftp.enabled
}
if (configparam == undefined) {
syncfrontedlocal = false
} else if (configparam.localserver.enabled == undefined) {
syncfrontedlocal = false
} else {
syncfrontedlocal = configparam.localserver.enabled
}
if (configparam == undefined) {
proEnv = false
} else if (configparam.proEnv == undefined) {
proEnv = false
} else {
proEnv = configparam.proEnv
}
console.log("proEnv: " + proEnv);
console.log("syncfrontedsftp: " + syncfrontedsftp);
console.log("syncfrontedlocal: " + syncfrontedlocal);
var pkg = require('./package.json'),
gulp = require('gulp'),
$ = require('gulp-load-plugins')();
var minifyCss = require('gulp-minify-css');
var path = require('path'),
del = require('del'),
fs = require('graceful-fs'),
cordova_lib = require('cordova-lib'),
cdv = cordova_lib.cordova.raw,
browsersync = require('browser-sync'),
amdoptimize = require('amd-optimize'),
mergestream = require('merge-stream'),
eventstream = require('event-stream'),
q = require('q'),
FTPS = require('node-sftp'),
streamqueue = require('streamqueue'),
http = require('http'),
ecstatic = require('ecstatic'),
format = require('format'),
util = require('util');
var srcWEBDir = path.join(__dirname, 'src', 'main', 'web'),
srcWWWDir = path.join(__dirname, 'src', 'main', 'web', 'www'),
cordovaBuildDir = path.join(__dirname, 'target', 'web'),
targetWEBDir = path.join(__dirname, 'target', 'web'),
targetWWWDir = path.join(__dirname, 'target', 'web', 'www'),
targetCSSDir = path.join(__dirname, 'target', 'web', 'www', 'css'),
targetIMGDir = path.join(__dirname, 'target', 'web', 'www', 'img'),
targetVENDORDir = path.join(__dirname, 'target', 'web', 'www', 'vendor');
var AUTOPREFIXER_BROWSERS = [
'ie >= 11',
'ie_mob >= 10',
'ff >= 30',
'chrome >= 34',
'safari >= 7',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
var paths = {
coffeescripts: ['src/main/web/www/**/*.coffee', '!src/main/web/www/vendor/**/*.coffee'],
//jsscripts: ['src/main/web/www/**/*.js', '!src/main/web/www/vendor/**/*.js'],
jsscripts: ['src/main/web/www/**/*.js', '!src/main/web/www/vendor/require/require*.js'],
vendorjsscripts: 'src/main/web/www/vendor/**/*.js',
sassscripts: ['src/main/web/**/*.scss', '!src/main/web/www/vendor/**/*.scss'],
lessscripts: ['src/main/web/**/*.less', '!src/main/web/www/vendor/**/*.less'],
cssscripts: ['src/main/web/**/*.css', '!src/main/web/www/vendor/**/*.css'],
images: 'src/main/web/www/img/**/*',
appconfig: 'src/main/web/config.xml',
indexhtml: 'src/main/web/www/index.html',
mainjs: 'src/main/web/www/main.js',
targetstylecss: 'target/web/www/css/style.css',
targetbrowserfiles: 'target/web/platforms/browser/www/**/*',
targetbrowsercordovafiles: ['target/web/platforms/browser/www/cordova*.js', 'target/web/platforms/browser/www/plugins/**/*'],
targetbrowserwww: 'target/web/platforms/browser/www/',
targetbrowserwwwtmp: 'target/web/platforms/browser/wwwtmp/',
targetbrowserwwwtmpmodules: 'target/web/platforms/browser/wwwtmp/modules',
targetbrowserwwwtmpjsfiles: ['target/web/wwwtmp/**/*.js', '!target/web/wwwtmp/vendor/require/require*.js'],
targetwww: 'target/web/www/',
targetwwwmodules: 'target/web/www/modules',
targetwwwfiles: 'target/web/www/**/*',
targetwwwnotjsfiles: ['target/web/www/**/*', '!target/web/www/**/*.js'],
targetwwwjsfiles: ['target/web/www/**/*.js', '!target/web/www/vendor/require/require*.js'],
targetwwwrequirejsfiles: 'target/web/www/**/require*.js',
htmls: 'src/main/web/www/**/*.html'
};
function errrHandler(e) {
// 控制台发声,错误时beep一下
$.util.beep();
$.util.log(e);
}
// http server Basic Options
var options = {
p: 'http://',
webServer: '0.0.0.0',
webPort: 8888,
index: 'index.html',
cache: 5,
selServer: 'localhost',
selPort: 4444,
mockServer: 'localhost',
mockPort: 9999
};
// List of platforms determined form pkd.dependencies. This way platform file
// downloading and version preferences are entirely handled by npm install.
var platforms = []; // List like ['cordova-ios', 'cordova-android']
var platform_dirs = []; // List of subdirs with platform files under node_moduels
var p;
for (p in cordova_lib.cordova_platforms) {
var pname = 'cordova-' + p;
if (pkg.dependencies[pname]) {
platforms.push(pname);
platform_dirs.push(path.join(__dirname, 'node_modules', pname));
// TODO: Check if those dirs exist and if not ask the user to run "npm install"
}
}
// Plugins can't be stores in package.josn right now.
// - They are published to plugin registry rather than npm.
// - They don't list their dependency plugins in their package.json.
// This might even be impossible because dependencies can be platform specific.
var plugins = pkg.cordovaplugins;
// Platform to use for run/emulate. Alternatively, create tasks like runios, runandroid.
var testPlatform = ['android', 'browser'];
// utils
//var mkdirsSync = module.exports.mkdirsSync =
function mkdirsSync(dirpath, mode) {
if (fs.existsSync(dirpath)) {
return true;
} else {
if (mkdirsSync(path.dirname(dirpath), mode)) {
fs.mkdirSync(dirpath, mode);
return true;
}
}
};
// var mkdirs = module.exports.mkdirs =
function mkdirs(dirpath, mode, callback) {
fs.exists(dirpath, function (exists) {
if (exists) {
callback(dirpath);
} else {
//尝试创建父目录,然后再创建当前目录
mkdirs(path.dirname(dirpath), mode, function () {
fs.mkdir(dirpath, mode, callback);
});
}
});
};
//////////////////////// TASKS /////////////////////////////
gulp.task('css', function () {
return streamqueue(
{objectMode: true},
//gulp.src(paths.lessscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.watch(paths.lessscripts)).pipe($.less()),
//gulp.src(paths.sassscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.watch(paths.sassscripts)).pipe($.sass()),
//gulp.src(paths.cssscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.watch(paths.cssscripts))
gulp.src(paths.lessscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.less()),
gulp.src(paths.sassscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.sass()),
gulp.src(paths.cssscripts).pipe($.plumber({errorHandler: errrHandler}))
)
//.pipe($.plumber({errorHandler: errrHandler}))
.pipe($.cond(proEnv, $.concat('style.css')))
.pipe($.cond(proEnv, $.rename({suffix: '.min'})))
.pipe($.cond(proEnv, minifyCss()))
.pipe(gulp.dest(targetCSSDir));
});
gulp.task('htmls', function () {
return gulp.src(paths.htmls)
.pipe($.plumber({errorHandler: errrHandler}))
//.pipe($.watch(paths.htmls))
.pipe($.cond(proEnv, $.htmlmin({collapseWhitespace: true})))
.pipe($.cond(proEnv, $.replace('main.js', 'main.min.js'))) // todo bugs
.pipe($.cond(proEnv, $.replace('style.css', 'style.min.css')))
.pipe(gulp.dest(targetWWWDir));
});
// 压缩图片
gulp.task('img', function () {
return gulp.src(paths.images)
.pipe($.plumber({errorHandler: errrHandler}))
//.pipe($.watch(paths.images))
.pipe($.imagemin({optimizationLevel: 3, progressive: true, interlaced: true}))
.pipe(gulp.dest(targetIMGDir));
});
gulp.task('requirescripts', function () {
gulp.src('src/main/web/www/vendor/require/require*.js')
.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest('target/web/www/vendor/require/'));
});
gulp.task('scripts', function () {
return streamqueue(
{objectMode: true},
//gulp.src(paths.coffeescripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.watch(paths.coffeescripts)).pipe($.cond(proEnv, $.sourcemaps.init())).pipe($.coffee({"bare":true})),
//gulp.src(paths.jsscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.watch(paths.jsscripts)).pipe($.cond(proEnv, $.sourcemaps.init()))
//gulp.src(paths.coffeescripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.cond(proEnv, $.sourcemaps.init())).pipe($.coffee({"bare": true})),
//gulp.src(paths.jsscripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.cond(proEnv, $.sourcemaps.init()))
gulp.src(paths.coffeescripts).pipe($.plumber({errorHandler: errrHandler})).pipe($.coffee({"bare": true})),
gulp.src(paths.jsscripts).pipe($.plumber({errorHandler: errrHandler}))
)
//.pipe($.plumber({errorHandler: errrHandler}))
//.pipe(amdoptimize())
.
pipe($.jshint())
.pipe($.jshint.reporter('default'))
.pipe(gulp.dest(targetWWWDir));
});
gulp.task('vendorscripts', function () {
return gulp.src(paths.vendorjsscripts)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest(targetVENDORDir));
});
// All cordova-lib calls (except "cordova create") must be done from within
// the cordova project dir because cordova-lib determines projectRoot from
// process.cwd() in cordova-lib/src/cordova/util.js:isCordova()
gulp.task('clean', function (cb) {
// Alternative package for cleaning is gulp-rimraf
return gulp.src('target', {read: false})
.pipe($.rimraf());
//del(['target'], cb);
});
gulp.task('cleanwww', function (cb) {
var deleteFolderRecursive = function (path) {
var files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
// Alternative package for cleaning is gulp-rimraf
// todo platforms 遍历删除
var delfiles = ['!target/web/platforms/browser/www/plugins',
'!target/web/platforms/browser/www/plugins/**/*',
'!target/web/platforms/browser/www/cordova*.js',
'target/web/platforms/browser/www/**/*',
'target/web/platforms/browser/wwwtmp/**/*',
'target/web/platforms/android/ant-build',
'target/web/www/**/*',];
if (syncfrontedlocal) {
deleteFolderRecursive(configparam.localserver.localPath)
//delfiles.push(configparam.localserver.localPath) //
}
if (syncfrontedsftp) {
var ftps = new FTPS({
username: configparam.sftp.user,
password: configparam.sftp.pass,
host: configparam.sftp.host,
port: configparam.sftp.port
}, function (err) {
console.log(err)
});
ftps.rmdir(configparam.sftp.remotePath)
}
return gulp.src(delfiles, {read: false}) // todo 删除sftp 远程目录
.pipe($.rimraf());
//del(['target/web/www/**/*', 'target/web/platforms/browser/www', 'target/web/platforms/android/ant-build'], cb);
});
gulp.task('syncapptmp', function () {
var files = paths.targetbrowsercordovafiles;
files.push(paths.targetwwwfiles);
return gulp.src(files)
//.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest(paths.targetbrowserwwwtmp));
});
gulp.task('synclocal', ['syncapptmp'], function () {
if (proEnv) {
return mergestream(
gulp.src(paths.targetwwwnotjsfiles)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest(paths.targetbrowserwww)),
gulp.src(paths.targetwwwrequirejsfiles)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest(paths.targetbrowserwww)),
gulp.src(paths.targetbrowserwwwtmpjsfiles)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe($.requirejs({
name: 'main',
baseUrl: paths.targetbrowserwwwtmpmodules,
out: 'main.min.js',
mainConfigFile: paths.targetbrowserwwwtmp + '/main.js',
shim: {}
}))
//.pipe($.cond(proEnv, amdoptimize))
//.pipe($.cond(proEnv, $.concat('all.js')))
//.pipe($.cond(proEnv, $.rename({suffix: '.min'})))
.pipe($.cond(proEnv, $.uglify()))
//.pipe($.cond(proEnv, $.sourcemaps.write()))
.pipe(gulp.dest(paths.targetbrowserwww))
);
} else {
return gulp.src(paths.targetwwwfiles)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe(gulp.dest(paths.targetbrowserwww))
//.pipe($.livereload())
}
});
gulp.task('syncfronted', ['synclcoal'], function () {
//console.log("syncfrontedsftp:" + syncfrontedsftp);
return gulp.src(paths.targetbrowserfiles)
.pipe($.plumber({errorHandler: errrHandler}))
.pipe($.changed(paths.targetbrowserfiles))
.pipe($.cond(syncfrontedsftp, $.sftp(configparam.sftp)))
.pipe($.cond(syncfrontedlocal, gulp.dest(configparam.localserver.localPath)))
//.pipe($.livereload())
});
// Prepare is not really needed
gulp.task('appprepare', function () {
process.chdir(cordovaBuildDir);
return cdv.prepare();
});
gulp.task('appbuild', function () {
process.chdir(cordovaBuildDir);
return cdv.build();
});
gulp.task('apprun', function (cb) {
process.chdir(cordovaBuildDir);
return cdv.run({platforms: testPlatform, options: ['--device']});
});
gulp.task('appemulate', function () {
process.chdir(cordovaBuildDir);
return cdv.emulate({platforms: testPlatform});
});
gulp.task('apprelease', function () {
process.chdir(cordovaBuildDir);
return cdv.build({options: ['--release']});
// TODO: copy the apk file(s) out of ./target/.
});
// Create the cordova project under ./app/. This version doesn't use cordova
// create, instead just links config.xml and www/
gulp.task('initdir', ['clean'], function () {
mkdirsSync(targetWWWDir);
return gulp.src(paths.appconfig)
.pipe(gulp.dest(targetWEBDir))
});
gulp.task('recreate', ['initdir'], function () {
console.log("=====recreate");
process.chdir(cordovaBuildDir);
//fs.symlinkSync(path.join('..', 'src', 'main','web','config.xml'), 'config.xml');
//fs.symlinkSync(path.join('..', 'target', 'web', 'www'), 'www');
// We could alternatively copy www and then watch it to copy changes.
// Useful if using SASS CoffeeScrite etc.
// Must first add plugins then platforms. If adding platforms first,
// cordova fails expecting to find the ./build/plugins directory.
// TODO: try 3rd param {cli_variables: {...}}.
return cdv.plugins('add', plugins)
.then(function () {
return cdv.platform('add', platform_dirs);
});
});
// Alternative version of recreate that uses "cordova create" rather than
// creating the links manually.
gulp.task('cdvcreate', ['clean'], function () {
var cfg = {lib: {www: {url: srcWEBDir, link: true}}};
return cdv.create(cordovaBuildDir, pkg.appId, pkg.name, cfg)
.then(function () {
// Further Cordova commands must be run inside the cordova project dir.
process.chdir(cordovaBuildDir);
})
.then(function () {
return cdv.platform('add', platform_dirs);
})
.then(function () {
// TODO: try 3rd param, cli_variables etc.
return cdv.plugins('add', plugins);
})
//.then(function () {
// gulp.src(paths.appconfig)
// .pipe(gulp.dest(targetWEBDir));
//});
});
gulp.task('appcreate', ['cdvcreate'], function () {
return gulp.src(targetWWWDir, {read: false}) // much faster
.pipe($.rimraf());
});
// A task to run a local server instace of the project
gulp.task('startLocalServer', function () {
console.log('cwd: ' + process.cwd());
//@todo: Add a promise here.
http.createServer(ecstatic({
root: __dirname + '/' + paths.targetbrowserwww,
cache: options.cache
})).listen(options.webPort);
util.puts(format('WebServer Listening on %s:%s', options.webServer, options.webPort));
});
gulp.task('startMockLocalServer', function () {
//@todo: Add a promise here.
http.createServer(ecstatic({root: __dirname + '/src/test/svr', cache: options.cache})).listen(options.mockPort);
util.puts(format('WebServer Listening on %s:%s, baseUrl: %s', options.mockServer, options.mockPort, process.cwd()));
});
// A task to open the website URL
gulp.task('open', function () {
var url = format("%s%s:%s/%s", options.p, options.webServer, options.webPort, options.index);
gulp.src("index.html")//@todo: find out why gulp.src
.pipe(open("", {url: url}));
});
// A task to run the Selenium Server Standalone
gulp.task("selenium:start", function () {
var Promise = require("bluebird");
var deferred = Promise.defer();
var jar = require("selenium-server-standalone-jar");
var webdriver = require("selenium-webdriver"),
remote = require("selenium-webdriver/remote"),
chromedriver = require("chromedriver");
var server = new remote.SeleniumServer(jar.path, {
host: options.selServer,
port: options.selPort,
args: [
"-Dwebdriver.chrome.driver=" + chromedriver.path,
"-singleWindow", "-trustAllSSLCertificates", "-browserSessionReuse"
]
});
server.loopbackOnly_ = true; // force use of 127.0.0.1
server.start().then(function (url) {
util.puts("Selenium Standalone server started at " + url);
deferred.resolve(server);
});
return deferred.promise;
});
//gulp.task('utest', ['startLocalServer'], $.shell.task([
gulp.task('utest', $.shell.task([
'intern-client config=src/test/intern'
//'dir'
]));
//gulp.task('ftest', ['startLocalServer', 'startMockLocalServer', "selenium:start"], $.shell.task([
gulp.task('ftest', $.shell.task([
//gulp.task('ftest', $.shell.task([
'intern-runner config=src/test/intern'
]));
// A task to run the tests. It has two dependencies: selenium and local servers.
//gulp.task('itest', ['selenium:start'], function () {
gulp.task('itest', ['selenium:start'], function () {
process.argv = ['node', './node_modules/intern/bin/intern-runner.js', 'config=src/test/intern'];
var runner = require('intern/runner');
//@todo: should be run this using gulp-spawn? We need to know when the process finished.
// We could also 'listen' for the 'runner/end' event to stop selenium and the local server.
// How do you run just: unit testing or just functional testing?
});
//gulp.task('dev', ['css', 'htmls', 'img', 'scripts', 'vendorscripts']);
gulp.task('dev', ['css', 'htmls', 'img', 'requirescripts', 'scripts']);
gulp.task('watchdev', function () {
gulp.run('css', 'htmls', 'img', 'requirescripts', 'scripts');
// 监听html文件变化
gulp.watch(paths.htmls, ['htmls']);
// Watch .css .less .scss files
gulp.watch('src/main/web/www/css/**/*', ['css']);
// Watch require*.js files
gulp.watch(['src/main/web/www/vendor/require/require*.js'], ['requirescripts']);
// Watch .js .coffee files
gulp.watch(['!src/main/web/www/vendor/require/require*.js',
'src/main/web/www/main.js',
'src/main/web/www/modules/**/*.js',
'src/main/web/www/modules/**/*.coffee',
'src/main/web/www/vendor/**/*'], ['scripts']);
// Watch image files
gulp.watch('src/main/web/www/img/**/*', ['img']);
//// Watch vendor files
//gulp.watch('src/main/web/www/vendor/**/*', ['vendorscripts', 'build']);
});
gulp.task('watchapp', function () {
gulp.watch(['target/web/config.xml', 'target/web/www/**/*'], ['appbuild']);
});
gulp.task('watchapprun', function () {
gulp.watch(['target/web/config.xml', 'target/web/www/**/*'], ['apprun']);
});
gulp.task('watchsync', function () {
gulp.watch(['target/web/www/**/*'], ['syncfronted']);
});
gulp.task('watchsynclocal', function () {
gulp.run('startLocalServer');
gulp.watch(['target/web/www/**/*'], ['synclocal']);
});
gulp.task('watchtest', function () {
//gulp.run('selenium:start');
gulp.watch([paths.targetbrowserfiles], ['utest', 'ftest']);
});
// 默认任务
gulp.task('default', ['watchdev']);