我正在尝试创建给定文件夹中所有文件(路径)的索引.到目前为止,我一直gulp.src(filePath)
努力实现这一目标.根据这篇博客文章,它应该工作:
gulp.src(files)是包含文件/文件路径的字符串或数组.
我目前的代码:
gulp.task("createFileIndex", function(){
var index = gulp.src(['./content/**/*.*']);
console.log("INDEX:", index[0]);
});
Run Code Online (Sandbox Code Playgroud)
通过输出gulp.src()
带有index[0]
I get 的返回值,undefined
整个index
输出只有一个没有任何文件路径的大型字典.
小智 9
目前的解决办法是:
var gulp = require('gulp');
var debug = require('gulp-debug');
gulp.src(sources)
.pipe(debug());
Run Code Online (Sandbox Code Playgroud)
正如OP在评论中所述,解决这个问题的一个简单方法是使用fs.readdirSync
而不是gulp.src
:
fs = require("fs");
fs.readdirSync(directoryPath); // ["file1", "file2"]
Run Code Online (Sandbox Code Playgroud)
小智 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);
});
});
Run Code Online (Sandbox Code Playgroud)
如果您想要的只是 glob 中的文件名数组(如 gulp.src),请使用:
const glob = require('glob');
const fileArray = glob.sync('./content/**/*.*');
Run Code Online (Sandbox Code Playgroud)
根据gulp.src上的 gulp 文档(https://github.com/gulpjs/gulp/blob/master/docs/API.md#gulpsrcglobs-options)
gulp.src(globs[, 选项])
发出与提供的 glob 或 glob 数组匹配的文件。返回可以通过管道传输到插件的 Vinyl 文件流。
Run Code Online (Sandbox Code Playgroud)gulp.src('client/templates/*.jade') .pipe(jade()) .pipe(minify()) .pipe(gulp.dest('build/minified_templates'));
glob 指的是节点-glob 语法,也可以是直接文件路径。
球体
类型:字符串或数组
要读取的 glob 或 glob 数组。
选项
类型:对象
通过 glob-stream 传递到 node-glob 的选项。
除了node-glob和glob-stream支持的选项之外,gulp还添加了一些额外的选项
所以看来你需要进一步研究这一点。否则这可能会有所帮助Get the current file name in gulp.src()
归档时间: |
|
查看次数: |
24817 次 |
最近记录: |