Javascript Gulp:调用一个异步函数,该函数在转换函数中提供自己的回调

Javascript Gulp:调用一个异步函数,该函数在转换函数中提供自己的回调,javascript,node.js,asynchronous,callback,gulp,Javascript,Node.js,Asynchronous,Callback,Gulp,我想在Gulp中创建一个用于pipe()调用的函数,该函数将支持xlsx文件到json的转换 我曾为Gulp3使用NPM包“excel as json”,但Gulp4迫使我真正理解它在做什么;-) 六个小时后,由于缺乏js/async/streaming方面的知识,我无法实现这一点,这打击了我的好奇心 代码如下: paths = {excel_sourcefiles: "./sourcefiles/*.xls*", excel_targetdir_local_csvjson: "./target

我想在Gulp中创建一个用于pipe()调用的函数,该函数将支持xlsx文件到json的转换

我曾为Gulp3使用NPM包“excel as json”,但Gulp4迫使我真正理解它在做什么;-)

六个小时后,由于缺乏js/async/streaming方面的知识,我无法实现这一点,这打击了我的好奇心

代码如下:

paths = {excel_sourcefiles: "./sourcefiles/*.xls*", excel_targetdir_local_csvjson: "./targetfiles_local/*.*"}


var replaceExt = require('replace-ext');
var PluginError = require('plugin-error')
var gulpFunction = require('gulp-function').gulpFunction // default ES6 export
var through = require("through2")
var convertExcel = require('excel-as-json').processFile;
var changed = require('gulp-changed');
var assign = Object.assign || require('object.assign');
var notify = require('gulp-notify');
var gulpIgnore = require('gulp-ignore');
var rename = require('gulp-rename');

gulp.task('excel-to-jsoncsv', function() {
  return gulp.src(paths.excel_sourcefiles)
    // .pipe(debug())
    .pipe(gulpIgnore.exclude("*\~*")) // Ignore temporary files  by Excel while xlsx is  open
    .pipe(gulpIgnore.exclude("*\$*")) // Ignore temporary files  by Excel while xlsx is  open
    .pipe(plumber({errorHandler: notify.onError('gulp-excel-to-jsoncsv error: <%= error.message %>')}))
    .pipe(changed(paths.excel_targetdir_local_glob, { extension: '.csv' }))
    .pipe(GulpConvertExcelToJson()) // This is where the magic should happen
    .pipe(rename({ extname: '.csv' })) // Saving as .csv for SharePoint (does not allow .json files to be saved)
    .pipe(gulp.dest(paths.excel_targetdir_local))
});

function GulpConvertExcelToJson() {
  return through.obj(function(chunk, enc, callback) {
    var self = this
    if (chunk.isNull() || chunk.isDirectory()) {
      callback() // Do not process directories
      // return
    }

    if (chunk.isStream()) {
      callback() // Do not process streams
      // return
    }

    if (chunk.isBuffer()) {
      convertExcel(chunk.path, null, null, // Converts file found at `chunk.path` and returns (err, `data`) its callback.
        function(err, data) {
          if (err) {
            callback(new PluginError("Excel-as-json", err))
          }
          chunk.contents = new Buffer(JSON.stringify(data))
          self.push(chunk)
          callback()
          // return
        })
    } else {
      callback()
    }
  })
}
path={excel\u sourcefiles:“./sourcefiles/*.xls*”,excel\u targetdir\u local\u csvjson:“./targetfiles\u local/*.*”}
var replaceExt=require('replace-ext');
var PluginError=require('plugin-error')
var gulpFunction=require('gulp-function')。gulpFunction//默认ES6导出
var-through=需要(“through2”)
var convertExcel=require('excel-as-json').processFile;
var变更=要求('gulp-changed');
var assign=Object.assign | | require('Object.assign');
var notify=需要('gulp-notify');
var gulpIgnore=require('gulp-ignore');
var rename=require('gulp-rename');
gulp.task('excel-to-jsoncsv',function(){
返回gulp.src(path.excel\u源文件)
//.pipe(debug())
.pipe(gulpIgnore.exclude(“*\~*”)//在xlsx打开时通过Excel忽略临时文件
.pipe(gulpIgnore.exclude(“*\$*”)//在xlsx打开时通过Excel忽略临时文件
.pipe(水管工({errorHandler:notify.onError('gulp-excel-to-jsoncsv error:'))
.pipe(已更改(paths.excel_targetdir_local_glob,{扩展名:'.csv'}))
.pipe(GulpConvertExcelToJson())//这就是神奇的地方
.pipe(重命名({extname:'.csv'}))//另存为SharePoint的.csv(不允许保存.json文件)
.pipe(gulp.dest(path.excel\u targetdir\u local))
});
函数GulpConvertExcelToJson(){
通过.obj(函数(块、enc、回调)返回{
var self=这个
if(chunk.isNull()| | chunk.isDirectory()){
callback()//不处理目录
//返回
}
if(chunk.isStream()){
callback()//不处理流
//返回
}
if(chunk.isBuffer()){
convertExcel(chunk.path,null,null,//转换在'chunk.path'找到的文件并返回其回调(err,'data`)。
功能(错误、数据){
如果(错误){
回调(新插件错误(“Excel作为json”,错误))
}
chunk.contents=新缓冲区(JSON.stringify(数据))
self.push(块)
回调函数()
//返回
})
}否则{
回调函数()
}
})
}
我(现在)知道还有其他excel>json gulp模块可以让我在不编写自己的模块的情况下解决这个问题,但我想了解我应该在这里做些什么

返回的错误是“您忘记发出异步完成信号了吗?”,我试图不这样做。然而,可能我试图用
var self=this
修复错误“this.push不是函数”并不是我应该做的

看着像gulp zip这样的例子,让我认识到了不熟悉的符号,使我无法自动摆脱这种情况

总之:

  • 在through.obj函数调用期间,如果块的内容被更新为(不在我的控制下)只向我提供回调(err,data)的异步函数,我将如何调用异步函数

这个问题来自
excel

失败的尝试 为了安装修改后的excel.js,我执行了以下操作:

rm -rf node_modules/excel
git clone https://github.com/bdbosman/excel.js node_modules/excel
cd node_modules/excel && npm i && cd ../..
这是不够的,因为
excelsas-json
使用了旧版本的
excel

要使用
excel@1.0.0
(合并拉取请求后)或手动编辑节点模块中的文件。我将在这里描述第二种方式

临时解决办法 安装模块后编辑
excel
文件

我用过:

sed -i "s/'end'/'finish'/g" node_modules/excel/excelParser.js
然后我跑:

$ gulp excel-to-jsoncsv
[19:48:21] Using gulpfile ~/so-question-gulp-async-function-call-xlsx/gulpfile.js
[19:48:21] Starting 'excel-to-jsoncsv'...
[19:48:21] Finished 'excel-to-jsoncsv' after 126 ms

而且它显然起了作用。

我认为如果你能用它创建回购协议并与我们分享,那会有所帮助。这将使调试变得更简单。有意义:请查看excel是否仍在使用json?它是为npm2编写的。当我运行它时,它实际上什么都没做。convertExcel('cols.xlsx','rows.csv',{},(err,data)=>{console.log(data)})@MattV就像前面提到的Aniket一样,
Excelas json
必须更新,但我给出了一个解决办法。如果你想一起做的话,给我发一个下午。