File size: 4,074 Bytes
699ccfe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | # RC9
<!-- automd:badges color=yellow codecov bundlejs -->
[](https://npmjs.com/package/rc9)
[](https://npmjs.com/package/rc9)
[](https://bundlejs.com/?q=rc9)
[](https://codecov.io/gh/unjs/rc9)
<!-- /automd -->
Read/Write RC configs couldn't be easier!
## Install
Install dependencies:
<!-- automd:pm-i -->
```sh
# ✨ Auto-detect
npx nypm install rc9
# npm
npm install rc9
# yarn
yarn add rc9
# pnpm
pnpm install rc9
# bun
bun install rc9
```
<!-- /automd -->
Import utils:
<!-- automd:jsimport cjs src="./src/index.ts"-->
**ESM** (Node.js, Bun)
```js
import {
defaults,
parse,
parseFile,
read,
readUser,
serialize,
write,
writeUser,
update,
updateUser,
} from "rc9";
```
**CommonJS** (Legacy Node.js)
```js
const {
defaults,
parse,
parseFile,
read,
readUser,
serialize,
write,
writeUser,
update,
updateUser,
} = require("rc9");
```
<!-- /automd -->
## Usage
`.conf`:
```ini
db.username=username
db.password=multi word password
db.enabled=true
```
**Update config:**
```ts
update({ 'db.enabled': false }) // or update(..., { name: '.conf' })
```
Push to an array:
```ts
update({ 'modules[]': 'test' })
```
**Read/Write config:**
```ts
const config = read() // or read('.conf')
// config = {
// db: {
// username: 'username',
// password: 'multi word password',
// enabled: true
// }
// }
config.enabled = false
write(config) // or write(config, '.conf')
```
**User Config:**
It is common to keep config in user home directory (MacOS: `/Users/{name}`, Linux: `/home/{name}`, Windows: `C:\users\{name}`)
you can use `readUser`/`writeuser`/`updateUser` shortcuts to quickly do this:
```js
writeUser({ token: 123 }, '.zoorc') // Will be saved in {home}/.zoorc
const conf = readUser('.zoorc') // { token: 123 }
```
## Unflatten
RC uses [flat](https://www.npmjs.com/package/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 `flat: true` option.
**Tip:** You can use keys ending with `[]` to push to an array like `test[]=A`
## Native Values
RC uses [destr](https://www.npmjs.com/package/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
```ts
const defaults: RCOptions;
function parse(contents: string, options?: RCOptions): RC
function parseFile(path: string, options?: RCOptions): RC
function read(options?: RCOptions | string): RC;
function readUser(options?: RCOptions | string): RC;
function serialize(config: RC): string;
function write(config: RC, options?: RCOptions | string): void;
function writeUser(config: RC, options?: RCOptions | string): void;
function update(config: RC, options?: RCOptions | string): RC;
function updateUser(config: RC, options?: RCOptions | string): RC;
```
**Types:**
```ts
type RC = Record<string, any>;
interface RCOptions {
name?: string;
dir?: string;
flat?: boolean;
}
```
**Defaults:**
```ini
{
name: '.conf',
dir: process.cwd(),
flat: false
}
```
### Why RC9?
Be the first one to guess 🐇 <!-- Hint: do research about rc files history -->
## License
<!-- automd:contributors license=MIT -->
Published under the [MIT](https://github.com/unjs/rc9/blob/main/LICENSE) license.
Made by [community](https://github.com/unjs/rc9/graphs/contributors) 💛
<br><br>
<a href="https://github.com/unjs/rc9/graphs/contributors">
<img src="https://contrib.rocks/image?repo=unjs/rc9" />
</a>
<!-- /automd -->
|