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

unreadconfig

v0.1.0

Published

<p align="center"> <a href="https://www.npmjs.com/package/unreadconfig" target="_blank" rel="noopener noreferrer"> <img src="https://api.iconify.design/eos-icons:configuration-file-outlined.svg?color=%2380ffb7" alt="logo" width='100'/></a> </p>

Downloads

226

Readme

Find and load configuration from a package.json property, rc file, or CommonJS module. It has smart default based on traditional expectations in the JavaScript ecosystem. But it's also flexible enough to search anywhere you want and load whatever you want.

Features

Install

$ npm i unreadconfig

Quick start

const { readConfig } = require('unreadconfig');

import { readConfig } from 'unreadconfig';

// will look for:
// process.cwd() + '.namespacerc'
// process.cwd() + '.namespacerc.js'
// process.cwd() + '.namespacerc.ts'
// process.cwd() + '.namespacerc.mjs'
// process.cwd() + '.namespacerc.cjs'
// process.cwd() + '.namespacerc.json'
// process.cwd() + '.namespacerc.json5'
// process.cwd() + '.namespacerc.jsonc'
// process.cwd() + '.namespacerc.yaml'
// process.cwd() + '.namespacerc.yml'
// process.cwd() + '.namespacerc.toml'
// process.cwd() + 'namespace.config.mjs'
// process.cwd() + 'namespace.config.cjs'
// process.cwd() + 'namespace.config.js'
// ........
const data = readConfig('namespace', {
  default: {
    testItem2: 'some value'
  }
});

Load JS

Load the JS file and return the result, support .js, .cjs, .mjs, .ts.

// => ./app/app.config.js
export default {
  name: 'app'
}
import { loadConf } from 'unreadconfig/load-conf';

interface Config {
  name: string;
}

const result = loadConf<Config>(path.resolve(process.cwd(), 'app/app.config.js'));
// => { name: 'app' }

Option

import { LoadConfOption } from 'unreadconfig';
export type LoaderFunc<T> = (filepath: string, content: string, jsOption?: LoadConfOption) => T;
export type Loader<T> = Record<string, LoaderFunc<T>>;
export interface readConfigOption<T> {
  searchPlaces?: string[];
  /** An object that maps extensions to the loader functions responsible for loading and parsing files with those extensions. */
  loaders?: Loader<T>;
  /** Specify default configuration. It has the lowest priority and is applied after extending config. */
  defaluts?: T;
  /** Resolve configuration from this working directory. The default is `process.cwd()` */
  cwd?: string;
  /** Default transform js configuration */
  jsOption?: LoadConfOption;
  /** @deprecated use `mustExist` instead */
  ignoreLog?: boolean;
  mustExist?: boolean;
}
/**
 * Find and load configuration from a `package.json` property, `rc` file, or `CommonJS` module.
 * @param namespace {string} Configuration base name. The default is `readConfig`.
 * @param option
 */
export default function readConfig<T>(namespace?: string, option?: readConfigOption<T>): {} & T;

Discover configurations in the specified directory order. When configuring a tool, you can use multiple file formats and put these in multiple places. Usually, a tool would mention this in its own README file, but by default, these are the following places, where ${moduleName} represents the name of the tool:

Default searchPlaces:

[
  'package.json',
  `.${moduleName}rc`,
  `.${moduleName}rc.json`,
  `.${moduleName}rc.json5`,
  `.${moduleName}rc.jsonc`,
  `.${moduleName}rc.yaml`,
  `.${moduleName}rc.yml`,
  `.${moduleName}rc.toml`,
  `.${moduleName}rc.ini`,
  `.${moduleName}rc.js`,
  `.${moduleName}rc.ts`,
  `.${moduleName}rc.cjs`,
  `.${moduleName}rc.mjs`,
  `.config/${moduleName}rc`,
  `.config/${moduleName}rc.json`,
  `.config/${moduleName}rc.json5`,
  `.config/${moduleName}rc.jsonc`,
  `.config/${moduleName}rc.yaml`,
  `.config/${moduleName}rc.yml`,
  `.config/${moduleName}rc.toml`,
  `.config/${moduleName}rc.ini`,
  `.config/${moduleName}rc.js`,
  `.config/${moduleName}rc.ts`,
  `.config/${moduleName}rc.cjs`,
  `.config/${moduleName}rc.mjs`,
  `${moduleName}.config.js`,
  `${moduleName}.config.ts`,
  `${moduleName}.config.cjs`,
  `${moduleName}.config.mjs`,
]

