JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 122997
  • Score
    100M100P100Q156355F
  • License MIT

Find the first config file matching a given name in the current directory or the nearest ancestor directory.

Package Exports

  • find-config

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (find-config) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

find-config

NPM version Downloads Build Status Coverage Status Chat

Finds the first matching config file, if any, in the current directory or the nearest ancestor directory. Supports finding files within a subdirectroy of an ancestor directory. Configurable with defaults set to support the XDG Base Directory Specification for configuration files.

Algorithm

Where X is the current directory:

  1. If X/file.ext exists, load it. STOP
  2. If X/.dir/file.ext exists, load it. STOP
  3. Change X to parent directory. GO TO 1

Install

With Node.js:

$ npm install find-config

Api

findConfig(filename, [options]) : String|Null

  • filename String - Name of the configuration file to find.
  • options {Object=}
    • cwd {String=} - Directory in which to start looking. (Default: process.cwd()).
    • dir {String=} - An optional subdirectory to check at each level. (Default: .config).
    • asModule {Boolean} - Whether to use Node.js module resolution. (Default: false).
    • keepDot {Boolean} - Whether to keep the leading dot in the filename for matches in a subdirectory. (Default: false).

Synchronously find the first config file matching a given name in the current directory or the nearest ancestor directory.

var findConfig = require('find-config');

// Find the nearest `package.json`
var pkg = findConfig('package.json');

// Find the nearest `.foorc` or `.config/foorc`
var foo = findConfig('.foorc');

// Find the nearest `.foorc` or `.config/.foorc`
var foo = findConfig('.foorc', { keepDot: true });

// Find the nearest module using Node.js module resolution.
// Will look for `bar.js` or `bar/index.js`, etc.
var foo = findConfig('bar', { asModule: true });

// Find the nearest `baz.json` or `some/path/baz.json`
var foo = findConfig('baz.json', { dir: 'some/path' });

// Find the nearest `qux.json` or `some/path/qux.json` in
// some other directory or its nearest ancestor directory.
var foo = findConfig('qux.json', { cwd: '/other/dir', dir: 'some/path' });

findConfig.read(filename, [options]) : String|Null

  • filename String - Name of the configuration file to find.
  • options {Object=} - Same as findConfig().
    • encoding {String} - File encoding. (Default: 'utf8').
    • flag {String} - Flag. (Default: 'r').

Finds and reads the first matching config file, if any.

var yaml = require('js-yaml');
var travis = yaml.safeLoad(findConfig.read('.travis.yml'));

findConfig.require(filename, [options]) : *

  • filename String - Name of the configuration file to find.
  • options {Object=} - Same as findConfig().

Finds and requires the first matching config file, if any. Implies asModule is true.

var version = findConfig.require('package.json').version;

Contribute

Tasks Tip

Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.

Test

$ npm test

© 2015 Shannon Moeller me@shannonmoeller.com

Licensed under MIT