Package Exports
- rc9
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 (rc9) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
RC9
Read/Write RC files couldn't be easier!
Install
Install using npm or yarn:
npm i rc9
# or
yarn add rc9
Import into your Node.js project:
// CommonJS
const { read, write } = require('rc9')
// ESM
import { read, write } from 'rc9'
Usage
.conf
:
db.username=db username
db.password=db pass
db.enabled=true
Reading config:
const config = read() // or read('.conf')
// config = {
// db: {
// username: 'db username',
// password: 'db pass',
// enabled: true
// }
// }
Update config:
config.enabled = false
write(config) // or write(config, '.conf')
User Config
It is common to read/write config from user directory, you can use readUser
/writeuser
shortcuts to quickly do this:
writeUser({ token: 123 }, '.zoorc')
const conf = readUser('.zoorc') // { token: 123 }
Unflatten
RC uses flat to automatically flat/unflat when writing and reading rcfile.
It means that you can use .
for keys to define objects. Some examples:
hello.world = true
<=>{ hello: { world: true }
test.0 = A
<=>tags: [ 'A' ]
Note: If you use keys that can override like x=
and x.y=
, you can disable this feature by passing unflatten: false
option
Native Values
RC uses destr to convert values into native javascript values.
So reading count=123
results { count: 123 }
(instead of { count: "123" }
) if you want to preserve strings as is, can use count="123"
.
Exports
type RC = {
[key: string]: any
}
interface RCOptions {
name?: string
dir?: string
unflatten?: boolean
}
const defaults: RCOptions = {
name: '.conf',
dir: process.cwd(),
unflatten: true
}
/**
* Parse rc contents
*/
function parse(contents: string, unflatten?: boolean): RC
/**
* Parse rc file
*/
function parseFile(path: string, unflatten?: boolean): RC
/**
* Read rc file
* @param name Name of rc file (default: '.conf')
* @param dir Working directory (default: process.cwd())
*/
function read(options?: RCOptions | string): RC
/**
* Read rc from user directory
* @param name Name of rc file (default: '.conf')
*/
function readUser(options?: RCOptions | string): RC
/**
* Serialize rc config
* @param config Unflatten config
*/
function serialize(config: RC, unflatten?: boolean): string
/**
* Write rc config
* @param config Unflatten config
* @param name Name of rc file (default: '.conf')
* @param dir Working directory (default: process.cwd())
*/
function write(config: RC, options?: RCOptions | string): void
/**
* Write rc from to user directory
* @param name Name of rc file (default: '.conf')
*/
function writeUser(config: RC, options?: RCOptions | string): void
Why RC9?
Be the first one to guess 🐇
License
MIT. Made with 💖