Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pkg-store

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pkg-store - npm Package Compare versions

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

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc