Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/apostrophecms/prettiest
Improbably easy data storage and locking for command line scripts. Pairs well with shelljs and a nice chianti.
https://github.com/apostrophecms/prettiest
Last synced: about 2 months ago
JSON representation
Improbably easy data storage and locking for command line scripts. Pairs well with shelljs and a nice chianti.
- Host: GitHub
- URL: https://github.com/apostrophecms/prettiest
- Owner: apostrophecms
- Created: 2014-12-19T18:52:31.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2020-01-04T23:21:12.000Z (almost 5 years ago)
- Last Synced: 2024-10-14T11:04:24.104Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 77
- Watchers: 15
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# prettiest
```javascript
// The simplest script: keeps track of how many
// times it has been run. It's like magic!var data = require('prettiest')();
data.count = data.count || 0;
data.count++;
console.log('I have been run ' + data.count + ' times.');
````prettiest` provides simple command line apps with two important features:
1. **Persistent data.** Any changes made to the data object returned by `prettiest` are automatically saved when the app exits.
2. **Concurrency locks.** If two copies of your app start at the same time, the second one will always wait for the first one to finish before it proceeds.
If you're replacing a shell script with something a little smarter, this module is a great companion for [shelljs](http://documentup.com/arturadib/shelljs).
## Install
`npm install prettiest`
## Options
You can specify where the JSON data file lives:
```javascript
var data = require('prettiest')({ json: __dirname + '/mydatafile.json' });
```If you don't, the JSON file lives in the same directory with your app, and will be called `data.json`.
`prettiest` also creates a lock file, which will have the same name as the JSON file, plus `.lock` at the end. To prevent race conditions, the lock file is not removed. Just leave it be.
## Caveats
* The save-and-unlock behavior lives in a `process.on('exit')` handler. Which is great, actually, but just bear in mind it won't fire if node itself crashes. In which case your `data` object probably isn't ready to save anyway, right?
* You don't want to use this in a web application. Duh. It's a simple, synchronous bit of magic for use in utilities with short execution times.
* You don't want to use this in a super-long-running script, because it only saves your data to disk at the very end. It's meant for utilities that do a relatively simple job and then exit.
## Questions
* "Can my code still be asynchronous?" Sure, knock yourself out. The save-and-unlock logic runs when your code exits.
## Credits
`prettiest` was built for [ApostropheCMS](https://apostrophecms.com).
## Changelog
1.1.0: dependency on `fs-ext` bumped to 2.0.0, in hopes of smoother cross-platform compilation than with the prereleases. Moved to `const` and `let` since they are supported back to well before currently supported versions of node.
1.0.0: accepted pull request to use newer `fs-ext` because of compilation issues on newer systems. Thanks to Kerrick. Bumped to 1.0.0 stable. Now following semver.
0.1.0: initial release.