简体   繁体   English

将管道文件放入具有特定目录的zip中

[英]Gulp pipe files into zip with specific directory

I have two file streams 我有两个文件流

var chromefiles = gulp.src(['./src/chrome/*', './external/jquery.js',      './icon/*']);

var userscriptfiles = gulp.src(['./src/meta.js', './src/jquery.extensions.js', './src/*.js'])
.pipe(concat('user.js'));

which I merge into a zip file 我合并成一个zip文件

return merge(chromefiles, userscriptfiles)
.pipe(zip('overpress.zip'))
.pipe(gulp.dest('./'));

I would like to put all files from "chromefiles" into a subdirectory called "chrome" and the file from "userscriptfiles" into a directory called "userscript" inside the zip file. 我想将所有来自“ chromefiles”的文件放入一个名为“ chrome”的子目录,并将来自“ userscriptfiles”的文件放入该zip文件中的一个名为“ userscript”的目录。 How can I achieve this? 我该如何实现?

overpress.zip
   |
   |-userscript
   |     |-user.js
   |
   |-chrome
        |-jquery.js
        |-icon.png
        |-someotherfile.js     

Use gulp-rename on the chromefiles and userscriptfiles streams to prepend the respective directory to each file path: chromefilesuserscriptfiles流上使用chromefiles gulp-rename将相应的目录userscriptfiles每个文件路径的前面:

var rename = require('gulp-rename');

gulp.task('zip', function() {
  var chromefiles = gulp.src([
      './src/chrome/*',
      './external/jquery.js',
      './icon/*'
    ])
    .pipe(rename(function(file) {
      file.dirname = 'chrome/' + file.dirname;
    }));

  var userscriptfiles = gulp.src([
      './src/meta.js', 
      './src/jquery.extensions.js', 
      './src/*.js'
    ])
    .pipe(concat('user.js'))
    .pipe(rename(function(file) {
      file.dirname = 'userscript/' + file.dirname;
    }));

  return merge(chromefiles, userscriptfiles)
    .pipe(zip('overpress.zip'))
    .pipe(gulp.dest('./'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM