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

@meltstudio/config-loader

v1.1.0

Published

Melt Studio's tool for loading configurations into a Node.js application.

Downloads

5

Readme

@meltstudio/config-loader

⚠️ WARNING: This project is in beta, so some features may change in the future. Use at your own discretion

Project Description

The Config Loader package is a powerful and user-friendly tool that simplifies the process of retrieving and collecting variables from one or multiple files for your project. It provides an efficient way to extract specific information from files and access those variables in your code. The result is a JSON object, making it easy to work with in various applications.

Features

  • Retrieve and collect variables from one or multiple files in your project.
  • YAML file support (support for other file types coming soon.)
  • Data can also be retrieved from CLI or environment variables .
  • Compatible with TypeScript/JavaScript environments, making it suitable for Node.js projects.

Table of Contents

Installation

To install the project, you can use the following steps:

  1. Ensure that you have Node.js installed on your machine.
  2. Open a terminal or command prompt.
  3. Run the following command to install the project and its dependencies via npm:
$ npm install @meltstudio/config-loader
$ yarn add @meltstudio/config-loader

Usage

Here's an example of how to use the @meltstudio/config-loader package in a TypeScript project:

import path from "path";

import c from "@meltstudio/config-loader";

const run = (): void => {
  const settings = c.schema({
    version: c.string({ required: true, cli: true }),
    website: {
      title: c.string({ required: true }),
      url: c.string({
        required: false,
        defaultValue: "www.mywebsite.dev",
      }),
      description: c.string({ required: true }),
      isProduction: c.bool({ required: true }),
    },
    database: {
      host: c.string({ required: true }),
      port: c.number({ required: true }),
      credentials: {
        username: c.string(),
        password: c.string(),
      },
    },
    socialMedia: c.array({
      required: true,
      item: c.string({ required: true }),
    }),
    features: c.array({
      required: true,
      item: {
        name: c.string(),
        enabled: c.bool(),
      },
    }),
  });
  const config = settings.load({
    env: false,
    args: true,
    files: path.join(__dirname, "./config.yaml"),
  });
  console.log(JSON.stringify(config, null, 2));
};

run();

With a config.yaml file with the following contents:

version: 1.0.0
website:
  title: My Website
  description: A simple and elegant website
  port: 3000
  isProduction: false

database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: secret

socialMedia: [https://twitter.com/example, https://instagram.com/example]

features:
  - name: Store
    enabled: true
  - name: Admin
    enabled: false

apiKeys:
  googleMaps: ${GOOGLE_MAPS_API_KEY}
  sendGrid: ${SENDGRID_API_KEY}

The expected output would be:

{
  "version": "1.0.0",
  "website": {
    "title": "My Website",
    "url": "www.mywebsite.dev",
    "description": "A simple and elegant website",
    "isProduction": false
  },
  "database": {
    "host": "localhost",
    "port": 5432,
    "credentials": {
      "username": "admin",
      "password": "secret"
    }
  },
  "socialMedia": [
    "https://twitter.com/example",
    "https://instagram.com/example"
  ],
  "features": [
    {
      "name": "Store",
      "enabled": true
    },
    {
      "name": "Admin",
      "enabled": false
    }
  ]
}

You can try executing our example in your project by following these steps with the command:

yarn example:run

Usage with CLI

When using our package with cli, it is important to have the cli attribute set to true. This will allow values to be sent when running the package from the command line.

import path from "path";

import c from "@meltstudio/config-loader";

const run = (): void => {
  const settings = c.schema({
    version: c.string({
      required: true,
      cli: true, 👈
    }),
  });
  const config = settings.load({
    env: false,
    args: true,
    files: path.join(__dirname, "./config.yaml"),
  });
  console.log(JSON.stringify(config, null, 2));
};

run();

To use it you need to send the property name on the command line with the new value

yarn example:run --version 2.0.0

Having the following config.yaml file:

version: 1.0.0

The expected output would be:

{
  "version": "2.0.0"
}

You can see that the CLI variable overrode the yaml file variable

Usage with Environment Variables

The Config Loader package allows you to use environment variables in your system configuration. You can specify variable names in your configuration and get them. To use this feature you need to set env: true

import path from "path";

import c from "@meltstudio/config-loader";

const run = (): void => {
  const settings = c.schema({
    database: {
      host: c.string({ required: true }),
      port: c.number({ required: true }),
      credentials: {
        username: c.string(),
        password: c.string({
          env: "DB_PASSWORD",
          cli: true,
        }),
      },
    },
  });
  const config = settings.load({
    env: true, 👈
    args: true,
    files: path.join(__dirname, "./config.yaml"),
  });
  console.log(JSON.stringify(config, null, 2));
};

run();

With the following config.yaml file:

database:
  host: localhost
  port: 5432
  credentials:
    username: admin
    password: IGNORED_PASSWORD
yarn example:run

If you have the environment variable DB_PASSWORD=ENV_USED_PASSWORD, the expected output would be:

{
  "database": {
    "host": "localhost",
    "port": 5432,
    "credentials": {
      "username": "admin",
      "password": "ENV_USED_PASSWORD"
    }
  }
}

You can notice that the environment variable overrode the value in the config.yaml file

License

This package is licensed under the Apache License 2.0. For more information, please see the LICENSE file.