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

figit

v0.0.8

Published

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!

Downloads

16

Readme

figit

JavaScript and Handlebars data templates - Producing templated configuration has never been easier!

Or expand JSON and YAML templates using Handlebars template syntax.

I use this to produce Kubernetes configurations from templates.

If you like this project, please star this repo and support my work

Features

  • Produce data from JavaScript code.
  • Produce data from templated YAML and JSON files.
  • Use variables, environment variables and whatever data sources you like.
  • Use whatever npm packages you like.
  • Use YAML and JSON data files and standard input for Handlebars template data.

Install it

npm install -g figit

Command line reference

Evaluate file.js and output JSON:

figit file.js

Evaluate multiple files and output YAML:

figit file1 file2 fileN --output yaml

General use:

figit [file+] --output json | yaml [--stdin json | yaml] [--data <json-or-yaml-data-file>]

A simple JavaScript template

Create a template:

// myfile.js
module.exports = {
    /* Whatever code you want here to produce data. */
    data: {
        msg: "Hello world!",
    },
};

Instantiate data from the template:

figit myfile.js

JSON output:

{
    "data": {
        "msg": "Hello world!"
    }
}

Or output YAML:

figit myfile.js --output yaml

YAML output:

data:
    msg: "Hello world!"

A simple data template

Create a JSON template:

// myfile.json
{
    "data": {
        "msg": "{{MESSAGE}}",
    },
};

Or create a YAML template instead:

# myfile.yaml
data:
    msg: "{{MESSAGE}}"

Create a file to contain the Handlebars template data (this can be JSON or YAML as well):

// mydata.json
{
    "MESSAGE": "Hello world!"
}

Note: Template variables are also populated from environment variables, so you don't necessarily need to create a template data, you could instead set a MESSAGE environment variable like this:

export MESSAGE="Hello world"

Instantiate data from the template and output as JSON:

figit myfile.json --data mydata.json --output json

JSON output:

{
    "data": {
        "msg": "Hello world!"
    }
}

Or output YAML:

figit myfile.json --data mydata.json --output yaml

YAML output:

data:
    msg: "Hello world!"

You can also combine template data from a data file, environment variables and standard input.

Load template data from a file like this:

figit myfile.json --data mydata.json

Template data is overwritten by environment variables, so in this case a MESSAGE environment variable would override the MESSAGE field from the data file.

You can read template from standard input in JSON or YAML format like this:

figit myfile.json --stdin json < mydata.json

You can use this to pipe sensitive variables from one process to another without having to save them to disk.

You can combine the methods of loading template data:

figit myfile.json --data base-data.json --stdin json < override-data.json

This allows you to have multiple levels of data. Base template data is read from the file specified by --data. Environment variables can then override the base data. Finally data read from standard input provides the final values for template data.

A complex JavaScript template

Here's my usecase for Figit, templating Kubernetes configurations.

// mydeployment.js
const APP_NAME = process.env.APP_NAME;
if (!APP_NAME) {
    throw new Error(`Expected environment variable ${APP_NAME}`);
}

module.exports = {
    apiVersion: "apps/v1",
    kind: "Deployment",
    metadata: {
        name: ,
        labels: {
            app: APP_NAME,
        },
    },
    spec: {
        replicas: 3,
        selector: {
            matchLabels: {
                app: APP_NAME,
            },
        },
        template: {
            metadata: {
                labels: {
                    app: APP_NAME,
                },
            },
            spec: {
                containers: [
                    {
                        name: APP_NAME,
                        image: IMAGE_NAME,
                        ports: [
                            {
                                containerPort: 80,
                            },
                        ],
                    },
                ],
            },
        },
    },
};

Then in my deployment pipeline:

export APP_NAME=myapp
export IMAGE_NAME=myimage:latest
figit mydeployment.js | kubectl apply -f -