Skip to content

TypeScript (JavaScript) package to get and merge configuration, made for Node.js and Deno

License

Notifications You must be signed in to change notification settings

tomchen/searchconfig

Repository files navigation

Search Config: Get and merge configuration, for Node and Deno

TypeScript (JavaScript) package to get and merge configuration, made for Node.js and Deno.

A lightweight alternative to cosmiconfig, it is easy to use, fully customizable and predictable, and has clear error types that are simple to handle.

Home Page & Docs (searchconfig.js.org) | GitHub repo | npm package | Deno Module

npm package version number Actions Status Node.js Deno License

For now it's an alpha release and I'll improve the documentation. But it should be usable and there is auto-generated API doc you can read.

npm install searchconfig

Basic Usage:

import { getConfig } from 'searchconfig'
const stra = defaultConfigGetStrategy('packagename')
;(async () => {
  const config = await getConfig(stra)
})()

With YAML:

import { getConfig, registry } from 'searchconfig'
import * as yaml from 'yaml'

registry.addLoader('yaml', yaml.parse)
const stra = defaultConfigGetStrategy('packagename', {
  hasYaml: true,
})
;(async () => {
  const config = await getConfig(stra)
})()

The stra variable, which represents a default config get strategy, looks like this (the previous one take precedence over the next ones):

[
  {
    filename: [
      'package.json',
      '.packagenamerc',
      '.packagenamerc.json',
      '.packagenamerc.yaml',
      '.packagenamerc.yml',
      '.packagenamerc.js',
      '.packagenamerc.mjs',
      '.packagenamerc.cjs',
      'packagename.config.json',
      'packagename.config.yaml',
      'packagename.config.yml',
      'packagename.config.js',
      'packagename.config.mjs',
      'packagename.config.cjs',
    ],
    loader: [null, 'jsonoryaml'], // only set 'jsonoryaml' for the second item which is '.packagenamerc'
    key: ['packagename'], // only set object key 'jsonoryaml' for the first item which is 'package.json'
  },
]