gulp观察者和承诺导致崩溃

时间:2015-10-08 14:32:27

标签: javascript node.js gulp gulp-watch

我正在尝试Gulp,我设置了一个任务来查看我的.js文件,这样当它们被修改后,我的测试就会运行,而且我的代码会被删除。

然而,自从我开始在我的代码中使用promises以来,watch任务就会中断。它会在我第一次修改监视文件时运行正常但在每次运行后都会崩溃:

Message:
    Object.keys called on non-object
Stack:
TypeError: Object.keys called on non-object
    at Function.keys (native)
    at processExports (/Users/hfp/code/coverall/node_modules/nodegit/node_modules/promisify-node/index.js:44:16)
    at module.exports (/Users/hfp/code/coverall/node_modules/nodegit/node_modules/promisify-node/index.js:112:10)
    at Object.<anonymous> (/Users/hfp/code/coverall/node_modules/nodegit/lib/nodegit.js:96:26)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/hfp/code/coverall/lib/archive.js:10:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/hfp/code/coverall/test/archive_spec.js:6:15)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

这是我的完整Gulpfile,我使用gulp dev运行监视任务:

var gulp = require('gulp');
var mocha = require('gulp-mocha');
var gutil = require('gulp-util');
var eslint = require('gulp-eslint');
var console = require('better-console');
var runSequence = require('run-sequence');
var istanbul = require('gulp-istanbul');
var insert = require('gulp-insert');

var spec_files = 'test/**/*_spec.js';
var integration_files = 'test/**/*_integration.js';
var code_files = ['lib/**/*.js', 'helpers/**/*.js', 'index.js'];
var all_files = code_files.concat(spec_files, integration_files);
var coverage_report_dir = 'test/coverage';
var mocha_reporter = 'list';
var eslint_mocha_header = '/*eslint-env mocha */\n';

gulp.task('mocha', function() {
  return gulp.src(spec_files, { read: false })
    .pipe(mocha({ reporter: mocha_reporter }));
});

gulp.task('cov', function(cb) {
  return gulp.src(code_files)
    .pipe(istanbul({ includeUntested: true }))
    .pipe(istanbul.hookRequire())
    .on('finish', function () {
      gulp.src([spec_files, integration_files])
        .pipe(mocha({ reporter: 'dot' }))
        .on('error', function(err) {
          console.log('Error in tests, not checking coverage.');
          cb(err);
        })
        .pipe(istanbul.writeReports( { reporters: ['html', 'text', 'text-summary'] }))
        .on('end', cb);
    });
});

gulp.task('dev', function() {
  return gulp.watch(all_files, ['test'], { read: false });
});

gulp.task('lint', ['eslint-add-mocha-headers'], function(cb) {
  return gulp.src(all_files)
    .pipe(eslint({ useEslintrc: true }))
    .pipe(eslint.format())
    cb(err);
});

gulp.task('clear-console', function() {
  return console.clear();
});

gulp.task('eslint-add-mocha-headers', function(cb) {
  var truncated_header = eslint_mocha_header.substring(0, eslint_mocha_header.length - 1);
  // turn the header into a regex so if I update the header, this task doesn't break
  var header_regex = new RegExp('^' + truncated_header.replace(/\*/gi, '\\*').replace(/\//gi, '\\/'));

  return gulp.src(spec_files)
    .pipe(insert.transform(function(contents, file) {
      if (contents.match(header_regex)) {
        return contents;
      }
      return eslint_mocha_header + contents;
    }))
    .pipe(gulp.dest('test/'));
});

gulp.task('test', function(cb) {
  return runSequence('clear-console',
              'lint',
              'mocha',
              cb);
});

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但解决方案因各种情况而异。其中一个通常会为我修复它:

1)切换到run-sequence的非回调语法。例如:

gulp.task('test', function(){ 
    return runSequence('clear-console', 'lint', 'mocha'); 
});

2)尝试在runSequence中定义eslint-add-mocha-headers的“顺序”依赖项,而不是以两种不同的方式定义所需的运行顺序。在过去的某些情况下,这已经解决了我的问题。 (无论如何它都更具可读性)。 E.g:

gulp.task('test', function(){ 
    return runSequence('eslint-add-mocha-headers', 'clear-console', 'lint', 'mocha'); 
});

3)这是你最好的选择:尽量不要返回gulp.watch。 gulp watch讨厌被退回:在大多数情况下,我已经将这个结果重复地分配给相同的文件,观察者的数量每次对文件进行更改时都会增加。这通常会导致运行Gulp的节点进程崩溃(由于内存耗尽,文件锁定冲突,和/或分配比进程支持更多的观察者)。 E.g:

gulp.task('dev', function() {
    gulp.watch(all_files, ['test'], { read: false });
});

抛出的错误代码是什么?你有没有得到一个堆栈跟踪?