1 |
|
2 |
|
3 | var gulp = require('gulp');
|
4 | var del = require('del');
|
5 | var preprocess = require('gulp-preprocess');
|
6 | var less = require('gulp-less');
|
7 | var rename = require("gulp-rename");
|
8 | var minifyCSS = require('gulp-minify-css');
|
9 | var uglify = require('gulp-uglify');
|
10 | var gutil = require('gulp-util');
|
11 | var concat = require('gulp-concat');
|
12 | var runSequence = require('run-sequence').use(gulp);
|
13 | var eventStream = require('event-stream');
|
14 | var ngTemplates = require('gulp-ng-templates');
|
15 | var htmlmin = require('gulp-htmlmin');
|
16 |
|
17 | var fs = require('fs-extra');
|
18 | var path = require('path');
|
19 | var server = require('server-root');
|
20 | var _ = require('underscore');
|
21 |
|
22 | _.str = require('underscore.string');
|
23 |
|
24 | var TARGET_FOLDER = 'www';
|
25 |
|
26 | var app = require('./src/app/index.js').create();
|
27 | var appMode = 'DEVELOPMENT';
|
28 |
|
29 | var modulePaths = [];
|
30 | var moduleNames = [];
|
31 |
|
32 | var getModuleFolderPath = function (installedPath) {
|
33 | var modulePath = installedPath.split('/');
|
34 | modulePath.pop();
|
35 | modulePath.pop();
|
36 | modulePath = modulePath.join('/');
|
37 | return modulePath;
|
38 | }
|
39 |
|
40 | gulp.task('setCurrentDirectory', function () {
|
41 | process.chdir(server.getPath(''));
|
42 | });
|
43 |
|
44 | gulp.task('copyLocalBowerComponents', function () {
|
45 | gulp.src('./bower/**')
|
46 | .pipe(gulp.dest('./bower_components'));
|
47 | });
|
48 |
|
49 | gulp.task('copyLocalNodeModules', function () {
|
50 | var gulps = [];
|
51 |
|
52 | var modules = undefined;
|
53 | if (fs.existsSync(server.getPath('npm/modules'))) {
|
54 | modules = _.filter(fs.readFileSync(server.getPath('npm/modules'), { encoding: 'utf8' }).split('\r\n'), function (x) {
|
55 | return x !== undefined && x !== '';
|
56 | });
|
57 | }
|
58 |
|
59 | if (fs.existsSync(server.getPath('npm'))) {
|
60 | var packages = fs.readdirSync(server.getPath('npm'));
|
61 | _.each(packages, function (packageItem) {
|
62 | if (fs.lstatSync(server.getPath('npm/' + packageItem)).isDirectory()) {
|
63 | del.sync('./node_modules/' + packageItem);
|
64 |
|
65 | if (modules !== undefined) {
|
66 | if (modules.indexOf(packageItem) !== -1) {
|
67 | gulps.push(gulp.src('./npm/' + packageItem + '/**')
|
68 | .pipe(gulp.dest('./node_modules/' + packageItem)));
|
69 | }
|
70 | }
|
71 | else {
|
72 | gulps.push(gulp.src('./npm/' + packageItem + '/**')
|
73 | .pipe(gulp.dest('./node_modules/' + packageItem)));
|
74 | }
|
75 | }
|
76 | });
|
77 | }
|
78 |
|
79 | if (gulps.length > 0)
|
80 | return eventStream.merge(gulps);
|
81 |
|
82 | });
|
83 |
|
84 | gulp.task('loadModules', function () {
|
85 |
|
86 | var paths = [];
|
87 | _.each(modulePaths, function (modulePath) {
|
88 | var mPath = getModuleFolderPath(modulePath);
|
89 | mPath = mPath.replace(/\//g, '\\') + '\\';
|
90 | paths.push(mPath);
|
91 | });
|
92 |
|
93 | Object.keys(require.cache).forEach(function (key) {
|
94 | var strippedKey = key.substring(__dirname.length + 1);
|
95 | if (_.any(paths, function (x) {
|
96 | return strippedKey.indexOf(x) === 0;
|
97 | }))
|
98 | delete require.cache[key];
|
99 | });
|
100 |
|
101 | app = require('./src/app/index.js').create();
|
102 |
|
103 | modulePaths = [];
|
104 |
|
105 | var getModule = function (indexPath) {
|
106 | if (fs.existsSync(indexPath)) {
|
107 | var packObject = require(indexPath);
|
108 | if (packObject && packObject.domain) {
|
109 | return packObject;
|
110 | }
|
111 | }
|
112 | return undefined;
|
113 | }
|
114 |
|
115 | moduleNames = [];
|
116 |
|
117 | var mainPackageInfoPath = server.getPath('package.json');
|
118 | if (fs.existsSync(mainPackageInfoPath)) {
|
119 | var mainPackInfo = require(mainPackageInfoPath);
|
120 | if (mainPackInfo.main) {
|
121 |
|
122 | var module = getModule(server.getPath(mainPackInfo.main));
|
123 | if (module) {
|
124 | if (module.domain === 'core') {
|
125 | modulePaths.push(mainPackInfo.main);
|
126 | moduleNames.push('jsnbt');
|
127 | app.register(module);
|
128 | }
|
129 | }
|
130 | }
|
131 | }
|
132 |
|
133 | if (fs.existsSync(server.getPath('node_modules'))) {
|
134 | var packages = fs.readdirSync(server.getPath('node_modules'));
|
135 | _.each(packages, function (packageItem) {
|
136 | if (_.str.startsWith(packageItem, 'jsnbt')) {
|
137 | if (moduleNames.indexOf(packageItem) === -1) {
|
138 | if (fs.lstatSync(server.getPath('node_modules/' + packageItem)).isDirectory()) {
|
139 | var nodeModulePackagePath = server.getPath('node_modules/' + packageItem + '/package.json');
|
140 |
|
141 | if (fs.existsSync(nodeModulePackagePath)) {
|
142 | var nodeModulePackage = require(nodeModulePackagePath);
|
143 |
|
144 | if (nodeModulePackage.main) {
|
145 | var nodeModuleIndexPath = server.getPath('node_modules/' + packageItem + '/' + nodeModulePackage.main);
|
146 | var nodeModuleIndexModule = getModule(server.getPath('node_modules/' + packageItem + '/' + nodeModulePackage.main));
|
147 | if (nodeModuleIndexModule) {
|
148 |
|
149 | modulePaths.push('node_modules/' + packageItem + '/' + nodeModulePackage.main);
|
150 | app.register(nodeModuleIndexModule);
|
151 |
|
152 | moduleNames.push(packageItem);
|
153 | }
|
154 | }
|
155 | }
|
156 | }
|
157 | }
|
158 | }
|
159 | });
|
160 | }
|
161 |
|
162 | var mainPackageInfoPath = server.getPath('package.json');
|
163 | if (fs.existsSync(mainPackageInfoPath)) {
|
164 | var mainPackInfo = require(mainPackageInfoPath);
|
165 | if (mainPackInfo.main) {
|
166 |
|
167 | var module = getModule(server.getPath(mainPackInfo.main));
|
168 | if (module) {
|
169 | if (module.domain === 'core') {
|
170 | var dbgModule = getModule(server.getPath('dbg/app/index.js'));
|
171 | if (dbgModule) {
|
172 | modulePaths.push('dbg/app/index.js');
|
173 | moduleNames.push('public');
|
174 | app.register(dbgModule);
|
175 | }
|
176 | }
|
177 | if (module.domain === 'public') {
|
178 | modulePaths.push(mainPackInfo.main);
|
179 | moduleNames.push('public');
|
180 | app.register(module);
|
181 | }
|
182 | }
|
183 | }
|
184 | }
|
185 |
|
186 | });
|
187 |
|
188 | gulp.task('installBowerComponents', function (done) {
|
189 | var exec = require('child_process').exec;
|
190 |
|
191 | var tasks = [];
|
192 |
|
193 | var bowerPackages = [];
|
194 |
|
195 | var bowerConfigs = [];
|
196 |
|
197 | _.each(app.modules.all, function (module) {
|
198 | if (typeof (module.getBower) === 'function') {
|
199 | var bowerConfig = module.getBower();
|
200 | bowerConfigs.push(bowerConfig);
|
201 | }
|
202 | });
|
203 |
|
204 | _.each(bowerConfigs, function (bowerConfig) {
|
205 | if (bowerConfig.dependencies) {
|
206 | for (var dep in bowerConfig.dependencies) {
|
207 | var packOptions = {
|
208 | name: dep,
|
209 | version: bowerConfig.dependencies[dep]
|
210 | };
|
211 | bowerPackages.push(packOptions);
|
212 | }
|
213 | }
|
214 | });
|
215 |
|
216 | var runTasks = function () {
|
217 | var task = tasks.shift();
|
218 | if (task)
|
219 | task(runTasks);
|
220 | else {
|
221 |
|
222 | var folders = fs.readdirSync(server.getPath('bower_components'));
|
223 |
|
224 | _.each(folders, function (folder) {
|
225 | if (!fs.existsSync(server.getPath('bower/' + folder))) {
|
226 | if (!_.any(bowerPackages, function (x) {
|
227 | return (x.name + '-' + x.version) === folder;
|
228 | })) {
|
229 | gutil.log('bower: deleting obsolete ' + folder);
|
230 | del.sync('./bower_components/' + folder);
|
231 | gutil.log('bower: deleted obsolete ' + folder);
|
232 | }
|
233 | }
|
234 | });
|
235 |
|
236 | done();
|
237 | }
|
238 | };
|
239 |
|
240 | _.each(bowerPackages, function (bowerPackage) {
|
241 | tasks.push(function (cb) {
|
242 | if (!fs.existsSync(server.getPath('bower_components/' + bowerPackage.name + '-' + bowerPackage.version)) && !fs.existsSync(server.getPath('bower/' + bowerPackage.name))) {
|
243 | gutil.log('bower: installing ' + bowerPackage.name + '#' + bowerPackage.version);
|
244 | exec('bower install ' + bowerPackage.name + '-' + bowerPackage.version + '=' + bowerPackage.name + '#' + bowerPackage.version
|
245 | + ' --config.analytics=false'
|
246 | + ' --allow-root'
|
247 | + ' -f',
|
248 | { cwd: './' }, function (err, stdout, stderr) {
|
249 | if (err)
|
250 | throw err;
|
251 |
|
252 | del.sync('./bower_components/' + bowerPackage.name);
|
253 | gutil.log('bower: installed ' + bowerPackage.name + '#' + bowerPackage.version);
|
254 |
|
255 | cb();
|
256 | });
|
257 | }
|
258 | else {
|
259 | cb();
|
260 | }
|
261 | });
|
262 | });
|
263 |
|
264 | runTasks();
|
265 | });
|
266 |
|
267 | gulp.task('cleanTarget', function () {
|
268 |
|
269 | var targets = [
|
270 | TARGET_FOLDER + '/migrations',
|
271 | TARGET_FOLDER + '/mode',
|
272 | TARGET_FOLDER + '/modules'
|
273 | ];
|
274 |
|
275 | if (fs.existsSync(server.getPath(TARGET_FOLDER + '/public'))) {
|
276 | var publicFolders = fs.readdirSync(server.getPath(TARGET_FOLDER + '/public'));
|
277 | var restricted = ['files', 'tmp'];
|
278 | _.each(publicFolders, function (pf) {
|
279 | if (restricted.indexOf(pf) === -1)
|
280 | targets.push(TARGET_FOLDER + '/public/' + pf);
|
281 | });
|
282 | }
|
283 |
|
284 | del.sync(targets);
|
285 |
|
286 | if (!fs.existsSync(server.getPath(TARGET_FOLDER))) {
|
287 | fs.mkdirpSync(server.getPath(TARGET_FOLDER));
|
288 | fs.mkdirpSync(server.getPath(TARGET_FOLDER + '/public'));
|
289 | fs.mkdirpSync(server.getPath(TARGET_FOLDER + '/migrations'));
|
290 | }
|
291 | });
|
292 |
|
293 | gulp.task('setMode:dev', function () {
|
294 |
|
295 | appMode = 'DEVELOPMENT';
|
296 | fs.writeFileSync(TARGET_FOLDER + '/mode', 'dev', {
|
297 | encoding: 'utf-8'
|
298 | });
|
299 |
|
300 | });
|
301 |
|
302 | gulp.task('setMode:prod', function () {
|
303 |
|
304 | appMode = 'PRODUCTION';
|
305 | fs.writeFileSync(TARGET_FOLDER + '/mode', 'prod', {
|
306 | encoding: 'utf-8'
|
307 | });
|
308 |
|
309 | });
|
310 |
|
311 | gulp.task('setModules', function () {
|
312 |
|
313 | var modulesText = modulePaths.join('\n');
|
314 | fs.writeFileSync(TARGET_FOLDER + '/modules', modulesText, {
|
315 | encoding: 'utf-8'
|
316 | });
|
317 |
|
318 | });
|
319 |
|
320 | gulp.task('copyMigrations', function () {
|
321 |
|
322 | var gulps = [];
|
323 |
|
324 | _.each(moduleNames, function (moduleName, i) {
|
325 | var modulePath = getModuleFolderPath(modulePaths[i]);
|
326 |
|
327 | gulps.push(gulp.src('./' + modulePath + '/dat/**')
|
328 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/migrations/' + moduleName)));
|
329 | });
|
330 |
|
331 | if (gulps.length > 0)
|
332 | return eventStream.merge(gulps);
|
333 | });
|
334 |
|
335 | var getFileCopyPublicPaths = function (module, modulePath) {
|
336 | var templatePaths = [];
|
337 |
|
338 | if (typeof (module.getConfig) === 'function') {
|
339 | var templates = module.getConfig().templates || [];
|
340 |
|
341 | templatePaths = _.union(templatePaths, [
|
342 | './' + modulePath + '/web/public/**',
|
343 | '!./' + modulePath + '/web/public/files/**',
|
344 | '!./' + modulePath + '/web/public/tmp/**',
|
345 | '!./' + modulePath + '/web/public/err/**'],
|
346 | _.filter(_.map(templates, function (x) {
|
347 | if (!_.str.startsWith(x.html, '/admin/') || !_.str.startsWith(x.html, 'admin/'))
|
348 | return '!./' + modulePath + '/web/public/' + _.str.ltrim(x.html, '/');
|
349 | else
|
350 | '';
|
351 | }), function (f) {
|
352 | return f !== '';
|
353 | }));
|
354 | }
|
355 |
|
356 | return templatePaths;
|
357 | }
|
358 |
|
359 | var getFileCopyAdminPaths = function (module, modulePath) {
|
360 | var adminTemplatePaths = [];
|
361 |
|
362 | if (typeof (module.getConfig) === 'function') {
|
363 | var templates = module.getConfig().templates || [];
|
364 |
|
365 | adminTemplatePaths = _.union(adminTemplatePaths, ['./' + modulePath + '/web/admin/**',
|
366 | '!./' + modulePath + '/web/admin/err/**',
|
367 | '!./' + modulePath + '/web/admin/index.html'], _.filter(_.map(templates, function (x) {
|
368 | if (_.str.startsWith(x.html, '/admin/') || _.str.startsWith(x.html, 'admin/'))
|
369 | return '!./' + modulePath + '/web/admin/' + _.str.ltrim(x.html, '/');
|
370 | else
|
371 | return '';
|
372 | }), function (f) {
|
373 | return f !== '';
|
374 | }));
|
375 | }
|
376 |
|
377 | return adminTemplatePaths;
|
378 | }
|
379 |
|
380 | gulp.task('copyFiles', function () {
|
381 |
|
382 | var gulps = [];
|
383 |
|
384 | var templatePaths = [];
|
385 | var adminTemplatePaths = [];
|
386 |
|
387 | _.each(moduleNames, function (moduleName, i) {
|
388 |
|
389 | var module = require(server.getPath(modulePaths[i]));
|
390 |
|
391 | var modulePath = getModuleFolderPath(modulePaths[i]);
|
392 |
|
393 | templatePaths = _.union(templatePaths, getFileCopyPublicPaths(module, modulePath));
|
394 | adminTemplatePaths = _.union(adminTemplatePaths, getFileCopyAdminPaths(module, modulePath));
|
395 |
|
396 | });
|
397 |
|
398 | gulps = [
|
399 | gulp.src(templatePaths)
|
400 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/')),
|
401 |
|
402 | gulp.src(adminTemplatePaths)
|
403 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/admin/'))];
|
404 |
|
405 | if (gulps.length > 0)
|
406 | return eventStream.merge(gulps);
|
407 |
|
408 | });
|
409 |
|
410 | gulp.task('parseTemplates', function () {
|
411 |
|
412 | var gulps = [];
|
413 |
|
414 | _.each(moduleNames, function (moduleName, i) {
|
415 |
|
416 | var module = require(server.getPath(modulePaths[i]));
|
417 |
|
418 | var modulePath = getModuleFolderPath(modulePaths[i]);
|
419 |
|
420 | if (typeof (module.getConfig) === 'function') {
|
421 | var templates = module.getConfig().templates || [];
|
422 |
|
423 | _.each(templates, function (template) {
|
424 | gulps.push(gulp.src('./' + modulePath + '/web/' + (!_.str.startsWith(template.html, '/admin/') && !_.str.startsWith(template.html, 'admin/') ? 'public/' : '') + _.str.ltrim(template.html, '/'))
|
425 | .pipe(preprocess({ context: { NODE_ENV: appMode, DEBUG: false } }))
|
426 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/' + _.str.ltrim(path.dirname(template.html), '/') + '/')));
|
427 | });
|
428 |
|
429 | }
|
430 |
|
431 | gulps.push(gulp.src('./' + modulePath + '/web/public/err/*')
|
432 | .pipe(preprocess({ context: { NODE_ENV: appMode, DEBUG: false } }))
|
433 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/err/')));
|
434 |
|
435 | gulps.push(gulp.src('./' + modulePath + '/web/admin/err/*')
|
436 | .pipe(preprocess({ context: { NODE_ENV: appMode, DEBUG: false } }))
|
437 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/admin/err/')));
|
438 |
|
439 | });
|
440 |
|
441 | if (gulps.length > 0)
|
442 | return eventStream.merge(gulps);
|
443 | });
|
444 |
|
445 | gulp.task('generateJsnbtScript', function () {
|
446 |
|
447 | var script = new require('./src/app/clib/script.js')(app);
|
448 | var file = script.get();
|
449 |
|
450 | fs.writeFileSync(server.getPath(TARGET_FOLDER + '/public/jsnbt.js'), file, {
|
451 | encoding: 'utf8'
|
452 | });
|
453 |
|
454 | });
|
455 |
|
456 | gulp.task('deployBowerComponents', function () {
|
457 |
|
458 | var gulps = [];
|
459 |
|
460 | var bowerConfigs = [];
|
461 |
|
462 | _.each(app.modules.all, function (module) {
|
463 | if (typeof (module.getBower) === 'function') {
|
464 | var bowerConfig = module.getBower();
|
465 | bowerConfigs.push(bowerConfig);
|
466 | }
|
467 | });
|
468 |
|
469 | _.each(bowerConfigs, function (bowerConfig) {
|
470 | var bowerComponents = 'bower_components';
|
471 | if (bowerConfig.dependencies) {
|
472 | if (bowerConfig.deploy) {
|
473 | var bowerConfigKeys = _.keys(bowerConfig.deploy);
|
474 | _.each(bowerConfigKeys, function (deployd) {
|
475 | var packName = deployd;
|
476 | if (bowerConfig.dependencies[packName]) {
|
477 | var packSpecs = bowerConfig.deploy[packName];
|
478 |
|
479 | if (packSpecs.folders) {
|
480 | _.each(packSpecs.folders, function (folderSpecs) {
|
481 | if (folderSpecs.src && folderSpecs.dest) {
|
482 | folderSpecs.src = typeof (folderSpecs.src) !== 'string' ? folderSpecs.src : [folderSpecs.src];
|
483 | folderSpecs.dest = typeof (folderSpecs.dest) !== 'string' ? folderSpecs.dest : [folderSpecs.dest];
|
484 |
|
485 | _.each(folderSpecs.src, function (folderSpecsSrc) {
|
486 | _.each(folderSpecs.dest, function (folderSpecsDest) {
|
487 | var sourceDir = server.getPath(bowerComponents + '/' + folderSpecsSrc);
|
488 | var targetDir = server.getPath(TARGET_FOLDER + '/public/' + folderSpecsDest);
|
489 |
|
490 | gulps.push(gulp.src('./' + bowerComponents + '/' + folderSpecsSrc + '/**')
|
491 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/' + folderSpecsDest)));
|
492 | });
|
493 | });
|
494 | }
|
495 | });
|
496 | }
|
497 | if (packSpecs.files) {
|
498 | _.each(packSpecs.files, function (fileSpecs) {
|
499 | if (fileSpecs.src && fileSpecs.dest) {
|
500 | fileSpecs.src = typeof (fileSpecs.src) !== 'string' ? fileSpecs.src : [fileSpecs.src];
|
501 | fileSpecs.dest = typeof (fileSpecs.dest) !== 'string' ? fileSpecs.dest : [fileSpecs.dest];
|
502 |
|
503 | _.each(fileSpecs.src, function (fileSpecsSrc) {
|
504 | _.each(fileSpecs.dest, function (fileSpecsDest) {
|
505 | var sourceFile = server.getPath(bowerComponents + '/' + fileSpecsSrc);
|
506 | var targetFile = server.getPath(TARGET_FOLDER + '/public/' + fileSpecsDest);
|
507 |
|
508 | gulps.push(gulp.src('./' + bowerComponents + '/' + fileSpecsSrc)
|
509 | .pipe(rename(path.basename(fileSpecsDest)))
|
510 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/' + path.dirname(fileSpecsDest))));
|
511 | });
|
512 | });
|
513 | }
|
514 | });
|
515 | }
|
516 | }
|
517 | });
|
518 | }
|
519 | }
|
520 | });
|
521 |
|
522 | if (gulps.length > 0)
|
523 | return eventStream.merge(gulps);
|
524 |
|
525 | });
|
526 |
|
527 | gulp.task('generateStyles', function () {
|
528 |
|
529 | var gulps = [];
|
530 |
|
531 | var bundler = require('./src/app/tmpl/bundle.js')(app);
|
532 | _.each(app.config.templates, function (tmpl) {
|
533 |
|
534 | if (tmpl.styles && _.isArray(tmpl.styles)) {
|
535 | var styleBundle = bundler.getStyleBundle(tmpl.styles);
|
536 | _.each(styleBundle.raw, function (r) {
|
537 | if (r.items.length > 0) {
|
538 |
|
539 | var g = gulp.src(_.map(r.items, function (x, i) {
|
540 | return './' + TARGET_FOLDER + '/public' + (_.str.startsWith(x, '/admin/') ? x : '' + x)
|
541 | })).pipe(less({
|
542 | }));
|
543 |
|
544 | if (appMode === 'PRODUCTION') {
|
545 | g = g.pipe(minifyCSS());
|
546 | }
|
547 |
|
548 | g = g.pipe(concat('less-files.less'))
|
549 | .pipe(rename(path.basename(r.target)))
|
550 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public' + path.dirname(r.target)));
|
551 |
|
552 | gulps.push(g);
|
553 | }
|
554 | });
|
555 | }
|
556 |
|
557 | });
|
558 |
|
559 | if (gulps.length > 0)
|
560 | return eventStream.merge(gulps);
|
561 | });
|
562 |
|
563 | gulp.task('checkStructure', function () {
|
564 | fs.mkdirpSync('./' + TARGET_FOLDER + '/public/tmp');
|
565 |
|
566 | fs.mkdirpSync('./' + TARGET_FOLDER + '/public/files');
|
567 |
|
568 | _.each(app.config.fileGroups, function (fileGroup) {
|
569 | fs.mkdirpSync('./' + TARGET_FOLDER + '/public/files/' + fileGroup);
|
570 | });
|
571 | });
|
572 |
|
573 | gulp.task('minifyScripts', function () {
|
574 |
|
575 | var gulps = [];
|
576 |
|
577 | var targets = [];
|
578 |
|
579 | var bundler = require('./src/app/tmpl/bundle.js')(app);
|
580 | _.each(app.config.templates, function (tmpl) {
|
581 |
|
582 | if (tmpl.scripts && _.isArray(tmpl.scripts)) {
|
583 | var scriptBundle = bundler.getScriptBundle(tmpl.scripts);
|
584 | _.each(scriptBundle.raw, function (r) {
|
585 | if (r.items.length > 0 && targets.indexOf(r.target === -1)) {
|
586 |
|
587 | var g = gulp.src(_.map(r.items, function (x, i) {
|
588 | return './' + TARGET_FOLDER + '/public' + x
|
589 | }))
|
590 | .pipe(uglify({
|
591 | preserveComments: false,
|
592 | mangle: false,
|
593 | compress: false,
|
594 | wrap: false
|
595 | }))
|
596 | .pipe(concat('js-files.js'))
|
597 | .pipe(rename(path.basename(r.target)))
|
598 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public' + path.dirname(r.target)));
|
599 |
|
600 | gulps.push(g);
|
601 | targets.push(r.target);
|
602 | }
|
603 | });
|
604 | }
|
605 |
|
606 | });
|
607 |
|
608 | if (gulps.length > 0)
|
609 | return eventStream.merge(gulps);
|
610 | });
|
611 |
|
612 | gulp.task('compressAngularTemplates', function () {
|
613 |
|
614 | var templateFiles = _.map(app.config.templates, function (x) {
|
615 | return '!./' + TARGET_FOLDER + '/public' + x;
|
616 | })
|
617 |
|
618 | return eventStream.merge([
|
619 | gulp.src(_.union(['./' + TARGET_FOLDER + '/public/admin/tmpl/**/*.html'], templateFiles))
|
620 | .pipe(htmlmin({
|
621 | collapseBooleanAttributes: true,
|
622 | collapseWhitespace: true,
|
623 | removeAttributeQuotes: true,
|
624 | removeComments: true,
|
625 | removeEmptyAttributes: true,
|
626 | removeRedundantAttributes: true,
|
627 | removeScriptTypeAttributes: true,
|
628 | removeStyleLinkTypeAttributes: true
|
629 | }))
|
630 | .pipe(ngTemplates({
|
631 | module: 'jsnbt',
|
632 | standalone: false,
|
633 | path: function (path, base) {
|
634 | return ('tmpl\\' + path.replace(base, '')).replace(/\\/gi, '//');
|
635 | }
|
636 | }))
|
637 | .pipe(rename('tmpl.js'))
|
638 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/admin')),
|
639 |
|
640 | gulp.src(_.union(['./' + TARGET_FOLDER + '/public/tmpl/**/*.html'], templateFiles))
|
641 | .pipe(htmlmin({
|
642 | collapseBooleanAttributes: true,
|
643 | collapseWhitespace: true,
|
644 | removeAttributeQuotes: true,
|
645 | removeComments: true,
|
646 | removeEmptyAttributes: true,
|
647 | removeRedundantAttributes: true,
|
648 | removeScriptTypeAttributes: true,
|
649 | removeStyleLinkTypeAttributes: true
|
650 | }))
|
651 | .pipe(ngTemplates({
|
652 | module: 'jsnbt',
|
653 | standalone: false,
|
654 | path: function (path, base) {
|
655 | return ('tmpl\\' + path.replace(base, '')).replace(/\\/gi, '//');
|
656 | }
|
657 | }))
|
658 | .pipe(rename('tmpl.js'))
|
659 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public'))
|
660 | ]);
|
661 | });
|
662 |
|
663 | function watch() {
|
664 | gutil.log('Watch enabled. Listening for file changes...');
|
665 |
|
666 | var processFile = function (event, prefix, destination) {
|
667 | var sourcePath = event.path.substring(event.path.indexOf(prefix));
|
668 | var targetPath = event.path.substring(event.path.indexOf(prefix) + prefix.length);
|
669 | gutil.log('File ' + sourcePath + ' was ' + event.type);
|
670 | if (event.type === 'changed') {
|
671 | gulp.src(event.path)
|
672 | .pipe(gulp.dest(destination + '/' + path.dirname(targetPath)))
|
673 | .on('end', function () {
|
674 | gutil.log('File updated on ' + path.join(destination + '/' + targetPath));
|
675 | });
|
676 | }
|
677 | else if (event.type === 'deleted') {
|
678 | del.sync(destination + '/' + targetPath);
|
679 | gutil.log('File deleted from ' + path.join(destination + '/' + targetPath));
|
680 | }
|
681 | };
|
682 |
|
683 | if (fs.existsSync(server.getPath('bower'))) {
|
684 | var localPackages = fs.readdirSync(server.getPath('bower'));
|
685 | _.each(localPackages, function (localPackage) {
|
686 | if (fs.lstatSync(server.getPath('bower/' + localPackage)).isDirectory()) {
|
687 |
|
688 | gulp.watch('./bower/' + localPackage + '/**', function (event) {
|
689 | processFile(event, '\\bower\\' + localPackage + '\\', './bower_components/' + localPackage);
|
690 | runSequence('deployBowerComponents', 'generateStyles');
|
691 | });
|
692 |
|
693 | }
|
694 | });
|
695 | }
|
696 |
|
697 | if (fs.existsSync(server.getPath('npm'))) {
|
698 | var localPackages = fs.readdirSync(server.getPath('npm'));
|
699 | _.each(localPackages, function (localPackage) {
|
700 | if (fs.lstatSync(server.getPath('npm/' + localPackage)).isDirectory()) {
|
701 | gulp.watch('./npm/' + localPackage + '/**', function (event) {
|
702 | processFile(event, '\\npm\\' + localPackage + '\\', './node_modules/' + localPackage);
|
703 | });
|
704 | }
|
705 | });
|
706 | }
|
707 |
|
708 | _.each(moduleNames, function (moduleName, i) {
|
709 |
|
710 | var module = require(server.getPath(modulePaths[i]));
|
711 |
|
712 | var modulePath = getModuleFolderPath(modulePaths[i]);
|
713 |
|
714 | gulp.watch(getFileCopyAdminPaths(module, modulePath), function (event) {
|
715 | processFile(event, 'web\\admin\\', './' + TARGET_FOLDER + '/public/admin/');
|
716 | });
|
717 |
|
718 | gulp.watch(getFileCopyPublicPaths(module, modulePath), function (event) {
|
719 | processFile(event, 'web\\public\\', './' + TARGET_FOLDER + '/public/');
|
720 | });
|
721 |
|
722 | gulp.watch(['./' + modulePath + '/app/clib/**', './' + modulePath + '/cfg/**'], function (event) {
|
723 | runSequence('loadModules', 'generateJsnbtScript');
|
724 | });
|
725 |
|
726 |
|
727 | var templates = module.getConfig().templates || [];
|
728 |
|
729 | _.each(templates, function (template) {
|
730 | gulp.watch('./' + modulePath + '/web/' + (!_.str.startsWith(template.html, '/admin/') && !_.str.startsWith(template.html, 'admin/') ? 'public/' : '') + _.str.ltrim(template.html, '/'), function (event) {
|
731 | gutil.log('File ' + event.path + ' was ' + event.type);
|
732 | gulp.src('./' + modulePath + '/web/' + (!_.str.startsWith(template.html, '/admin/') && !_.str.startsWith(template.html, 'admin/') ? 'public/' : '') + _.str.ltrim(template.html, '/'))
|
733 | .pipe(preprocess({ context: { NODE_ENV: appMode, DEBUG: false } }))
|
734 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/' + _.str.ltrim(path.dirname(template.html), '/') + '/'))
|
735 | .on('end', function () {
|
736 | gutil.log('File updated on ' + path.join(TARGET_FOLDER + '/public/' + _.str.ltrim(template.html, '/')));
|
737 | });
|
738 | });
|
739 | });
|
740 |
|
741 | gulp.watch('./' + modulePath + '/web/public/err/*', function (event) {
|
742 | gutil.log('File ' + event.path + ' was ' + event.type);
|
743 | gulp.src(event.path)
|
744 | .pipe(preprocess())
|
745 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/err/'))
|
746 | .on('end', function () {
|
747 | gutil.log('File updated on ' + path.join(TARGET_FOLDER + '/public/err/' + path.basename(event.path)));
|
748 | });
|
749 | });
|
750 |
|
751 | gulp.watch('./' + modulePath + '/web/admin/err/*', function (event) {
|
752 | gutil.log('File ' + event.path + ' was ' + event.type);
|
753 | gulp.src(event.path)
|
754 | .pipe(preprocess())
|
755 | .pipe(gulp.dest('./' + TARGET_FOLDER + '/public/admin/err/'))
|
756 | .on('end', function () {
|
757 | gutil.log('File updated on ' + path.join(TARGET_FOLDER + '/public/admin/err/' + path.basename(event.path)));
|
758 | });
|
759 | });
|
760 |
|
761 | });
|
762 |
|
763 | gulp.watch([
|
764 | './' + TARGET_FOLDER + '/public/css/**',
|
765 | './' + TARGET_FOLDER + '/public/admin/css/**'
|
766 | ], ['generateStyles']);
|
767 | };
|
768 |
|
769 | gulp.task('dev', function (callback) {
|
770 |
|
771 | runSequence(
|
772 | 'setCurrentDirectory',
|
773 | 'copyLocalBowerComponents',
|
774 | 'copyLocalNodeModules',
|
775 | 'loadModules',
|
776 | 'installBowerComponents',
|
777 | 'cleanTarget',
|
778 | 'setMode:dev',
|
779 | 'setModules',
|
780 | 'copyMigrations',
|
781 | 'copyFiles',
|
782 | 'parseTemplates',
|
783 | 'generateJsnbtScript',
|
784 | 'deployBowerComponents',
|
785 | 'checkStructure',
|
786 | 'generateStyles',
|
787 | callback
|
788 | );
|
789 |
|
790 | });
|
791 |
|
792 | gulp.task('dev-update', function (callback) {
|
793 |
|
794 | runSequence(
|
795 | 'setCurrentDirectory',
|
796 | 'copyLocalBowerComponents',
|
797 | 'copyLocalNodeModules',
|
798 | 'loadModules',
|
799 | 'installBowerComponents',
|
800 | 'cleanTarget',
|
801 | 'setMode:dev',
|
802 | 'setModules',
|
803 | 'copyMigrations',
|
804 | 'copyFiles',
|
805 | 'parseTemplates',
|
806 | 'generateJsnbtScript',
|
807 | 'deployBowerComponents',
|
808 | 'checkStructure',
|
809 | 'generateStyles',
|
810 | function () {
|
811 | watch();
|
812 | }
|
813 | );
|
814 |
|
815 | });
|
816 |
|
817 | gulp.task('prod', function (callback) {
|
818 |
|
819 | runSequence(
|
820 | 'setCurrentDirectory',
|
821 | 'copyLocalBowerComponents',
|
822 | 'copyLocalNodeModules',
|
823 | 'loadModules',
|
824 | 'installBowerComponents',
|
825 | 'cleanTarget',
|
826 | 'setMode:prod',
|
827 | 'setModules',
|
828 | 'copyMigrations',
|
829 | 'copyFiles',
|
830 | 'parseTemplates',
|
831 | 'generateJsnbtScript',
|
832 | 'deployBowerComponents',
|
833 | 'checkStructure',
|
834 | 'generateStyles',
|
835 | 'minifyScripts',
|
836 | 'compressAngularTemplates',
|
837 | callback
|
838 | );
|
839 |
|
840 | });
|
841 |
|
842 | module.exports = gulp; |
\ | No newline at end of file |