diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa1ec1e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.tgz diff --git a/LICENSE b/LICENSE index a4c6947..b3031e5 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 6eb3105..f813bce 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..b51029b --- /dev/null +++ b/lib/index.js @@ -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; diff --git a/package.json b/package.json new file mode 100644 index 0000000..077eebb --- /dev/null +++ b/package.json @@ -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" + ] +}