title: Gulp.js: How to dynamically concatenate subdirectory files, but not files in main directory
body: <p>Using Gulp.js, I want to be able to be able to compile individual files in my <code>js</code> folder as normal, but concatenate content placed in subdirectories and assign the folder name to the concatenated file. (there's also some linting and minification going on)</p>
  
  <p><strong>Example Source</strong></p>
  
  <pre><code>|-- js/
  |   |-- main/
  |   |   |-- script1.js
  |   |   |-- script2.js
  |   |   |-- script3.js
  |   |-- detects/
  |   |   |-- scriptA.js
  |   |   |-- scriptB.js
  |   |   |-- scriptC.js
  |   |-- myplugin.js
  |   |-- myplugin2.js
  </code></pre>
  
  <p><strong>Example Output</strong></p>
  
  <pre><code>|-- js/
  |   |-- myplugin.js
  |   |-- myplugin.min.js
  |   |-- myplugin2.js
  |   |-- myplugin2.min.js
  |   |-- main.js
  |   |-- main.min.js
  |   |-- detects.js
  |   |-- detects.min.js
  </code></pre>
  
  <p><strong>What I've tried</strong></p>
  
  <pre><code>gulp.task('scripts', ['clean'], function() {
      return gulp.src('src/js/*')
          .pipe(plumber())
          .pipe(flatten())
          .pipe(tap(function (file, t) {
              if ( file.stat.isDirectory() ) {
                  var name = file.relative + '.js';
                  return gulp.src(file.path + '/*.js')
                      .pipe(concat(name))
                      .pipe(header(banner.full, { package : package }))
                      .pipe(gulp.dest(paths.scripts.output))
                      .pipe(rename({ suffix: '.min.' + Date.now() }))
                      .pipe(uglify())
                      .pipe(header(banner.min, { package : package }))
                      .pipe(gulp.dest(paths.scripts.output));
              }
          }))
          .pipe(header(banner.full, { package : package }))
          .pipe(gulp.dest(paths.scripts.output))
          .pipe(rename({ suffix: '.min.' + Date.now() }))
          .pipe(uglify())
          .pipe(header(banner.min, { package : package }))
          .pipe(gulp.dest(paths.scripts.output));
  });
  </code></pre>
  
  <p>This actually works, but I don't like that I'm repeating code both in and after <code>gulp-tap</code>. Ideally I should be able to do something like this and have it feed back into the stream:</p>
  
  <pre><code>gulp.task('scripts', ['clean'], function() {
      return gulp.src('src/js/*')
          .pipe(plumber())
          .pipe(flatten())
          .pipe(tap(function (file, t) {
              if ( file.stat.isDirectory() ) {
                  var name = file.relative + '.js';
                  return gulp.src(file.path + '/*.js')
                      .pipe(concat(name));
              }
          }))
          .pipe(header(banner.full, { package : package }))
          .pipe(gulp.dest(paths.scripts.output))
          .pipe(rename({ suffix: '.min.' + Date.now() }))
          .pipe(uglify())
          .pipe(header(banner.min, { package : package }))
          .pipe(gulp.dest(paths.scripts.output));
  });
  </code></pre>
  
  <p>However, the subdirectory files are never compiled to the <code>dest</code> this way. other things I've tried that have not worked either:</p>
  
  <p><strong>1</strong></p>
  
  <pre><code>.pipe(tap(function (file, t) {
      if ( file.stat.isDirectory() ) {
          var name = file.relative + '.js';
          t.through(gulp.src(file.path + '/*.js')
              .pipe(concat(name)));
      }
  }))
  </code></pre>
  
  <p><strong>2</strong></p>
  
  <pre><code>.pipe(tap(function (file, t) {
      if ( file.stat.isDirectory() ) {
          var name = file.relative + '.js';
          .pipe(gulp.src(file.path + '/*.js')
              .pipe(concat(name)));
      }
  }))
  </code></pre>
  
  <p><strong>3</strong></p>
  
  <pre><code>.pipe(tap(function (file, t) {
      if ( file.stat.isDirectory() ) {
          var name = file.relative + '.js';
          gulp.src(file.path + '/*.js')
              .pipe(concat(name));
      }
  }))
  </code></pre>
  
  <p><strong>4</strong></p>
  
  <pre><code>.pipe(tap(function (file, t) {
      var scripts;
      if ( file.stat.isDirectory() ) {
          var name = file.relative + '.js';
          scripts = gulp.src(file.path + '/*.js')
              .pipe(concat(name));
      }
      return scripts;
  }))
  </code></pre>
  
  <p>I've got functioning code so I'm not totally stuck, but I know there's a better way to do this. I would also like to be checking for a <code>.js</code> extension before running my uglifying and minifying (something covered for styles already because I'm only grabbing <code>.scss</code> files). Any insights here would be greatly appreciated.</p>