0

I have a nodejs app running with a gulp script. This gulp is started with forever.

forever start gulp

When any exception is thrown, it just gets freezed with below error msg.

[gulp] [nodemon] app crashed - waiting for file changes before starting...

Here for this exception I know that there is no file change required, it was just caused by some connection timeout event.

So, its a forever + gulp + nodejs

Can we automatically restart this gulp script on any exception? Any suggestions would be greatly helpful.

Update 1:

Gulp script

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'
    ]))
});
2
  • There also seems to be nodemon involved. Could you post the gulp script? Commented Jul 21, 2015 at 6:19
  • @ZeMoon - I have updated with gulp script Commented Jul 22, 2015 at 14:13

1 Answer 1

1

Install nodemon globally npm i -g nodemon

And add in your .bashrc (or .bash_profile or .profile) an alias alias gulp='nodemon --watch gulpfile.js --watch gulpfile.babel.js --quiet --exec gulp'

This will watch for file gulpfile.js and gulpfile.babel.js (see Google) changes

p.s. this can be helpful for endless tasks (like watch, ...) but not for single run tasks. I mean it uses watch so it will continue process even after gulp task is done. ;) p.s.2. nodemon has flag to exit on crash it's --exitcrash. so don't include it :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.