UNPKG

pkg-store

Version:
203 lines (130 loc) 5.39 kB
# 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 Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save pkg-store ``` ## Usage ```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 const pkg = require('pkg-store')(process.cwd()); const pkg = new Pkg(cwd, options); // or const pkg = new Pkg(options); console.log(pkg.path); //=> '~/your-project/package.json' console.log(pkg.data); //=> { name: 'your-project', ... } ``` ### [.save](index.js#L63) Write the `pkg.data` object to the file system at `pkg.path`. **Params** * `callback` **{Function}**: (optional) **Example** ```js pkg.save(); ``` ### [.read](index.js#L83) Reads `pkg.path` from the file system and returns an object. * `returns` **{undefined}** **Example** ```js const data = pkg.read(); ``` ### .set ```js pkg.set(key, value); ``` Set property `key` with the given `value`. **Example** ```js // given {"name": "my-project"} pkg.set('bin.foo', 'bar'); console.log(pkg.data); //=> {"name": "my-project", "bin": {"foo": "bar"}} ``` ### .get ```js pkg.get(key); ``` Get property `key` from package.json. **Example** ```js // given {"name": "my-project"} pkg.set('bin.foo', 'bar'); console.log(pkg.get('bin')); //=> {"foo": "bar"} ``` ### .has ```js pkg.has(key); ``` Returns `true` if `package.json` has property `key`. **Example** ```js // given: {"name": "my-project"} console.log(pkg.has('name')); //=> true console.log(pkg.has('zzzzzzz')); //=> false ``` ### .union ```js pkg.union(key, val); ``` Create array `key`, or concatenate values to array `key`. Also uniquifies the array. **Example** ```js pkg.union('keywords', 'foo'); pkg.union('keywords', ['bar', 'baz']); console.log(pkg.get('keywords')); //=> ['foo', 'bar', 'baz'] ``` ## About <details> <summary><strong>Contributing</strong></summary> Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). </details> <details> <summary><strong>Running Tests</strong></summary> 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: ```sh $ npm install && npm test ``` </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 $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` </details> ### Related projects You might also be interested in these projects: * [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](https://twitter.com/jonschlinkert) ### License Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 21, 2017._