data-store
Advanced tools
Comparing version 3.0.2 to 3.0.3
@@ -35,8 +35,22 @@ # Release history | ||
## [3.0.3] - 2017-05-22 | ||
## [2.0.0] - 2017-05-22 | ||
- Improvements to debounce for `.save()` | ||
- Use XDG directory | ||
- Merge pull request #8 from jamen/master | ||
**Deprecated** | ||
- `.deleteFile()` will be removed in the next major release | ||
## [3.0.2] - 2017-05-22 | ||
**Fixed** | ||
- Merge pull request #13 from nytamin/master - adds typings, `EPERM` error handling for Windows | ||
## [2.0.0] - 2018-05-01 | ||
**Added** | ||
- Merge pull request #8 from jamen/master - use XDG directory | ||
## [1.0.0] - 2017-05-22 | ||
@@ -43,0 +57,0 @@ |
87
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
const assert = require('assert'); | ||
const XDG_CONFIG_HOME = process.env.XDG_CONFIG_HOME; | ||
const flatten = (...args) => [].concat.apply([], args); | ||
@@ -37,3 +38,3 @@ const unique = arr => arr.filter((v, i) => arr.indexOf(v) === i); | ||
assert.equal(typeof name, 'string', 'expected store name to be a string'); | ||
const { debounce = 5, indent = 2, home, base } = options; | ||
let { debounce = 5, indent = 2, home, base } = options; | ||
if (!base && options.cwd) base = options.cwd; | ||
@@ -43,7 +44,8 @@ this.name = name; | ||
this.defaults = defaults || options.default; | ||
this.debounce = debounce; | ||
this.indent = indent; | ||
this.debounce = debounce; | ||
this.home = home || process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config'); | ||
this.home = home || XDG_CONFIG_HOME || path.join(os.homedir(), '.config'); | ||
this.base = base || path.join(this.home, 'data-store'); | ||
this.path = this.options.path || path.join(this.base, this.name + '.json'); | ||
this.path = this.options.path || path.join(this.base, `${this.name}.json`); | ||
this.timeouts = {}; | ||
} | ||
@@ -226,3 +228,3 @@ | ||
/** | ||
* Reset `store.data` to an empty object. | ||
* Clear `store.data` to an empty object. | ||
* | ||
@@ -271,38 +273,9 @@ * ```js | ||
save() { | ||
if (!this.debounce) return this.writeFile(); | ||
if (this.save.debounce) return; | ||
this.save.debounce = setTimeout(() => this.writeFile(), this.debounce); | ||
const write = this.writeFile.bind(this); | ||
if (!this.debounce) return write(); | ||
if (this.timeouts.save) clearTimeout(this.timeouts.save); | ||
this.timeouts.save = setTimeout(write, this.debounce); | ||
} | ||
/** | ||
* Delete the store from the file system. | ||
* | ||
* ```js | ||
* store.unlink(); | ||
* ``` | ||
* @name .unlink | ||
* @return {undefined} | ||
* @api public | ||
*/ | ||
unlink() { | ||
let wait = 0; | ||
if (this.unlink.clear) this.unlink.clear(); | ||
const debounce = () => { | ||
const timeout = setTimeout(() => { | ||
if (this.save.debounce) { | ||
debounce(); | ||
} else { | ||
this.deleteFile(); | ||
} | ||
}, wait++); | ||
return () => clearTimeout(timeout); | ||
}; | ||
this.unlink.clear = debounce(); | ||
} | ||
/** | ||
* Immediately write the store to the file system. This method should probably | ||
@@ -320,8 +293,3 @@ * not be called directly. Unless you are familiar with the inner workings of | ||
writeFile() { | ||
if (this.save.debounce) { | ||
clearTimeout(this.save.debounce); | ||
this.save.debounce = null; | ||
} | ||
if (!this.saved) mkdir(path.dirname(this.path), this.options.mkdir); | ||
this.saved = true; | ||
mkdir(path.dirname(this.path), this.options.mkdir); | ||
fs.writeFileSync(this.path, this.json(), { mode: 0o0600 }); | ||
@@ -331,18 +299,22 @@ } | ||
/** | ||
* Immediately delete the store from the file system. This method should probably | ||
* not be called directly. Unless you are familiar with the inner workings of | ||
* the code, it's recommended that you use .unlink() instead. | ||
* Delete the store from the file system. | ||
* | ||
* ```js | ||
* store.deleteFile(); | ||
* store.unlink(); | ||
* ``` | ||
* @name .deleteFile | ||
* @name .unlink | ||
* @return {undefined} | ||
* @api public | ||
*/ | ||
deleteFile() { | ||
if (this.unlink.clear) this.unlink.clear(); | ||
unlink() { | ||
clearTimeout(this.timeouts.save); | ||
tryUnlink(this.path); | ||
} | ||
// DEPRECATED: will be removed in the next major release | ||
deleteFile() { | ||
return this.unlink(); | ||
} | ||
/** | ||
@@ -395,8 +367,2 @@ * Load the store. | ||
/** | ||
* Utils | ||
*/ | ||
const mode = opts => opts.mode || 0o777 & ~process.umask(); | ||
/** | ||
* Create a directory and any intermediate directories that might exist. | ||
@@ -406,6 +372,8 @@ */ | ||
function mkdir(dirname, options = {}) { | ||
if (fs.existsSync(dirname)) return; | ||
assert.equal(typeof dirname, 'string', 'expected dirname to be a string'); | ||
const opts = Object.assign({ cwd: process.cwd(), fs }, options); | ||
const mode = opts.mode || 0o777 & ~process.umask(); | ||
const segs = path.relative(opts.cwd, dirname).split(path.sep); | ||
const make = dir => fs.mkdirSync(dir, mode(opts)); | ||
const make = dir => fs.mkdirSync(dir, mode); | ||
for (let i = 0; i <= segs.length; i++) { | ||
@@ -485,3 +453,4 @@ try { | ||
/** | ||
* Deeply clone plain objects and arrays. | ||
* Deeply clone plain objects and arrays. We're only concerned with | ||
* cloning values that are valid in JSON. | ||
*/ | ||
@@ -488,0 +457,0 @@ |
{ | ||
"name": "data-store", | ||
"description": "Easily persist and load config data. No dependencies.", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"homepage": "https://github.com/jonschlinkert/data-store", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
@@ -49,3 +49,3 @@ # data-store [![NPM version](https://img.shields.io/npm/v/data-store.svg?style=flat)](https://www.npmjs.com/package/data-store) [![NPM monthly downloads](https://img.shields.io/npm/dm/data-store.svg?style=flat)](https://npmjs.org/package/data-store) [![NPM total downloads](https://img.shields.io/npm/dt/data-store.svg?style=flat)](https://npmjs.org/package/data-store) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/data-store.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/data-store) | ||
### [Store](index.js#L27) | ||
### [Store](index.js#L28) | ||
@@ -70,3 +70,3 @@ Initialize a new `Store` with the given `name`, `options` and `default` data. | ||
### [.set](index.js#L68) | ||
### [.set](index.js#L70) | ||
@@ -93,3 +93,3 @@ Assign `value` to `key` and save to the file system. Can be a key-value pair, array of objects, or an object. | ||
### [.union](index.js#L98) | ||
### [.union](index.js#L100) | ||
@@ -115,3 +115,3 @@ Add the given `value` to the array at `key`. Creates a new array if one doesn't exist, and only adds unique values to the array. | ||
### [.get](index.js#L124) | ||
### [.get](index.js#L126) | ||
@@ -136,3 +136,3 @@ Get the stored `value` of `key`. | ||
### [.has](index.js#L146) | ||
### [.has](index.js#L148) | ||
@@ -157,3 +157,3 @@ Returns `true` if the specified `key` has a value. | ||
### [.hasOwn](index.js#L174) | ||
### [.hasOwn](index.js#L176) | ||
@@ -184,3 +184,3 @@ Returns `true` if the specified `key` exists. | ||
### [.del](index.js#L195) | ||
### [.del](index.js#L197) | ||
@@ -204,3 +204,3 @@ Delete one or more properties from the store. | ||
### [.clone](index.js#L218) | ||
### [.clone](index.js#L220) | ||
@@ -217,5 +217,5 @@ Return a clone of the `store.data` object. | ||
### [.clear](index.js#L233) | ||
### [.clear](index.js#L235) | ||
Reset `store.data` to an empty object. | ||
Clear `store.data` to an empty object. | ||
@@ -230,3 +230,3 @@ * `returns` **{undefined}** | ||
### [.json](index.js#L249) | ||
### [.json](index.js#L251) | ||
@@ -243,3 +243,3 @@ Stringify the store. Takes the same arguments as `JSON.stringify`. | ||
### [.save](index.js#L266) | ||
### [.save](index.js#L268) | ||
@@ -256,3 +256,3 @@ Calls [.writeFile()](#writefile) to persist the store to the file system, after an optional [debounce](#options) period. This method should probably not be called directly as it's used internally by other methods. | ||
### [.unlink](index.js#L283) | ||
### [.unlink](index.js#L303) | ||
@@ -326,3 +326,3 @@ Delete the store from the file system. | ||
| --- | --- | | ||
| 156 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 159 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 4 | [doowb](https://github.com/doowb) | | ||
@@ -329,0 +329,0 @@ | 3 | [nytamin](https://github.com/nytamin) | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31901
443