npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-sync-json

v1.1.0

Published

Gulp plugin to synchronize JSON file key structures

Downloads

8

Readme

gulp-sync-json NPM version Build Status

Gulp plugin for synchronizing JSON file key structures against a primary source

What it's for

This plugin can synchronize the key structures of JSON files. It must be provided a 'source of truth', or primary filename. For each directory it encounters, the plugin will make all JSON files present conform to that directory's primary file's key structure. It is recursive, so it handles nested key structures as one would expect.

What it's not for

  • It does not synchronize values. It only synchronizes keys.
  • It cannot synchronize files across different directories.
  • It will not synchronize array structures, including objects inside arrays. Arrays are treated as primitives; only objects are recursed and processed.

This plugin has no relation to grunt-sync-json.

License

MIT License (Expat)

Example

Given these files:

a.json

{
    "key_one": "value",
    "key_two": 42,
    "nested": {
        "key": "nested value"
    }
}

b.json

{
    "key_two": 100,
    "nested": {
        "key": "different value",
        "other_key": "other value"
    }
}

Running the plugin with a.json as the primary file will change b.json to the following:

{
    "key_one": "value",
    "key_two": 100,
    "nested": {
        "key": "different value"
    }
} 

Usage

To make all JSON files within the cwd conform to an 'en.json' sibling file:

var gulp = require('gulp');
var syncJSON = require('gulp-sync-json');

gulp.task('sync-json', function() {
    return gulp.src('./**/*.json')
        .pipe(syncJSON('en.json'))
        .pipe(gulp.dest('./'));
});

API

syncJSON(primaryFile: string, options?: {
    report?: boolean,
    errorOnReportFail?: boolean,
    spaces?: number,
    verbose?: boolean    
})

primaryFile

A filename, or the basename portion of a path, that is the source of truth for key structure for every other JSON file in the directory

options

An optional options object. The following properties are supported; all are optional:

  • report - Default false. If set to true, the plugin will audit files instead of changing them on the filesystem. Key mismatches are treated as errors and (almost) all errors are supressed and collected instead of being emitted onto the stream as they occur. If the audit finds anything it will log everything out at the end
  • errorOnReportFail - Default false. If set to true, the plugin will emit an error onto the stream if report mode finds anything. Since this causes gulp to exit with a non-zero exit code, it's possible to fail a CI/build step with this. The error is emitted after auditing all files in the stream, and after logging the report output. Has no effect if report is false
  • spaces - Default 4. How many spaces to use when formatting JSON. Passed directly to JSON.stringify
  • verbose - Default false. If set to true, the plugin will log out a summary of key additions and removals as it processes each file

Notes on behavior

Keys

  • When filling in a new key, the plugin will use the value from the primary file
  • When the plugin encounters a key not present in the primary file, it will remove it
  • If a key is present in both a source and target file but the value types do not match, the plugin will emit an error with the file, key, and types

Files

  • The plugin only cares about files in directories with both a primary file and other files present. Any files in the stream that aren't in such a directory are piped through untouched
  • For files it does care about, the plugin will emit an error when it encounters invalid JSON, whether in report mode or not. There are a couple gulp plugins already available for linting JSON, which can sit in front of this plugin on the stream

Need to handle line endings differently? Pipe the results through gulp-eol.