简体   繁体   English

如何依次运行几个gulp函数?

[英]How to run several gulp functions sequentially?

To register a gulp task I use the following code: 要注册一个gulp任务,我使用以下代码:

gulp.task('test-module', function() {
    return testModule(__dirname);
});

This is testModule function : 这是testModule函数:

export function testModule(modulePath) {
    let task1 = buildModule(modulePath, true);
    let task2 = buildTestModule(modulePath);
    let task3 = runModuleTests(modulePath);
    return [task1, task2, task1];
}

The problem with this code is that runModuleTests(modulePath) is called BEFORE buildModule(modulePath, true) and buildTestModule(modulePath) generate files. 此代码的问题在于,在buildModule(modulePath, true)buildTestModule(modulePath)生成文件之前,将runModuleTests(modulePath)称为。 So, when runModuleTests(modulePath) is executed there are no files for testing and no files with tests. 因此,当执行runModuleTests(modulePath) ,没有要测试的文件,也没有带有测试的文件。

I tried also 我也尝试过

import gulpall from 'gulp-all';

export function testModule(modulePath) {
    return gulpall(
            buildModule(modulePath, true),
            buildTestModule(modulePath),
            runModuleTests(modulePath)
    );
}

but the result is the same. 但结果是一样的。 How can I fix it? 我该如何解决?

Your functions, especially the buildModule and buildTestModule are doing something asynchronous inside them. 您的函数,尤其是buildModulebuildTestModule在它们内部执行异步操作。 So runModuleTests is called before they finish as you know. 因此,如您所知, runModuleTests在它们完成之前被调用。 I've simulated this behavior with the code below: 我使用以下代码模拟了这种行为:

const gulp = require('gulp');

// gulp.task('test-module', function() {
gulp.task('default', function() {
  return testModule(__dirname);
});

function testModule(modulePath) {
  let task1 = buildModule(modulePath, true);
  let task2 = buildTestModule(modulePath);
  let task3 = runModuleTests(modulePath);
  return [task1, task2, task1];
}

function buildModule (path)  {

  setTimeout(() => {
    console.log("in buildModule, should be step 1");
  }, 2000);
};

function buildTestModule (path)  {

    setTimeout(() => {
      console.log("in buildTestModule, should be step 2");
    }, 2000);
};

function runModuleTests (path)  {

  console.log("in runModuleTests, should be step 3");
};

I've put in delays in the first two functions to show what is happening when the earlier functions are asynchronous. 我在前两个函数中添加了延迟,以显示早期函数异步时发生的情况。 The result: 结果:

in runModuleTests, should be step 3
in buildModule, should be step 1
in buildTestModule, , should be step 2

One way to fix this is to use async/await and promises if you can. 解决此问题的一种方法是使用async / await,并承诺是否可以。 so try this code: 因此,请尝试以下代码:

gulp.task('test-module', function(done) {
    testModule(__dirname);
    done();
});

// function testModule(modulePath) {

async function testModule(modulePath) {

  let task1 = await buildModule(modulePath, true);
  let task2 = await buildTestModule(modulePath);
  let task3 = await runModuleTests(modulePath);

  return [task1, task2, task1];
}

function buildModule (path)  {
  return new Promise(resolve => {

    setTimeout(() => {
        resolve(console.log("in buildModule, should be step 1"));
    }, 2000);

    // put your functionality here without the setTimeout() call above
  });
};

function buildTestModule (path)  {
  return new Promise(resolve => {

    setTimeout(() => {
      resolve(console.log("in buildTestModule, , should be step 2"));
    }, 2000);

    // put your functionality here without the setTimeout() call above
  });
};

function runModuleTests (path)  {
  return new Promise(resolve => {

   // put your gulp.src pipeline here
   console.log("in runModuleTests, should be step 3");
 });
};

Results: 结果:

in buildModule, should be step 1
in buildTestModule, , should be step 2
in runModuleTests, should be step 3

So make your functions return Promises and then await their result. 因此,使您的函数返回Promises,然后等待其结果。 This will guarantee that the functions return in the right order. 这将确保函数以正确的顺序返回。

In your gulp task method you can have a second argument where you define the task dependencies, that means that the dependencies are run before your main task. 在gulp任务方法中,您可以使用第二个参数定义任务依赖项,这意味着依赖项在主任务之前运行。

Example: 例:

gulp.task('after', ['before1', 'before2', ...], function() {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM