gulp-data icon indicating copy to clipboard operation
gulp-data copied to clipboard

Using multiple json files with extended arrays

Open lubomirblazekcz opened this issue 8 years ago • 7 comments

The current behaviour is that, when you using multiple json files and you create the same array in different json file. The array get's overwritten, the better behaviour would be to extend it / merge it with existing array.

Example: I have global json file, where I have array and I need to extend it on different page.

lubomirblazekcz avatar Jul 26 '16 09:07 lubomirblazekcz

Is it related to issue #5 and #6 ?

ghost avatar Jul 27 '16 19:07 ghost

probably, but doesn't seems to work for me

.pipe(data(function() { return require('./src/templates/_core/main.json') })) .pipe(data(function(file) { return require('./src/templates/_core/pages/'+ path.basename(file.path).replace('.hbs','.json')) }))

I'm using one main json file, and then one for each page.

lubomirblazekcz avatar Jul 30 '16 18:07 lubomirblazekcz

I solved it with gulp-merge-json. Every time a json file get changed I create a combined .cache-data.json and pass the single json file to my templates. This speed up also page generation since i have a lot json files.

ghost avatar Aug 02 '16 10:08 ghost

cool, thanks!

lubomirblazekcz avatar Aug 02 '16 10:08 lubomirblazekcz

I used merge-json to solve this problem.

Like this:

gulp.task('pug', () => {
	return gulp.src('src/index.pug')
		.pipe(data(() => mergeJson.merge(require('./src/data/projects.json'),
			require(
				'./src/data/skills.json'))))
		.pipe(pug({}))
		.pipe(gulp.dest('dist/'))
})

But I think isn't best solution. :thinking:

raulfdm avatar Jul 03 '17 01:07 raulfdm

@raulfdm yeah the problem here is that you have to add a line per every new json file. This is what i have tried but ending up with an empty html page. any ideas?

gulp.task('views:build', ['views:templates'] , () => {
  return gulp.src(config.views.src + '*.twig')
      .pipe($.data(function() {
        return JSON.parse(fs.readFileSync('src/data/timber.json'))
      })) // load data from base json file
      .pipe($.twig())
      .pipe(gulp.dest(config.views.tmp))
      .pipe(reload({stream: true}));
});

gulp.task('views:templates', () => {
  return gulp.src(config.views.src + 'templates/*.twig')
      .pipe($.data(function(file) {
        return JSON.parse(fs.readFileSync('src/data/' + path.basename(file.path).replace('.twig', '.json')));
      }))
      .pipe($.twig())
});

mortensassi avatar Jul 26 '17 10:07 mortensassi

@moesphemie I changed my approach to solve this problem, creating an object with objects, like this:

gulp.task('pug', () => {
	return gulp.src('src/index.pug')
		.pipe(data(() => {

			const result = {
				projects: require('./src/data/projects.json'),
				about: require('./src/data/about.json'),
				abilities: require('./src/data/skills.json')
			}

			if (PRODUCTION)
				result.assets = require('./src/data/rev-manifest.json')

			return result
		}))
		.pipe(pug({}))
		.pipe(gulp.dest(DEST_FOLDER))
})

In this way, I just can check:

if (assets)
  //Do something

raulfdm avatar Jul 26 '17 17:07 raulfdm