2017-01-31 13:13:06 +00:00
|
|
|
var gulp = require('gulp');
|
|
|
|
var gutil = require('gulp-util');
|
|
|
|
var wrap = require('gulp-wrap');
|
|
|
|
var concat = require('gulp-concat');
|
|
|
|
var extend = require('gulp-extend');
|
|
|
|
var merge = require('merge-stream');
|
2017-05-29 11:38:40 +00:00
|
|
|
var fs = require('fs');
|
2017-01-31 13:13:06 +00:00
|
|
|
var del = require('del');
|
|
|
|
var webpack = require('webpack');
|
|
|
|
var WebpackDevServer = require('webpack-dev-server');
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
2017-03-01 08:55:17 +00:00
|
|
|
var srcDir = './client';
|
2017-01-31 13:13:06 +00:00
|
|
|
var buildDir = './services/nginx/static';
|
|
|
|
var langs = ['es', 'en'];
|
|
|
|
|
|
|
|
var modules = require('./spliting/modules.json');
|
|
|
|
var webpackConfig = require('./webpack.config.js');
|
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
// Main tasks
|
|
|
|
|
|
|
|
gulp.task('build', ['clean'], function() {
|
|
|
|
return gulp.start('routes', 'locales', 'webpack');
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('default', ['clean'], function() {
|
|
|
|
return gulp.start('services', 'watch', 'routes', 'locales', 'webpack-dev-server');
|
|
|
|
});
|
|
|
|
|
|
|
|
gulp.task('client', ['clean'], function() {
|
|
|
|
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
|
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('clean', function() {
|
|
|
|
return del(`${buildDir}/*`, {force: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Spliting
|
|
|
|
|
|
|
|
var splitingFiles = './spliting/*';
|
|
|
|
|
2017-05-17 10:07:42 +00:00
|
|
|
gulp.task('spliting', function(cb) {
|
2017-01-31 13:13:06 +00:00
|
|
|
var importTpl = fs.readFileSync('./spliting/import.tpl.js', 'utf8');
|
|
|
|
var requireTpl = fs.readFileSync('./spliting/require.tpl.js', 'utf8');
|
|
|
|
|
2017-03-01 08:55:17 +00:00
|
|
|
for (var modName in modules) {
|
2017-01-31 13:13:06 +00:00
|
|
|
var deps = modules[modName];
|
|
|
|
var splitFile = `${srcDir}/${modName}/src/spliting.js`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(splitFile);
|
2017-03-01 08:55:17 +00:00
|
|
|
} catch (e) {}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
fs.appendFileSync(splitFile, importTpl);
|
|
|
|
|
|
|
|
var i = deps.length;
|
2017-05-17 10:07:42 +00:00
|
|
|
while (i--)
|
2017-01-31 13:13:06 +00:00
|
|
|
fs.appendFileSync(splitFile,
|
|
|
|
requireTpl.replace(/\$module/g, deps[i]));
|
|
|
|
}
|
2017-05-17 10:07:42 +00:00
|
|
|
|
|
|
|
cb();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Webpack
|
|
|
|
|
2017-05-17 10:07:42 +00:00
|
|
|
gulp.task('webpack', ['spliting'], function(cb) {
|
2017-01-31 13:13:06 +00:00
|
|
|
var configCopy = Object.create(webpackConfig);
|
|
|
|
var compiler = webpack(configCopy);
|
|
|
|
|
|
|
|
compiler.run(function(err, stats) {
|
|
|
|
if (err) throw new gutil.PluginError('webpack', err);
|
|
|
|
gutil.log('[webpack]', stats.toString({colors: true}));
|
2017-05-17 10:07:42 +00:00
|
|
|
cb();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-05-17 10:07:42 +00:00
|
|
|
gulp.task('webpack-dev-server', ['spliting'], function() {
|
2017-01-31 13:13:06 +00:00
|
|
|
var configCopy = Object.create(webpackConfig);
|
|
|
|
|
2017-03-01 08:55:17 +00:00
|
|
|
for (var entry in configCopy.entry) {
|
2017-01-31 13:13:06 +00:00
|
|
|
configCopy.entry[entry]
|
|
|
|
.unshift('webpack-dev-server/client?http://0.0.0.0:8081/');
|
2017-03-01 08:55:17 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
var compiler = webpack(configCopy);
|
2017-03-01 08:55:17 +00:00
|
|
|
new WebpackDevServer(compiler, {
|
2017-01-31 13:13:06 +00:00
|
|
|
publicPath: '/',
|
|
|
|
contentBase: buildDir,
|
|
|
|
quiet: false,
|
|
|
|
noInfo: false,
|
|
|
|
// hot: true,
|
|
|
|
stats: {
|
|
|
|
assets: true,
|
|
|
|
colors: true,
|
|
|
|
version: false,
|
|
|
|
hash: false,
|
|
|
|
timings: true,
|
|
|
|
chunks: false,
|
|
|
|
chunkModules: false
|
|
|
|
}
|
2017-03-07 16:11:56 +00:00
|
|
|
}).listen(8081, '0.0.0.0', function(err) {
|
2017-01-31 13:13:06 +00:00
|
|
|
if (err) throw new gutil.PluginError('webpack-dev-server', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Locale
|
|
|
|
|
|
|
|
var localeFiles = `./${srcDir}/**/locale/*.json`;
|
|
|
|
|
|
|
|
gulp.task('locales', function() {
|
|
|
|
var streams = [];
|
|
|
|
|
2017-03-07 16:11:56 +00:00
|
|
|
for (var mod in modules)
|
2017-03-01 08:55:17 +00:00
|
|
|
for (var lang of langs) {
|
|
|
|
var localeFiles = `./client/${mod}/**/locale/${lang}.json`;
|
|
|
|
streams.push(gulp.src(localeFiles)
|
|
|
|
.pipe(extend(`${lang}.json`))
|
|
|
|
.pipe(gulp.dest(`${buildDir}/locale/${mod}`)));
|
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
return merge(streams);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2017-05-17 10:07:42 +00:00
|
|
|
var routeFiles = `./${srcDir}/**/routes.json`;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
gulp.task('routes', function() {
|
|
|
|
function cb(file) {
|
|
|
|
var relative = file.relative.replace(/\\/g, '/');
|
2017-05-17 10:07:42 +00:00
|
|
|
var dirname = relative.match(/^(.*)\/routes\.json$/)[1];
|
|
|
|
return {
|
|
|
|
dirname: dirname,
|
|
|
|
json: file.contents
|
|
|
|
};
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gulp.src(routeFiles)
|
2017-05-17 10:07:42 +00:00
|
|
|
.pipe(wrap('\n"<%=dirname%>": <%=json%>', cb))
|
2017-01-31 13:13:06 +00:00
|
|
|
.pipe(concat('routes.js', {newLine: ','}))
|
2017-05-17 10:07:42 +00:00
|
|
|
.pipe(wrap('var routes = {<%=contents%>\n};'))
|
2017-01-31 13:13:06 +00:00
|
|
|
.pipe(gulp.dest(buildDir));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Watch
|
|
|
|
|
|
|
|
gulp.task('watch', function() {
|
|
|
|
gulp.watch(splitingFiles, ['spliting']);
|
|
|
|
gulp.watch(routeFiles, ['routes']);
|
|
|
|
gulp.watch(localeFiles, ['locales']);
|
|
|
|
});
|
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
// Services
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
gulp.task('services', function() {
|
|
|
|
var auth = require('./services/auth/server/server.js');
|
|
|
|
var client = require('./services/client/server/server.js');
|
|
|
|
var server = require('./services/salix/server/server.js');
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-05-16 10:37:48 +00:00
|
|
|
auth.start();
|
|
|
|
client.start();
|
|
|
|
server.start();
|
2017-01-31 13:13:06 +00:00
|
|
|
});
|