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

config-valley

v1.0.0

Published

configuration reading package

Downloads

3

Readme

config-valley

ts Download Status Github Star Github Issues NPM version License ci codecov code style: prettier

config-valley is a simple and easy-to-use utility for reading configuration from various sources. When developing CLI utilities, it may be necessary to read configuration from files like package.json or tsconfig.json to enhance user convenience. config-valley allows you to consistently read configuration from various formats, such as JSON, JSONC, JSON5, YAML, and TOML, using DI (Dependency Injection).

  1. Supports JSON, JSONC, JSON5, YAML, and TOML.
  2. Customizable configuration reading through DI.

Enhance the convenience of your CLI utility by using config-valley!

installation

# npm
npm install config-valley --save

# yarn
yarn install config-valley --save

# pnpm
pnpm add config-valley --save

Usage

architecture

config-valley has an architecture as shown in the diagram. It includes the FileHandle class for reading configuration files (including package.json and tsconfig.json), and various implementations of the IParser interface such as JSONParser, JSON5Parser, PackageJsonParser, TsconfigParser, TomlParser, and YamlParser. The ConfigHandle class contains both a FileHandle and an IParser, while the ConfigHandleContainer holds multiple ConfigHandle instances.

When using config-valley, you pass an array of ConfigHandle instances to the ConfigHandleContainer. See the example below:

import pathe from 'pathe';
import { 
  ConfigHandleContainer, 
  ConfigHandle,
  JsonKeyParser,
  PackageJsonParser,
  TsconfigJsonParser
} from 'config-valley';

function getConfigHandle() {
  const filename = 'your configuration file name, for example, .appconfig';
  const keyname = 'key name for extract configuration from package.json or tsconfig.json';

  const handle = new ConfigHandleContainer({
    handles: [
      // extract configuration from config file using keyname
      ConfigHandle.factory(
        new JsonKeyParser(keyname),
        pathe.join(process.cwd(), filename),
      ),
      // read configuration from config file
      ConfigHandle.factory(pathe.join(process.cwd(), filename)),
      // extract configuration from package.json using keyname
      ConfigHandle.factory(
        new PackageJsonParser(keyname),
        pathe.join(process.cwd(), 'package.json'),
      ),
      // extract configuration from tsconfig.json using keyname
      ConfigHandle.factory(
        new TsconfigJsonParser(keyname),
        pathe.join(process.cwd(), 'tsconfig.json'),
      ),
    ]
  });

  return handle;
}

const handle = getConfigHandle();
const config = await handle.read();

// show your configuration
console.log(config);

// change configuration
config.output = 'mytools/dist'

// save your configuration
await container.write(config)

How can I read various configuration formats?

config-valley reads configuration files using various parsers that implement the IParser interface. The FileHandle reads the configuration file, and the IParser implementation parses the read buffer. The ConfigHandle combines these two. The ConfigHandleContainer holds multiple ConfigHandle instances in an array. Finally, the ConfigHandleContainer reads the configuration files sequentially until successful.

Synchronous vs Asynchronous

You can use SyncConfigHandle and SyncConfigHandleContainer, or ConfigHandle and ConfigHandleContainer, depending on your needs. They have the same usage, but the FileHandle and IParser implementations must be developed specifically for synchronous or asynchronous use.

Support formats

config-valley supports JSON, JSONC, JSON5, YAML, and TOML. When reading configuration from package.json or tsconfig.json, you need to extract the configuration from a specific key. This method is also supported for other file formats. In such cases, you can use Json5KeyParser, YamlKeyParser, or TomlKeyParser to extract the configuration by specifying a key in the input file.

function getConfigHandle() {
  const filename = 'your configuration file name, for example, .appconfig';

  const handle = new ConfigHandleContainer({
    handles: [
      ConfigHandle.factory(
        new TomlKeyParser(args?.configKey),
        pathe.join(process.cwd(), filename),
      ),
    ]
  });

  return handle;
}

| Class Name | Usage | | :- | :- | | JSONParser | The JSONParser class parses JSON format files. | | JSONKeyParser | The JSONKeyParser class parses JSON format files and extracts configuration from a specific key. | | JSONCParser | The JSONCParser class parses JSON with Comments format files. | | JSONCKeyParser | The JSONCKeyParser class parses JSON with Comments format files and extracts configuration from a specific key. | | JSON5Parser | The JSON5Parser class parses JSON5 format files. | | JSON5KeyParser | The JSON5KeyParser class parses JSON5 format files and extracts configuration from a specific key. | | YamlParser | The YamlParser class parses YAML format files. | | YamlKeyParser | The YamlKeyParser class parses YAML format files and extracts configuration from a specific key. | | TomlParser | The TomlParser class parses TOML format files. | | TomlKeyParser | The TomlKeyParser class parses TOML format files and extracts configuration from a specific key. | | PackageJsonParser | The PackageJsonParser class parses package.json files and extracts configuration from a specific key. | | TsconfigJsonParser | The TsconfigJsonParser class parses tsconfig.json files and extracts configuration from a specific key. |

License

This software is licensed under the MIT.