pax_global_header00006660000000000000000000000064140325455770014526gustar00rootroot0000000000000052 comment=02f07ead1cbbc1c8b49e3c9ba6e7a4896594368d configstore-6.0.0/000077500000000000000000000000001403254557700140535ustar00rootroot00000000000000configstore-6.0.0/.editorconfig000066400000000000000000000002571403254557700165340ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 configstore-6.0.0/.gitattributes000066400000000000000000000000231403254557700167410ustar00rootroot00000000000000* text=auto eol=lf configstore-6.0.0/.github/000077500000000000000000000000001403254557700154135ustar00rootroot00000000000000configstore-6.0.0/.github/funding.yml000066400000000000000000000000571403254557700175720ustar00rootroot00000000000000github: sindresorhus tidelift: npm/configstore configstore-6.0.0/.github/security.md000066400000000000000000000002631403254557700176050ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. configstore-6.0.0/.github/workflows/000077500000000000000000000000001403254557700174505ustar00rootroot00000000000000configstore-6.0.0/.github/workflows/main.yml000066400000000000000000000006451403254557700211240ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test configstore-6.0.0/.gitignore000066400000000000000000000000271403254557700160420ustar00rootroot00000000000000node_modules yarn.lock configstore-6.0.0/.npmrc000066400000000000000000000000231403254557700151660ustar00rootroot00000000000000package-lock=false configstore-6.0.0/index.js000066400000000000000000000045501403254557700155240ustar00rootroot00000000000000import path from 'path'; import os from 'os'; import fs from 'graceful-fs'; import {xdgConfig} from 'xdg-basedir'; import writeFileAtomic from 'write-file-atomic'; import dotProp from 'dot-prop'; import uniqueString from 'unique-string'; const configDirectory = xdgConfig || path.join(os.tmpdir(), uniqueString()); const permissionError = 'You don\'t have access to this file.'; const mkdirOptions = {mode: 0o0700, recursive: true}; const writeFileOptions = {mode: 0o0600}; export default class Configstore { constructor(id, defaults, options = {}) { const pathPrefix = options.globalConfigPath ? path.join(id, 'config.json') : path.join('configstore', `${id}.json`); this._path = options.configPath || path.join(configDirectory, pathPrefix); if (defaults) { this.all = { ...defaults, ...this.all }; } } get all() { try { return JSON.parse(fs.readFileSync(this._path, 'utf8')); } catch (error) { // Create directory if it doesn't exist if (error.code === 'ENOENT') { return {}; } // Improve the message of permission errors if (error.code === 'EACCES') { error.message = `${error.message}\n${permissionError}\n`; } // Empty the file if it encounters invalid JSON if (error.name === 'SyntaxError') { writeFileAtomic.sync(this._path, '', writeFileOptions); return {}; } throw error; } } set all(value) { try { // Make sure the folder exists as it could have been deleted in the meantime fs.mkdirSync(path.dirname(this._path), mkdirOptions); writeFileAtomic.sync(this._path, JSON.stringify(value, undefined, '\t'), writeFileOptions); } catch (error) { // Improve the message of permission errors if (error.code === 'EACCES') { error.message = `${error.message}\n${permissionError}\n`; } throw error; } } get size() { return Object.keys(this.all || {}).length; } get(key) { return dotProp.get(this.all, key); } set(key, value) { const config = this.all; if (arguments.length === 1) { for (const k of Object.keys(key)) { dotProp.set(config, k, key[k]); } } else { dotProp.set(config, key, value); } this.all = config; } has(key) { return dotProp.has(this.all, key); } delete(key) { const config = this.all; dotProp.delete(config, key); this.all = config; } clear() { this.all = {}; } get path() { return this._path; } } configstore-6.0.0/license000066400000000000000000000024311403254557700154200ustar00rootroot00000000000000BSD 2-Clause License Copyright (c) Google All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. configstore-6.0.0/package.json000066400000000000000000000016711403254557700163460ustar00rootroot00000000000000{ "name": "configstore", "version": "6.0.0", "description": "Easily load and save config without having to think about where and how", "license": "BSD-2-Clause", "repository": "yeoman/configstore", "funding": "https://github.com/yeoman/configstore?sponsor=1", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": ">=12" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "config", "store", "storage", "configuration", "settings", "preferences", "json", "data", "persist", "persistent", "save" ], "dependencies": { "dot-prop": "^6.0.1", "graceful-fs": "^4.2.6", "unique-string": "^3.0.0", "write-file-atomic": "^3.0.3", "xdg-basedir": "^5.0.1" }, "devDependencies": { "ava": "^3.15.0", "xo": "^0.38.2" }, "ava": { "serial": true } } configstore-6.0.0/readme.md000066400000000000000000000055421403254557700156400ustar00rootroot00000000000000# configstore > Easily load and persist config without having to think about where and how The config is stored in a JSON file located in `$XDG_CONFIG_HOME` or `~/.config`.\ Example: `~/.config/configstore/some-id.json` *If you need this for Electron, check out [`electron-store`](https://github.com/sindresorhus/electron-store) instead.*\ *And check out [`conf`](https://github.com/sindresorhus/conf) for a more modern version of `configstore`.* ## Install ``` $ npm install configstore ``` ## Usage ```js import Configstore from 'configstore'; const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); // Create a Configstore instance. const config = new Configstore(packageJson.name, {foo: 'bar'}); console.log(config.get('foo')); //=> 'bar' config.set('awesome', true); console.log(config.get('awesome')); //=> true // Use dot-notation to access nested properties. config.set('bar.baz', true); console.log(config.get('bar')); //=> {baz: true} config.delete('awesome'); console.log(config.get('awesome')); //=> undefined ``` ## API ### Configstore(packageName, defaults?, options?) Returns a new instance. #### packageName Type: `string` Name of your package. #### defaults Type: `object` Default config. #### options Type: `object` ##### globalConfigPath Type: `boolean`\ Default: `false` 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. ##### configPath Type: `string`\ Default: Automatic **Please don't use this option unless absolutely necessary and you know what you're doing.** Set the path of the config file. Overrides the `packageName` and `globalConfigPath` options. ### Instance You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a `key` to access nested properties. ### .set(key, value) Set an item. ### .set(object) Set multiple items at once. ### .get(key) Get an item. ### .has(key) Check if an item exists. ### .delete(key) Delete an item. ### .clear() Delete all items. ### .size Get the item count. ### .path 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. ### .all Get all the config as an object or replace the current config with an object: ```js config.all = { hello: 'world' }; ``` ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
configstore-6.0.0/test.js000066400000000000000000000073461403254557700154020ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import os from 'os'; import test from 'ava'; import Configstore from './index.js'; const configstorePath = new Configstore('configstore-test').path; const cleanUpFile = () => { if (fs.existsSync(configstorePath)) { fs.unlinkSync(configstorePath); } }; test.beforeEach(t => { cleanUpFile(); t.context.config = new Configstore('configstore-test'); }); test('.set() and .get()', t => { const {config} = t.context; config.set('foo', 'bar'); config.set('baz.boo', true); t.is(config.get('foo'), 'bar'); t.is(config.get('baz.boo'), true); }); test('.set() with object and .get()', t => { const {config} = t.context; config.set({ foo1: 'bar1', foo2: 'bar2', baz: { boo: 'foo', foo: { bar: 'baz' } } }); t.is(config.get('foo1'), 'bar1'); t.is(config.get('foo2'), 'bar2'); t.deepEqual(config.get('baz'), { boo: 'foo', foo: { bar: 'baz' } }); t.is(config.get('baz.boo'), 'foo'); t.deepEqual(config.get('baz.foo'), {bar: 'baz'}); t.is(config.get('baz.foo.bar'), 'baz'); }); test('.has()', t => { const {config} = t.context; config.set('foo', '🦄'); config.set('baz.boo', '🦄'); t.true(config.has('foo')); t.true(config.has('baz.boo')); t.false(config.has('missing')); }); test('.delete()', t => { const {config} = t.context; config.set('foo', 'bar'); config.set('baz.boo', true); config.set('baz.foo.bar', 'baz'); config.delete('foo'); t.not(config.get('foo'), 'bar'); config.delete('baz.boo'); t.not(config.get('baz.boo'), true); config.delete('baz.foo'); t.not(config.get('baz.foo'), {bar: 'baz'}); config.set('foo.bar.baz', {awesome: 'icecream'}); config.set('foo.bar.zoo', {awesome: 'redpanda'}); config.delete('foo.bar.baz'); t.is(config.get('foo.bar.zoo.awesome'), 'redpanda'); }); test('.clear()', t => { const {config} = t.context; config.set('foo', 'bar'); config.set('foo1', 'bar1'); config.set('baz.boo', true); config.clear(); t.is(config.size, 0); }); test('.all', t => { const {config} = t.context; config.set('foo', 'bar'); config.set('baz.boo', true); t.is(config.all.foo, 'bar'); t.deepEqual(config.all.baz, {boo: true}); }); test('.size', t => { const {config} = t.context; config.set('foo', 'bar'); t.is(config.size, 1); }); test('.path', t => { const {config} = t.context; config.set('foo', 'bar'); t.true(fs.existsSync(config.path)); }); test('use default value', t => { const config = new Configstore('configstore-test', {foo: 'bar'}); t.is(config.get('foo'), 'bar'); }); test('support `globalConfigPath` option', t => { const config = new Configstore('configstore-test', {}, {globalConfigPath: true}); t.regex(config.path, /configstore-test(\/|\\)config.json$/); }); test('support `configPath` option', t => { const customPath = path.join(os.tmpdir(), 'configstore-custom-path', 'foo.json'); const config = new Configstore('ignored-namespace', {}, { globalConfigPath: true, configPath: customPath }); t.regex(config.path, /configstore-custom-path(\/|\\)foo.json$/); }); test('ensure `.all` is always an object', t => { cleanUpFile(); t.notThrows(() => { t.context.config.get('foo'); }); }); test('the store is NOT created until write', t => { cleanUpFile(); const config = new Configstore('configstore-test'); t.false(fs.existsSync(config.path)); config.set('foo', 'bar'); t.true(fs.existsSync(config.path)); }); test('ensure necessary sub-directories are created', t => { const customPath = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'configstore-recursive-')), 'foo', 'bar', 'baz.json'); const config = new Configstore('ignored-namespace', undefined, { globalConfigPath: true, configPath: customPath }); t.false(fs.existsSync(config.path)); config.set('foo', 'bar'); t.true(fs.existsSync(config.path)); });