简体   繁体   中英

Gulp 4 `series` does not complete tasks synchronously

I have a bunch of targeted/branded apps that I am creating a build routine for. The basic concept is that I call something like gulp iosBuild --target app1 , and various files/folders will be tweaked/moved before the compilation. I seem to be a bit stupid, because the compile step kicks off before the file/folder tweaking bit is done. I have cobbled the following together after going down the Google hole. Can someone tell me what I am doing wrong?


// fetch command line arguments
const arg = (argList => {
  let arg = {}, a, opt, thisOpt, curOpt;
  for (a = 0; a < argList.length; a++) {
    thisOpt = argList[a].trim();
    opt = thisOpt.replace(/^\-+/, '');
    if (opt === thisOpt) {
      // argument value
      if (curOpt) arg[curOpt] = opt;
      curOpt = null;
    } else {
      // argument name
      curOpt = opt;
      arg[curOpt] = true;
    }
  }
  return arg;
})(process.argv);

gulp.task('clean-ios', () => {
  return gulp.src(['App_Resources/iOS/*.xcassets', 'platforms/ios/build/*.*'], { read: false })
    .pipe(clean());
});

// various items that are specific to each branded app
const iosConfigs = [
  {
    name: 'app1',
    bundleName: 'Sample App',
    profile: 'xxxx-xxxx-xxxx',
    appcenterId: 'xxxx-xxxx-xxxx'
  },
  {
    name: 'app2',
    bundleName: 'Sample App2',
    profile: 'xxxx-xxxx-xxxx',
    appcenterId: 'xxxx-xxxx-xxxx'
  },
  {
    name: 'app3',
    bundleName: 'Sample App3',
    profile: 'xxxx-xxxx-xxxx',
    appcenterId: 'xxxx-xxxx-xxxx'
  }
];

