-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
executable file
·209 lines (186 loc) · 5.63 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
'use strict';
var gulp = require('gulp');
var fs = require('fs');
var nodemon = require('nodemon');
var webpack = require('webpack');
var Sandbox = require('sandboxjs');
var Promise = require('bluebird');
var readFile = Promise.promisify(fs.readFile);
var colors = require('colors/safe');
var jscs = require('gulp-jscs');
var jshint = require('gulp-jshint');
// We need to exclude node_modules, otherwise webpack will bundle them
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function (x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function (mod) {
nodeModules[mod] = 'commonjs ' + mod;
})
;
// This helper function creates the configuration object that is sent to webpack
// for triggering local and webtask builds
function buildWebpackConfig(config) {
var webpackConfig = {
module: {
loaders: [
{test: /\.js$/, exclude: /node_modules/, loaders: ['babel']}
]
},
entry: [
config.entry
],
target: 'node',
output: {
path: config.outputFolder,
filename: config.output
},
node: {
__dirname: true,
__filename: true
},
externals: config.externals,
recordsPath: config.outputFolder + '/_records',
plugins: [new webpack.IgnorePlugin(/\.(css|less)$/)]
}
// This adds source-map-support in orded to enable debugging ES6 files
if (config.sourceMap) {
webpackConfig.devtool = 'source-map'
webpackConfig.debug = true;
webpackConfig.plugins.push(new webpack.BannerPlugin('require("source-map-support").install();', {raw: true, entryOnly: false}));
}
// This hack is used for exporting the entry point module of the file generated by webpack
if (config.addModuleExports) {
webpackConfig.plugins.push(new webpack.BannerPlugin('module.exports = ', {raw: true, entryOnly: false}));
}
return webpackConfig;
}
// This helper is used for running the api locally, with debug support
function runApi(config) {
var nodemonConfig = {
script: config.script,
env: {NODE_ENV: config.env || 'development'},
ext: 'noop'
};
if (config.debug) {
nodemonConfig.execMap = {
js: 'node-inspector --web-port=8484 & node --debug'
};
}
nodemon(nodemonConfig);
}
// This is the reporter used for the webpack build
function onBuild(done) {
return function (err, stats) {
if (err) {
console.log('Error', err);
} else {
console.log(stats.toString());
}
if (done) {
done();
}
}
}
// This helper creates a webtask using sandboxjs
function deployWebtask(deployConfig, done) {
var config = require(deployConfig.config)();
if (!config.webtaskName) {
return done(colors.white('=> ') +
colors.red('error deploying webtask: ') +
'Missing webtask name, please define the webtaskName key in ' +
colors.cyan(deployConfig.config));
}
if (!config.webtaskToken) {
return done(colors.white('=> ') +
colors.red('error deploying webtask: ') +
'Missing webtask token, please define the webtaskToken key in ' +
colors.cyan(deployConfig.config));
}
var profile = Sandbox.fromToken(config.webtaskToken);
readFile(deployConfig.source)
.then(function (code) {
return profile.create(code.toString(), {
name: config.webtaskName,
secrets: config.secret || {},
params: config.param || {},
parse: false,
merge: false
});
})
.then(function (webtask) {
console.log(colors.white(' => ') +
colors.green('webtask successfully deployed to: ') +
colors.yellow(webtask.url));
})
.then(done)
.catch(function (error) {
return done(colors.white('=> ') +
colors.red('error deploying webtask: ') +
error);
})
;
}
gulp.task('jscs', function () {
return gulp.src('src/**/*.js')
.pipe(jscs())
.pipe(jscs.reporter());
});
gulp.task('jshint', function () {
return gulp.src('src/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter());
});
// This task builds the api for running it locally
gulp.task('build', ['jshint', 'jscs'], function (done) {
webpack(buildWebpackConfig({
entry: './src/server.js',
sourceMap: true,
output: 'backend.js',
outputFolder: 'build',
externals: nodeModules
})).run(onBuild(done));
});
// This task builds and run the api locally
gulp.task('run', ['build'], function () {
runApi({
script: './build/backend.js',
env: 'development'
});
});
// This task builds and run the api locally in debug mode
gulp.task('debug', ['build'], function () {
runApi({
script: './build/backend.js',
env: 'development',
debug: true
});
});
// This task builds the webtask script containg the entire api in a single file
gulp.task('buildWebtask', function (done) {
webpack(buildWebpackConfig({
entry: './src/webtask.js',
output: 'webtask.js',
outputFolder: 'build',
externals: nodeModules,
addModuleExports: true
})).run(onBuild(done));
});
// This task builds the configuration file for the webtask
gulp.task('buildWebtaskConfig', ['jshint', 'jscs'], function (done) {
webpack(buildWebpackConfig({
entry: './config/webtask.config.js',
output: 'webtask.config.js',
outputFolder: 'build',
externals: nodeModules,
addModuleExports: true
})).run(onBuild(done));
});
// This task builds and creates the webtask using wt-cli
gulp.task('deployWebtask', ['buildWebtask', 'buildWebtaskConfig'], function (done) {
deployWebtask({
source: './build/webtask.js',
config: './build/webtask.config.js'
}, done);
});