@auturge/config-resolver
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

config-resolver

A function that resolves a config file and returns its content, or null if not found.

[![npm][npm]][npm-url] [![Build][travis-badge]][travis-url] [![Coverage Status][coverage-badge]][coverage-url]

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @auturge/config-resolver

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

26.9 kB

Total Files

8

Last publish

Collaborators

  • curtis.kaler