2016-09-12 6 views
1

私は興味深い質問が1つあります。開いているストリーム(EventStream)内のいくつかのファイルをgulp内で変更することは可能ですか?他のgulpのEventStreamの中の他のファイルを変更してください

私はそれを持っています。開いているストリームの中のあるファイルを読み込み、そのファイルを他のファイルに書きたい。私はそれをどのようにすることができますか?ありがとう。

gulp.task('handleGlobalStorage', function() { 
    return gulp.src('./global_storage.js') 
    .pipe(setEnvHostAndProxy()) 
    .pipe(gulp.dest('./built')); 
}); 

function setEnvHostAndProxy() { 
    return es.map(function(file, cb) { 
    var fileContent = file.contents.toString(); 
    //some changes inside content 
    // if (!gutil.env.dev) return; 
    /* I have stuff that fetched from file and I modify it that I send it 
     down to pipe. But also I want to insert this stuff inside other 
     file. How I can do it? Should I create WritableStream of that file  
     and merge it ?*/ 
    file.contents = new Buffer(fileContent); 
    // send the updated file down the pipe 
    cb(null, file); 
    }); 
} 
+0

もっと具体的にする必要があります。それ以外のファイルは既に存在するのですか、まったく新しいファイルを作成したいだけですか? '。/ built'フォルダに他のファイルを書きたいのですか? –

+0

回答ありがとうSven。実際にファイルが存在し、インポートするだけでファイルを変更したいだけですが、メインストリームをインポートする必要があります。 – Velidan

答えて

0

私はこの問題を解決しました。ここでは解決策です(すべてのコードは表示されず、一般的なものです)。主なコンセプトは - ファイルを開いてパイプの中に新しいものを書くことです。それだけです。

function setEnvHostAndProxy() { 
     var bsConfigFileContent = fs.readFileSync('./bs-config.js', 'utf8'), 
      bsConfigProxyPattern = /(proxyUrl\s?=\s?)["|'](.*)["|']/; 

    return es.map(function (file, cb) { 
     var fileContent = file.contents.toString(), 
      prodHost = fileContent.match(generateRegExpForHosts('prodHost'))[1], 
      prodProxy = fileContent.match(generateRegExpForHosts('prodProxy'))[1], 
      devProxy = fileContent.match(generateRegExpForHosts('devProxy'))[1], 
      devHost = fileContent.match(generateRegExpForHosts('devHost'))[1], 
      changedBsConfigFileStream, 
      res; 

     if (!gutil.env.dev) { 
      res = prodHandler(); 
      changedBsConfigFileStream = bsConfigHandler(prodHost); 
     } else { 
      res = devHandler(); 
      changedBsConfigFileStream = bsConfigHandler(devProxy); 
     } 

     fs.writeFile('./bs-config.js', changedBsConfigFileStream, 'utf8', function (err) { 
      if (err) throw (err); 
     }); 

     file.contents = new Buffer(res); 

     cb(null, file); 
} }); 
関連する問題