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

@tib/configload

v0.3.0

Published

Load configuration file in various formats: yaml, json5, json, toml and js

Downloads

4

Readme

@tib/configload

Build Coverage

Load configuration file in various formats: yaml, json5, json, toml and js.

Supported Formats

  • yaml
  • json
  • json5
  • toml (install @iarna/toml manually in your package)

Installation

npm i @tib/configload

Usage

import {load, loadSync, autoLoad, autoLoadSync} from '@tib/configload';

// load sync
console.log(loadSync('foo.yml'));
console.log(loadSync('foo.yaml'));
console.log(loadSync('foo.json'));
console.log(loadSync('foo.json5'));
console.log(loadSync('foo.toml'));

// load async
(async () => {
  console.log(await load('foo.yml'));
})();

// auto load sync
console.log(autoLoadSync('foo'));

// auto load async
(async () => {
  console.log(await autoLoad('foo'));
})();

API

detect(file: string, lang?: string): {file: string, lang: string, loader: Loader} | undefined

detect config file with lang or default loaders

Params

  • file: string - path of the file or prefix to read.
  • lang?: string - force using specified lang loader.
  • return: {file: string, lang: string, loader: Loader} | undefined - detected result

Example

import {detect} from '@tib/configload';

// Suppose we have the following files
//
// ├─┬ config
//   ├── foo.yml
//   ├── foo.json
//   └── foo.toml

console.log(detect('foo')); // => {file: '<..>/foo.yml', lang: 'yaml', loader: <YamlLoader>}
console.log(detect('foo.json')); // => {file: '<..>/foo.json', lang: 'json', loader: <JsonLoader>}
console.log(detect('foo', 'toml')); // => {file: '<..>/foo.toml', lang: 'toml', loader: <TomlLoader>}
console.log(detect('foo', 'js')); // => undefied
console.log(detect('foo.json', 'toml')); // => undefied

load(filepath: string, options: LoadOptions = {}): Promise

load config file async

Params

  • filepath: string - path of the file to read.
  • options: LoadOptions - options to pass to loader
  • returns: Promise<Object> - promised JSON

Example

import {load} from '@tib/configload';

(async () => {
  console.log(await load('foo.yml'));
  console.log(await load('foo.json'));
})();

loadSync(filepath: string, options: LoadOptions = {}): any

load config file sync

Params

  • filepath: string - path of the file to read.
  • options: LoadOptions - options to pass to loader
  • returns: Object - JSON

Example

import {loadSync} from '@tib/configload';

console.log(loadSync('foo.yml'));
console.log(loadSync('foo.json'));

autoLoad(filepath: string, options: LoadOptions = {}): Promise

Async auto detect config file with the filepath and load the config file

Params

  • filepath: string - path of the file to read.
  • options: LoadOptions - options to pass to loader
  • returns: Promise<Object> - promised JSON

Example

import {autoLoad} from '@tib/configload';

(async () => {
  console.log(await autoLoad('foo.yml'));
  console.log(await autoLoad('foo.json'));
})();

autoLoadSync(filepath: string, options: LoadOptions = {}): any

Sync auto detect config file with the filepath and load the config file

Params

  • filepath: string - path of the file to read.
  • options: LoadOptions - options to pass to loader
  • returns: Object - JSON

Example

import {autoLoadSync} from '@tib/configload';

console.log(autoLoadSync('foo.yml'));
console.log(autoLoadSync('foo.json'));

LoadOptions

YamlLoadOptions

  • lang?: string - The language to force using
  • encoding?: BufferEncoding - Pass to fs.readFile
  • flag?: OpenMode - Pass to fs.readFile
  • throws?: boolean - Throw a wrapped error if parse throws
  • filename?: string - Pass to js-yaml

JsonLoadOptions

  • lang?: string - The language to force using
  • encoding?: BufferEncoding - Pass to fs.readFile
  • flag?: OpenMode - Pass to fs.readFile
  • throws?: boolean - Throw a wrapped error if parse throws
  • reviver?: Function - A function that transforms the results. Pass to JSON.parse

TomlLoadOptions

  • lang?: string - The language to force using
  • encoding?: BufferEncoding - Pass to fs.readFile
  • flag?: OpenMode - Pass to fs.readFile
  • throws?: boolean - Throw a wrapped error if parse throws

JsLoadOptions

  • lang?: string - The language to force using
  • encoding?: BufferEncoding - Pass to fs.readFile
  • flag?: OpenMode - Pass to fs.readFile
  • throws?: boolean - Throw a wrapped error if parse throws