Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeppe Hasseriis committed Jan 14, 2020
1 parent 5cf2aff commit 02081fa
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.tgz
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Jeppe
Copyright (c) 2020 Jeppe Hasseriis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
75 changes: 73 additions & 2 deletions README.md
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.
72 changes: 72 additions & 0 deletions lib/index.js
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;
30 changes: 30 additions & 0 deletions package.json
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"
]
}

0 comments on commit 02081fa

Please sign in to comment.