Skip to content

Commit

Permalink
BLD: Add helpers/code check scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
royriojas committed Sep 11, 2015
1 parent ce5fe88 commit bdb82f3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
14 changes: 8 additions & 6 deletions README.md
Expand Up @@ -14,7 +14,7 @@ npm i --save flat-cache

```js
var flatCache = require('flat-cache')
// loads the cache, if one does not exists for the given
// loads the cache, if one does not exists for the given
// Id a new one will be prepared to be created
var cache = flatCache.load('cacheId');

Expand All @@ -30,7 +30,7 @@ cache.removeKey('key'); // removes a key from the cache
// save it to disk
cache.save(); // very important, if you don't save no changes will be persisted.

// loads the cache from a given directory, if one does
// loads the cache from a given directory, if one does
// not exists for the given Id a new one will be prepared to be created
var cache = flatCache.load('cacheId', path.resolve('./path/to/folder'));

Expand All @@ -44,22 +44,24 @@ flatCache.clearAll(); // remove the cache directory

## Motivation for this module

I needed a super simple and dumb **in-memory cache** with optional disk persistance in order to make
I needed a super simple and dumb **in-memory cache** with optional disk persistance in order to make
a script that will beutify files with `esformatter` only execute on the files that were changed since the last run.
To make that possible we need to store the `fileSize` and `modificationTime` of the files. So a simple `key/value`
To make that possible we need to store the `fileSize` and `modificationTime` of the files. So a simple `key/value`
storage was needed and Bam! this module was born.

## Important notes
- If no directory is especified when the `load` method is called, a folder named `.cache` will be created
- If no directory is especified when the `load` method is called, a folder named `.cache` will be created
inside the module directory when `cache.save` is called. If you're committing your `node_modules` to any vcs, you
might want to ignore the default `.cache` folder, or specify a custom directory.
- The values set on the keys of the cache should be `stringify-able` ones, meaning no circular references
- All the changes to the cache state are done to memory
- I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
intentionally dumb and simple

## License
## License

MIT

## Changelog


20 changes: 8 additions & 12 deletions cache.js
Expand Up @@ -17,8 +17,8 @@ var cache = {
load: function ( docId, cacheDir ) {
var me = this;

me._visited = {};
me._persisted = {};
me._visited = { };
me._persisted = { };
me._pathToFile = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId );

if ( fs.existsSync( me._pathToFile ) ) {
Expand Down Expand Up @@ -80,7 +80,7 @@ var cache = {
*/
_prune: function () {
var me = this;
var obj = {};
var obj = { };

var keys = Object.keys( me._visited );

Expand All @@ -93,7 +93,7 @@ var cache = {
obj[ key ] = me._persisted[ key ];
} );

me._visited = {};
me._visited = { };
me._persisted = obj;
},

Expand All @@ -115,18 +115,16 @@ var cache = {
* @return {Boolean} true or false if the file was successfully deleted
*/
removeCacheFile: function () {
return del( this._pathToFile, {
force: true
} );
return del( this._pathToFile, { force: true } );
},
/**
* Destroy the file cache and cache content.
* @method destroy
*/
destroy: function () {
var me = this;
me._visited = {};
me._persisted = {};
me._visited = { };
me._persisted = { };

me.removeCacheFile();
}
Expand Down Expand Up @@ -175,9 +173,7 @@ module.exports = {
*/
clearCacheById: function ( docId, cacheDir ) {
var filePath = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId );
return del( filePath, {
force: true
} ).length > 0;
return del( filePath, { force: true } ).length > 0;
},
/**
* Remove all cache stored in the cache directory
Expand Down
24 changes: 24 additions & 0 deletions package.json
Expand Up @@ -15,7 +15,26 @@
"engines": {
"node": ">=0.10.0"
},
"precommit": [
"npm run verify --silent"
],
"prepush": [
"npm run verify --silent"
],
"scripts": {
"beautify": "esbeautifier 'cache.js' 'specs/**/*.js'",
"beautify-check": "esbeautifier -k 'cache.js' 'specs/**/*.js'",
"eslint": "eslinter 'cache.js' 'specs/**/*.js'",
"lint": "npm run beautify && npm run eslint",
"verify": "npm run beautify-check && npm run eslint",
"install-hooks": "prepush install && changelogx install-hook && precommit install",
"changelog": "changelogx -f markdown -o ./changelog.md",
"do-changelog": "npm run changelog && git add ./changelog.md && git commit -m 'DOC: Generate changelog' --no-verify",
"pre-v": "npm run verify",
"post-v": "npm run do-changelog && git push --no-verify && git push --tags --no-verify",
"bump-major": "npm run pre-v && npm version major -m 'BLD: Release v%s' && npm run post-v",
"bump-minor": "npm run pre-v && npm version minor -m 'BLD: Release v%s' && npm run post-v",
"bump-patch": "npm run pre-v && npm version patch -m 'BLD: Release v%s' && npm run post-v",
"test": "mocha -R spec test/specs",
"cover": "istanbul cover test/runner.js html text-summary",
"watch": "watch-run -i -p 'test/specs/**/*.js' istanbul cover test/runner.js html text-summary"
Expand All @@ -39,8 +58,13 @@
"watch-run": "^1.2.2"
},
"dependencies": {
"changelogx": "^1.0.18",
"del": "^2.0.2",
"esbeautifier": "^4.2.11",
"eslinter": "^2.3.3",
"graceful-fs": "^4.1.2",
"precommit": "^1.1.5",
"prepush": "^3.1.4",

This comment has been minimized.

Copy link
@TrySound

TrySound Sep 11, 2015

Contributor

Why these packages are in dependencies not in dev? 30 mb! Fix please

This comment has been minimized.

Copy link
@royriojas

royriojas Sep 11, 2015

Contributor

Sorry my bad. :) you're right sir, moving them now

"read-json-sync": "^1.1.0",
"write": "^0.2.1"
}
Expand Down

0 comments on commit bdb82f3

Please sign in to comment.