【问题标题】:creating tar archives using gulp使用 gulp 创建 tar 档案
【发布时间】:2015-04-13 21:18:47
【问题描述】:

我正在使用 gulp-tar 创建一个 tar 文件...如何添加顶级文件夹,以便当用户运行 tar -xzf myArchive.tar 时,它会提取到特定文件夹中。

这是我的代码:

gulp.task('prod', ['min', 'gittag'], function() {

  //copy all files under /server into a zip file
    gulp.src('../server/**/*')
    .pipe(tar('xoserver' + '-'+ gittag +'.tar'))
    .pipe(gzip())
    .pipe(gulp.dest('../prod'));
});

上面创建了一个tar.zip 文件,但我必须小心在提取时添加-C <folder>,否则文件会被提取到当前文件夹。

[编辑]

我在这里要做的是生成格式为xoserver-alpha-d414ddf.tar.gz 的压缩包,当使用tar xvf 提取该压缩包时,将创建一个文件夹xoserver-alpha-d414ddf 并解压缩该文件夹下的所有文件。本质上,我正在尝试在打包文件上方添加新文件夹名称。 如果我添加base 选项,解压到的文件夹就是server

[回答]

感谢 ddprrt 的好回答。我正在复制最终代码,以防其他人想要使用类似的策略将 git 标签嵌入到 tarball 的名称中以进行分发/测试。

gulp.task('gittag', function(cb) {   // generate the git tag 
    git.exec({args : 'branch -v'}, function (err, stdout) {
      var lines = stdout.split('\n');
      for (var l in lines) {
        if (lines[l][0] == '*') {
          var words = lines[l].split(/\s+/);
          gittag = words[1]+ '-' + words[2];
          console.log('Gittag is %s', gittag);
          break;
        }
      }
      cb();
    });
});

gulp.task('min', ['runbmin', 'template', 'vendor']); // generate min files

gulp.task('prod', ['min', 'gittag'], function() { // create tarball
  //copy all files under /server into a zip file
    return gulp.src('../server/**/*')
    .pipe(rename(function(path) {
        path.dirname = 'server-' + gittag + '/' + path.dirname;
    }))
    .pipe(tar('xoserver-'+gittag+'.tar'))
    .pipe(gzip())
    .pipe(gulp.dest('../prod'));
});

【问题讨论】:

    标签: gulp tar


    【解决方案1】:

    这就是base 选项的用途。

    gulp.task('prod', ['min', 'gittag'], function() {
        return gulp.src('../server/**/*', { base: '../server/' })
            .pipe(tar('xoserver' + '-'+ gittag +'.tar'))
            .pipe(gzip())
            .pipe(gulp.dest('../prod'));
    });
    

    有了它,您可以告诉 gulp 在处理您收到的 glob 时要包含哪些路径。

    顺便说一句。不要忘记在任务中返回流或调用 done 回调。帮助 gulp 协调您的构建管道

    至于第二个问题,你可以使用gulp-rename任务来改变你的虚拟文件所在的目录。会是这样的

    .pipe(rename(function(path) {
         path.dirname = 'whatever/' + path.dirname
    }));
    

    【讨论】:

    • 谢谢.. 不过,这只能解决部分问题。我已经编辑了这个问题以澄清我想要做什么。
    • 好吧!查看我的更新...我想这可能会有所帮助:-)
    • 谢谢!奇迹般有效。我正在用我正在使用的最终 gulp 代码更新我的问题......以防万一有人感兴趣
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多