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

@auturge/config-resolver

v1.0.0

Published

auturge/config-resolver - finds and loads your config file, or returns null

Downloads

4

Readme

config-resolver

Table of Contents

About

Reading a config file is a pretty standard operation for just about any node project. The config-resolver provides a function to avoid repeating the same boilerplate config-loading code in every project.

How to install

The following commands will install config-resolver:

npm install --save-dev @auturge/config-resolver

or

yarn add @auturge/config-resolver --dev

Getting Started (Examples)

config-resolver supports JavaScript or JSON config files.

The following examples assume the project has this shape:

demo
├── conf/
│   ├── config.json
│   ├── config.js
│   └── config.export.js
│
├── src/
│   ├── index.js

EXAMPLE: JSON config file

config.json

{
        source: './package.json',
        destination: './build/package.json',
        keeplist: ['name', 'version'],
        loglevel: 'INFO',
}

index.js

const path = require('path')
const { resolveConfig } = require('@auturge/config-resolver')

const options = {
    explicit: {
        path: path.resolve(__dirname, '../conf/demo.config.json'),
        type: 'json',
    },
}
const config = resolveConfig(options)

console.log(JSON.stringify(config, undefined, 2))

result

{
  "source": "./package.json",
  "destination": "./build/package.json",
  "keeplist": [
    "name",
    "version"
  ],
  "loglevel": "INFO"
}

EXAMPLE: JavaScript function config file

config.js

module.exports = (env) => {
    const isProd = env && env['prod'] === true

    const DESTINATION = './build/{0}/package.json'.replace(
        '{0}', isProd ? 'prod' : 'dev'
    )

    const config = {
        source: './package.json',
        destination: DESTINATION,
        keeplist: ['name', 'version'],
        loglevel: 'INFO',
    }

    return config
}

index.js

const path = require('path')
const { resolveConfig } = require('@auturge/config-resolver')

const env = { prod: true }
const options = {
    explicit: {
        path: path.resolve(__dirname, '../conf/demo.config.js'),
        type: 'function',
    },
}
const configFunction = resolveConfig(options)
const config = configFunction(env)

console.log(JSON.stringify(config, undefined, 2))

result

{
  "source": "./package.json",
  "destination": "./build/prod/package.json",
  "keeplist": [
    "name",
    "version"
  ],
  "loglevel": "INFO"
}

EXAMPLE: JavaScript config file exporting a JSON object

config.export.js

module.exports = {
    source: './package.json',
    destination: './build/package.json',
    keeplist: ['name', 'version'],
    loglevel: 'INFO',
}

index.js

const path = require('path')
const { resolveConfig } = require('@auturge/config-resolver')

const env = { prod: true }
const options = {
    explicit: {
        path: path.resolve(__dirname, '../conf/demo.config.js'),
        type: 'json',
    },
}
const config = resolveConfig(options)

console.log(JSON.stringify(config, undefined, 2))

result

{
  "source": "./package.json",
  "destination": "./build/package.json",
  "keeplist": [
    "name",
    "version"
  ],
  "loglevel": "INFO"
}

EXAMPLE: Loading a config file from one of several places or formats

Suppose you want to allow users to place their config files in one of several places, or support multiple formats. Say you want to search for config files with the following precedence (where higher on the list is where we look first):

./.trimrc		or	 	./trim.config.js,
./conf/.trimrc 	or		./conf/trim.config.js
./conf/demo.config.js
./conf/demo.config.json

index.js

const path = require('path')
const { resolveConfig } = require('@auturge/config-resolver')

const env = { prod: true }
const options = {
    alternatives: [
        {
			// another file that does not exist in our demo
			path: './.trimrc', type: 'json'
        },
        {
			// a file that does not exist in our demo
			path: './trim.config.js', type: 'function',
			priority: 0
        },
        {
			// a file that does not exist in our demo
			path: './conf/trim.config.js', type: 'function',
			priority: 1
        },
        {
			// another file that does not exist in our demo
			path: './conf/.trimrc', type: 'json',
			priority: 1
        },
        {
            path: './conf/demo.config.js'), type: 'function',
            priority: 2
        },
{
            path: './conf/demo.config.json'), type: 'json',
            priority: 3
        },
    ],
}
const config = resolveConfig(options)

console.log(JSON.stringify(config, undefined, 2))

result

Using the above code, config-resolver would find and attempt to load ./conf/demo.config.js.


Contributing and Internal Documentation

The auturge family welcomes any contributor, small or big. We are happy to elaborate, guide you through the source code and find issues you might want to work on! To get started have a look at our documentation on contributing.