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

config-locator

v0.1.0

Published

Find the config file from ancestor folders.

Downloads

46

Readme

config-locator

Build Status codecov install size

Find the config file from ancestor folders.

Features

  • The result is cached.
  • Load CommonJS modules or JSON files.
  • Everything runs asynchronously.

Installation

npm install config-locator

Usage

const {createConfigLocator} = require("config-locator");
/*
- my-config.js
- src:
  - index.js
  - lib:
    - some-file.js
*/
const locator = createConfigLocator({
  config: [
    "my-config.json",
    "my-config.js"
  ]
});
const result = await locator.findConfig("src/lib/some-file.js");
const result2 = await locator.findConfig("src/index.js");

result.filename === result2.filename; // true
result.config === result2.config; // true

API

This module exports following members:

  • findConfig(file, options) - find config fore file.
  • createConfigLocator(options) - create a config locator.

findConfig

async findConfig(file: String, options: Object) => null|Array|Object

This is a shortcut of createConfigLocator(options).findConfig(file).

createConfigLocator

createConfigLocator(options: Object) => locator

options has following properties:

  • config: String|Array<String> - the filename of the config file(s). The locator would check if these files are in the directory.

  • findAll?: Boolean - by default, the locator would return the first found config file. If findAll is true then find all config files. Default: false.

  • stopAtRoot?: Boolean - stop finding if package.json is found in the directory. Default: true.

  • stopAt?: (dirname: String, pendingConfig: Promise) => shouldStop: Boolean|Promise<Boolean> - a hook to customize when to stop finding. The function could be async.

  • extensions?: {extensionName: filename => null|config} - a plain object that map each extension name (e.g. .js) to a loader function. The loader function should return null if filename doesn't exist. By default, the locator uses node-require-async to load .js and .json files.

    The loader function may return a promise.

  • race?: Boolean - by default, when finding multiple configs in the same directory, the locator reads the file in parallel (but ordered). If a config file is found (i.e. the loader returns a truthy value), the locator returns the config immediately.

    If race is false, the locator would wait for all loaders to finish and return the first found config.

    This option has no effect if config has only one item or findAll is true.

    Default: true.

locator has following methods:

  • async findConfig(filename) => null|result|Array<result> - find the config for file. It's a shortcut to searchDir(path.dirname(file)).

  • async searchDir(dirname) => null|result|Array<result> - start searching the config from dirname.

    result is an object {filename, config} that filename is the filename of the config and config is the object returned by the loader function registered in options.extensions.

    If options.findAll is true then it would be an array of result objects.

  • clearCache() - clear the cache. Note that this function only clears the cache of config locator, you may want to remove the module from require.cache that is created by node-require-async.

  • async close() - make sure all files are closed i.e. all loaders have finished.

Changelog

  • 0.1.0 (Jun 27, 2018)

    • First release.