使用gulp.src()列出数组中的所有文件

时间:2015-08-02 11:40:06

标签: javascript gulp

我试图在给定文件夹中创建所有文件(路径)的索引。到目前为止,我与gulp.src(filePath)合作实现了这一目标。根据这个blog post,它应该有效:

  

gulp.src(files)是一个包含文件/文件路径的字符串或数组。

我目前的代码:

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

通过gulp.src()输出index[0]的返回值,我得到undefined,而整个index只输出一个没有任何文件路径的大字典。

7 个答案:

答案 0 :(得分:7)

正如OP在评论中所述,解决此问题的一个简单方法是使用fs.readdirSync代替gulp.src

fs = require("fs");
fs.readdirSync(directoryPath); // ["file1", "file2"]

答案 1 :(得分:4)

目前的解决方案是:

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

gulp.src(sources)
  .pipe(debug());

答案 2 :(得分:3)

根据关于 gulp.src https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options)的gulp文档

  

gulp.src(globs [,options])

     

发出匹配提供的glob或一组globs的文件。返回一个   可以通过管道传输到插件的乙烯基文件流。

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

glob指的是node-glob语法,或者它可以是直接文件路径。

     

<强>水珠

     

类型:字符串或数组

     

要阅读的全球或一系列球。

     

选项

     

类型:对象

     

通过glob-stream传递给node-glob的选项。

     除了支持的选项外,

gulp还添加了一些其他选项   by node-glob和glob-stream

所以看来你需要进一步研究这个问题。否则,这可能有用Get the current file name in gulp.src()

答案 3 :(得分:3)

&#13;
&#13;
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);
        });
});
&#13;
&#13;
&#13;

答案 4 :(得分:2)

如果您想要的是来自glob的文件名数组(如gulp.src),请使用:

const glob = require('glob');

const fileArray = glob.sync('./content/**/*.*');

答案 5 :(得分:0)

您可以在gulp 4中执行以下操作:

gulp.src(['./lib/file3.js', './lib/file1.js', './lib/file2.js'])

答案 6 :(得分:0)

或者您可以使用本机Node.js函数readdirSync

result=(
        df1
        .join(
            df2,
            (df1.col1 == df2.col1) & 
            (df1.col2 == df2.col2) & 
            (df1.col3 == df2.col3),
            how="left"
        )