我有一个运行了gulp脚本的nodejs应用程序。这口吞是永远的。
forever start gulp
当抛出任何异常时,它会被以下错误消息冻结。
[gulp] [nodemon] app crashed - waiting for file changes before starting...
这里针对此异常我知道不需要更改文件,它只是由某些连接超时事件引起的。
所以,它是一个永远+ gulp + nodejs
我们可以在任何异常时自动重启这个gulp脚本吗?任何建议都会有很大帮助。
更新1:
Gulp脚本
var fs = require('fs'),
path = require('path'),
gulp = require('gulp'),
gutil = require('gulp-util'),
es = require('event-stream'),
concat = require('gulp-concat'),
runSequence = require('gulp-run-sequence'),
templateCache = require('gulp-angular-templatecache'),
minifyHtml = require('gulp-minify-html'),
ngmin = require('gulp-ngmin'),
less = require('gulp-less'),
rimraf = require('gulp-rimraf'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
uglify = require('gulp-uglify'),
minifyCss = require('gulp-minify-css'),
gulpif = require('gulp-if'),
nodemon = require('gulp-nodemon'),
_ = require('lodash'),
moment = require('moment'),
shell = require('gulp-shell'),
minimist = require('minimist'),
rename = require('gulp-rename');
var BASE = 'src/client/app',
ASSETS_BASE = 'src/client/assets'
var appFiles = applyPrefix(BASE, ['/entry/app.js', '/**/*.js']),
templateFiles = applyPrefix(BASE, ['/**/*.html']),
lessFiles = applyPrefix(BASE, ['/entry/less/main.less', '/**/*.less']),
adminLess = applyPrefix(BASE, ['/entry/less/admin.less']),
libFiles = applyPrefix(ASSETS_BASE, ['/js/lib/jquery.min.js', '/js/lib/lodash.min.js', '/js/lib/angular.min.js', '/js/lib/**/*.js', '/js/components/**/*.js']),
assetFiles = applyPrefix(ASSETS_BASE, ['/fonts/**/*', '/img/**/*', '/translations/**/*', '/favicon.ico']),
viewsFiles = applyPrefix(BASE, ['/entry/views/*.html']);
var compile = gutil.env.compile;
gulp.task('default', function(cb) {
runSequence(
'clean',
['scripts', 'assets', 'less', 'adminLess', 'views'],
'watch',
cb
);
});
gulp.task('clean', function() {
return gulp.src('src/server/public', { read: false })
.pipe(rimraf());
});
gulp.task('scripts', function() {
gulp.src(['src/server/public/js/*main.js', 'src/server/public/js/*lib.js', 'src/server/public/js/*templates.js'], { read: false })
.pipe(rimraf());
var buildTimestamp = moment().format("MM-D-YYYY-HH:mm:ss");
fs.writeFile('src/server/js-build-timestamp.json', JSON.stringify({ buildTimestamp: buildTimestamp }), function(err) {
if (err) return console.log("Could not create js-build-timestamp file");
console.log("Created js-build-timestamp file: " + buildTimestamp);
});
// App files
gulp.src(appFiles)
.pipe(jshint({
strict: false,
laxbreak: true,
debug: true,
globals: {
angular: true,
$: true,
_: true
}
}))
.pipe(jshint.reporter(stylish))
.pipe(concat(buildTimestamp + '-main.js'))
.pipe(gulpif(compile, ngmin()))
.pipe(gulpif(compile, uglify()))
.pipe(gulp.dest('src/server/public/js'));
// Template files
gulp.src(templateFiles)
.pipe(minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(templateCache({
module: 'xxxProjName',
root: '',
base: function(file) {
return 'templates/' + path.basename(file.relative);
}
}))
.pipe(concat(buildTimestamp + '-templates.js'))
.pipe(gulpif(compile, ngmin()))
.pipe(gulpif(compile, uglify()))
.pipe(gulp.dest('src/server/public/js'));
// Library files
gulp.src(libFiles)
.pipe(concat(buildTimestamp + '-lib.js'))
.pipe(gulpif(compile, uglify({
preserveComments: 'some'
})))
.pipe(gulp.dest('src/server/public/js'));
});
gulp.task('less', function() {
gulp.src(['src/server/public/css/*.css'], { read: false })
.pipe(rimraf());
var buildTimestamp = moment().format("MM-D-YYYY-HH:mm:ss");
fs.writeFile('src/server/css-build-timestamp.json', JSON.stringify({ buildTimestamp: buildTimestamp }), function(err) {
if (err) return console.log("Could not create css-build-timestamp file");
console.log("Created css-build-timestamp file: " + buildTimestamp);
});
gulp.src(lessFiles[0])
.pipe(less())
.pipe(rename(buildTimestamp + '-main.css'))
.pipe(gulpif(compile, minifyCss()))
.pipe(gulp.dest('src/server/public/css'));
});
gulp.task('adminLess', function() {
gulp.src(adminLess)
.pipe(less())
.pipe(gulpif(compile, minifyCss()))
.pipe(gulp.dest('src/server/public/css'));
});
gulp.task('assets', function() {
gulp.src(assetFiles, { base: ASSETS_BASE})
.pipe(gulp.dest('src/server/public/'));
});
gulp.task('views', function() {
gulp.src(viewsFiles)
.pipe(gulp.dest('src/server/views/'));
});
gulp.task('watch', function() {
if (compile) return;
gulp.watch([appFiles, templateFiles, libFiles], ['scripts']);
gulp.watch(['client/index.html', assetFiles], ['assets']);
gulp.watch([lessFiles], ['less', 'adminLess']);
gulp.watch([viewsFiles], ['views']);
nodemon({
script: 'src/server/server.js',
watch: 'src/server',
ext: 'json js',
ignore: ['client/*, src/server/public/*']
});
});
function applyPrefix(prefix, fileNames) {
return _.map(fileNames, function(fileName) {
return prefix + fileName;
});
}
gulp.task('bleh', function() {
gulp.src('')
.pipe(shell([
'touch test'
]))
});
答案 0 :(得分:0)
全局安装nodemon npm i -g nodemon
并在.bashrc(或.bash_profile或.profile)中添加别名
alias gulp='nodemon --watch gulpfile.js --watch gulpfile.babel.js --quiet --exec gulp'
这将监视文件gulpfile.js和gulpfile.babel.js(参见Google)更改
<强> P.S。这对于无休止的任务(如手表,......)非常有用,但对于单次运行任务则无效。我的意思是它使用手表,所以即使在gulp任务完成后它也会继续处理。 ;) p.s.2。 nodemon有标记在崩溃时{#1}}退出。所以不要包含它:)