salix/gulpfile.js

189 lines
5.0 KiB
JavaScript
Raw Normal View History

var gulp = require('gulp');
var gutil = require('gulp-util');
var wrap = require('gulp-wrap');
var concat = require('gulp-concat');
var merge = require('merge-stream');
2017-06-02 12:17:57 +00:00
var extend = require('gulp-extend');
var install = require('gulp-install');
var print = require('gulp-print');
var del = require('del');
2017-05-29 16:48:50 +00:00
var fs = require('fs');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var services = require("./gulpfiles/services.json");
// Configuration
2017-03-01 08:55:17 +00:00
var srcDir = './client';
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('default', function() {
return gulp.start('services', 'client');
2017-05-16 10:37:48 +00:00
});
gulp.task('build', ['clean'], function() {
return gulp.start('routes', 'locales', 'webpack');
2017-05-16 10:37:48 +00:00
});
gulp.task('client', ['clean'], function() {
return gulp.start('watch', 'routes', 'locales', 'webpack-dev-server');
});
gulp.task('copy', function() {
var streams = [];
2017-06-08 07:29:03 +00:00
for (i = 0; i < services.services.length; i++) {
var service = services.services[i];
2017-06-08 07:29:03 +00:00
for (j = 0; j < services.files.length; j++) {
var file = services.files[j];
streams.push(gulp.src("./services/service/models/" + file)
.pipe(gulp.dest(service + "/common/models/")));
}
}
return merge(streams);
});
gulp.task('services', ['copy'], function() {
require('./services/auth/server/server.js').start();
require('./services/salix/server/server.js').start();
require('./services/mailer/server.js').start();
2017-06-08 07:29:03 +00:00
for (i = 0; i < services.services.length; i++) {
require(services.services[i] + "/server/server.js").start();
}
});
gulp.task('clean', function() {
return del([`${buildDir}/*`, `!${buildDir}/templates`, `!${buildDir}/images`], {force: true});
});
// Spliting
var splitingFiles = './spliting/*';
2017-05-17 10:07:42 +00:00
gulp.task('spliting', function(cb) {
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) {
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) {}
fs.appendFileSync(splitFile, importTpl);
var i = deps.length;
2017-05-17 10:07:42 +00:00
while (i--)
fs.appendFileSync(splitFile,
requireTpl.replace(/\$module/g, deps[i]));
}
2017-05-17 10:07:42 +00:00
cb();
});
// Webpack
2017-05-17 10:07:42 +00:00
gulp.task('webpack', ['spliting'], function(cb) {
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-05-17 10:07:42 +00:00
gulp.task('webpack-dev-server', ['spliting'], function() {
var configCopy = Object.create(webpackConfig);
2017-03-01 08:55:17 +00:00
for (var entry in configCopy.entry) {
configCopy.entry[entry]
2017-09-17 13:09:10 +00:00
.unshift('webpack-dev-server/client?http://127.0.0.1:8081/');
2017-03-01 08:55:17 +00:00
}
var compiler = webpack(configCopy);
2017-03-01 08:55:17 +00:00
new WebpackDevServer(compiler, {
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-09-17 13:09:10 +00:00
}).listen(8081, '127.0.0.1', function(err) {
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}`)));
}
return merge(streams);
});
// Routes
var routeFiles = `${srcDir}/**/routes.json`;
gulp.task('routes', function() {
return gulp.src(routeFiles)
.pipe(concat('routes.js', {newLine: ','}))
.pipe(wrap('var routes = [<%=contents%>\n];'))
.pipe(gulp.dest(buildDir));
});
// install packages from package.json in folders.
gulp.task('install', () => {
const pathServices = './services/';
const fileJson = [];
const services = fs.readdirSync(pathServices);
services.push('..');
services.forEach((service) => {
fileJson.push(pathServices.concat(service, '/package.json'));
});
return gulp.src(fileJson)
.pipe(print((filepath) => {
return `Installing packages in ${filepath}`;
}))
.pipe(install({
npm: ['--no-package-lock']
}));
});
// Watch
gulp.task('watch', function() {
gulp.watch(splitingFiles, ['spliting']);
gulp.watch(routeFiles, ['routes']);
gulp.watch(localeFiles, ['locales']);
});