Sunday, 20 September 2020

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;



from Gulp 4 `series` does not complete tasks synchronously

No comments:

Post a Comment