【发布时间】: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'));
});
【问题讨论】: