Comparing version 0.2.2 to 1.0.0
154
index.js
/*! | ||
* pkg-store <https://github.com/jonschlinkert/pkg-store> | ||
* | ||
* Copyright (c) 2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2015, 2017, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,8 +10,6 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var cache = require('cache-base'); | ||
var Cache = cache.namespace('data'); | ||
var utils = require('./utils'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Cache = require('cache-base'); | ||
const write = require('write-json'); | ||
@@ -23,3 +21,6 @@ /** | ||
* ```js | ||
* var pkg = require('pkg-store')(process.cwd()); | ||
* const pkg = require('pkg-store')(process.cwd()); | ||
* const pkg = new Pkg(cwd, options); | ||
* // or | ||
* const pkg = new Pkg(options); | ||
* | ||
@@ -30,90 +31,79 @@ * console.log(pkg.path); | ||
* console.log(pkg.data); | ||
* //=> {name: 'your-project', ...} | ||
* //=> { name: 'your-project', ... } | ||
* ``` | ||
* | ||
* @name Pkg | ||
* @param {String} `cwd` Directory of the package.json to read. | ||
* @param {Object} `options` Options to pass to [expand-pkg][] and [normalize-pkg][]. | ||
* @param {Object} `options` | ||
* @api public | ||
*/ | ||
function Pkg(cwd, options) { | ||
if (!(this instanceof Pkg)) { | ||
return new Pkg(cwd, options); | ||
class Pkg extends Cache { | ||
constructor(cwd, options) { | ||
super('cache'); | ||
if (typeof cwd !== 'string') { | ||
options = cwd; | ||
cwd = process.cwd(); | ||
} | ||
this.options = Object.assign({cwd: cwd}, options); | ||
this.cwd = path.resolve(this.options.cwd); | ||
this.path = this.options.path || path.resolve(this.cwd, 'package.json'); | ||
} | ||
Cache.call(this); | ||
/** | ||
* Get and set `pkg.data`. | ||
* | ||
* ```js | ||
* console.log(pkg.data); | ||
* ``` | ||
* @name .data | ||
* @api public | ||
*/ | ||
if (utils.isObject(cwd)) { | ||
options = cwd; | ||
cwd = null; | ||
set data(val) { | ||
this.cache = val; | ||
} | ||
get data() { | ||
return this.cache || (this.cache = this.read()); | ||
} | ||
this.options = options || {}; | ||
cwd = this.options.cwd || cwd || process.cwd(); | ||
this.path = this.options.path || path.resolve(cwd, 'package.json'); | ||
var data; | ||
/** | ||
* Write the `pkg.data` object to the file system at `pkg.path`. | ||
* | ||
* ```js | ||
* pkg.save(); | ||
* ``` | ||
* @name .save | ||
* @param {Function} `callback` (optional) | ||
* @api public | ||
*/ | ||
Object.defineProperty(this, 'data', { | ||
configurable: true, | ||
enumerable: true, | ||
set: function(val) { | ||
data = val; | ||
}, | ||
get: function() { | ||
return data || (data = this.read()); | ||
save(cb) { | ||
if (typeof cb !== 'function') { | ||
write.sync(this.path, this.data); | ||
return; | ||
} | ||
}); | ||
} | ||
write(this.path, this.data, cb); | ||
return this; | ||
} | ||
/** | ||
* Inherit `cache-base` | ||
*/ | ||
/** | ||
* Reads `pkg.path` from the file system and returns an object. | ||
* | ||
* ```js | ||
* const data = pkg.read(); | ||
* ``` | ||
* @name .read | ||
* @return {undefined} | ||
* @api public | ||
*/ | ||
util.inherits(Pkg, Cache); | ||
/** | ||
* Concatenate the given val and uniquify array `key`. | ||
* | ||
* ```js | ||
* pkg.union('keywords', ['foo', 'bar', 'baz']); | ||
* ``` | ||
* @param {String} `key` | ||
* @param {String} `val` Item or items to add to the array. | ||
* @return {Object} Returns the instance for chaining. | ||
* @api public | ||
*/ | ||
Pkg.prototype.union = function(key, val) { | ||
utils.union(this.data, key, val); | ||
return this; | ||
}; | ||
/** | ||
* Write the `pkg.data` object to `pkg.path` on the file system. | ||
* | ||
* ```js | ||
* pkg.save(); | ||
* ``` | ||
* @api public | ||
*/ | ||
Pkg.prototype.save = function() { | ||
utils.writeJson.sync(this.path, this.data); | ||
}; | ||
/** | ||
* Reads `pkg.path` from the file system and returns an object. | ||
* | ||
* ```js | ||
* var data = pkg.read(); | ||
* ``` | ||
* @api public | ||
*/ | ||
Pkg.prototype.read = function() { | ||
if (utils.exists(this.path)) { | ||
return JSON.parse(fs.readFileSync(this.path, 'utf8')); | ||
read() { | ||
if (fs.existsSync(this.path)) { | ||
return JSON.parse(fs.readFileSync(this.path, 'utf8')); | ||
} | ||
return {}; | ||
} | ||
return {}; | ||
}; | ||
} | ||
@@ -120,0 +110,0 @@ /** |
{ | ||
"name": "pkg-store", | ||
"description": "Use package.json as a config store.", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"homepage": "https://github.com/jonschlinkert/pkg-store", | ||
@@ -13,8 +13,7 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"files": [ | ||
"index.js", | ||
"utils.js" | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=6" | ||
}, | ||
@@ -25,12 +24,9 @@ "scripts": { | ||
"dependencies": { | ||
"cache-base": "^0.8.2", | ||
"kind-of": "^3.0.2", | ||
"lazy-cache": "^1.0.3", | ||
"union-value": "^0.2.3", | ||
"write-json": "^0.2.2" | ||
"cache-base": "^2.0.2", | ||
"write-json": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"delete": "^0.3.0", | ||
"gulp-format-md": "^0.1.7", | ||
"mocha": "^2.4.5" | ||
"delete": "^1.1.0", | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^3.5.3" | ||
}, | ||
@@ -43,4 +39,4 @@ "keywords": [ | ||
"package", | ||
"package-json", | ||
"package.json", | ||
"package-json", | ||
"pkg", | ||
@@ -64,11 +60,7 @@ "set", | ||
"data-store", | ||
"expand-pkg", | ||
"find-pkg", | ||
"expand-pkg", | ||
"normalize-pkg" | ||
] | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"cache-base" | ||
], | ||
"lint": { | ||
@@ -75,0 +67,0 @@ "reflinks": true |
137
README.md
@@ -1,5 +0,7 @@ | ||
# pkg-store [![NPM version](https://img.shields.io/npm/v/pkg-store.svg?style=flat)](https://www.npmjs.com/package/pkg-store) [![NPM downloads](https://img.shields.io/npm/dm/pkg-store.svg?style=flat)](https://npmjs.org/package/pkg-store) [![Build Status](https://img.shields.io/travis/jonschlinkert/pkg-store.svg?style=flat)](https://travis-ci.org/jonschlinkert/pkg-store) | ||
# pkg-store [![NPM version](https://img.shields.io/npm/v/pkg-store.svg?style=flat)](https://www.npmjs.com/package/pkg-store) [![NPM monthly downloads](https://img.shields.io/npm/dm/pkg-store.svg?style=flat)](https://npmjs.org/package/pkg-store) [![NPM total downloads](https://img.shields.io/npm/dt/pkg-store.svg?style=flat)](https://npmjs.org/package/pkg-store) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/pkg-store.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/pkg-store) | ||
> Use package.json as a config store. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
@@ -10,29 +12,75 @@ | ||
```sh | ||
$ npm install pkg-store --save | ||
$ npm install --save pkg-store | ||
``` | ||
Inherits [cache-base](https://github.com/jonschlinkert/cache-base), please see the `cache-base` documentation for more details. | ||
## Usage | ||
Pass the `cwd` and options to use, or an options object with `cwd` or `path`. If nothing is passed, the current working directory will be used. | ||
```js | ||
const Pkg = require('pkg-store'); | ||
``` | ||
## API | ||
Extends [cache-base](https://github.com/jonschlinkert/cache-base), please see the `cache-base` documentation for more details. | ||
### [Pkg](index.js#L37) | ||
Initialize a new `Pkg` store at the given `cwd` with the specified `options`. | ||
**Params** | ||
* `cwd` **{String}**: Directory of the package.json to read. | ||
* `options` **{Object}** | ||
**Example** | ||
```js | ||
var pkg = require('pkg-store')(cwd, options); | ||
const pkg = require('pkg-store')(process.cwd()); | ||
const pkg = new Pkg(cwd, options); | ||
// or | ||
var pkg = require('pkg-store')(options); | ||
// or | ||
var pkg = require('pkg-store')(); | ||
const pkg = new Pkg(options); | ||
console.log(pkg.path); | ||
//=> '~/your-project/package.json' | ||
console.log(pkg.data); | ||
//=> { name: 'your-project', ... } | ||
``` | ||
### [.data](index.js#L61) | ||
Get and set `pkg.data`. | ||
**Example** | ||
```js | ||
var pkg = require('pkg-store')(process.cwd()); | ||
console.log(pkg.data); | ||
``` | ||
## API | ||
### [.save](index.js#L79) | ||
Inherits [cache-base](https://github.com/jonschlinkert/cache-base), please see the `cache-base` documentation for more details. | ||
Write the `pkg.data` object to the file system at `pkg.path`. | ||
**Params** | ||
* `callback` **{Function}**: (optional) | ||
**Example** | ||
```js | ||
pkg.save(); | ||
``` | ||
### [.read](index.js#L99) | ||
Reads `pkg.path` from the file system and returns an object. | ||
* `returns` **{undefined}** | ||
**Example** | ||
```js | ||
const data = pkg.read(); | ||
``` | ||
### .set | ||
@@ -56,10 +104,2 @@ | ||
### .save | ||
Persist package.json to the file system at `pkg.path`. | ||
```js | ||
pkg.save(); | ||
``` | ||
### .get | ||
@@ -119,51 +159,58 @@ | ||
## Related projects | ||
## About | ||
You might also be interested in these projects: | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [data-store](https://www.npmjs.com/package/data-store): Easily get, set and persist config data. | [homepage](https://github.com/jonschlinkert/data-store) | ||
* [expand-pkg](https://www.npmjs.com/package/expand-pkg): Parse string values in package.json into objects. | [homepage](https://github.com/jonschlinkert/expand-pkg) | ||
* [find-pkg](https://www.npmjs.com/package/find-pkg): Find the first directory with a package.json, recursing up, starting with the given directory. Similar… [more](https://www.npmjs.com/package/find-pkg) | [homepage](https://github.com/jonschlinkert/find-pkg) | ||
* [normalize-pkg](https://www.npmjs.com/package/normalize-pkg): Normalize values in package.json using the map-schema library. | [homepage](https://github.com/jonschlinkert/normalize-pkg) | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
## Contributing | ||
</details> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/pkg-store/issues/new). | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
## Building docs | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
Generate readme and API documentation with [verb](https://github.com/verbose/verb): | ||
```sh | ||
$ npm install verb && npm run docs | ||
$ npm install && npm test | ||
``` | ||
Or, if [verb](https://github.com/verbose/verb) is installed globally: | ||
</details> | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
## Running tests | ||
</details> | ||
Install dev dependencies: | ||
### Related projects | ||
```sh | ||
$ npm install -d && npm test | ||
``` | ||
You might also be interested in these projects: | ||
## Author | ||
* [data-store](https://www.npmjs.com/package/data-store): Easily get, set and persist config data. | [homepage](https://github.com/jonschlinkert/data-store "Easily get, set and persist config data.") | ||
* [expand-pkg](https://www.npmjs.com/package/expand-pkg): Parse string values in package.json into objects. | [homepage](https://github.com/jonschlinkert/expand-pkg "Parse string values in package.json into objects.") | ||
* [find-pkg](https://www.npmjs.com/package/find-pkg): Find the first directory with a package.json, recursing up, starting with the given directory. Similar… [more](https://github.com/jonschlinkert/find-pkg) | [homepage](https://github.com/jonschlinkert/find-pkg "Find the first directory with a package.json, recursing up, starting with the given directory. Similar to look-up but does not support globs and only searches for package.json. Async and sync.") | ||
* [normalize-pkg](https://www.npmjs.com/package/normalize-pkg): Normalize values in package.json using the map-schema library. | [homepage](https://github.com/jonschlinkert/normalize-pkg "Normalize values in package.json using the map-schema library.") | ||
### Author | ||
**Jon Schlinkert** | ||
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
## License | ||
### License | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/pkg-store/blob/master/LICENSE). | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb](https://github.com/verbose/verb), v, on March 31, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 21, 2017._ |
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9918
2
0
213
2
4
97
+ Added@sellside/emitter@1.2.1(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedcache-base@2.0.2(transitive)
+ Addedcollection-visit@1.0.0(transitive)
+ Addedextend-shallow@3.0.2(transitive)
+ Addedis-extendable@1.0.1(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedset-value@2.0.1(transitive)
+ Addedsplit-string@3.1.0(transitive)
+ Addedunion-value@1.0.1(transitive)
+ Addedunset-value@1.0.0(transitive)
+ Addedwrite@1.0.3(transitive)
+ Addedwrite-json@2.0.0(transitive)
- Removedkind-of@^3.0.2
- Removedlazy-cache@^1.0.3
- Removedunion-value@^0.2.3
- Removedcache-base@0.8.5(transitive)
- Removedcollection-visit@0.2.3(transitive)
- Removedcomponent-emitter@1.3.1(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedlazy-cache@1.0.42.0.2(transitive)
- Removedmap-visit@0.1.5(transitive)
- Removedobject-visit@0.3.4(transitive)
- Removedset-getter@0.1.1(transitive)
- Removedset-value@0.4.3(transitive)
- Removedto-object-path@0.3.0(transitive)
- Removedunion-value@0.2.4(transitive)
- Removedunset-value@0.1.2(transitive)
- Removedwrite@0.2.1(transitive)
- Removedwrite-json@0.2.2(transitive)
Updatedcache-base@^2.0.2
Updatedwrite-json@^2.0.0