提交 690b146d 编写于 作者: B bryk

Spawn backend application process on serve task

This setup autoreloads backend process on source change and ensures that
there is only one process running.

Follow up would be to wire this setup with browser sync to proxy API
calls to the backend.
上级 6c7c22db
......@@ -3,9 +3,10 @@ node_modules/
bower_components/
## Build and test artifacts
coverage/
.go_workspace/
.sass-cache/
.tmp/
coverage/
dist/
## IDE specific files
......
......@@ -45,7 +45,7 @@ const goBackendDependencies = [
*/
function spawnGoProcess(args, doneFn, opt_env) {
let goTask = child.spawn('go', args, {
env: lodash.merge(process.env, {GOPATH: conf.paths.backendTmp}, opt_env || {}),
env: lodash.merge(process.env, {GOPATH: conf.paths.goWorkspace}, opt_env || {}),
});
// Call Gulp callback on task exit. This has to be done to make Gulp dependency management
......@@ -75,7 +75,7 @@ function spawnGoProcess(args, doneFn, opt_env) {
gulp.task('backend', ['backend-dependencies'], function(doneFn) {
spawnGoProcess([
'build',
'-o', path.join(conf.paths.serve, 'dashboard'),
'-o', path.join(conf.paths.serve, conf.backend.binaryName),
path.join(conf.paths.backendSrc, 'dashboard.go'),
], doneFn);
});
......@@ -89,7 +89,8 @@ gulp.task('backend', ['backend-dependencies'], function(doneFn) {
* dependencies inside it and is targeted for Linux.
*/
gulp.task('backend:prod', ['backend-dependencies'], function(doneFn) {
let outputBinaryPath = path.join(conf.paths.dist, 'dashboard');
let outputBinaryPath = path.join(conf.paths.dist, conf.backend.binaryName);
// Delete output binary first. This is required because prod build does not override it.
del(outputBinaryPath)
.then(function() {
......
......@@ -32,14 +32,8 @@ import conf from './conf';
/**
* Builds production package and places it in the dist directory.
*
* Following steps are done here:
* 1. Vendor CSS and JS files are concatenated and minified.
* 2. index.html is minified.
* 3. CSS and JS assets are suffixed with version hash.
* 4. Everything is saved in the dist directory.
*/
gulp.task('build', ['assets', 'backend:prod', 'index:prod'], function () {
gulp.task('build', ['backend:prod', 'build-frontend'], function () {
let htmlFilter = gulpFilter('*.html', {restore: true});
let vendorCssFilter = gulpFilter('**/vendor.css', {restore: true});
let vendorJsFilter = gulpFilter('**/vendor.js', {restore: true});
......@@ -75,6 +69,18 @@ gulp.task('build', ['assets', 'backend:prod', 'index:prod'], function () {
});
/**
* Builds production version of the frontend application.
*
* Following steps are done here:
* 1. Vendor CSS and JS files are concatenated and minified.
* 2. index.html is minified.
* 3. CSS and JS assets are suffixed with version hash.
* 4. Everything is saved in the dist directory.
*/
gulp.task('build-frontend', ['assets', 'index:prod']);
/**
* Copies assets to the dist directory.
*/
......@@ -88,5 +94,5 @@ gulp.task('assets', function () {
* Cleans all build artifacts.
*/
gulp.task('clean', function () {
return del([path.join(conf.paths.dist, '/'), path.join(conf.paths.tmp, '/')]);
return del([conf.paths.dist, conf.paths.goWorkspace, conf.paths.tmp]);
});
......@@ -28,6 +28,16 @@ const basePath = path.join(__dirname, '../');
* Exported configuration object with common constants used in build pipeline.
*/
export default {
/**
* Backend application constants.
*/
backend: {
/**
* The name of the backend binary.
*/
binaryName: 'dashboard',
},
/**
* Deployment constants configuration.
*/
......@@ -64,6 +74,7 @@ export default {
externs: path.join(basePath, 'src/app/externs'),
frontendSrc: path.join(basePath, 'src/app/frontend'),
frontendTest: path.join(basePath, 'src/test/frontend'),
goWorkspace: path.join(basePath, '.go_workspace'),
integrationTest: path.join(basePath, 'src/test/integration'),
karmaConf: path.join(basePath, 'build/karma.conf.js'),
nodeModules: path.join(basePath, 'node_modules'),
......
......@@ -17,12 +17,21 @@
*/
import browserSync from 'browser-sync';
import browserSyncSpa from 'browser-sync-spa';
import child from 'child_process';
import gulp from 'gulp';
import path from 'path';
import conf from './conf';
/**
* Currently running backend process object. Null if the backend is not running.
*
* @type {?child.ChildProcess}
*/
let runningBackendProcess = null;
/**
* Initializes BrowserSync tool. Files are server from baseDir directory list.
*
......@@ -35,6 +44,7 @@ function browserSyncInit(baseDir) {
}));
browserSync.instance = browserSync.init({
// TODO(bryk): Add proxy to the backend here.
startPath: '/',
server: {
baseDir: baseDir,
......@@ -47,7 +57,7 @@ function browserSyncInit(baseDir) {
/**
* Serves the application in development mode.
*/
gulp.task('serve', ['watch'], function () {
gulp.task('serve', ['spawn-backend', 'watch'], function () {
browserSyncInit([
conf.paths.serve,
conf.paths.frontendSrc, // For angular templates to work.
......@@ -60,11 +70,51 @@ gulp.task('serve', ['watch'], function () {
/**
* Serves the application in production mode.
*/
gulp.task('serve:prod', ['build'], function () {
gulp.task('serve:prod', ['build-frontend', 'spawn-backend'], function () {
browserSyncInit(conf.paths.dist);
});
/**
* Spawns new backend application process and finishes the task immediately. Previously spawned
* backend process is killed beforehand, if any.
*/
gulp.task('spawn-backend', ['backend', 'kill-backend'], function () {
runningBackendProcess = child.spawn(path.join(conf.paths.serve, conf.backend.binaryName));
runningBackendProcess.on('exit', function() {
// Mark that there is no backend process running anymore.
runningBackendProcess = null;
});
runningBackendProcess.stdout.on('data', function (data) {
console.log('' + data);
});
runningBackendProcess.stderr.on('data', function (data) {
console.error('' + data);
});
});
/**
* Kills running backend process (if any).
*/
gulp.task('kill-backend', function (doneFn) {
if (runningBackendProcess) {
runningBackendProcess.on('exit', function() {
// Mark that there is no backend process running anymore.
runningBackendProcess = null;
// Finish the task only when the backend is actually killed.
doneFn();
});
runningBackendProcess.kill();
} else {
doneFn();
}
});
/**
* Watches for changes in source files and runs Gulp tasks to rebuild them.
*/
......@@ -84,4 +134,5 @@ gulp.task('watch', ['index'], function () {
});
gulp.watch(path.join(conf.paths.frontendSrc, '**/*.js'), ['scripts']);
gulp.watch(path.join(conf.paths.backendSrc, '**/*.go'), ['spawn-backend']);
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册