-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
322 lines (272 loc) · 9.93 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
(function () {
'use strict'
/* Config */
// Load plugins
const gulp = require('gulp-help')(require('gulp'))
const babel = require('gulp-babel')
const browserSync = require('browser-sync').create()
const bump = require('gulp-bump')
const cleanCSS = require('gulp-clean-css')
const concat = require('gulp-concat')
const del = require('del')
const filter = require('gulp-filter')
const fs = require('fs')
const ftp = require('vinyl-ftp')
const git = require('gulp-git')
const gutil = require('gulp-util')
const header = require('gulp-header')
const jsonminify = require('gulp-jsonminify')
const ngAnnotate = require('gulp-ng-annotate')
const rename = require('gulp-rename')
const runSequence = require('run-sequence')
const sass = require('gulp-sass')
const shell = require('gulp-shell')
const sourcemaps = require('gulp-sourcemaps')
const tagVersion = require('gulp-tag-version')
const uglify = require('gulp-uglify')
// Config variables
const config = {
root_dir: './',
src_dir: './src/app',
dist_dir: './src/dist',
build_dir: './build',
node_dir: './node_modules',
bower_dir: './bower_components'
}
// JS include files
const jsIncludes = {
vendor: [
config.node_dir + '/wowjs/dist/wow.js',
config.node_dir + '/zenscroll/zenscroll.js'
],
app: [
config.node_dir + '/angular/angular.min.js',
config.node_dir + '/angular-route/angular-route.min.js',
config.node_dir + '/angular-animate/angular-animate.min.js',
config.node_dir + '/angular-lazy-image/release/lazy-image.js',
config.node_dir + '/angular-sanitize/angular-sanitize.min.js',
config.src_dir + '/app.js'
]
}
/* Main tasks */
// Build
gulp.task('build', 'Standard build task', function (callback) {
runSequence('clean', ['sass', 'js:build:vendor', 'js:build:app', 'copy-assets', 'app-html', 'app-json'], 'js:build:concat', 'styleguide', callback)
})
// Build test
gulp.task('build:test', 'Standard build task', function (callback) {
runSequence('clean', ['sass', 'js:build:vendor', 'js:build:app', 'copy-assets', 'app-html', 'app-json:test'], 'js:build:concat', 'styleguide', callback)
})
// Serve
gulp.task('serve', 'Serves the application', ['sass', 'js'], function (callback) {
browserSync.init({
server: 'src/'
})
gulp.watch('src/app/assets/scss/**/*.scss', ['sass'])
gulp.watch('src/app/**/*.js', ['js']).on('change', browserSync.reload)
gulp.watch('src/**/*.html').on('change', browserSync.reload)
})
/* Sub tasks */
// Clean build task:
gulp.task('clean', 'Removes old build artifects', function (callback) {
return del([config.build_dir], callback)
})
// JS task:
gulp.task('js', 'Concats and minify js files', function (callback) {
return gulp.src(jsIncludes.vendor.concat(jsIncludes.app))
.pipe(ngAnnotate())
.pipe(concat('bundle.js'))
.pipe(gulp.dest(config.src_dir + '/assets/javascript/'))
.pipe(browserSync.stream())
})
// JS build task vendor:
gulp.task('js:build:vendor', 'Concats and minify vendor js files', function (callback) {
// Update the banner var
const packJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const banner = '/*! Build: ' + packJson.version + ' - ' + new Date().toString() + ' */\n'
return gulp.src(jsIncludes.vendor)
.pipe(concat('vendor.min.js'))
.pipe(uglify())
.pipe(header(banner))
.pipe(gulp.dest(config.build_dir + '/app/javascript/'))
})
// JS build task app:
gulp.task('js:build:app', 'Concats and minify app js files', function (callback) {
// Update the banner var
const packJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const banner = '/*! Build: ' + packJson.version + ' - ' + new Date().toString() + ' */\n'
return gulp.src(jsIncludes.app)
.pipe(ngAnnotate())
.pipe(babel({
presets: ['es2015', 'es2016']
}))
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(header(banner))
.pipe(gulp.dest(config.build_dir + '/app/javascript/'))
})
// JS build concat:
gulp.task('js:build:concat', 'Concats the build js files', function (callback) {
return gulp.src([
config.build_dir + '/app/javascript/vendor.min.js',
config.build_dir + '/app/javascript/app.min.js'
])
.pipe(concat('bundle.min.js'))
.pipe(gulp.dest(config.build_dir + '/app/javascript/'))
})
// Sass task:
gulp.task('sass', 'Concats and minify scss files', function (callback) {
// Update the banner var
const packJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const banner = '/*! Build: ' + packJson.version + ' - ' + new Date().toString() + ' */\n'
return gulp.src(config.src_dir + '/assets/scss/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(config.src_dir + '/assets/css/'))
// Minified CSS
.pipe(rename('main.min.css'))
.pipe(cleanCSS({compatibility: 'ie9'}))
.pipe(header(banner))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.build_dir + '/app/assets/css/'))
.pipe(browserSync.stream())
})
// Copy assets task:
gulp.task('copy-assets', ['copy-favicon', 'copy-fonts'], function () {
return gulp.src(config.src_dir + '/assets/images/**/*')
.pipe(gulp.dest(config.build_dir + '/app/assets/images/'))
})
// Copy favicon task:
gulp.task('copy-favicon', 'Copys favicon files to the build folder', function () {
return gulp.src(config.src_dir + '/assets/favicon/**/*')
.pipe(gulp.dest(config.build_dir + '/app/assets/favicon/'))
})
// Copy fonts task:
gulp.task('copy-fonts', 'Copys font files to the build folder', function () {
return gulp.src(config.src_dir + '/assets/fonts/**/*')
.pipe(gulp.dest(config.build_dir + '/app/assets/fonts/'))
})
// app HTML task:
gulp.task('app-html', ['move-partials'], function () {
return gulp.src([
config.dist_dir + '/index.html',
config.dist_dir + '/.htaccess',
config.dist_dir + '/sitemap.xml',
config.dist_dir + '/robots.txt'
])
.pipe(gulp.dest(config.build_dir))
})
// Move partials dir task:
gulp.task('move-partials', function () {
return gulp.src(config.src_dir + '/partials/*.html')
.pipe(gulp.dest(config.build_dir + '/app/partials/'))
})
// app JSON dev task:
gulp.task('app-json:test', 'Copy app .json files to the build folder', function () {
return gulp.src(config.src_dir + '/feeds/*.json')
.pipe(jsonminify())
.pipe(gulp.dest(config.build_dir + '/app/feeds/'))
})
// app JSON prod task:
gulp.task('app-json', 'Copy app .json files to the build folder', function () {
return gulp.src([config.src_dir + '/feeds/*.json', '!' + config.src_dir + '/feeds/dummy.json'])
.pipe(jsonminify())
.pipe(gulp.dest(config.build_dir + '/app/feeds/'))
})
// yarn run styleguide
gulp.task('styleguide', () => {
return gulp.src('')
.pipe(shell([
'yarn run styleguide'
]))
})
/* Versioning and publishing */
// Publish-patch
gulp.task('create-patch', 'Create patch release', function (callback) {
runSequence('patch', 'build', 'commit', callback)
})
// Publish-minor
gulp.task('create-minor', 'Create minor release', function (callback) {
runSequence('minor', 'build', 'commit', callback)
})
// Publish-major
gulp.task('create-major', 'Create major release', function (callback) {
runSequence('major', 'build', 'commit', callback)
})
// gulp patch # makes v0.1.0 → v0.1.1
gulp.task('patch', function () { return inc('patch') })
// gulp minor # makes v0.1.1 → v0.2.0
gulp.task('minor', function () { return inc('minor') })
// gulp major # makes v0.2.1 → v1.0.0
gulp.task('major', function () { return inc('major') })
function inc (importance) {
// get all the files to bump version in
return gulp.src(['./package.json'])
// bump the version number in those files
.pipe(bump({type: importance}))
// save it back to filesystem
.pipe(gulp.dest('./'))
}
// Commit task
gulp.task('commit', function () {
const packJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
return gulp.src(['./package.json'])
.pipe(git.commit('v' + packJson.version))
// read only one file to get the version number
.pipe(filter('package.json'))
// tag it in the repository
.pipe(tagVersion())
})
// Pubish test task
gulp.task('publish-test', ['build:test'], function () {
const cred = require('./cred.json')
const conn = ftp.create({
host: cred.host,
user: cred.user,
password: cred.pass,
parallel: 10,
log: gutil.log
})
const globs = [
config.build_dir + '/**/*'
]
return gulp.src(globs, { base: 'build', buffer: false })
.pipe(conn.newer(cred.test)) // only upload newer files
.pipe(conn.dest(cred.test))
})
// Pubish feature task
gulp.task('publish-feature', ['build:test'], function () {
const cred = require('./cred.json')
const conn = ftp.create({
host: cred.host,
user: cred.user,
password: cred.pass,
parallel: 10,
log: gutil.log
})
const globs = [
config.build_dir + '/**/*'
]
return gulp.src(globs, { base: 'build', buffer: false })
.pipe(conn.newer(cred.feat)) // only upload newer files
.pipe(conn.dest(cred.feat))
})
// Pubish prod task
gulp.task('publish-prod', ['build'], function () {
const cred = require('./cred.json')
const conn = ftp.create({
host: cred.host,
user: cred.user,
password: cred.pass,
parallel: 10,
log: gutil.log
})
const globs = [
config.build_dir + '/**/*',
config.build_dir + '/.htaccess'
]
return gulp.src(globs, { base: 'build', buffer: false })
.pipe(conn.newer(cred.prod)) // only upload newer files
.pipe(conn.dest(cred.prod))
})
})()