Programmatically passing directory name as string to gulp-rename

273 views Asked by At

Is it possible to pass the name of the parent directory to gulp-rename or any other renaming plugin? I'm trying to programmatically concatenate all the files in the 'src' folders and rename them with the name of their parent directory. Code below is what I have so far.

gulp.task('js-test', function() {
    return gulp.src('dev/scripts/**/src/*.min.js')
        .pipe(concat('interim-name.js'))
        .pipe(rename('How do I give it the name of the parent directory of src'))
        .pipe(gulp.dest('dev/scripts'))
});

The end result would look like this:

  • Dev
    • Scripts
      • Bootstrap
        • Src
          • alert.js (file)
            • alert.js.map (file)
            • alert.min.js (file)
          • tooltip.js (file)
            • tooltip.js.map (file)
            • tooltip.min.js (file)
        • bootstrap.js
        • bootstrap.min.js
      • Fontawesome
        • Src
          • file-1.js
            • file-1.js.map (file)
            • file-1.min.js (file)
          • file-2.js
            • file-2.js.map (file)
            • file-2.min.js (file)
          • file-3.js
            • file-3.js.map (file)
            • file-3.min.js (file)
        • fontawesome.js
        • fontawesome.min.js
1

There are 1 answers

0
Mark On

gulp-rename has the option of using a function, for example from the documentation:

// rename via function 
gulp.src("./src/**/hello.txt")
 .pipe(rename(function (path) {
     path.dirname += "/ciao";
     path.basename += "-goodbye";
     path.extname = ".md"
 }))
 .pipe(gulp.dest("./dist")); // ./dist/main/text/ciao/hello-goodbye.md 

So I would log out path.dirname and then work on a string manipulation of that:

.pipe(rename(function (path) {

   // I use nodePath.sep here but you may have to require path for it to work
   // var nodePath = require('path');
   // but maybe just path.sep without the require will work just fine

   var temp = path.dirname.split(nodePath.sep);
   path.basename = temp[some index here];
}))