Configurations are loaded sequentially, and the configuration file search is terminated when a configuration file exists.

The content of these files is defined by the tool. For example, you can add a semi configuration value to false using a file called .config/readConfig.yml:

semi: true

Additionally, you have the option to put a property named after the tool in your package.json file, with the contents of that property being the same as the file contents. To use the same example as above:

{
  "name": "your-project",
  "readConfig": {
    "semi": true
  }
}

This has the advantage that you can put the configuration of all tools (at least the ones that use unreadconfig) in one file.

loader

.js,.ts,.cjs,.mjs

import type { JITIOptions } from 'jiti/dist/types';
import { Options } from 'sucrase';
export interface LoadConfOption {
  jiti?: boolean;
  jitiOptions?: JITIOptions;
  transformOption?: Options;
}
export declare function loadConf<T>(path: string, option?: LoadConfOption): T;
export declare function jsLoader<T>(filepath: string, content: string, option?: LoadConfOption): T;

Modify default .js,.ts,.cjs,.mjs loader parameters.

import { readConfig, jsLoader } from 'unreadconfig';

function loadJS(filepath, content) {
  return jsLoader(filepath, content, {
    // change option...
  });
}

const data = readConfig('namespace', {
  loaders: {
    '.js': loadJS,
    '.ts': loadJS,
    '.cjs': loadJS,
    '.mjs': loadJS,
  },
  default: {
    testItem2: 'some value'
  }
});

example:

import { jsLoader } from 'unreadconfig';

const data = jsLoader('/path/to/file/name.js')

.ini

export declare function iniLoader<T>(_: string, content: string): T;

example:

import { iniLoader } from 'unreadconfig';

const data = iniLoader(undefined, `...`)

.json,.jsonc, json5

export declare function jsonLoader<T>(_: string, content: string): T;

example:

import { jsonLoader } from 'unreadconfig';

const data = jsonLoader(undefined, `{ "a": 123 }`)

.toml

export declare function tomlLoader<T>(_: string, content: string): T;

example:

import { tomlLoader } from 'unreadconfig';

const data = tomlLoader(undefined, `...`)

.yaml

export declare function yamlLoader<T>(_: string, content: string): T;

example:

import { yamlLoader } from 'unreadconfig';

const data = yamlLoader(undefined, `...`)

Custom Yaml loader

This is an example, the default yaml/yml does not require a loader.

import { readConfig } from 'unreadconfig';
import yaml from 'yaml';

function loadYaml(filepath, content) {
  return yaml.parse(content);
}

const data = readConfig('namespace', {
  searchPlaces: [
    '.namespacerc.yaml',
    '.namespacerc.yml',
  ],
  loaders: {
    '.yaml': loadYaml,
    '.yml': loadYaml,
  },
  default: {
    testItem2: 'some value'
  }
});

utils

merge

export declare const merge: {
  <TObject, TSource>(object: TObject, source: TSource): TObject & TSource;
  <TObject_1, TSource1, TSource2>(object: TObject_1, source1: TSource1, source2: TSource2): TObject_1 & TSource1 & TSource2;
  <TObject_2, TSource1_1, TSource2_1, TSource3>(object: TObject_2, source1: TSource1_1, source2: TSource2_1, source3: TSource3): TObject_2 & TSource1_1 & TSource2_1 & TSource3;
  <TObject_3, TSource1_2, TSource2_2, TSource3_1, TSource4>(object: TObject_3, source1: TSource1_2, source2: TSource2_2, source3: TSource3_1, source4: TSource4): TObject_3 & TSource1_2 & TSource2_2 & TSource3_1 & TSource4;
  (object: any, ...otherArgs: any[]): any;
};

findConfigFile

export declare function findConfigFile(moduleName: string, root: string, searchPlaces?: string[]): string;

getConfigPath

export declare const getConfigPath: () => string;

Example:

import { readConfig, getConfigPath } from 'unreadconfig';

const data = readConfig<Config>('idoc');
const configPath = getConfigPath();
// => /.readConfigrc.js

Related

  • cosmiconfig Find and load configuration from a package.json property, rc file, or CommonJS module
  • cjson Comments enabled json loader (Commented JavaScript Object Notation)
  • Config Loader Load user config files for node js projects.
  • Lilconfig Zero-dependency nodejs config seeker.
  • proload Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
  • rc The non-configurable configuration loader for lazy people.