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

@scvo/json-inc

v0.0.15

Published

A rudimentary JSON transpiler that allows JSON files to be split up into separate files

Downloads

13

Readme

JSON inc.

A rudimentary JSON transpiler CLI that allows JSON files to be split up.

WARNING!

This is a part of an ever evolving internal tool chain and although I'm going to try not to change the way it works too much, there may be compatability issues when this eventually gets tidied up and updated.

Purpose

The purpose of this tool is to assist with large JSON configuration files that need to contain huge strings or have bits of JSON that can be logically broken out into their own thing. On top of this, we wanted the JSON inc. files to be valid JSON to keep our code editors happy and to allow for scripted transformations.

Installation

Installation is pretty simple

npm i -g @scvo/json-inc

Syntax

There are 4 basic things that can be done using JSON inc.

Include file contents as strings

Include the contents of an external file as a JSON safe string. Check out this example:

routes.json2:

{
    "test_route": {
        "pattern": "/test-route",
        "html": "{$./html/test-route.html}"
    }
}

./html/test-route.html:

<html>
    <head>
        <title>Test Route</title>
    </head>
    <body>
        <h1>I'm a big ol' HTML file</h1>
        <p>I'd otherwise be annoying to include in a JSON file</p>
    </body>
</html>

If you were to run routes.json2 through JSON inc. you'd get the following output:

{
    "test_route": {
        "pattern": "/test-route",
        "html": "<html>\n    <head>\n        <title>Test Route</title>\n    </head>\n    <body>\n        <h1>I\'m a big ol\' HTML file</h1>\n        <p>I\'d otherwise be annoying to include in a JSON file</p>\n    </body>\n</html>\n"
    }
}

Include entire JSON files as values

Include a complete external JSON file as the value of a property in your JSON inc. file. Check out this example:

routes.json2:

{
    "test_route": "{:./routes/test-route.json}"
}

./routes/test-route.json:

{
    "pattern": "/test-route",
    "html": "<html>...</html>"
}

If you were to run routes.json2 through JSON inc. you'd get the following output:

{
    "test_route": {
        "pattern": "/test-route",
        "html": "<html>...</html>"
    }
}

Include parts of JSON files as values

Include specific bits of an external JSON file as the value of a property in your JSON inc. file. Check out this example:

site.json2:

{
    "routes": {
        "test_route": "{:./routes.json@test_route}"
    }
}

./routes.json:

{
    "test_route": {
        "pattern": "/test-route",
        "html": "<html>...</html>"
    }
}

If you were to run site.json2 through JSON inc. you'd get the following output:

{
    "routes:" {
        "test_route": {
            "pattern": "/test-route",
            "html": "<html>...</html>"
        }
    }
}

Include parts of a JSON file as part of your current JSON file

As you may have noticed. We are just including stuff into the values of JSON properties. This is limiting because we might want to include an array or just split up a larger object from an external file.

This has a slightly messier syntax as we still want to keep our JSON. inc files as valid JSON. Here's an example:

site.json2:

{
    "routes": {
        "index": {
            "pattern": "/",
            "html": "<html>...</html>"
        },
        "{:./other-routes.json}": ""
    }
}

./other-routes.json:

{
    "test_route": {
        "pattern": "/test-route",
        "html": "<html>...</html>"
    },
    "another_route": {
        "pattern": "/another-route",
        "html": "<html>...</html>"
    }
}

Run this through JSON inc. And you'll get the following:

{
    "routes": {
        "index": {
            "pattern": "/",
            "html": "<html>...</html>"
        },
        "test_route": {
            "pattern": "/test-route",
            "html": "<html>...</html>"
        },
        "another_route": {
            "pattern": "/another-route",
            "html": "<html>...</html>"
        }
    }
}

This works with arrays too and you can also do the @ trick to point to a specific path inside the external JSON file e.g:

{
    "routes": {
        "index": {
            "pattern": "/",
            "html": "<html>...</html>"
        },
        "{:./[email protected]}": ""
    }
}

Usage

This tool requires a minimum of 2 arguments:

  • a space separated array of globs for input files; and
  • an output path to where the resulting JSON file or files should go. All input files should have the extension .json2.

Examples

The following example with take all files with the extension .json2 in the ./db-src directory, process them, and save each file using its existing filename to the ./db-dst directors (creating it if it doesn't exist), changing the file extensions to .json:

json-inc ./db-src/**/*.json2 ./db-dst

The next example does the same as the previous one but also processes a specific additional file ./db.json2. They all still get output to the .db-dst directors:

json-inc ./db-src1/**/*.json2 ./db.json2 ./db-dst

As you can see, the last argument is always treated as the output directory

TODO

  • Create unit tests
  • Support recursion
  • Some actual error handling
  • Rewrite all terribly hacky code (basically do it again but better)