This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
forked from sidecar/tpcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
283 lines (252 loc) · 10 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
// Include gulp plugins
var gulp = require('gulp')
, runSequence = require('run-sequence')
, jshint = require('gulp-jshint')
, browserify = require('gulp-browserify')
, uglify = require('gulp-uglifyjs')
, concat = require('gulp-concat-sourcemap')
, minifyCSS = require('gulp-minify-css')
, imageMin = require('gulp-imagemin')
, sass = require('gulp-sass')
, rename = require('gulp-rename')
, replace = require('gulp-replace')
, clean = require('gulp-clean')
, http = require('http')
, gulpIf = require('gulp-if')
, gulpIgnore = require('gulp-if')
, open = require('opn')
//http://rhumaric.com/2014/01/livereload-magic-gulp-style/
, livereload = require('gulp-livereload')
, karma = require('gulp-karma')
, shell = require('gulp-shell');
// Project configuration object
var config = {
port: 9000,
app: {},
dev: {},
build: {},
test: {}
}
config.app.baseDir = 'app/'
config.app.styles = config.app.baseDir +'styles/'
config.app.js = config.app.baseDir + 'js/'
config.app.templates = config.app.baseDir + 'templates/'
config.app.views = config.app.baseDir + 'views/'
config.app.images = config.app.baseDir + 'img/'
config.dev.baseDir = 'dev/'
config.dev.styles = config.dev.baseDir +'css/'
config.dev.js = config.dev.baseDir + 'js/'
config.dev.images = config.dev.baseDir + 'img/'
// config.build.baseDir = './'
config.build.baseDir = 'dist/'
config.build.styles = config.build.baseDir +'css/'
config.build.js = config.build.baseDir + 'js/'
config.build.images = config.build.baseDir + 'img/'
config.test.baseDir = 'test/';
gulp.task('test-prep', function() {
//runSequence('testPrepBrowserifyTests');
return gulp.src('test/main.spec.js')
.pipe(browserify({
insertGlobals: true,
transform: ['hbsfy'],
extensions: ['.hbs']
}))
.pipe(gulp.dest('test/browserified-test'));
})
gulp.task('karma', function() {
return gulp.src(['test/browserified-test/*spec.js'])
.pipe(karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function(err) {
// Make sure failed tests cause gulp to exit non-zero
throw err;
});
});
gulp.task('test', function () {
runSequence('test-prep', 'karma');
})
// Lint JavaScript and log lint errors
gulp.task('lint', function() {
return gulp.src([config.app.js + '**/*.js', '!./'+config.app.js + 'vendor/**/*.js'])
.pipe(jshint({ strict: false }))
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// gulp.task('package-vendor-libs', function() {
// gulp.src([config.app.js + 'vendor/jquery-1.10.1.min.js', config.app.js + 'vendor/jquery.magnific-popup.min.js', config.app.js + 'vendor/bootstrap.min.js', config.app.js + 'vendor/bootstrap-slider.js'])
// // .pipe(concat('libs.js'))
// // .pipe(gulp.dest(config.dev.js));
// // Concatenate AND minify files and create a source map
// .pipe(uglify('libs.min.js', {
// outSourceMap: 'libs.min.js.map'
// }))
// .pipe(gulp.dest(config.dev.js));
// });
gulp.task('copy-html-to-dev', function(){
// Copy the index.html file into the dist dir
return gulp.src(config.app.baseDir + '*.html')
.pipe(gulp.dest(config.dev.baseDir));
});
gulp.task('copy-gate-js-to-dev', function(){
// Copy the index.html file into the dist dir
return gulp.src(config.app.baseDir + 'gate.js')
.pipe(gulp.dest(config.dev.baseDir));
});
gulp.task('copy-php-to-dev', function(){
// Copy the index.html file into the dist dir
return gulp.src(config.app.baseDir + 'php/**/*.{php,html}')
.pipe(gulp.dest(config.dev.baseDir + 'php/'));
});
gulp.task('copy-images-to-dev', function() {
//copy images to the dev folder
return gulp.src(config.app.images + '**/*.{jpg,png,gif,jpeg,ico}')
.pipe(gulp.dest(config.dev.images));
});
gulp.task('copy-jqueryui-images-to-dev', function() {
//copy images to the dev folder
return gulp.src(config.app.styles + 'jquery-ui/images/**/*.{jpg,png,gif,jpeg,ico}')
.pipe(gulp.dest(config.dev.styles + 'jquery-ui/images/'));
});
gulp.task('copy-fonts-to-dev', function() {
//copy images to the dev folder
return gulp.src(config.app.styles + 'bootstrap-sass/fonts/bootstrap/**/*.{eot,svg,ttf,woff}')
.pipe(gulp.dest(config.dev.styles + 'fonts/bootstrap/'));
});
// Compile sass then concatenate and minify all css
gulp.task('sass', function() {
// return gulp.src([config.app.styles + '**/*.scss', config.app.styles + '**/*.scss'])
return gulp.src(config.app.styles + 'main.scss')
// Only compile sass for scss files
//http://stackoverflow.com/questions/21719833/gulp-how-to-add-src-files-in-the-middle-of-a-pipe?lq=1
.pipe(gulpIf(/[.]scss$/, sass({quiet: true})))
.pipe(concat('styles.min.css'))
.pipe(minifyCSS())
.pipe(gulp.dest(config.dev.styles))
});
gulp.task('clean-dev', function() {
return gulp.src(config.dev.baseDir).pipe(clean());
// ^^^^^
// This is the key here, to make sure tasks run asynchronously!
});
gulp.task('clean-build', function() {
//return gulp.src(['index.html', 'css', 'js', 'php']).pipe(clean());
return gulp.src(config.build.baseDir).pipe(clean());
});
// Bundle javascript
//for now trying to install all packages with NPM here goes
gulp.task('browserify', function() {
// give browserify a start point not a list of files
return gulp.src(config.app.js + 'init.js')
.pipe(browserify({
insertGlobals: true,
transform: ['hbsfy'],
// Allows you to skip .hbs in require statement along with .js and .json
extensions: ['.hbs']
}))
.pipe(gulp.dest(config.dev.js));
});
// Minify javascript after its been browserfied
gulp.task('minify-js', function() {
//grab init.js and libs.js mash em into one ugly messs
return gulp.src(config.dev.js + 'init.js')
// Concatenate AND minify files and create a source map
.pipe(uglify('init.min.js', {
outSourceMap: 'init.min.js.map'
}))
.pipe(gulp.dest(config.build.js));
});
gulp.task('optimize-images', function() {
//squish down images and place in build folder
return gulp.src(config.app.images + '**/*.{jpg,png,gif,jpeg,ico}')
.pipe(imageMin())
.pipe(gulp.dest(config.build.images));
});
// Create web server
// Using gulp-livereload in the watch task instead of connect-livereload middleware
//.use(require('connect-livereload')({ port: 35729 }))
// gulp.task('connect', function() {
// var connect = require('connect'), server = connect();
// //??? Not what connect.directory is for ???
// server.use(connect.static(config.dev.baseDir))
// //.use(connect.directory(config.dev.baseDir));
// //??? Why dont I have to include the http module to use createServer method ???
// http.createServer(server)
// .listen(config.port)
// .on('listening', function() {
// console.log('Started connect web server on http://localhost:' + config.port + '.');
// // Open default browser at this address
// opn('http://localhost:' + config.port);
// });
// });
gulp.task('xampp', shell.task([
'sudo xampp start'
]));
gulp.task('start-node-server', shell.task([
"CONTEXT='local' nodemon server.js"
]));
//Can't get this to work with the ser
gulp.task('open-browser', function() {
var options = {
url: "http://localhost:8080",
app: "google-chrome"
};
gulp.src("./dev/index.html")
.pipe(open("", options));
});
gulp.task('watch-for-changes', function() {
// this does not work for me without the client side script but I don't see that documented Arghhh :-/
var server = livereload();
// If html files are changed copy them to the dev folder
gulp.watch(config.app.baseDir + '**/*.html').on('change', function (file) {
// ??? why am I able to access server inside this callback ???
runSequence(['copy-html-to-dev', 'copy-php-to-dev'], function() {
server.changed(file.path);
});
});
// If sass files are changed run sass task
gulp.watch(config.app.styles + '**/*.scss').on('change', function (file) {
// ??? why am I able to access server inside this callback ???
runSequence('sass', function() {
server.changed(file.path);
});
});
// If js files are changed run scripts task
gulp.watch(config.app.js + '**/*.js').on('change', function (file) {
// ??? why am I able to access server inside this callback ??
runSequence('browserify', function() {
server.changed(file.path);
});
});
// If js files are changed run scripts task
gulp.watch(config.app.baseDir + 'gate.js').on('change', function (file) {
// ??? why am I able to access server inside this callback ??
runSequence('copy-gate-js-to-dev', function() {
server.changed(file.path);
});
});
});
// Build a dev version of the app and serve it locally
gulp.task('server', function() {
//run these subtasks in sequence
runSequence('clean-dev', ['sass', 'browserify', 'copy-html-to-dev', 'copy-gate-js-to-dev', 'copy-php-to-dev', 'copy-images-to-dev', 'copy-jqueryui-images-to-dev', 'copy-fonts-to-dev'],'watch-for-changes', 'start-node-server');
});
//Copy necessary files to build dir
gulp.task('copy-to-build', function() {
gulp.src(config.app.baseDir + '*.html')
// Using gulpIf to limit replace to the index.html file, replace the script calls in index to pull in the concatenated and minified single js script
.pipe(gulpIf(/index.html$/, replace('init.js','init.min.js')))
.pipe(gulpIf(/index.html$/, replace("<script>document.write('<script src=\"http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1\"></' + 'script>')</script>","")))
.pipe(gulp.dest(config.build.baseDir));
//copy the php files
gulp.src(config.app.baseDir + 'php/**/*.{php,html}')
.pipe(gulp.dest(config.build.baseDir + 'php/'));
// Copy css into build dir - this will include the jquery-ui images that need to be copied
gulp.src(config.dev.styles + '**/*').pipe(gulp.dest(config.build.styles));
})
// Build a production version of the app
// I should be able to run build task without running server task first
gulp.task('build', function() {
runSequence('clean-build',['sass', 'browserify'], 'minify-js', 'copy-to-build', 'optimize-images' );
});