Search code examples
gulptranslation

Gulpfile end of task with two different operations


I am kind of lacking imagination on that one. My goal is to retrieve a json object so I can run a replace string on all the files I want to translate, I have looked into a lot of translation libraries but this way is the best i can think of for my use. Anyway my issue here is Once I got my json object, I have to run on all the files and when it is done, finish the task 'trad'. I have done some research and tried a lot of things but there is something that I miss, something that I didn't understood about the good way to do that ? Please help !

    gulp.task('trad', gulp.series( 'createTradFile', 'copyBeforeTrad',  function( done ) {
    var data = require('gulp-data');
    var path = require('path');
    var fs = require('fs');
    var replace2 = require('gulp-string-replace');
    var transObj = null;

    var translateAll = function()
    {
        var files = gulp.src(['fr/**/*.html', 'fr/**/*.js']);

        for (var k in transObj)
        {
            if (transObj[k].ID)
            {
             console.log("TRAD " + transObj[k].ID + " TO " + transObj[k].LANG1);

                files.pipe(replace2(new RegExp('\\+' + transObj[k].ID + '\\+', 'g'),
                                    transObj[k].LANG1,
                                    {'logs': {'enabled': true}}))
                .pipe(chmod(755));
            }
        }
        files.pipe(gulp.dest("fr"))
             .on('end', done);
    };

    gulp.src('distTemp/wording.json')
                .pipe(data(function(file) {
                    transObj = JSON.parse( fs.readFileSync('distTemp/' + path.basename(file.path)));
                    console.log("TRAD first part OK");
                    translateAll();
                 })); 
   }));

So this code will translate like I want it too, but the task does not end :

[16:38:34] The following tasks did not complete: trad, <anonymous>
[16:38:34] Did you forget to signal async completion?

Solution

  • So, after a bit of research I found this ( almost crappy ) solution, which do the trick ( please answer if you hava a better solution )

    var transObj = null;
    
    gulp.task("retrieveTradObject", function(){
    var data = require('gulp-data');
    var path = require('path');
    var fs = require('fs');
    
    return gulp.src('distTemp/wording.json')
                .pipe(data(function(file) {
                    transObj = JSON.parse( fs.readFileSync('distTemp/' + path.basename(file.path)));
                    console.log("TRAD first part OK");
                 }));
    });
    
    gulp.task('trad', gulp.series( 'createTradFile', 'copyBeforeTrad', 'retrieveTradObject',  function( done ) {
    
    var replace2 = require('gulp-string-replace');
    
    var files = gulp.src(['fr/**/*.html', 'fr/**/*.js']);
    
    for (var k in transObj)
    {
        if (transObj[k].ID)
        {
         console.log("TRAD " + transObj[k].ID + " TO " + transObj[k].LANG1);
    
            files = files.pipe(replace2(new RegExp('\\+' + transObj[k].ID + '\\+', 'g'),
                                transObj[k].LANG1,
                                {'logs': {'enabled': true}}))
            .pipe(chmod(755));
        }
    }
    files.pipe(gulp.dest("fr"));
    return files;
    }));
    

    So main idea here was to separate the two promises into task ( mainly for a better understanding of the code for later ) and then to do the files = files.pipe( ... ) Which is explained here : How to create repeating pipe in gulp?

    Hope this can help !