2016-03-06 80 views
2

有没有办法让这个通用的点,我可以有一个副本,并通过配置项目和文件列表进入它而不是复制它为每个文件/配置组合?如何创建参数化和可重用的吞咽任务

我很想有潜在古怪像

gulp.task('foo_test', function (cb) { 
    run_tests(files.foo_list, config.fooCoverage); 
    cb(); 
} 

注意,在我使用lazypipegulp-load-plugins完整的文件here

// test the server functions and collect coverage data 
gulp.task('api_test', function (cb) { 
    gulp.src(files.api_files) 
     .pipe(istanbulPre()) 
     .on('end', function() { 
      gulp.src(files.api_test_files) 
      .pipe(mochaTask()) 
      .pipe(istanbulAPI()) 
      .on('end', cb); 
     }); 
}); 

var istanbulAPI = lazypipe() 
    .pipe(plugins.istanbul.writeReports, config.apiCoverage); 

config = { 
    apiCoverage: { 
     reporters: ['json'], 
     reportOpts: { 
      json: { 
       dir: 'coverage', 
       file: 'coverage-api.json' 
      } 
     } 
    }, 

回答

2

咕嘟咕嘟代码 仅仅是JavaScript的。

你可以写普通的旧规则的功能,就像平时那样:

function run_tests(srcFiles, srcTestFiles, coverageConfig, cb) { 
    var istanbul = lazypipe() 
    .pipe(plugins.istanbul.writeReports, coverageConfig); 

    gulp.src(srcFiles) 
    .pipe(istanbulPre()) 
    .on('end', function() { 
     gulp.src(srcTestFiles) 
     .pipe(mochaTask()) 
     .pipe(istanbul()) 
     .on('end', cb); 
    }); 
} 

gulp.task('unit_test', function (cb) { 
    run_tests(files.lib_files, files.unit_test_files, config.unitCoverage, cb); 
}); 

gulp.task('api_test', function (cb) { 
    run_tests(files.api_files, files.api_test_files, config.apiCoverage, cb); 
}); 

注意回调cb是传递给run_tests功能只是一个参数。如果在调用run_tests之后立即调用该函数,该函数将在run_tests中的异步代码实际完成之前发出任务完成消息。

+0

谢谢。我用了很长时间的咕噜声,以至于我认为我忘记了这个咕嘟咕噜的声音真的是老式的js。在我的大文件中重复的数量正在扰乱我。感谢您花时间根据实际的src扩展答案。 – skarface