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

ex-config

v5.0.0

Published

Extendable configuration processor.

Downloads

189

Readme

ex-config

npm Linux Build Status Windows Build Status Code Coverage

About

ex-config is an extendable configuration processor. It is used to merge multiple configurations together like babel and eslint configurations do.

Installation

npm install --save ex-config

Usage

'use strict';

const cosmiconfig = require('cosmiconfig');
const {
	/* async: */ exConfig,
	/* sync: */ exConfigSync,
} = require('ex-config');

/**
 * Example using cosmiconfig to load the base config from disk
 */
const explorer = cosmiconfig('example');
const baseConfig = await explorer.load();

/**
 * Original configs are not mutated
 *
 * all options are optional
 *
 * preprocessor, validator, processor, and postProcessor lifecycles all
 *   are given one argument object containing:
 * {
 *   value: new value
 *   current: current value, can be undefined
 *   config: current config
 *   dirname: directory where the preset was loaded from
 * }
 */
const config = await exConfig(
	/* initial config: */ baseConfig,
	/* options: */ {
		/**
		 * directory to initially resolve presets/plugins from
		 *
		 * default: process.cwd()
		 */
		baseDirectory: process.cwd(),

		/**
		 * Add an API accessible to all lifecycles/presets/plugins
		 *
		 * default: {}
		 */
		api: new (class ApiExample {
			someCustomApiExample() {
				return this;
			}
		})(),

		/**
		 * key id that extends configurations via resolve
		 * See also, overrides.presets.resolve
		 *
		 * default: 'presets'
		 * set to false to disable
		 */
		presets: 'presets',

		/**
		 * key id that resolves plugins
		 *
		 * default: 'plugins'
		 * set to false to disable
		 */
		plugins: 'plugins',

		/**
		 * Pre-process the preset before validation
		 *
		 * Whatever is returned will be the new value of the current preset
		 *
		 * Runs once with each preset
		 */
		preprocessor: async ({ value, current, config, dirname, api }) => {
			// returned value will be the updated preset value
			return value;
		},

		/**
		 * Used to validate a preset. if preset is invalid, throw with an error
		 *
		 * Runs once with each preset
		 */
		validator: async ({ value, current, config, dirname, api }) => {
			if (value !== 'valid') {
				throw new Error('value is invalid');
			}

			// does not expect a return value
			return undefined;
		},

		/**
		 * Post Processor runs once after all configurations
		 *   have been successfully processed.
		 */
		postProcessor: async ({ config, dirname, api }) => {
			// returned value will equal the final config
			return value;
		},

		/**
		 * processor runs once per key per preset
		 *
		 * used merge the value with the overall current value
		 *
		 * by default, this will deep merge any objects,
		 *   push new value onto array, or replace current value
		 *
		 * other built-in processors are:
		 * arrayPush: all values are an array that are appended
		 *   to the end of the current array
		 *
		 * arrayConcat: all values are an array that are added
		 *   to the front of the current array
		 *
		 * mergeDeep: all values are treated as an object
		 *
		 * Pass a function to use a custom processor
		 */
		processor: async ({ value, current = [], config, dirname, api }) => {
			const val = Array.isArray(value) ? value : [value];

			// returned value will be the new value of the key
			return [
				...current,
				...val,
			];
		},

		/**
		 * overrides can override/change the settings above
		 */
		overrides: {
			presets: {
				resolve: {
					/**
					 * Prefix to add to packageId
					 *
					 * example: one -> example-preset-one
					 *
					 * Optional
					 *
					 * Accepts a single or an array of prefixes
					 */
					prefix: 'example-preset',

					/**
					 * NPM Scope of organization to override prefix
					 *
					 * Optional
					 */
					org: '@example',

					/**
					 * Org prefixes
					 *
					 * example: @example/one -> @example/preset-one
					 *
					 * Optional
					 *
					 * Accepts a single or an array of prefixes
					 */
					orgPrefix: 'preset',

					/**
					 * Only allow prefixed module resolution.
					 * Explicit modules can be required by prepending module:
					 * For example, module:local-module
					 *
					 * Default: true
					 * Optional
					 */
					strict: true,
				},
			},

			files: {
				/**
				 * preprocessor override is in addition to the base preprocessor
				 */
				preprocessor: async ({
					value,
					current,
					config,
					dirname,
					api,
				}) => {
					return value;
				},

				/**
				 * validator override will validate in addition to the base validator
				 */
				validator: async ({ value, current, config, dirname, api }) => {
					if (typeof value.source !== 'string') {
						throw new Error('files source must be a string');
					}
				},

				/**
				 * processor override will override the base processor
				 */
				processor: async ({
					value,
					current = [],
					config,
					dirname,
					api,
				}) => {
					const val = Array.isArray(value) ? value : [value];

					return [
						...val,
						...current,
					];
				},
			},
		},
	},
);

Creating Presets and Plugins

A preset or plugin needs to export an object or function.

Only export default is supported when using es modules.

If a function is used it will be invoked with the following argument:

// preset-example.js
function presetExample(context) {
	// options given as second index when calling preset/plugin
	const options = context.options;
	// dirname package was found from
	const dirname = context.dirname;
	// api provided in options
	const api = context.api;

	return {
		verbose: options.verbose,
	};
}

module.exports = presetExample;

// config.js
const exampleConfig = {
	presets: [
		[
			// packageId
			'preset-example',
			// options for preset/plugin
			{ verbose: true },
		],
	],
};

module.exports = exampleConfig;

Thanks To

This package was created with the great work / lessons learned from: