【问题标题】:Get JSON/String of through2 stream?获取 through2 流的 JSON/字符串?
【发布时间】:2015-11-30 16:50:31
【问题描述】:

我有一堆 zip 文件,每个文件都包含一个 .config 文件。

我想迭代每个 zip,解压缩,读取配置文件,然后使用该配置文件将 zip 文件上传到某处。

gulp.task('deploy-zips', function () {
  const filter = config()[target].filter;

  return gulp.src([destination + '/' + filter])
    .pipe(deployZips());
});

这是任务入口点。

function deployZips() {
  return through({ objectMode: true }, function (zipFile, zipEncoding, zipCallback) {
    gutil.log(zipFile.path.split('\\').reverse()[0]);

    gulp.src(zipFile.path)
      .pipe(unzip({
        filter : function(entry){

          if (entry.type !== 'File') {
            return false;
          }

          return entry.path.indexOf('deploy-config.json') > -1;
        }
      }))
      .pipe(through({ objectMode: true }, function(configFile, configEncoding, uploadCallback){
          gutil.log(configFile.path); // Outputs the file name as deploy-config.json

          //????
          var config = JSON.parse(configFile);

          uploadCallback(null, configFile);
      }))
      .on('end', function() {
          zipCallback(null, zipCallback);
      })
      ;
  });

}

这似乎显示了我要查看的所有 zip 文件,然后输出了我要查看的配置文件。但是此时我对如何解析配置文件以获取配置感到困惑。

我尝试解析配置文件,使用fs.readFileSync(...)读取文件

但似乎没有任何效果。配置包含要上传到的凭据,因此我需要阅读它,然后使用它将其发送到 S3 或任何为其配置的位置。

【问题讨论】:

    标签: node.js gulp through2


    【解决方案1】:

    configFile 更改为configFile.contents

    var config = JSON.parse(configFile.contents);

    https://github.com/gulpjs/vinyl#file

    function deployZips() {
      return through({ objectMode: true }, function (zipFile, zipEncoding, zipCallback) {
        gutil.log(zipFile.path.split('\\').reverse()[0]);
    
        gulp.src(zipFile.path)
          .pipe(unzip({
            filter : function(entry){
    
              if (entry.type !== 'File') {
                return false;
              }
    
              return entry.path.indexOf('deploy-config.json') > -1;
            }
          }))
          .pipe(through({ objectMode: true }, function(configFile, configEncoding, uploadCallback){
              gutil.log(configFile.path); // Outputs the file name as deploy-config.json
    
              var config = JSON.parse(configFile.contents);
    
              uploadCallback(null, configFile);
          }))
          .on('end', function() {
              zipCallback(null, zipCallback);
          })
          ;
      });
    
    }
    

    【讨论】:

    • 非常感谢@kilianc - 缺少的部分是.contents 现在一切正常:D
    猜你喜欢
    • 2019-06-04
    • 2013-04-27
    • 1970-01-01
    • 2015-04-08
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多