Romain
Romain

Reputation: 3673

JSON combining gulp task that keep the folder structure - cross platform

While I found many gulp plugins that combine the json, they always discard the folder structure to merge the data. My use would be with this kind of structure:

game-data/
    |____ cards/
    |       |__ card1.json
    |       |__ card2.json
    |       |__ ...
    |____ infos/
            |___ en
            |     |__ properties1.json
            |     |__ properties2.json
            |___ fr
            |     |__ properties1.json
            |     |__ properties2.json
            |___ es
                  |__ properties1.json
                  |__ properties2.json

The output I get with every plugin I found would get each card overriden by the next one and each properties by the next language since they have the same structures.

I did try with gulp-json-concat and it worked only on a linux environment since the data wouldn't keep track of windows paths. Here's the callback I passed to

// paths.resources:
resources: ['./public/gameData/**/*.json']

// gulpfile.json
return gulp.src(paths.resources)
.pipe(plugins.jsonConcat('gameData.json',jsonFolderParser))
.pipe(gulp.dest(paths.distDev + '/gameData/'));

// Callback
  var jsonFolderParser = function (data) {
  var computed = {};
  for (var i in data) {
    var keys = i.split("/");
    if (!computed[keys[0]]) {
      computed[keys[0]] = {};
    }
    if (keys.length == 2) {
      computed[keys[0]][keys[1]] = data[i]
    } else if (keys.length == 3) {
      if (!computed[keys[0]][keys[1]]) {
        computed[keys[0]][keys[1]] = {};
      }
      computed[keys[0]][keys[1]][keys[2]] = data[i]
    } else if (keys.length == 4) {
      if (!computed[keys[0]][keys[1]][keys[2]]) {
        computed[keys[0]][keys[1]][keys[2]] = {};
      }
      computed[keys[0]][keys[1]][keys[2]][keys[3]] = data[i]
    } else if (keys.length == 5) {
      if (!computed[keys[0]][keys[1]][keys[2]][keys[3]]) {
        computed[keys[0]][keys[1]][keys[2]][keys[3]] = {};
      }
      computed[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]] = data[i]
    }
  }
  return new Buffer(JSON.stringify(computed));
};

It work on linux, up to a depth 5 (I'm not so good with recursive thinking in callbacks ...) and it doesn't work on Windows.

Is there something I'm missing with this plugin ? Is there a plugin that work on both Windows and Linux dist ?

Upvotes: 2

Views: 101

Answers (1)

Romain
Romain

Reputation: 3673

Finally I ended up removing gulp-json-concat in favor of gulp-merge-json.

Here's the code of my solution:

return gulp.src(paths.resources)
.pipe(plugins.mergeJson('gameData.json', function(parsedJson, file) {
  var filePath = file.path.replace(file.base, "").replace(".json", "");
  if (filePath.indexOf("\\") !== -1) {
    // Windows
    filePath = filePath.split('\\');
  } else {
    // Linux
    filePath = filePath.split('/');
  }
  var finalJson = parsedJson;
  for (var i = filePath.length - 1; i >= 0 ; i--) {
    var tmp = {};
    tmp[filePath[i]] = finalJson;
    finalJson = tmp;
  }
  return finalJson;
}))
.pipe(gulp.dest(paths.distDev + '/game_data/'));

First, it split the path name according to the separator used whether it's Windows or Linux.

Then we construct the object in reverse, englobing each iteration by the next one.

The result is :

{
    "cards": {
        "card1": { data contained in the json },
        "card2": { ... },
        ...
    },
    "infos": {
        "en": {
            "properties1": { ... },
            ...
        },
        ...
    }
}

Upvotes: 1

Related Questions