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

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:

 

主站蜘蛛池模板: 嘉峪关市| 新巴尔虎右旗| 会同县| 高尔夫| 马山县| 乐平市| 阿尔山市| 万山特区| 林甸县| 渭南市| 洞头县| 南川市| 宽城| 柘荣县| 万山特区| 泽库县| 柳州市| 宁远县| 乌兰县| 丰宁| 拉萨市| 灵丘县| 双鸭山市| 新竹市| 洛浦县| 赞皇县| 合水县| 沧源| 乡城县| 六安市| 安新县| 平泉县| 惠水县| 海伦市| 天峨县| 凯里市| 阿拉善盟| 天等县| 梁河县| 句容市| 金平|