如何依次运行几个gulp函数? [英] How to run several gulp functions sequentially?

查看:98
本文介绍了如何依次运行几个gulp函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要注册一个gulp任务,我使用以下代码:

To register a gulp task I use the following code:

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

这是testModule函数:

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

此代码的问题是runModuleTests(modulePath)buildModule(modulePath, true)buildTestModule(modulePath)生成文件之前被称为.因此,执行runModuleTests(modulePath)时,没有要测试的文件,也没有带有测试的文件.

The problem with this code is that runModuleTests(modulePath) is called BEFORE buildModule(modulePath, true) and buildTestModule(modulePath) generate files. So, when runModuleTests(modulePath) is executed there are no files for testing and no files with tests.

我也尝试过

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?

推荐答案

您的函数(尤其是buildModulebuildTestModule)正在它们内部进行异步操作.因此,如您所知,runModuleTests会在它们完成之前被调用.我已经使用以下代码模拟了这种行为:

Your functions, especially the buildModule and buildTestModule are doing something asynchronous inside them. So runModuleTests is called before they finish as you know. 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

解决此问题的一种方法是使用async/await并承诺(如果可以).因此,请尝试以下代码:

One way to fix this is to use async/await and promises if you can. 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");
 });
};

结果:

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

因此,使您的函数返回Promises,然后等待其结果.这样可以确保函数以正确的顺序返回.

So make your functions return Promises and then await their result. This will guarantee that the functions return in the right order.

这篇关于如何依次运行几个gulp函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