// The simplest script: keeps track of how many// times it has been run. It's like magic!var data = ;datacount = datacount || 0;datacount++;console;
prettiest provides simple command line apps with two important features:
Persistent data. Any changes made to the data object returned by
prettiestare automatically saved when the app exits.
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.
npm install prettiest
You can specify where the JSON data file lives:
var data = json: __dirname + '/mydatafile.json' ;
If you don't, the JSON file lives in the same directory with your app, and will be called
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.
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
dataobject 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.
- "Can my code still be asynchronous?" Sure, knock yourself out. The save-and-unlock logic runs when your code exits.
prettiest was built for ApostropheCMS.
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
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.