20

I'm trying to create an index of all file(path)s within a given folder. So far I worked with gulp.src(filePath) to achieve that. According to this blog post, it should work:

gulp.src(files) is a string or array containing the file(s) / file paths.

My current code:

gulp.task("createFileIndex", function(){
    var index = gulp.src(['./content/**/*.*']);
    console.log("INDEX:", index[0]);
});

By outputing the returned values of gulp.src() with index[0] I get undefined and the whole index outputs only a large dictionary without any filepaths.

user3147268
  • 1,814
  • 7
  • 26
  • 39

7 Answers7

14

The current solution is:

var gulp = require('gulp');
var debug = require('gulp-debug');

gulp.src(sources)
  .pipe(debug());
Blitz
  • 249
  • 3
  • 16
  • This is the best answer: Avoids all unnecessary extra code and only shows the minimal code needed to test this functionality. – Capy Dec 23 '19 at 09:05
9

As the OP stated in a comment, a simple solution to this problem would be to use fs.readdirSync instead of gulp.src:

fs = require("fs");
fs.readdirSync(directoryPath); // ["file1", "file2"]
Chase Sandmann
  • 4,795
  • 3
  • 30
  • 42
7

var through = require('through2');
gulp.task('getFileList', function () {
    var fileList = [];
    gulp.src(['./someFolder/**/*.ext', '!./someFolder/unwantedFolder/**/*'])
        .pipe(through.obj(function (file, enc, cb) {
            fileList.push(file.path);
            cb(null);
        }))
        .pipe(gulp.dest('./temp/'))
        .on ('end', function () {
            console.log(fileList);
        });
});
fdr
  • 71
  • 1
  • 1
4

According to the gulp documentation on gulp.src (https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options)

gulp.src(globs[, options])

Emits files matching provided glob or an array of globs. Returns a stream of Vinyl files that can be piped to plugins.

gulp.src('client/templates/*.jade')
  .pipe(jade())
  .pipe(minify())
  .pipe(gulp.dest('build/minified_templates'));

glob refers to node-glob syntax or it can be a direct file path.

globs

Type: String or Array

Glob or array of globs to read.

options

Type: Object

Options to pass to node-glob through glob-stream.

gulp adds some additional options in addition to the options supported by node-glob and glob-stream

So it seems you need to look in further on this. Otherwise this maybe helpful Get the current file name in gulp.src()

Community
  • 1
  • 1
Rokie
  • 222
  • 2
  • 11
  • 4
    I created a workaround with nodejs' internal `js` library. `fs.readdirSync()` lists all files of a defined folder. – user3147268 Aug 02 '15 at 16:53
4

If all you want is the array of filenames from a glob (like gulp.src) use:

const glob = require('glob');

const fileArray = glob.sync('./content/**/*.*');
Community
  • 1
  • 1
Mark
  • 143,421
  • 24
  • 428
  • 436
0

Or you can use native Node.js function readdirSync

const fs = require('fs');

const syncImgFolder = (done) => {
    const files = fs.readdirSync('/path/');
}
Tibor
  • 73
  • 1
  • 1
  • 8
-2

You can do the following in gulp 4 :

gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js'])
Wildhammer
  • 2,017
  • 1
  • 27
  • 33