Get the current file name in gulp.src()
GulpGulp Problem Overview
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.
Gulp Solutions
Solution 1 - Gulp
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.
Solution 2 - Gulp
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
Solution 3 - Gulp
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'));
});
Solution 4 - Gulp
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
Solution 5 - Gulp
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/'));
});
Solution 6 - Gulp
If you want to use @OverZealous' answer (https://stackoverflow.com/a/21806974/1019307) 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
).