2

Well, I need to get all the values from an array and put them in a loop, however I can not pass the value to the property name of the object. Any idea how to do this?

var functionCases = [
    "functions",
    "document.ready",
    "window.load",
    "document.scroll",
    "window.resize"
];

functionCases.forEach(function(i){
    gulp.src(bermuda())
    .pipe(gfi({
        "/* "+ i + " */": "generator/assets/js/" + i + ".js"
    }))
    .pipe(gulp.dest('generator/assets/js'));
});

The problem is here: "/* "+ i + " */", Javascript does not recognize this as the name of an object property...

Here is my entire Gulp code:

var gulp = require('gulp');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var concat = require('gulp-concat');
var gfi = require("gulp-file-insert");
var fs = require('fs');

gulp.task('modules', function() {

    var bermuda = function(){
        var fileExist = fs.existsSync("generator/assets/js/modules.js");
        if ( fileExist == true ) {
            var source = "generator/assets/js/modules.js";
            console.log("existe");
        } else{
            var source = "js/modules/modules.js";
            console.log("não existe");
        }
        return source;
    }

    var functionCases = [
        "functions",
        "document.ready",
        "window.load",
        "document.scroll",
        "window.resize"
    ];

    functionCases.forEach(function(i){

        console.log(i);

        gulp.src('js/modules/**/' + i + '.js')
            .pipe(concat('' + i + '.js'))
            .pipe(gulp.dest('generator/assets/js'))
            .on('end',function(){
                fs.exists("generator/assets/js/" + i + ".js", function(exists) {
                    if (exists) {
                        var gfiInput = {};
                        gfiInput["/* "+ i + " */"] = "generator/assets/js/" + i + ".js";
                        gulp.src(bermuda())
                            .pipe(gfi(gfiInput))
                            .pipe(gulp.dest('generator/assets/js'));
                    }
                });
            });

    });
});
Caio Kawasaki
  • 2,894
  • 6
  • 33
  • 69

1 Answers1

2

Try replacing

functionCases.forEach(function(i){
    gulp.src(bermuda())
    .pipe(gfi({
        "/* "+ i + " */": "generator/assets/js/" + i + ".js"
    }))
    .pipe(gulp.dest('generator/assets/js'));
});

with

functionCases.forEach(function(i){
    var gfiInput = {};
    gfiInput["/* "+ i + " */"] = "generator/assets/js/" + i + ".js";
    gulp.src(bermuda())
        .pipe(gfi(gfiInput))
        .pipe(gulp.dest('generator/assets/js'));
});
howderek
  • 2,224
  • 14
  • 23
  • works, however now I have another problem, gulp does not respect the order of each loop item, I did the loop just thinking that would solve this problem ... – Caio Kawasaki May 19 '15 at 17:28
  • @CaioKawasaki you could try asking another question so people with more experience with gulp can help you – howderek May 19 '15 at 17:30