Gulp 如何从对象一次完成多个动态流任务

ykejflvf  于 2022-12-16  发布在  Gulp
关注(0)|答案(1)|浏览(174)

我正在尝试基于json对象动态复制gulp文件中的文件。我尝试使用merge-stream包但没有成功。

const DEV_PATH = "./dev/";
const DIST_PATH = "./dist/";
const DEV_PATH_TOOLS = DEV_PATH + "tools/";
const DEV_PATH_COMMON = DEV_PATH + "common/";
const DIST_PATH_TOOLS = DIST_PATH + "tools/";
const TOOLS_DIST_PATH = [
    {
        "dir" : 'dir1',
        "options"  : {
            "class" : [ "modules/csv/**/*" ]
        }
    },
    {
        "dir" : 'dir2',
        "options"  : {
            "class" : [ "modules/csv/CsvImporter.php" ]
        }
    }
]; 

const gulp = require('gulp');
const plumber = require('gulp-plumber');
const merged = require('merge-stream')(); 

gulp.task( 'copy-class', function(){
    TOOLS_DIST_PATH.map( t => function () {
        var stream = new stream(
            gulp.src( t.options.class.map( c =>  DEV_PATH_COMMON + c ))
                .pipe(plumber({
                    handleError: function (err) {
                        console.log(err);
                        this.emit('end');
                    }
                }))
                .pipe( gulp.dest( DIST_PATH_TOOLS + t.dir + '/class' ) )
                .pipe(plumber({
                    handleError: function (err) {
                        console.log(err);
                        this.emit('end');
                    }
                }))
        );
        merged.add(stream);
    });
    return merged.isEmpty() ? null : merged;
});

gulp.task( 'default', gulp.series( 'copy-class' ) );

我得到这个错误:
下列任务未完成:缺省,复制类
你忘了通知异步完成了吗?
我很困惑,有人能帮我吗?

ldioqlga

ldioqlga1#

我找到一个answer

Const DEV_PATH = "./dev/";
const DIST_PATH = "./dist/";
const DEV_PATH_TOOLS = DEV_PATH + "tools/";
const DEV_PATH_COMMON = DEV_PATH + "common/";
const DIST_PATH_TOOLS = DIST_PATH + "tools/";
const TOOLS_DIST_PATH = [
    {
        "dir" : 'dir1',
        "options"  : {
            "class" : [ "modules/csv/**/*" ]
        }
    },
    {
        "dir" : 'dir2',
        "options"  : {
            "class" : [ "modules/csv/CsvImporter.php" ]
        }
    }
]; 

const gulp = require('gulp');
const plumber = require('gulp-plumber');
const merge= require('merge-stream'); 

function compileScripts(t) {
    const classStream = gulp.src( t.options.class.map( c =>  DEV_PATH_COMMON + c ))
        .pipe(plumber({
            handleError: function (err) {
                console.log(err);
                this.emit('end');
            }
        }))
        .pipe( gulp.dest( DIST_PATH_TOOLS + t.dir + '/class' ) )
        .pipe(plumber({
            handleError: function (err) {
                console.log(err);
                this.emit('end');
            }
        }))
    /* TODO : when there wil be several options
    return merge(classStream , otherStream );*/

    return classStream;
}

gulp.task( 'copy-options', function(){
    var streams = TOOLS_DIST_PATH.map( t => {
        return compileScripts(t);
    });
    return merge(streams);
});

gulp.task( 'default', gulp.series( 'copy-options' ) );

相关问题