title: How to execute the same task with gulp, synchronously, once per each folder (in a monorepo with subpackages)?
body: <p>I have the following project structure (monorepo with many packages)</p>
  
  <pre><code>  /pkgA
      gulpfile.js
    /pkgB
      gulpfile.js
    /pkgC
      gulpfile.js
  </code></pre>
  
  <p>Each package has a <code>gulpfile.js</code> which just loads <code>/shared/shared-gulp-tasks.js</code></p>
  
  <p><em>Important: we want to keep this independence (so that I can run <code>gulp whatever</code> only for a given package, if wanted) - i.e. I don't want to remove the existing tasks from the shared gulpfile, I want to reuse them.</em></p>
  
  <p>If we want to build <strong>everything at once</strong>, we run a task synchronously like this:</p>
  
  <pre><code>bash for-each-package.sh "gulp package"
  </code></pre>
  
  <p>Which does something like</p>
  
  <pre><code>  cd pkgA
    gulp package
    cd pkgB
    gulp package
    cd pkgC
    gulp package
  </code></pre>
  
  <p><strong>However this is slow, because I start <code>gulp</code> executable from scratch for each package, and it takes ~3 seconds every time to load gulp and all the needed deps.</strong> (we have 20+ subpackages).</p>
  
  <p>What I want is to have a task defined in <code>gulpfile.js</code> in the root which would let me do</p>
  
  <pre><code>gulp package-all
  </code></pre>
  
  <p>The code would look like this:</p>
  
  <pre><code>  gulp.task('package-all', function(done) {
      ['pkgA', 'pkgB', 'pkgC'].forEach(function(pkgName) {
        process.chdir(path.join(__dirname, pkgName));
        // need to run 'package' task here, synchronously
        // gulp.start('package'); is async
      });
      done();
    }
  </code></pre>
  
  <p>Note that folder-specific <code>package</code> task is already declared in shared gulpfile and I don't want to rewrite it.</p>
  
  <p>The problem is that I want to do this synchronously, and when all folders are finished processing, call <code>done()</code>.</p>
  
  <p>Options explored so far:</p>
  
  <ul>
  <li><p><code>gulp.run</code> is deprecated, <code>gulp.start</code> is undocumented, generally not advised
  and they don't work in this case (they are async)</p></li>
  <li><p><code>runSequence</code> looks promising, but how would I run same task many times, per-folder, with <code>cd</code> to that folder before</p></li>
  </ul>
  
  <p>I am aware that what I ask is kind-of orthogonal to "the gulp way" but I don't want to rewrite all my tasks.</p>
  
  <p>What can be a good way to achieve my goals?</p>