Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

readme.md 3.0KB

před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # configstore [![Build Status](https://travis-ci.org/yeoman/configstore.svg?branch=master)](https://travis-ci.org/yeoman/configstore)
  2. > Easily load and persist config without having to think about where and how
  3. The config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.<br>
  4. Example: `~/.config/configstore/some-id.json`
  5. *If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.*<br>
  6. *And check out [`conf`](https://github.com/sindresorhus/conf) for an updated approach to this concept.*
  7. ## Install
  8. ```
  9. $ npm install configstore
  10. ```
  11. ## Usage
  12. ```js
  13. const Configstore = require('configstore');
  14. const packageJson = require('./package.json');
  15. // Create a Configstore instance
  16. const config = new Configstore(packageJson.name, {foo: 'bar'});
  17. console.log(config.get('foo'));
  18. //=> 'bar'
  19. config.set('awesome', true);
  20. console.log(config.get('awesome'));
  21. //=> true
  22. // Use dot-notation to access nested properties
  23. config.set('bar.baz', true);
  24. console.log(config.get('bar'));
  25. //=> {baz: true}
  26. config.delete('awesome');
  27. console.log(config.get('awesome'));
  28. //=> undefined
  29. ```
  30. ## API
  31. ### Configstore(packageName, defaults?, options?)
  32. Returns a new instance.
  33. #### packageName
  34. Type: `string`
  35. Name of your package.
  36. #### defaults
  37. Type: `object`
  38. Default config.
  39. #### options
  40. Type: `object`
  41. ##### globalConfigPath
  42. Type: `boolean`<br>
  43. Default: `false`
  44. Store the config at `$CONFIG/package-name/config.json` instead of the default `$CONFIG/configstore/package-name.json`. This is not recommended as you might end up conflicting with other tools, rendering the "without having to think" idea moot.
  45. ##### configPath
  46. Type: `string`<br>
  47. Default: Automatic
  48. **Please don't use this option unless absolutely necessary and you know what you're doing.**
  49. Set the path of the config file. Overrides the `packageName` and `globalConfigPath` options.
  50. ### Instance
  51. You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties.
  52. ### .set(key, value)
  53. Set an item.
  54. ### .set(object)
  55. Set multiple items at once.
  56. ### .get(key)
  57. Get an item.
  58. ### .has(key)
  59. Check if an item exists.
  60. ### .delete(key)
  61. Delete an item.
  62. ### .clear()
  63. Delete all items.
  64. ### .size
  65. Get the item count.
  66. ### .path
  67. Get the path to the config file. Can be used to show the user where the config file is located or even better open it for them.
  68. ### .all
  69. Get all the config as an object or replace the current config with an object:
  70. ```js
  71. config.all = {
  72. hello: 'world'
  73. };
  74. ```
  75. ---
  76. <div align="center">
  77. <b>
  78. <a href="https://tidelift.com/subscription/pkg/npm-configstore?utm_source=npm-configstore&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  79. </b>
  80. <br>
  81. <sub>
  82. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  83. </sub>
  84. </div>