-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
238 lines (206 loc) · 7.2 KB
/
gulpfile.js
File metadata and controls
238 lines (206 loc) · 7.2 KB
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
/* jshint node: true */
/* global $: true */
'use strict';
/*
FIXME/TODO:
x css sourcemaps not working (they were not properly uploaded)
x only upload changed files
x switch to eslint
* css reload seems to happen before upload is finished -> check again. fix is in upload-styles
* allow multiple js files -> should be fixed?
* delete before deploy / or some kind of sync
* allow inserting bower dependencies via wiredep
* how to deal with base path being specified in .bowerrc?
* make this a repo
* option for ruby-sass
* compile sass on partial changes
* use mac notifications for errors
*/
/**
* initialization
*/
// read config files
var config = require('./project.config.json');
var ftpConfig = require(config.ftpConfig);
// init gulp and plugins
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({
pattern: ['gulp-*', 'gulp.*', 'browser-sync', 'main-bower-files', 'vinyl-ftp', 'uglify-save-license']
});
var joinPath = require('path').join;
// configure ftp
ftpConfig.log = $.util.log;
$.ftp = $.vinylFtp.create(ftpConfig);
/**
* utilities
*/
// common error handler vor gulp plugins
var errorHandler = function(title) {
return function(err) {
$.util.log($.util.colors.red('[' + title + ']'), err.toString());
this.emit('end');
};
};
// get the src glob array from config
// TODO: error handling
var srcPath = function(name) {
var glob = config.paths[name]['src']; // could be an array of globs
if (!Array.isArray(glob)) {
glob = [glob];
}
return glob.map(function (el) {
if (el.startsWith('!')) return joinPath('!' + config.basePath, el.substring(1));
return joinPath(config.basePath, el);
});
};
// get the dest path (string) from config (optionally appending and ext to the path)
var destPath = function(name, ext) {
var path = config.paths[name]['dest']; // needs to be a string
ext = ext || '';
return joinPath(config.basePath, path, ext);
};
// console.log(srcPath('styles'));
// console.log(destPath('styles', '*'));
// process.exit();
/**
* JS / jshint, uglify, sourcemaps
*/
gulp.task('scripts', ['bower-scripts'], function() {
return gulp.src(srcPath('scripts') )
.pipe( $.eslint() )
.pipe( $.eslint.format() )
.pipe( $.sourcemaps.init() )
// .pipe( $.uglify({ preserveComments: $.uglifySaveLicense })).on('error', errorHandler('Uglify') )
.pipe( $.terser() )
.pipe( $.rename({extname: '.min.js'}) )
.pipe( $.sourcemaps.write('.') )
.pipe( gulp.dest(destPath('scripts')) )
.pipe( $.size({title: "scripts:", showFiles: true}) );
});
gulp.task('bower-scripts', function() {
// TODO: minify before concat. only files that don't end in .min.js
var mainBowerFilesOptions = {
debugging: false,
includeDev: true,
checkExistence: true,
filter: "**/*.js"
};
return gulp.src( $.mainBowerFiles(mainBowerFilesOptions) )
.pipe( $.debug({title: "bower-scripts:"}) )
// .pipe($.sourcemaps.init())
.pipe( $.concat('vendor.js') )
.pipe( $.uglify({ preserveComments: $.uglifySaveLicense })).on('error', errorHandler('Uglify') )
// .pipe($.rename({extname: '.min.js'}))
// .pipe($.sourcemaps.write( '.' ))
.pipe( gulp.dest(destPath('scripts')) )
.pipe( $.size({title: "bower-scripts:", showFiles: true}) );
});
gulp.task('upload-scripts', ['scripts'], function() {
var globs = srcPath('scripts'); // sources (glob array)
globs.push( destPath('scripts', '*')); // add dest folder
return gulp.src( globs, {base: config.basePath} )
.pipe( $.cached() ) // only pass through changed files
// .pipe( $.ftp.newerOrDifferentSize(ftpConfig.remoteBase) )
.pipe( $.ftp.dest(ftpConfig.remoteBase) )
.pipe( $.browserSync.stream({ once: true }) )
.pipe( $.size() );
});
/**
* CSS / sass, autoprefixer, minify-css, sourcemaps, livereload
*/
gulp.task('styles', ['bower-styles'], function() {
var sassOptions = {
outputStyle: 'expanded'
};
return gulp.src( srcPath('styles') )
// .pipe($.inject(injectFiles, injectOptions))
// .pipe(wiredep(_.extend({}, conf.wiredep)))
.pipe( $.sourcemaps.init() )
.pipe( $.sass(sassOptions) ).on( 'error', errorHandler('Sass') )
.pipe( $.autoprefixer() ).on( 'error', errorHandler('Autoprefixer') )
.pipe( $.cleanCss() ).on( 'error', errorHandler('Minify CSS') )
.pipe( $.sourcemaps.write('.') )
.pipe( gulp.dest(destPath('styles')) )
.pipe( $.size({title: "styles:", showFiles: true}) );
});
gulp.task('bower-styles', function() {
var mainBowerFilesOptions = {
debugging: false,
includeDev: true,
checkExistence: true,
filter: "**/*.css"
};
return gulp.src( $.mainBowerFiles(mainBowerFilesOptions) )
.pipe( $.debug({title: "bower-styles:"}) )
// .pipe($.sourcemaps.init())
.pipe( $.concat('vendor.css') )
.pipe( $.cleanCss() ).on( 'error', errorHandler('Minify CSS') )
// .pipe($.rename({extname: '.min.js'}))
// .pipe($.sourcemaps.write( '.' ))
.pipe( gulp.dest(destPath('styles')) )
.pipe( $.size({title: "bower-styles:", showFiles: true}) );
});
gulp.task('upload-styles', ['styles'], function() {
var globs = srcPath('styles'); // sources (glob array)
globs.push( destPath('styles', '*') ); // add everything in destination folder
return gulp.src( globs, {base: config.basePath} )
.pipe( $.cached() ) // only pass through changed files
.pipe( $.ftp.dest(ftpConfig.remoteBase) ).on('end', function(argument) {
// console.log("ALL UPLOADED");
})
// .pipe( $.ftp.newerOrDifferentSize(ftpConfig.remoteBase) )
.pipe( $.browserSync.stream({ once: true }) )
.pipe( $.size() );
});
/**
* start the browsersync server
*/
gulp.task('browsersync', [], function() {
$.browserSync.init({
proxy: config.browserSyncProxy,
browser: config.browser
});
});
/**
* build (compile all scripts & styles)
*/
gulp.task('build', ['scripts', 'styles']);
/**
* watch styles & scripts (with livereload via browsersync)
*/
gulp.task('serve', ['browsersync', 'build'], function() {
// watch styles
gulp.watch( srcPath('styles'), ['styles', 'upload-styles'] );
// watch scripts
gulp.watch( srcPath('scripts'), ['scripts', 'upload-scripts']);
// watch templates etc.
gulp.watch( srcPath('templates'), function(event) {
gulp.src( event.path, {base: config.basePath} )
.pipe( $.cached() ) // only pass through changed files
.pipe( $.ftp.dest(ftpConfig.remoteBase) )
.pipe( $.browserSync.stream({ once: true }) )
.pipe( $.size() );
// browserSync.reload(event.path);
});
});
gulp.task('watch', ['serve']);
/**
* deploy to ftp
*/
gulp.task('deploy', ['build'], function() {
// upload everything in base folder
var globs = [
config.basePath + '/**/*',
// '!' + config.basePath + '/bower_components/**/*'
];
// TODO: fix paths beginning with !
// globs = globs.map(function(path) {
// return joinPath(config.basePath, path);
// });
return gulp.src( globs, {base: config.basePath, buffer: false} )
.pipe( $.ftp.dest(ftpConfig.remoteBase) );
});
/**
* default task
*/
gulp.task('default', ['build']);