Get the current file name in gulp.src()
Asked Answered
V

7

146

In my gulp.js file I'm streaming all HTML files from the examples folder into the build folder.

To create the gulp task is not difficult:

var gulp = require('gulp');

gulp.task('examples', function() {
    return gulp.src('./examples/*.html')
        .pipe(gulp.dest('./build'));
});

But I can't figure out how retrieve the file names found (and processed) in the task, or I can't find the right plugin.

Vannessavanni answered 16/2, 2014 at 3:29 Comment(2)
If you’ve solved your problem, you should either post your answer below. You should not simply edit your question with the solution.Cimbura
@AndrewMarshall ... as my comment to the edit, that you deleted, explains, I don't want to claim to have found the correct answer but want to give the credits to the answer that deserves it. That's why I didn't post the small modification as an answer. If you think, that removing my edit is better for the users (or just complies with the rules of this site), then I'm OK with that. For anyone else, just click at the edit history to see what was there before.Vannessavanni
C
183

I'm not sure how you want to use the file names, but one of these should help:

  • If you just want to see the names, you can use something like gulp-debug, which lists the details of the vinyl file. Insert this anywhere you want a list, like so:

    var gulp = require('gulp'),
        debug = require('gulp-debug');
    
    gulp.task('examples', function() {
        return gulp.src('./examples/*.html')
            .pipe(debug())
            .pipe(gulp.dest('./build'));
    });
    
  • Another option is gulp-filelog, which I haven't used, but sounds similar (it might be a bit cleaner).

  • Another options is gulp-filesize, which outputs both the file and it's size.

  • If you want more control, you can use something like gulp-tap, which lets you provide your own function and look at the files in the pipe.

Cay answered 16/2, 2014 at 3:37 Comment(2)
Thank you! The "log" plugins where not what I was looking for, because it only writes to the console log. But gulp-tap allows to get the path for each file, so I can use it for updating my list in another HTML file.Vannessavanni
What if I want to store all the src files in an array? debug doesn't give many options.Ephemeral
F
26

I found this plugin to be doing what I was expecting: gulp-using

Simple usage example: Search all files in project with .jsx extension

gulp.task('reactify', function(){
        gulp.src(['../**/*.jsx']) 
            .pipe(using({}));
        ....
    });

Output:

[gulp] Using gulpfile /app/build/gulpfile.js
[gulp] Starting 'reactify'...
[gulp] Finished 'reactify' after 2.92 ms
[gulp] Using file /app/staging/web/content/view/logon.jsx
[gulp] Using file /app/staging/web/content/view/components/rauth.jsx
Fitzpatrick answered 22/5, 2014 at 22:34 Comment(1)
gulp-using did what I was looking for thanks a lot.Bagley
H
14

Here is another simple way.

var es, log, logFile;

es = require('event-stream');

log = require('gulp-util').log;

logFile = function(es) {
  return es.map(function(file, cb) {
    log(file.path);
    return cb(null, file);
  });
};

gulp.task("do", function() {
 return gulp.src('./examples/*.html')
   .pipe(logFile(es))
   .pipe(gulp.dest('./build'));
});
Hannus answered 26/6, 2014 at 21:14 Comment(3)
You know, most people don't know coffeescript, so it would be much more beneficial to just write the answer in the most basic way to the maximum number of readers would benefit :)Gerita
For me es.map throws an error: SyntaxError: Unexpected token .Gerita
+1 for this solution (without plugins). But there's a bug, instead return cb(); it should be cb(null, file);. Otherwise the files will be filtered out and won't go further down the pipe chain. More info check docs for es.map()Ridinger
S
6

You can use the gulp-filenames module to get the array of paths. You can even group them by namespaces:

var filenames = require("gulp-filenames");

gulp.src("./src/*.coffee")
    .pipe(filenames("coffeescript"))
    .pipe(gulp.dest("./dist"));

gulp.src("./src/*.js")
  .pipe(filenames("javascript"))
  .pipe(gulp.dest("./dist"));

filenames.get("coffeescript") // ["a.coffee","b.coffee"]  
                              // Do Something With it 
Sacramentalist answered 11/9, 2015 at 16:26 Comment(0)
A
4

For my case gulp-ignore was perfect. As option you may pass a function there:

function condition(file) {
 // do whatever with file.path
 // return boolean true if needed to exclude file 
}

And the task would look like this:

var gulpIgnore = require('gulp-ignore');

gulp.task('task', function() {
  gulp.src('./**/*.js')
    .pipe(gulpIgnore.exclude(condition))
    .pipe(gulp.dest('./dist/'));
});
Anchor answered 11/4, 2016 at 8:9 Comment(0)
A
0

If you want to use @OverZealous' answer (https://mcmap.net/q/158321/-get-the-current-file-name-in-gulp-src) in Typescript, you need to import instead of require:

import * as debug from 'gulp-debug';

...

    return gulp.src('./examples/*.html')
        .pipe(debug({title: 'example src:'}))
        .pipe(gulp.dest('./build'));

(I also added a title).

Around answered 14/9, 2016 at 3:17 Comment(2)
why import instead of require ?Gerita
Yeah I don't know. That was in my early days of JS development and in reflection i should have put more investigation in to why that worked for me. But it did and thus why I put it up!Around
D
0

I use gulp-flatmap

I even can get every html page browser tab to be auto named after the file name

for more info, you might find this gulp-boilerplate usful

Doorpost answered 30/3, 2023 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.