-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
executable file
·402 lines (369 loc) · 11.5 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
'use strict';
////////////////////////////////////////////////////////////////////////////////
// REQUIRE
////////////////////////////////////////////////////////////////////////////////
const async = require('async');
const del = require('del');
const fs = require('fs');
const gulp = require('gulp');
const ip = require('ip');
const path = require('path');
const recread = require('recursive-readdir');
const sass = require('gulp-sass');
const vfs = require('vinyl-fs');
const webpack = require('webpack');
const WebpackDevServer = require("webpack-dev-server");
////////////////////////////////////////////////////////////////////////////////
// CONFIGURATION - Develop
////////////////////////////////////////////////////////////////////////////////
const DEVELOP_HOST = process.env.RODAN_CLIENT_DEVELOP_HOST || "localhost";
const DEVELOP_PORT = 9002;
const DEVELOP_SOURCEMAP = 'eval-source-map';
const DEVELOP_WEBROOT = '__develop__';
////////////////////////////////////////////////////////////////////////////////
// CONFIGURATION - Dist
////////////////////////////////////////////////////////////////////////////////
const DIST_WEBROOT = 'dist';
////////////////////////////////////////////////////////////////////////////////
// NOTE: don't edit this unless you know what you're doing.
////////////////////////////////////////////////////////////////////////////////
const CONFIGURATION_FILE = 'configuration.json';
const CONFIGURATION_EXAMPLE_FILE = 'configuration.example.json';
const ENTRY_FILE = './src/js/main.js';
const INFO_FILE = 'info.json';
const NODE_MODULES_DIRECTORY = '/node_modules';
const OUTPUT_FILE = 'rodan_client.min.js';
const PACKAGE_FILE = 'package.json';
const PLUGINS_INCLUSION_FILE = '/plugins.json';
const PLUGINS_FILE = '.plugins.js';
const RESOURCES_DIRECTORY = 'resources';
const SOURCE_DIRECTORY = 'src/js';
const TEMPLATE_DIRECTORY = 'templates';
////////////////////////////////////////////////////////////////////////////////
// WEBPACK
////////////////////////////////////////////////////////////////////////////////
var webpackConfig =
{
entry: ENTRY_FILE,
output:
{
filename: OUTPUT_FILE
},
module: {rules: []},
resolve:
{
modules: [
path.resolve(__dirname + '/src'),
path.resolve(__dirname + '/node_modules')
]
},
plugins: [new webpack.ProvidePlugin({jQuery: "jquery"})]
};
var webpackServerConfig = {};
////////////////////////////////////////////////////////////////////////////////
// TASKS - Develop
////////////////////////////////////////////////////////////////////////////////
/**
* Cleans out develop.
*/
gulp.task('develop:clean', function()
{
return del([DEVELOP_WEBROOT]);
});
/**
* Make web directory.
*/
gulp.task('develop:mkdir', ['develop:clean'], function(callback)
{
return fs.mkdir(DEVELOP_WEBROOT, callback);
});
/**
* Build Webpack configs for develop.
*/
gulp.task('develop:config', function(callback)
{
webpackConfig.devtool = DEVELOP_SOURCEMAP;
webpackConfig.output.path = path.resolve(__dirname, DEVELOP_WEBROOT);
webpackServerConfig.contentBase = DEVELOP_WEBROOT;
callback();
});
/**
* Build templates.
*/
gulp.task('develop:templates', ['develop:mkdir'], function(callback)
{
// Get plugin templates. When done, get the main templates.
var pluginList = getPluginList();
var pluginTemplates = '';
async.each(pluginList, function(plugin, done)
{
var pluginPath = path.resolve(__dirname, NODE_MODULES_DIRECTORY + '/' + plugin + '/templates');
buildTemplates(pluginPath, [], function(error, templates)
{
if (error)
{
done(error);
}
pluginTemplates += templates;
done();
});
},
function(error)
{
if (error)
{
callback(error);
}
// Get the main templates.
buildTemplates(TEMPLATE_DIRECTORY, ['index.html'], function(err, mainTemplates)
{
var indexFile = fs.readFileSync(TEMPLATE_DIRECTORY + '/index.html', 'utf8');
indexFile = indexFile.replace('{templates}', mainTemplates + pluginTemplates);
fs.writeFileSync(DEVELOP_WEBROOT + '/index.html', indexFile);
callback();
});
});
});
/**
* Compile SCSS to CSS.
*/
gulp.task('develop:styles', ['develop:mkdir'], function()
{
return gulp.src('styles/default.scss')
.pipe(sass())
.pipe(gulp.dest(DEVELOP_WEBROOT));
});
/**
* Creates info.json. This holds client data, such as version. It's basically
* a trimmed 'package.json'.
*/
gulp.task('develop:info', ['develop:mkdir'], function(callback)
{
var info = createInfo(function(err, data)
{
fs.writeFileSync(DEVELOP_WEBROOT + '/' + INFO_FILE, JSON.stringify(data, null, 4));
callback();
});
});
/**
* Links build results to web directory.
*/
gulp.task('develop:link', ['develop:mkdir'], function()
{
return gulp.src([CONFIGURATION_FILE, RESOURCES_DIRECTORY]).pipe(vfs.symlink(DEVELOP_WEBROOT));
});
/**
* Bundle (Webpack) and start the development server.
*/
gulp.task('develop', ['plugins:import', 'develop:mkdir', 'develop:config', 'develop:link', 'develop:templates', 'develop:styles', 'develop:info'], function(callback)
{
var compiler = webpack(webpackConfig);
var server = new WebpackDevServer(compiler, webpackServerConfig);
server.listen(DEVELOP_PORT, DEVELOP_HOST, function(err)
{
console.log('');
console.log('==========');
console.log('Starting server on: http://' + DEVELOP_HOST + ':' + DEVELOP_PORT);
console.log('Serving: ' + DEVELOP_WEBROOT);
console.log('');
console.log('Make sure ' + DEVELOP_HOST + ':' + DEVELOP_PORT + ' is allowed access to the Rodan server');
console.log('==========');
console.log('');
});
});
////////////////////////////////////////////////////////////////////////////////
// TASKS - Distribution
////////////////////////////////////////////////////////////////////////////////
/**
* Cleans out dist.
*/
gulp.task('dist:clean', function()
{
return del([DIST_WEBROOT]);
});
/**
* Make dist directory.
*/
gulp.task('dist:mkdir', ['dist:clean'], function(callback)
{
return fs.mkdir(DIST_WEBROOT, callback);
});
/**
* Build Webpack configs for dist.
*/
gulp.task('dist:config', function(callback)
{
var babelRule =
{
test: /\.(js)$/,
use: 'babel-loader'
};
webpackConfig.module.rules.push(babelRule);
webpackConfig.output.path = path.resolve(__dirname, DIST_WEBROOT);
callback();
});
/**
* Build templates.
*/
gulp.task('dist:templates', ['dist:mkdir'], function(callback)
{
// Get plugin templates. When done, get the main templates.
var pluginList = getPluginList();
var pluginTemplates = '';
async.each(pluginList, function(plugin, done)
{
var pluginPath = path.resolve(__dirname, NODE_MODULES_DIRECTORY + '/' + plugin + '/templates');
buildTemplates(pluginPath, [], function(error, templates)
{
if (error)
{
done(error);
}
pluginTemplates += templates;
done();
});
},
function(error)
{
if (error)
{
callback(error);
}
// Get the main templates.
buildTemplates(TEMPLATE_DIRECTORY, ['index.html'], function(err, mainTemplates)
{
var indexFile = fs.readFileSync(TEMPLATE_DIRECTORY + '/index.html', 'utf8');
indexFile = indexFile.replace('{templates}', mainTemplates + pluginTemplates);
fs.writeFileSync(DIST_WEBROOT + '/index.html', indexFile);
callback();
});
});
});
/**
* Compile SCSS to CSS.
*/
gulp.task('dist:styles', ['dist:mkdir'], function()
{
return gulp.src('styles/default.scss')
.pipe(sass())
.pipe(gulp.dest(DIST_WEBROOT));
});
/**
* Creates info.json.
*/
gulp.task('dist:info', ['dist:mkdir'], function(callback)
{
var info = createInfo(function(err, data)
{
fs.writeFileSync(DIST_WEBROOT + '/' + INFO_FILE, JSON.stringify(data, null, 4));
callback();
});
});
/**
* Copy stuff to dist.
*/
gulp.task('dist:copy', ['dist:mkdir'], function()
{
return gulp.src([RESOURCES_DIRECTORY + '/*', CONFIGURATION_FILE, PACKAGE_FILE], {base: './'})
.pipe(gulp.dest(DIST_WEBROOT));
});
/**
* Bundle (Webpack) for distribution.
*/
gulp.task('dist', ['plugins:import', 'dist:mkdir', 'dist:config', 'dist:copy', 'dist:templates', 'dist:styles', 'dist:info'], function(callback)
{
webpack(webpackConfig, callback);
});
////////////////////////////////////////////////////////////////////////////////
// TASKS - Master
////////////////////////////////////////////////////////////////////////////////
/**
* Default task (develop).
*/
gulp.task('default', function(callback)
{
gulp.start('develop');
callback();
});
/**
* Clean everything.
*/
gulp.task('clean', function(callback)
{
gulp.start('develop:clean');
gulp.start('dist:clean');
callback();
});
////////////////////////////////////////////////////////////////////////////////
// TASKS - Plugins
////////////////////////////////////////////////////////////////////////////////
/**
* Create '.plugins.js' based on contents of config.
*/
gulp.task('plugins:import', function(callback)
{
var pluginList = getPluginList();
var pluginsPath = path.resolve(__dirname, SOURCE_DIRECTORY + '/' + PLUGINS_FILE);
var plugins = 'import WorkflowBuilderGUI from \'./WorkflowBuilderGUI.js\'';
/**
for (var i = 0; i < pluginList.length; i++)
{
plugins += 'import \'' + pluginList[i] + '\';\n';
} */
fs.writeFileSync(pluginsPath, plugins);
// Set the creation and modification time for the newly generated file to 10 seconds ago,
// to work around a bug in webpack.
// See https://github.com/webpack/watchpack/issues/25#issuecomment-287789288
var tenSecondsAgo = Date.now() / 1000 - 10
fs.utimes(pluginsPath, tenSecondsAgo, tenSecondsAgo, function (err) { if (err) throw err; });
callback();
});
////////////////////////////////////////////////////////////////////////////////
// UTILITIES
////////////////////////////////////////////////////////////////////////////////
/**
* Returns '<script>'-enclosed templates to be placed in index.html.
*/
function buildTemplates(directory, ignoreArray, callback)
{
recread(directory, ignoreArray, function(err, files)
{
var templates = '';
for (var index in files)
{
var filename = files[index];
var templateName = filename.substring(filename.lastIndexOf('/') + 1);
templateName = templateName.substring(0, templateName.length - 5);
var data = fs.readFileSync(files[index], 'utf8');
templates += '<script type="text/template" id="' + templateName + '">';
templates += data;
templates += '</script>';
}
callback(null, templates);
});
}
/**
* Creates data for info.json.
*/
function createInfo(callback)
{
var json = require('./' + PACKAGE_FILE);
var info = {CLIENT: json};
callback(null, info);
}
/**
* Reads config file to determine which plugins should be included.
*/
function getPluginList()
{
var pluginsInclusionFile = path.resolve(__dirname, + '/' + PLUGINS_INCLUSION_FILE);
try
{
var plugins = require(pluginsInclusionFile);
return Object.keys(plugins);
}
catch (error)
{
return [];
}
}