web-dev-qa-db-ja.com

node.jsストリームでこの<File>オブジェクトを操作するにはどうすればよいですか?

私はgulpを使用して、globが一致するファイルのストリームを作成し、それらをすべて入れ子構造で新しい場所に移動します。これを行うために、最初に単純な「スルー」ストリームを作成して、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]

それらのオブジェクトは何ですか?どうすればそれらと対話できますか?

19
rhodesjason

それらは ビニル オブジェクトです。これらは、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 を使用すると、ファイルの内容を簡単に変更できます
  • gulp-filter は、グロビングが機能しない場合にストリームをフィルタリングするのに役立ちます。
26
OverZealous

次の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());
10
user1491819

これにも偶然出会い、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);
            }
          });
0
Sohail