-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
155 lines (131 loc) · 4.46 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
var fs = require('fs');
var packageInfo = JSON.parse(fs.readFileSync('package.json', 'utf8'));
var config = {};
var glob = require('glob');
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var Builder = require('systemjs-builder');
var less = require('gulp-less');
var concatCss = require('gulp-concat-css');
var gulpgo = require('gulp-go-windows-fork');
var go;
var browserSync = require('browser-sync').create();
var pathToStaticDist = 'static/';
var pathToStaticSrc = 'frontend/';
var pathToServerSrc = 'server/'
var bootstrap = [
'jspm_packages/github/twbs/[email protected]/css/bootstrap.css',
'jspm_packages/github/angular-ui/[email protected]/ui-bootstrap-csp.css'
];
var bootstrapFonts = 'jspm_packages/github/twbs/[email protected]/fonts/*';
var watchJs = pathToStaticSrc+'**/*.js';
var watchGo = 'server/*.go';
var watchLess = [pathToStaticSrc+'**/*.less'];
var watchJsTemplates = pathToStaticSrc+'**/*.tmpl.html';
var watchServerTemplates = 'server/*.html';
function systemjsBundle (fileName, options) {
var builder = new Builder('./', packageInfo.jspm.configFile)
return builder.buildStatic(pathToStaticSrc+fileName, pathToStaticDist+fileName, options)
.catch(function(err) {
console.log('Build error');
console.log(err);
});
}
gulp.task('setup-config', [], function () {
var configLocal = JSON.parse(fs.readFileSync('config-local.json', 'utf8'));
var configDeploy = JSON.parse(fs.readFileSync('config-deploy.json', 'utf8'));
if(process.env.TICKSCRIPT_STUDIO_BUILD_ENVIRONMENT == 'deploy') {
config = Object.assign(configLocal, configDeploy);
} else {
config = configLocal;
}
fs.writeFileSync('config.json', JSON.stringify(config, null, 2), 'utf8');
});
gulp.task('bundle-vendor-js', ['setup-config'], function () {
return systemjsBundle('vendor.js', {
runtime: false,
minify: config.gulpVendorMinify,
sourceMaps: config.gulpVendorSourceMaps,
lowResSourceMaps: true
});
});
gulp.task('bundle-js', ['setup-config'], function () {
return systemjsBundle('index.js', {
runtime: false,
minify: config.gulpAppMinify,
sourceMaps: config.gulpAppSourceMaps,
lowResSourceMaps: true
});
});
gulp.task('copy-css', [], function() {
return gulp.src(bootstrap)
.pipe(concatCss("bootstrap.css"))
.pipe(gulp.dest(pathToStaticDist));
});
gulp.task('copy-fonts', [], function() {
return gulp.src(bootstrapFonts)
.pipe(gulp.dest(pathToStaticDist+'/fonts'));
});
gulp.task("go-run", function() {
var cwd = __dirname.replace(new RegExp('\\\\', 'g'), '/');
glob(watchGo, {cwd: cwd}, function (er, files) {
//console.log(cwd, files.join(" "));
go = gulpgo.run(files, [], {
cwd: cwd,
onStdout: function(buffer) { console.log(buffer.toString('utf8')); },
onStderr: function(buffer) { console.error(buffer.toString('utf8')); }
});
});
});
gulp.task("go-restart", function() {
go.restart();
});
gulp.task('build', [
'copy-css',
'copy-fonts',
'bundle-less',
'bundle-js',
'bundle-vendor-js'
], function(){});
gulp.task('bundle-less', function () {
return gulp.src(watchLess)
.pipe(plumber())
.pipe(less({}))
.pipe(concatCss("bundle.css"))
.pipe(gulp.dest(pathToStaticDist))
.pipe(browserSync.stream());
});
gulp.task('bundle-js-watch', ['bundle-js'], function() { browserSync.reload(); });
gulp.task('go-server-watch', ['go-restart'], function() {
setTimeout(function() { browserSync.reload(); }, 2200);
});
var debounceLESS = debounce(function () { gulp.start('bundle-less'); }, 200);
var debounceJS = debounce(function () { gulp.start('bundle-js-watch'); }, 200);
var debounceGo = debounce(function () { gulp.start('go-server-watch'); }, 200);
gulp.task('watch', ['build'], function() {
gulp.watch(watchLess, function () { debounceLESS(); });
gulp.watch([watchJs, watchJsTemplates], function () { debounceJS(); });
gulp.watch([watchGo, watchServerTemplates], function () { debounceGo(); });
});
gulp.task('serve', ['go-run', 'watch'], function() {
browserSync.init({
proxy: {
target: "http://localhost:"+config.listenPort,
}
});
});
gulp.task('default', ['serve'], function(){});
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};