-
Notifications
You must be signed in to change notification settings - Fork 41
/
gulpfile.babel.js
278 lines (247 loc) · 6.4 KB
/
gulpfile.babel.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
/*eslint strict:0 */
'use strict';
let fork = require('child_process').fork;
let fs = require('fs');
let path = require('path');
let gulp = require('gulp');
let gutil = require('gulp-util');
let rename = require('gulp-rename');
let sass = require('gulp-sass');
let sourcemaps = require('gulp-sourcemaps');
let watchify = require('watchify');
let browserify = require('browserify');
let babelify = require('babelify');
let strictify = require('strictify');
let uglify = require('gulp-uglify');
let cleanCss= require('gulp-clean-css');
let source = require('vinyl-source-stream');
let buffer = require('vinyl-buffer');
let async = require('async');
let _ = require('lodash');
let configTemplate = require('config-template');
let config = require('./lib/config');
let pjson = require('./package.json');
function bundle(b) {
b.bundle()
.on('error', (err) => {
gutil.log(gutil.colors.red('Browserify'), err.toString());
gutil.beep();
})
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/scripts'));
}
let dirs = {
app: [
'views/**/*.ejs',
'routes/**/*.js',
'models/**/*.js',
'lib/**/*.js',
'config/**/*.js',
'errors/**/*.js',
'app.js'
]
};
let app = {
instance: {},
path: './bin/www',
env: _.extend({}, process.env, { NODE_ENV: 'development', port: 3000 }),
start: function( callback ) {
process.execArgv.push('--use_strict');
app.instance = fork( app.path, { silent: true, env: app.env } );
app.instance.stdout.pipe( process.stdout );
app.instance.stderr.pipe( process.stderr );
gutil.log( gutil.colors.cyan( 'Starting' ), 'express server ( PID:', app.instance.pid, ')' );
if( callback ) {
callback();
}
},
stop: function( callback ) {
if( app.instance.connected ) {
app.instance.on( 'exit', function() {
gutil.log( gutil.colors.red( 'Stopping' ), 'express server ( PID:', app.instance.pid, ')' );
if( callback ) {
callback();
}
});
return app.instance.kill( 'SIGINT' );
}
if( callback ) {
callback();
}
},
restart: function( event ) {
async.series([
app.stop,
app.start
]);
}
};
gulp.task('watchify', ['check-config'], function() {
let b = browserify({
cache: {},
packageCache: {},
plugin: [watchify],
debug: true,
entries: ['./public/scripts/main.js']
});
b.on('log', (message) => {
gutil.log(gutil.colors.green('Browserify'), message);
});
b.on('update', bundle.bind(this, b));
b.transform(babelify, {presets: ['es2015', 'react']});
b.transform(strictify);
bundle(b);
});
gulp.task('server', ['watchify', 'sass'], app.start);
gulp.task('sass', ['check-config'], function() {
return gulp.src('public/styles/**/*.{scss,sass}')
.pipe(rename(function(p) {
p.basename += p.extname;
}))
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true
}).on('error', sass.logError))
.pipe(sourcemaps.write('./'))
.pipe(rename(function(p) {
if(p.extname !== '.map') {
p.extname = '.css';
}
}))
.pipe(gulp.dest('public/styles'));
});
gulp.task('check-config', function(cb) {
let localExists = false;
async.series({
checkLocal: function(cb) {
fs.lstat(path.join(__dirname, 'config', 'local.js'), function(err, stat) {
if(err) {
cb();
}
else if(!stat.isFile()) {
cb('config/local.js must be a file, not a directory.');
}
else {
localExists = true;
cb();
}
});
},
checkTemplate: function(cb) {
if(localExists) {
return cb();
}
doConfig(cb);
}
}, function(err) {
if(err) {
gutil.log(gutil.colors.red('config'), err);
process.exit(1);
}
else {
cb();
}
});
});
function doConfig(cb) {
let templatePath = path.join(__dirname, 'config', 'local.template.js');
fs.lstat(templatePath, function(err, stat) {
if(err) {
cb('Unable to load local config template: '+err.toString());
}
else if(!stat.isFile()) {
cb('config/local.template.js must be a file, not a directory.');
}
else {
let template = require(templatePath);
configTemplate(template)
.then(function(config) {
let localPath = path.join(__dirname, 'config', 'local.js');
fs.writeFile(localPath, 'module.exports = '+JSON.stringify(config, null, '\t')+';', function(err) {
if(err) {
cb('There was a problem saving the local.js config file: '+err.toString());
}
else {
cb();
}
});
})
.catch(function(err) {
cb('Something went wrong while configuring the local.js file.');
});
}
});
}
gulp.task('config', doConfig);
gulp.task('dev', ['watchify', 'server', 'sass'], function() {
gulp.watch( dirs.app, app.restart );
gulp.watch('public/styles/**/*.{scss,sass}', ['sass']);
});
gulp.task('build', ['jsmin', 'cssmin'], function() {
console.log('build complete');
process.exit(0);
});
gulp.task('jsmin', ['prod-js-bundle'], function() {
return gulp.src('./public/scripts/bundle.js')
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./public/scripts'));
});
gulp.task('cssmin', ['prod-sass-bundle'], function() {
return gulp.src('./public/styles/main.scss.css')
.pipe(cleanCss({compatibility: 'ie8', s0: true}))
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./public/styles'));
});
gulp.task('prod-js-bundle', function() {
let b = browserify({
cache: {},
packageCache: {},
plugin: [watchify],
debug: false,
fullPaths: false,
entries: ['./public/scripts/main.js']
});
b.transform(babelify, {presets: ['es2015', 'react']});
b.transform(strictify);
return b.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulp.dest('./public/scripts'));
});
gulp.task('prod-sass-bundle', function() {
return gulp.src('public/styles/**/*.{scss,sass}')
.pipe(rename(function(p) {
p.basename += p.extname;
}))
.pipe(sass({
errLogToConsole: true
}).on('error', sass.logError))
.pipe(rename(function(p) {
if(p.extname !== '.map') {
p.extname = '.css';
}
}))
.pipe(gulp.dest('public/styles'));
});
// gulp deploy
// gulp deploy --env=digitalocean
gulp.task('deploy', require('./gulp/deploy/gulp-deploy')(config, configTemplate, pjson));
gulp.task('test', function() {
let LinuxTools = require('./gulp/deploy/linuxTools');
let lt = new LinuxTools('root', '104.236.44.29');
return lt.execute('gitLatestTag', '/usr/share/nginx/apps/test-app', '[email protected]:alarner/impact-web.git')
.then((data) => {
console.log('got data', data);
})
.catch((err) => {
console.log('caught error', err);
});
});