官术网_书友最值得收藏!

Using Gulp for creating auto code compiling and server reloading

With the little introduction we gave about Gulp in the preceding section, we are going to write a gulpfile for telling the computer to execute a few tasks.

I install Gulp and Gulp-shell using npm:

npm install gulp gulp-shell

After this, create a gulpfile.js in the root directory of the project. Here, it is github.com/src/narenaryan/romanserver. Now add this content to gulpfile.js. First, whenever a file changes, install binary task gets executed. Then, the supervisor will be restarted. The watch task looks for any file change and executes the preceding tasks. We are also ordering the tasks so that they occur one after the other synchronously. All of these tasks are Gulp tasks and can be defined by the gulp.task function. It takes two arguments with task name, task. sell.task allows Gulp to execute system commands:

var gulp = require("gulp");
var shell = require('gulp-shell');

// This compiles new binary with source change
gulp.task("install-binary", shell.task([
'go install github.com/narenaryan/romanserver'
]));

// Second argument tells install-binary is a deapendency for restart-supervisor
gulp.task("restart-supervisor", ["install-binary"], shell.task([
'supervisorctl restart myserver'
]))

gulp.task('watch', function() {
// Watch the source code for all changes
gulp.watch("*", ['install-binary', 'restart-supervisor']);

});

gulp.task('default', ['watch']);

Now, if you run the  gulp command in the source directory, it starts watching your source code changes:

gulp

Now, if we modify the code, then the code is compiled, installed, and the server restarted in a flash:

 

主站蜘蛛池模板: 灌阳县| 静海县| 盐源县| 武功县| 连南| 阿拉善左旗| 建昌县| 巍山| 宜良县| 乌兰察布市| 峨山| 四会市| 永年县| 西畴县| 涿州市| 慈溪市| 黄山市| 泾源县| 达拉特旗| 九龙坡区| 逊克县| 罗田县| 汾阳市| 介休市| 兴安盟| 黑河市| 新闻| 岫岩| 南安市| 惠东县| 上饶市| 孟州市| 博爱县| 普兰店市| 景德镇市| 分宜县| 根河市| 迭部县| 广南县| 宝兴县| 南昌县|