-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·266 lines (246 loc) · 6.96 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
// native packages
var path = require('path');
// vendor packages
var gulp = require('gulp');
var gutil = require('gulp-util');
var del = require('del');
var babel = require('gulp-babel');
var webpack = require('webpack');
var sourcemaps = require('gulp-sourcemaps');
var nodemon = require('gulp-nodemon');
var notify = require('gulp-notify');
var async = require('async');
var mergeStream = require('merge-stream');
var jsesc = require('jsesc');
// local modules
var webpackConfig = {
development: require('./configs/env/webpack.config.dev'),
production: require('./configs/env/webpack.config.prod'),
};
var babelConfig = {
developmentClient: require('./configs/env/babel.config.dev.client'),
developmentServer: require('./configs/env/babel.config.dev.server'),
production: require('./configs/env/babel.config.prod'),
};
var paths = {
scripts: './src/server/**/*.js',
reacts: './src/common/**/*.js',
componentStyles: [
'./src/common/**/*.scss',
// './src/common/components/**/*.less',
// './src/common/components/**/*.styl',
// './src/common/components/**/*.css',
// './src/common/container/**/*.scss',
],
statics: './src/public/**/*',
nodemonWatchIgnore: [
'gulpfile.js',
'node_modules/**/*',
'build/**/*',
'.deploy/**/*',
'public/js/bundle.js',
'src/common/**/*',
],
targetDir: 'build',
isomorphicConfigDir: 'configs/project/isomorphic',
};
function _babelStream(src, dest, config) {
return gulp
.src(src)
.pipe(sourcemaps.init())
.pipe(babel(config))
.on('error', notify.onError({
title: 'babel fail',
message: '<%= error.message %>',
}))
.pipe(sourcemaps.write({
includeContent: false,
sourceRoot: './src',
}))
.pipe(gulp.dest(dest));
}
function _webpackTask(config, cb) {
webpack(config, function(err, stats) {
if (err) {
return cb(err);
}
var jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
return cb(jsonStats.errors);
}
if (jsonStats.warnings.length > 0) {
gutil.log(gutil.colors.yellow(jsonStats.warnings));
}
cb();
});
}
// clean build files
gulp.task('clean', function() {
return del.sync([
paths.targetDir,
paths.isomorphicConfigDir,
]);
});
// build nodejs source files
gulp.task('build:nodejs', function() {
return _babelStream(
paths.scripts,
path.join(paths.targetDir, 'server'),
babelConfig.production
);
});
// build reactjs source files
gulp.task('build:reactjs', ['build:nodejs'], function() {
return _babelStream(
paths.reacts,
path.join(paths.targetDir, 'common'),
babelConfig.production
);
});
// bundle react components
gulp.task('webpack:production', ['build:reactjs'], function(cb) {
_webpackTask(webpackConfig.production, cb);
});
// copy static files
gulp.task('copy', function() {
var staticTask = gulp
.src(paths.statics)
.pipe(gulp.dest(path.join(paths.targetDir, 'public')));
var componentStyleTask = gulp
.src(paths.componentStyles)
.pipe(gulp.dest(path.join(paths.targetDir, 'common')));
return mergeStream(staticTask, componentStyleTask);
});
// launch development server
gulp.task('serve', function(cb) {
var started = false;
var entryPath = path.join(__dirname, 'src/server/index.js');
return nodemon({
script: entryPath,
watch: ['src/**/*.js'],
ext: 'js',
env: {
NODE_ENV: 'development',
},
ignore: paths.nodemonWatchIgnore,
})
.on('start', function() {
if (!started) {
cb();
started = true;
}
})
.on('restart', function() {
});
});
// deploy to heroku
gulp.task('deploy', function(cb) {
var pkg = require('./package.json');
var fs = require('fs');
var exec = require('child_process').exec;
var appName = gutil.env.a || gutil.env.app || pkg.name;
var isCreateNewApp = Boolean(gutil.env.c || gutil.env.create);
gutil.log('deploy to heroku app:', gutil.colors.underline(appName));
var execCmds = function(cmds, cwd, cbExec) {
async.eachSeries(cmds, function(cmd, cbSeries) {
gutil.log('execute command', gutil.colors.grey(cmd));
exec(cmd, {
cwd: cwd? path.join(process.cwd(), cwd): process.cwd(),
}, function(err, stdout, stderr) {
if (err) {
return cbSeries({
err: err,
msg: err.message,
cmd: cmd,
});
}
if (stdout) {
console.log(stdout);
}
cbSeries();
});
}, cbExec);
};
var cbCmdDone = function(err) {
if (err) {
gutil.log(gutil.colors.red('failed to deploy'));
gutil.log(gutil.colors.red('\terror command:'));
gutil.log(gutil.colors.red('\t\t' + err.cmd));
gutil.log(gutil.colors.red('\terror message:'));
gutil.log(gutil.colors.red('\t\t' + err.msg));
return cb(err.err);
}
gutil.log(
'Please open',
gutil.colors.underline('https://' + appName + '.herokuapp.com'));
cb();
};
async.series([
function checkFolder(cbSeries) {
if (!fs.existsSync('./.deploy')) {
fs.mkdir('.deploy', 0766, cbSeries);
} else {
cbSeries();
}
},
function checkGit(cbSeries) {
if (!fs.existsSync('./.deploy/.git')) {
execCmds([
'git init',
], './.deploy', cbSeries);
} else {
cbSeries();
}
},
function prepareFiles(cbSeries) {
execCmds([
'rm -f ./package.json',
'rm -rf ./build',
'rm -rf ./configs',
'mkdir configs',
'cp -r ../configs/project ./configs/',
'cp ../configs/templates/Procfile ./',
'cp ../package.json ./',
'cp -r ../build ./',
], './.deploy', cbSeries);
},
function publish(cbSeries) {
if (isCreateNewApp) {
execCmds([
'git add . -A',
'heroku create ' + appName,
'git commit -m "Deploy"',
'git push heroku master -f',
], './.deploy', cbSeries);
} else {
execCmds([
'git add . -A',
'heroku git:remote -a ' + appName,
'git commit -m "Deploy upgrade"',
'git push heroku master -f',
], './.deploy', cbSeries);
}
},
], cbCmdDone);
});
gulp.task('dumpConfigs', function() {
var projectServerConfigs = require('./configs/project/server');
var dumpValue = jsesc(JSON.stringify(projectServerConfigs), { json: true });
console.log(gutil.colors.gray('\n\n======================='));
console.log('Please follow the instructions:');
console.log('1. Go to `https://travis-ci.org/<owner>/<repo>/settings`');
console.log('2. Add new environment variable named `' + gutil.colors.yellow('PROJECT_SERVER_CONFIGS') + '`');
console.log('3. Set its value as\n' + gutil.colors.yellow(dumpValue));
console.log(gutil.colors.gray('=======================\n\n'));
});
gulp.task('build:production', function() {
gulp.start(
'clean',
'build:nodejs',
'build:reactjs',
'webpack:production',
'copy');
});
gulp.task('default', function() {
gulp.start('serve');
});