-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeppe Hasseriis
committed
Jan 14, 2020
1 parent
5cf2aff
commit 02081fa
Showing
5 changed files
with
177 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,73 @@ | ||
# rcconfig | ||
Find and load json configuration from a package.json property, rc file, or CommonJS module | ||
# rcload | ||
|
||
Find and load json configuration from a package.json property, rc file, or CommonJS module. | ||
|
||
Heavily inspired by [cosmiconfig](https://www.npmjs.com/package/cosmiconfig), but super simplified for minimal bundlesize. | ||
The big difference from cosmiconfig is that **rcload does not support yaml files**. | ||
|
||
**rcload** will search for the following: | ||
|
||
- a `package.json` property | ||
- a JSON extensionless "rc file" | ||
- an "rc file" with the extensions `.json` or `.js` | ||
- a `.config.js` CommonJS module | ||
|
||
It is also meant as a partial drop-in replacement which means it return a `result` object same as cosmiconfig. | ||
|
||
## Usage | ||
|
||
Install as a dependency. | ||
|
||
``` | ||
npm i rcload | ||
``` | ||
|
||
Use in your application. | ||
|
||
``` | ||
const rcload = require('rcload'); | ||
const result = rcload('myapp'); | ||
``` | ||
|
||
## Result | ||
|
||
The result object has the following properties: | ||
|
||
- `config`: The parsed configuration object. `undefined` if the file is empty. | ||
- `filepath`: The path to the configuration file that was found. | ||
- `isEmpty`: true if the configuration file is empty. | ||
|
||
In contrast to cosmiconfig, `isEmpty` will remain in the result object when the config is found, e.g.: | ||
|
||
``` | ||
const result = rcload('myapp'); | ||
// { | ||
// config: { | ||
// ... | ||
// }, | ||
// filepath: "/users/johndoe/www/myapp/.myapprc", | ||
// isEmpty: false | ||
// } | ||
``` | ||
|
||
## Options | ||
|
||
**rcload** takes an options object: | ||
|
||
- `cwd`: the full directory path to search in. Common for `package.json` and rc files. Defaults to `process.cwd()`. | ||
|
||
If you want your config files in another directory this will help, e.g.: | ||
|
||
``` | ||
rcload('myapp', { | ||
cwd: path.join(process.cwd(), 'configs') | ||
}) | ||
``` | ||
|
||
## Differences from [cosmiconfig](https://www.npmjs.com/package/cosmiconfig) | ||
|
||
- Supports only JSON and CommonJS formats. | ||
- Only looks in `process.cwd()`. | ||
- Limited options. | ||
- Only synchronous load. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const path = require("path"); | ||
const envDebug = Boolean(process.env.DEBUG); | ||
|
||
function debug() { | ||
envDebug && console.debug.apply(this, arguments); | ||
} | ||
|
||
function rcload(key, opts) { | ||
const options = { | ||
cwd: (opts && opts.cwd) || process.cwd() | ||
}; | ||
const result = { | ||
config: undefined, | ||
filepath: undefined, | ||
isEmpty: true | ||
}; | ||
|
||
const hostPkgPath = path.join(options.cwd, "package.json"); | ||
let hostPkg; | ||
try { | ||
hostPkg = require(hostPkgPath); | ||
} catch (e) { | ||
hostPkg = false; | ||
} | ||
|
||
if (hostPkg && hostPkg.hasOwnProperty(key)) { | ||
result.isEmpty = false; | ||
result.config = hostPkg[key]; | ||
result.file = hostPkgPath; | ||
} else { | ||
debug(`"${key}" not found in ${hostPkgPath}`); | ||
} | ||
|
||
if (result.isEmpty) { | ||
const files = [ | ||
`.${key}rc`, | ||
`.${key}rc.json`, | ||
`.${key}rc.js`, | ||
`${key}.config.js` | ||
]; | ||
let i = 0; | ||
let fileName; | ||
let filePath; | ||
let config; | ||
|
||
while (result.isEmpty && i < files.length) { | ||
fileName = files[i++]; | ||
filePath = path.join(options.cwd, fileName); | ||
try { | ||
config = require(filePath); | ||
result.isEmpty = false; | ||
} catch (e) { | ||
debug(`${filePath} not found.`); | ||
} | ||
} | ||
|
||
if (!result.isEmpty) { | ||
debug(`Using ${filePath}.`); | ||
result.file = filePath; | ||
const isFunction = typeof config === "function"; | ||
result.config = isFunction ? config() : config; | ||
} | ||
} | ||
|
||
if (!result.isEmpty) { | ||
delete result.isEmpty; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = rcload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "rcload", | ||
"version": "1.0.0", | ||
"description": "Find and load json configuration from a package.json property, rc file, or CommonJS module", | ||
"main": "lib/index.js", | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cenobitedk/rcconfig.git" | ||
}, | ||
"author": "Jeppe Hasseriis", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/cenobitedk/rcconfig/issues" | ||
}, | ||
"homepage": "https://github.com/cenobitedk/rcconfig#readme", | ||
"keywords": [ | ||
"rc", | ||
"config", | ||
"json" | ||
] | ||
} |