如何与node.js流中的<file>对象进行交互?</file>

时间:2014-03-09 19:21:19

标签: node.js stream gulp

我正在使用gulp构建一个全局匹配文件流,并将它们的嵌套结构全部移动到一个新位置。要做到这一点,我首先想要构建一个简单的“直通”流,看看如果我从gulp.src()管道传递给我的东西。

这是我的测试gulpfile.js:

var through = require("through");
var fs = require("fs");

function write(file) {
  console.log(file);
  console.log(file.toString());
}

gulp.task("move", function () {

  return gulp.src("./**")
    .pipe(through(write));

});

如果我在命令行上运行gulp'move'任务,我会得到如下输出:

<File "some/path">
[object Object]
<File "some/path/file.js" <Buffer 2f 2a 0a 0a 4f 72 67 69 6e 61 6c 20 53 74 79 6c 65 20 66 72 6f 6d 20 65 74 68 61 6e 73 63 68 6f 6f 6e 6f 76 65 72 2e 63 6f 6d 2f 73 6f 6c 61 72 69 7a 65 ...>>
[object Object]

那些物品是什么?我该如何与他们互动?

3 个答案:

答案 0 :(得分:26)

这些是vinyl个对象。它们是通过gulp流传递的核心数据类型。包含有关文件的信息(例如路径信息和内容作为缓冲区或流)。您可以使用gulp-debug更好地查看数据。

如果你想移动一堆文件,同时保留它们的相对路径,你可以做以下其中一种,不需要自己深入研究代码:

gulp.src('/a/single/src/path/**/*.foo').pipe(gulp.dest('/a/single/dest/path'));

或者,如果你有一堆不同的球:

gulp.src(['/a/src/path/foo/**/*.foo', '/a/src/path/bar/**/*.bar'], {base: '/a/src/path/'})
    .pipe(gulp.dest('/a/dest/path/'));

大多数情况下,您将使用gulp plugins来操作文件,然后将结果传递给gulp.dest(),而不是自己操纵它们。

如果需要来操作文件,可以使用一些插件来帮助:

  • gulp-tap允许您进入流峰值,并可选择修改文件或缓冲区。
  • vinyl-map可让您轻松修改文件内容
  • 如果globbing不起作用,
  • gulp-filter可以帮助您过滤流。

答案 1 :(得分:10)

您可以使用此js:

查看文件属性
var propValue;
for(var propName in file) {
    propValue = file[propName];
    console.log('name:' + propName, ', value:<<<',propValue,'>>>');
}

Sample Output
    name:history ,     value:"C:\Temp\test.txt"
    name:cwd ,         value:"C:\Temp"
    name:base ,        value:"C:\Temp"
    name:_contents ,   value: full file contents
    name:isBuffer ,    value:"function () {
    name:isStream ,    value:"function () {
    name:isNull ,      value:"function () {
    name:isDirectory , value:"function () {
    name:clone ,       value:"function (opt) {
    name:pipe ,        value:"function (stream, opt) {
    name:inspect ,     value:"function () {
    name:stat , value:<<< { dev: 0,
      mode: 33206,
      nlink: 1,
      uid: 0,
      gid: 0,
      rdev: 0,
      ino: 0,
      size: 874,
      atime: Sat Sep 19 2015 14:34:51 GMT+1000 (AUS Eastern Standard Time),
      mtime: Sat Sep 19 2015 14:34:51 GMT+1000 (AUS Eastern Standard Time),
      ctime: Sat Sep 12 2015 14:59:40 GMT+1000 (AUS Eastern Standard Time) } >>>

Usage:
console.log('file name:', file.relative);
console.log('file current working directory:', file.cwd);
console.log('file isDirectory:', file.isDirectory());

答案 2 :(得分:0)

对于那些也偶然发现这个问题并且不想使用gulp的人,这是我的做法:

假设filesvinyl个对象的数组-

        const outputPath = ... // some directory path

        files.forEach(file => {
            const filePath = path.join(outputPath, file.relative);
            // if its a directory then create the directory if not already present
            if (file.isDirectory()) {
              if (!fs.existsSync(filePath)) {
                fs.mkdirSync(filePath, { recursive: true });
              }
            } else {
              // if its a file then save the contents of the file
              fs.writeFileSync(filePath, file.contents);
            }
          });