// Google told me to do this
function build(done) {

  const setupTasks = iosConfigs.map(config => {
    log(arg);
    if (arg.target !== config.name) {
      log('no match'); // if called with --target app2, return no-ops for app1,app3, etc
      return (taskDone) => {
        taskDone();
      }
    } else
      return (taskDone) => {


        gulp
          .src('src/main.tns.template.ts')
          .pipe(replace('%appcenterid%', config.appcenterId))
          .pipe(rename('main.tns.ts'))
          .pipe(gulp.dest('src'));

        gulp
          .src('package.json')
          .pipe(replace(/(com\.acme\.)([^"]+)"/, '$1' + config.name + '"'))
          .pipe(gulp.dest('.'));

        gulp
          .src('src/app/core/config/app.config.tns.ts')
          .pipe(replace(/(https:\/\/)[^.]+(\.acme\.com)/, '$1' + config.name + '$2'))
          .pipe(gulp.dest('src/app/core/config'));

        gulp
          .src('targets/' + config.name + '/build.xcconfig')
          .pipe(gulp.dest('App_Resources/iOS'));

        gulp
          .src('targets/' + config.name + '/email_logo.png')
          .pipe(rename('brandicon.png'))
          .pipe(gulp.dest('App_Resources/iOS'));

        gulp
          .src('targets/' + config.name + '/*.scss')
          .pipe(gulp.dest('src/styles'));

        gulp
          .src('targets/' + config.name + '/Assets.xcassets/**/*.*', { base: 'targets/' + config.name })
          .pipe(gulp.dest('App_Resources/iOS'));

        taskDone();
      }
  });

  const buildTasks = iosConfigs.map(config => {
    log(arg);
    if (arg.target !== config.name) {
      log('no match');
      return (taskDone) => {
        taskDone();
      }
    } else {
      return (taskDone) => {
        log('running runner');
        run('tns build ios --release --for-device --env.production=true --bundle --env.aot --provision=' + config.profile).exec(config.name, () => {
          log('build done');
        });
        taskDone();
      }
    }
  });

// my expectation is that everything in 'setupTasks' will complete before 'buildTasks' is called... doesn't happen that way though
  return gulp.series('clean-ios', ...setupTasks, ...buildTasks, (seriesDone) => {
    seriesDone();
    done();
  })();
}

exports.iosBuild = build;

Yep, there is no waiting for completion of all your many asynchronous setup tasks.

return (taskDone) => {
  gulp
    .src('src/main.tns.template.ts')
    .pipe(...)

  gulp
    .src('package.json')
    .pipe(...);

  ...

  taskDone(); // will be executed before any of above streams
}

You can try to rewrite this logic to use gulp.parallel composable function like:

return gulp.parallel(
    () => gulp
        .src('src/main.tns.template.ts')
        .pipe(...),
    () => gulp
        .src('package.json')
        .pipe(...),
    ...
)

Here's complete setupTasks code:

const setupTasks = iosConfigs.map(config => {
  log(arg);
  if (arg.target !== config.name) {
    log('no match'); // if called with --target app2, return no-ops for app1,app3, etc
    return (taskDone) => {
      taskDone();
    }
  } else
    return gulp.parallel(
      () => gulp
        .src('src/main.tns.template.ts')
        .pipe(replace('%appcenterid%', config.appcenterId))
        .pipe(rename('main.tns.ts'))
        .pipe(gulp.dest('src')),
      () => gulp
        .src('package.json')
        .pipe(replace(/(com\.acme\.)([^"]+)"/, '$1' + config.name + '"'))
        .pipe(gulp.dest('.')),
      () => gulp
        .src('src/app/core/config/app.config.tns.ts')
        .pipe(replace(/(https:\/\/)[^.]+(\.acme\.com)/, '$1' + config.name + '$2'))
        .pipe(gulp.dest('src/app/core/config')),
      () => gulp
        .src('targets/' + config.name + '/build.xcconfig')
        .pipe(gulp.dest('App_Resources/iOS')),
      () => gulp
        .src('targets/' + config.name + '/!*.scss')
        .pipe(gulp.dest('src/styles')),
      () => gulp
        .src('targets/' + config.name + '/Assets.xcassets/!**!/!*.*', {base: 'targets/' + config.name})
        .pipe(gulp.dest('App_Resources/iOS'))
    )
});

As mentioned by @yurzui problem is that your code is not waiting for all gulp subtasks specified in setupTasks to complete and executes taskDone earlier. Update your setupTasks function to:

 const setupTasks = iosConfigs.map(config => { log(arg); if (arg.target !== config.name) { log('no match'); // if called with --target app2, return no-ops for app1,app3, etc return (taskDone) => { taskDone(); } } else { return (taskDone) => { const task1 = () => gulp .src('src/main.tns.template.ts') .pipe(replace('%appcenterid%', config.appcenterId)) .pipe(rename('main.tns.ts')) .pipe(gulp.dest('src')); const task2 = () => gulp .src('package.json') .pipe(replace(/(com\\.acme\\.)([^"]+)"/, '$1' + config.name + '"')) .pipe(gulp.dest('.')); const task3 = () => gulp .src('src/app/core/config/app.config.tns.ts') .pipe(replace(/(https:\\/\\/)[^.]+(\\.acme\\.com)/, '$1' + config.name + '$2')) .pipe(gulp.dest('src/app/core/config')); const task4 = () => gulp .src('targets/' + config.name + '/build.xcconfig') .pipe(gulp.dest('App_Resources/iOS')); const task5 = () => gulp .src('targets/' + config.name + '/email_logo.png') .pipe(rename('brandicon.png')) .pipe(gulp.dest('App_Resources/iOS')); const task6 = () => gulp .src('targets/' + config.name + '/*.scss') .pipe(gulp.dest('src/styles')); const task7 = () => gulp .src('targets/' + config.name + '/Assets.xcassets/**/*.*', { base: 'targets/' + config.name }) .pipe(gulp.dest('App_Resources/iOS')); return gulp.series( //gulp.parallel task1, task2, task3, task4, task5, task6, task7 )(taskDone); } } });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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