0

I am trying to run through a list of file names, move the file and zip them, then delete the file in gulp. I keep running into the issue that the next task is started before the previous task is finished.

function moveFiles(cb) {

folders.forEach(function(item,index){
    var _tmp = '../final_files/' + item;

      return Promise.all([
        new Promise(function(resolve, reject) {
           gulp.src('../' + item + '/**/*').pipe(gulp.dest(_tmp))
            .on('end', resolve)
        }),
        new Promise(function(resolve, reject) {
             gulp.src('../master/assets/**/*.*').pipe(gulp.dest(_tmp + '/assets/'))
            .on('end', resolve)
        }),
        new Promise(function(resolve, reject) {
             gulp.src([_tmp + '/**', _tmp + '/style/**', _tmp + '/js/**'], {base: "./"})
                .pipe(replace('../master/assets', 'assets'))
                .pipe(gulp.dest('.'))   
                .on('end', resolve)   
        }), 
        new Promise(function(resolve, reject) {
            return gulp.src(_tmp + '/**/*.*')
                .pipe(zip(item + '.zip'))
                .pipe(gulp.dest( '../final_files/'))
                .on('end', resolve) 
        }),         
      ]).then(function () {
         return gulp.src(_tmp, {read: false}).pipe(clean({force: true}));                   
      });       


});

}

I've tried to break it out into task, but I can't figure out how to pass a parameter to a task, or have the task wait for it to finish. Any direction would be greatly appreciated.

Regards

mconnors
  • 155
  • 2
  • 13
  • Have a look at https://stackoverflow.com/questions/52102143/is-array-foreach-asynchronous - maybe try a simple for loop instead. – Mark May 22 '20 at 02:50

1 Answers1

0

this is what I came up with:

function moveFiles(cb) {

    const files = folders.map( function (item) {

        let _tmp = '../final_files/' + item;

        let _mv = async () => {
            return promisifyStream( gulp.src('../' + item + '/**/*').pipe(gulp.dest(_tmp)) );
        };
        let _mv_assets = async () => {
            return promisifyStream( gulp.src('../master/assets/**/*.*').pipe(gulp.dest(_tmp + '/assets/')) );
        };
        let _replce = async () => {
            return promisifyStream( gulp.src([_tmp + '/**', _tmp + '/style/**', _tmp + '/js/**'], {base: "./"}).pipe(replace('../master/assets', 'assets')).pipe(gulp.dest('.')) );
        };          
        let _zip = async () => {
            return promisifyStream( gulp.src(_tmp + '/**/*.*').pipe(zip(item + '.zip')).pipe(gulp.dest( '../final_files/')) );
        };                  
        let _clean = async () => {
            return promisifyStream( gulp.src(_tmp, {read: false}).pipe(clean({force: true})) );
        };              
        let tsk = series(_mv, _mv_assets, _replce, _zip, _clean);

        tsk();

    });

    cb();
}


function promisifyStream(stream) {
    return new Promise( res => stream.on('end',res));
}

Although _clean doesnt finish

mconnors
  • 155
  • 2
  • 13