版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # RC9
  2. > Read/Write config couldn't be easier!
  3. [![npm version][npm-version-src]][npm-version-href]
  4. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  5. [![Github Actions][github-actions-src]][github-actions-href]
  6. [![Codecov][codecov-src]][codecov-href]
  7. ## Install
  8. Install using npm or yarn:
  9. ```bash
  10. npm i rc9
  11. # or
  12. yarn add rc9
  13. ```
  14. Import into your Node.js project:
  15. ```js
  16. // CommonJS
  17. const { read, write, update } = require('rc9')
  18. // ESM
  19. import { read, write, update } from 'rc9'
  20. ```
  21. ## Usage
  22. `.conf`:
  23. ```ts
  24. db.username=db username
  25. db.password=db pass
  26. db.enabled=true
  27. ```
  28. **Update config:**
  29. ```ts
  30. update({ 'db.enabled': true }) // or update(..., { name: '.conf' })
  31. ```
  32. Push to an array:
  33. ```ts
  34. update({ 'modules[]': 'test' })
  35. ```
  36. **Read/Write config:**
  37. ```ts
  38. const config = read() // or read('.conf')
  39. // config = {
  40. // db: {
  41. // username: 'db username',
  42. // password: 'db pass',
  43. // enabled: true
  44. // }
  45. // }
  46. config.enabled = false
  47. write(config) // or write(config, '.conf')
  48. ```
  49. **User Config:**
  50. It is common to keep config in user home directory (MacOS: `/Users/{name}`, Linux: `/home/{name}`, Windows: `C:\users\{name}`)
  51. you can use `readUser`/`writeuser`/`updateUser` shortcuts to quickly do this:
  52. ```js
  53. writeUser({ token: 123 }, '.zoorc') // Will be saved in {home}/.zoorc
  54. const conf = readUser('.zoorc') // { token: 123 }
  55. ```
  56. ## Unflatten
  57. RC uses [flat](https://www.npmjs.com/package/flat) to automatically flat/unflat when writing and reading rcfile.
  58. It means that you can use `.` for keys to define objects. Some examples:
  59. - `hello.world = true` <=> `{ hello: { world: true }`
  60. - `test.0 = A` <=> `tags: [ 'A' ]`
  61. **Note:** If you use keys that can override like `x=` and `x.y=`, you can disable this feature by passing `flat: true` option.
  62. **Tip:** You can use keys ending with `[]` to push to an array like `test[]=A`
  63. ## Native Values
  64. RC uses [destr](https://www.npmjs.com/package/destr) to convert values into native javascript values.
  65. So reading `count=123` results `{ count: 123 }` (instead of `{ count: "123" }`) if you want to preserve strings as is, can use `count="123"`.
  66. ## Exports
  67. ```ts
  68. const defaults: RCOptions;
  69. function parse(contents: string, options?: RCOptions): RC
  70. function parseFile(path: string, options?: RCOptions): RC
  71. function read(options?: RCOptions | string): RC;
  72. function readUser(options?: RCOptions | string): RC;
  73. function serialize(config: RC): string;
  74. function write(config: RC, options?: RCOptions | string): void;
  75. function writeUser(config: RC, options?: RCOptions | string): void;
  76. function update(config: RC, options?: RCOptions | string): RC;
  77. function updateUser(config: RC, options?: RCOptions | string): RC;
  78. ```
  79. **Types:**
  80. ```ts
  81. type RC = Record<string, any>;
  82. interface RCOptions {
  83. name?: string;
  84. dir?: string;
  85. flat?: boolean;
  86. }
  87. ```
  88. **Defaults:**
  89. ```ts
  90. {
  91. name: '.conf',
  92. dir: process.cwd(),
  93. flat: false
  94. }
  95. ```
  96. ### Why RC9?
  97. Be the first one to guess 🐇 <!-- Hint: do research about rc files history -->
  98. ## License
  99. MIT. Made with 💖
  100. <!-- Badges -->
  101. [npm-version-src]: https://img.shields.io/npm/v/rc9?style=flat-square
  102. [npm-version-href]: https://npmjs.com/package/rc9
  103. [npm-downloads-src]: https://img.shields.io/npm/dm/rc9?style=flat-square
  104. [npm-downloads-href]: https://npmjs.com/package/rc9
  105. [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/rc9/ci/main?style=flat-square
  106. [github-actions-href]: https://github.com/unjs/rc9/actions?query=workflow%3Aci
  107. [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/rc9/main?style=flat-square
  108. [codecov-href]: https://codecov.io/gh/unjs/rc9