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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31771541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:10:29  来源:igfitidea点击:

List all files in array with gulp.src()

javascriptgulp

提问by user3147268

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(filePath)努力实现这一目标。根据这篇博客文章,它应该可以工作:

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

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

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 undefinedand the whole indexoutputs only a large dictionary without any filepaths.

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

采纳答案by Rokie

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

根据gulp.src上的gulp文档(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

gulp.src(globs[, options])

发出与提供的 glob 或一组 glob 匹配的文件。返回可以通过管道传输到插件的 Vinyl 文件流。

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

glob 是指 node-glob 语法,也可以是直接文件路径。

球体

类型:字符串或数组

要读取的 Glob 或 glob 数组。

选项

类型:对象

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

gulp 除了 node-glob 和 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()

因此,您似乎需要进一步研究这一点。否则这可能有帮助在 gulp.src() 中获取当前文件名

回答by Chase Sandmann

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

正如 OP 在评论中所述,这个问题的一个简单解决方案是使用fs.readdirSync而不是gulp.src

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

回答by Blitz

The current solution is:

目前的解决办法是:

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

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

回答by fdr

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);
        });
});

回答by Mark

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

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

const glob = require('glob');

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

回答by Wildhammer

You can do the following in gulp 4 :

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

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

回答by Tibor

Or you can use native Node.js function readdirSync

或者你可以使用原生 Node.js 函数readdirSync

const fs = require('fs');

const syncImgFolder = (done) => {
    const files = fs.readdirSync('/path/');
}