简体   繁体   English

gulp循环同步运行任务

[英]gulp loop running task synchronously

Now I have a problem below is my code 现在我有一个问题,下面是我的代码

//code //config.json //代码//config.json

[
    {
        "name": "1"
    },
    {
         "name": "2"
    },
    {
         "name": "3"
    }
]

//gulp //一饮而尽

var gulp = require('gulp');
var fs = require('fs');
var gulpsync = require('gulp-sync')(gulp);

var config = JSON.parse(fs.readFileSync(paths.config, 'utf-8'));
var tasks = [];

warehouseConfig.forEach(function(item) {
    var task = item.name;
    tasks.push(task);
    template.dist = task;
    gulp.task('copy:' + task, function() {
        template.dist = task;
        console.log('start copy ' + task + 'dist:' + template.dist);
        gulp.start('copy');
    });
    gulp.task(task, ['copy:' + task], function() {
        return gulp.src(task)
            .pipe(gulp.dest(task + '/'));
    });
});

gulp.task('copy', ['copy:www', 'copy:plugins', 'copy:platforms']);

gulp.task( 'build:pre', gulpsync.sync(tasks));

gulp.task( 'build', ['clean'], function() {
    gulp.start('build:pre');
});

Now,I have the problem, the same task "copy" just run once,I want it run three times. 现在,我遇到了问题,同一任务“复制”只运行一次,我希望它运行三次。 Do you have any good ideas? 你有什么好主意吗?

if you just want to run that gulp task run thrice you can actually run it in child process : 如果您只想运行三次gulp任务,则可以实际在子进程中运行它:

const exec = require('child_process').exec;
//replace gulp.start('copy') with 
exec('gulp copy');

this will make it async you can make it sync by replacing it with : child_process.execSync 这将使其异步,您可以通过将其替换为child_process.execSync使其同步

here's link : https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options 这是链接: https : //nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options

hope this works as gulp.start() is depreciated: https://github.com/gulpjs/gulp/issues/505 希望这能随着gulp.start()的使用而贬值: https : //github.com/gulpjs/gulp/issues/505

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

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