Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在gulp.rename()调用中使用先前gulp.src中的文件名_Javascript_Gulp_Pipe_Gulp Rename - Fatal编程技术网

Javascript 在gulp.rename()调用中使用先前gulp.src中的文件名

Javascript 在gulp.rename()调用中使用先前gulp.src中的文件名,javascript,gulp,pipe,gulp-rename,Javascript,Gulp,Pipe,Gulp Rename,我在文件夹中有一个zip文件,我想提取其中一个(压缩)文件,并在文件夹/sourcefiles\u unpacket/ ./sourcefiles/test.zip => 解压缩和过滤与gulp unzip配合得很好,但是我不确定如何从gulp.src调用访问文件名 gulp.task('unzip-filtered-rename', function() { return gulp.src(paths.unzip_sourcefiles) // .pipe(debug())

我在文件夹中有一个zip文件,我想提取其中一个(压缩)文件,并在文件夹
/sourcefiles\u unpacket/

./sourcefiles/test.zip
=>

解压缩和过滤与gulp unzip配合得很好,但是我不确定如何从gulp.src调用访问文件名

gulp.task('unzip-filtered-rename', function() {
  return gulp.src(paths.unzip_sourcefiles)
    // .pipe(debug())
    .pipe(plumber({
      errorHandler: notify.onError('unzip-filtered-rename error: <%= error.message %>')
    }))
    .pipe(changed(paths.excel_targetdir_local_glob, {
      extension: '.xml'
    }))
    .pipe(unzip({filter : function(entry){return minimatch(entry.path, "contents.xml")}}))
    .pipe(gulp.rename(function(path){       

        // ? What do I put here to rename each target file to 
        // ?   its originating zip file's basename?


    })) //   "==> test.xml",
    .pipe(gulp.dest(paths.sourcefiles_unpacked)) //   sourcefiles_unpacked: "./sourcefiles_unpacked/"
});
gulp.task('unzip-filtered-rename',function(){
return gulp.src(path.unzip_sourcefiles)
//.pipe(debug())
.水管工({
errorHandler:notify.onError('unzip-filtered-rename错误:')
}))
.pipe(已更改(paths.excel\u targetdir\u local\u glob、{
扩展名:'.xml'
}))
.pipe(解压({filter:function(entry){return minimatch(entry.path,“contents.xml”)}))
.pipe(gulp.rename(函数(路径){
//?我在这里放什么来重命名每个目标文件
//?其原始zip文件的基本名称?
}))/“==>test.xml”,
.pipe(gulp.dest(path.sourcefiles\u unpacted))//sourcefiles\u unpacted:“./sourcefiles\u unpacted/”
});
一旦调用gulp.rename(),块就被重命名为zipfile中的名称

  • 如何访问或存储早期管道的文件路径,以便在重命名函数调用中使用
  • 如果路径包含glob“/sourcefiles\u unpacket/”,那么gulp.dest是否正确配置
试试这个:

const glob = require("glob");
const zipFiles = glob.sync('sourcefiles/*.zip');

gulp.task('unzip-filtered-rename', function (done) {

  zipFiles.forEach(function (zipFile) {

    const zipFileBase = path.parse(zipFile).name;

    return gulp.src(zipFile)
        // .pipe(debug())
        //   .pipe(plumber({
        //     errorHandler: notify.onError('unzip-filtered-rename error: <%= error.message %>')
        //   }))
        //   .pipe(changed(paths.excel_targetdir_local_glob, {
        //     extension: '.xml'
        //   }))
        //   .pipe(unzip({filter : function(entry){return minimatch(entry.path, "contents.xml")}}))
        .pipe(rename({
            basename: zipFileBase,
        }))
        .pipe(gulp.dest("./sourcefiles_unpacked")) //   sourcefiles_unpacked: "./sourcefiles_unpacked/"
    });
    done();
});
const glob=require(“glob”);
const zipFiles=glob.sync('sourcefiles/*.zip');
gulp.task('unzip-filtered-rename',函数(完成){
forEach(函数(zipFile){
const zipFileBase=path.parse(zipFile).name;
回灌src(zipFile)
//.pipe(debug())
//.水管工({
//errorHandler:notify.onError('unzip-filtered-rename错误:')
//   }))
//.pipe(已更改(paths.excel\u targetdir\u local\u glob、{
//扩展名:'.xml'
//   }))
//.pipe(解压({filter:function(entry){return minimatch(entry.path,“contents.xml”)}))
.管道(重命名)({
basename:zipFileBase,
}))
.pipe(gulp.dest(“./sourcefiles\u unpacted”)///sourcefiles\u unpacted:“./sourcefiles\u unpacted/”
});
完成();
});
我注释掉了您仅为测试目的而做的其他事情。通过forEach或map运行每个文件,可以在流的开头设置一个变量,该变量将在下一个流中可用


有关设置流中使用的变量的更多讨论,请参见。

这很好。小跟进:我在哪里可以找到更多关于这个的信息,或者你在哪里学会了如何让吞咽像这样工作?不幸的是,标准文档并不能做到这一点,我很想学习。有两件事:因此,它本身和现在的gulp v4任务都可以设置为函数,而在javascript函数中可以执行的任何操作都可以在gulp任务函数中执行。它们比吞咽任务的方式(参见)更通用,但在我的回答中,在你进入流部分之前,你可以做任何你想要的javascripty内容。我可以看到它可以是非常通用的,但是使用through.obj处理异步仍然让我困惑(例如)。例如:在您的回答中,我希望将其调整为函数而不是任务(用于pipe())。不知道从哪里开始,我会写一些东西,然后发布一个新问题;谢谢你的支持。
const glob = require("glob");
const zipFiles = glob.sync('sourcefiles/*.zip');

gulp.task('unzip-filtered-rename', function (done) {

  zipFiles.forEach(function (zipFile) {

    const zipFileBase = path.parse(zipFile).name;

    return gulp.src(zipFile)
        // .pipe(debug())
        //   .pipe(plumber({
        //     errorHandler: notify.onError('unzip-filtered-rename error: <%= error.message %>')
        //   }))
        //   .pipe(changed(paths.excel_targetdir_local_glob, {
        //     extension: '.xml'
        //   }))
        //   .pipe(unzip({filter : function(entry){return minimatch(entry.path, "contents.xml")}}))
        .pipe(rename({
            basename: zipFileBase,
        }))
        .pipe(gulp.dest("./sourcefiles_unpacked")) //   sourcefiles_unpacked: "./sourcefiles_unpacked/"
    });
    done();
});