file-entry-cache 1.2.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -14
- package/cache.js +41 -11
- package/changelog.md +20 -0
- package/package.json +7 -8
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# file-entry-cache
|
|
2
|
-
> Super simple cache for file metadata, useful for process that work o a given series of files
|
|
2
|
+
> Super simple cache for file metadata, useful for process that work o a given series of files
|
|
3
3
|
> and that only need to repeat the job on the changed ones since the previous run of the process — Edit
|
|
4
4
|
|
|
5
5
|
[](https://npmjs.org/package/file-entry-cache)
|
|
@@ -14,7 +14,7 @@ npm i --save file-entry-cache
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
16
|
```js
|
|
17
|
-
// loads the cache, if one does not exists for the given
|
|
17
|
+
// loads the cache, if one does not exists for the given
|
|
18
18
|
// Id a new one will be prepared to be created
|
|
19
19
|
var fileEntryCache = require('file-entry-cache');
|
|
20
20
|
|
|
@@ -25,18 +25,23 @@ var files = expand('../fixtures/*.txt');
|
|
|
25
25
|
// the first time this method is called, will return all the files
|
|
26
26
|
var oFiles = cache.getUpdatedFiles(files);
|
|
27
27
|
|
|
28
|
-
// this will persist this to disk checking each file stats and
|
|
28
|
+
// this will persist this to disk checking each file stats and
|
|
29
29
|
// updating the meta attributes `size` and `mtime`.
|
|
30
30
|
// custom fields could also be added to the meta object and will be persisted
|
|
31
31
|
// in order to retrieve them later
|
|
32
|
-
cache.reconcile();
|
|
32
|
+
cache.reconcile();
|
|
33
|
+
|
|
34
|
+
// use this if you want the non visited file entries to be kept in the cache
|
|
35
|
+
// for more than one execution
|
|
36
|
+
//
|
|
37
|
+
// cache.reconcile( true /* noPrune */)
|
|
33
38
|
|
|
34
39
|
// on a second run
|
|
35
40
|
var cache2 = fileEntryCache.create('testCache');
|
|
36
41
|
|
|
37
42
|
// will return now only the files that were modified or none
|
|
38
43
|
// if no files were modified previous to the execution of this function
|
|
39
|
-
var oFiles = cache.getUpdatedFiles(files);
|
|
44
|
+
var oFiles = cache.getUpdatedFiles(files);
|
|
40
45
|
|
|
41
46
|
// if you want to prevent a file from being considered non modified
|
|
42
47
|
// something useful if a file failed some sort of validation
|
|
@@ -64,21 +69,21 @@ entry = {
|
|
|
64
69
|
|
|
65
70
|
## Motivation for this module
|
|
66
71
|
|
|
67
|
-
I needed a super simple and dumb **in-memory cache** with optional disk persistence (write-back cache) in order to make
|
|
72
|
+
I needed a super simple and dumb **in-memory cache** with optional disk persistence (write-back cache) in order to make
|
|
68
73
|
a script that will beautify files with `esformatter` to execute only on the files that were changed since the last run.
|
|
69
74
|
|
|
70
75
|
In doing so the process of beautifying files was reduced from several seconds to a small fraction of a second.
|
|
71
76
|
|
|
72
|
-
This module uses [flat-cache](https://www.npmjs.com/package/flat-cache) a super simple `key/value` cache storage with
|
|
77
|
+
This module uses [flat-cache](https://www.npmjs.com/package/flat-cache) a super simple `key/value` cache storage with
|
|
73
78
|
optional file persistance.
|
|
74
79
|
|
|
75
|
-
The main idea is to read the files when the task begins, apply the transforms required, and if the process succeed,
|
|
76
|
-
then store the new state of the files. The next time this module request for `getChangedFiles` will return only
|
|
80
|
+
The main idea is to read the files when the task begins, apply the transforms required, and if the process succeed,
|
|
81
|
+
then store the new state of the files. The next time this module request for `getChangedFiles` will return only
|
|
77
82
|
the files that were modified. Making the process to end faster.
|
|
78
83
|
|
|
79
84
|
This module could also be used by processes that modify the files applying a transform, in that case the result of the
|
|
80
|
-
transform could be stored in the `meta` field, of the entries. Anything added to the meta field will be persisted.
|
|
81
|
-
Those processes won't need to call `getChangedFiles` they will instead call `normalizeEntries` that will return the
|
|
85
|
+
transform could be stored in the `meta` field, of the entries. Anything added to the meta field will be persisted.
|
|
86
|
+
Those processes won't need to call `getChangedFiles` they will instead call `normalizeEntries` that will return the
|
|
82
87
|
entries with a `changed` field that can be used to determine if the file was changed or not. If it was not changed
|
|
83
88
|
the transformed stored data could be used instead of actually applying the transformation, saving time in case of only
|
|
84
89
|
a few files changed.
|
|
@@ -86,10 +91,16 @@ a few files changed.
|
|
|
86
91
|
In the worst case scenario all the files will be processed. In the best case scenario only a few of them will be processed.
|
|
87
92
|
|
|
88
93
|
## Important notes
|
|
89
|
-
- The values set on the meta attribute of the entries should be `stringify-able` ones,
|
|
90
|
-
- All the changes to the cache state are done to memory first and only persisted after reconcile
|
|
94
|
+
- The values set on the meta attribute of the entries should be `stringify-able` ones if possible, flat-cache uses `circular-json` to try to persist circular structures, but this should be considered experimental. The best results are always obtained with non circular values
|
|
95
|
+
- All the changes to the cache state are done to memory first and only persisted after reconcile.
|
|
96
|
+
- By default non visited entries are removed from the cache. This is done to prevent the file from growing too much. If this is not an issue and
|
|
97
|
+
you prefer to do a manual pruning of the cache files, you can pass `true` to the `reconcile` call. Like this:
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
cache.reconcile( true /* noPrune */ );
|
|
101
|
+
```
|
|
91
102
|
|
|
92
|
-
## License
|
|
103
|
+
## License
|
|
93
104
|
|
|
94
105
|
MIT
|
|
95
106
|
|
package/cache.js
CHANGED
|
@@ -14,7 +14,28 @@ module.exports = {
|
|
|
14
14
|
var assign = require( 'object-assign' );
|
|
15
15
|
var normalizedEntries = { };
|
|
16
16
|
|
|
17
|
+
var removeNotFoundFiles = function removeNotFoundFiles() {
|
|
18
|
+
const cachedEntries = cache.keys();
|
|
19
|
+
// remove not found entries
|
|
20
|
+
cachedEntries.forEach( function remover( fPath ) {
|
|
21
|
+
try {
|
|
22
|
+
fs.statSync( fPath );
|
|
23
|
+
} catch (err) {
|
|
24
|
+
if ( err.code === 'ENOENT' ) {
|
|
25
|
+
cache.removeKey( fPath );
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} );
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
removeNotFoundFiles();
|
|
32
|
+
|
|
17
33
|
return {
|
|
34
|
+
/**
|
|
35
|
+
* the flat cache storage used to persist the metadata of the `files
|
|
36
|
+
* @type {Object}
|
|
37
|
+
*/
|
|
38
|
+
cache: cache,
|
|
18
39
|
/**
|
|
19
40
|
* Return whether or not a file has changed since last time reconcile was called.
|
|
20
41
|
* @method hasFileChanged
|
|
@@ -145,7 +166,6 @@ module.exports = {
|
|
|
145
166
|
*/
|
|
146
167
|
deleteCacheFile: function () {
|
|
147
168
|
cache.removeCacheFile();
|
|
148
|
-
//flatCache.clearCacheById(cacheId);
|
|
149
169
|
},
|
|
150
170
|
|
|
151
171
|
/**
|
|
@@ -157,29 +177,39 @@ module.exports = {
|
|
|
157
177
|
},
|
|
158
178
|
/**
|
|
159
179
|
* Sync the files and persist them to the cache
|
|
160
|
-
*
|
|
161
180
|
* @method reconcile
|
|
162
181
|
*/
|
|
163
182
|
reconcile: function () {
|
|
164
|
-
|
|
183
|
+
removeNotFoundFiles();
|
|
165
184
|
|
|
185
|
+
var entries = normalizedEntries;
|
|
166
186
|
var keys = Object.keys( entries );
|
|
187
|
+
|
|
167
188
|
if ( keys.length === 0 ) {
|
|
168
189
|
return;
|
|
169
190
|
}
|
|
191
|
+
|
|
170
192
|
keys.forEach( function ( entryName ) {
|
|
171
193
|
var cacheEntry = entries[ entryName ];
|
|
172
|
-
var stat = fs.statSync( cacheEntry.key );
|
|
173
|
-
|
|
174
|
-
var meta = assign( cacheEntry.meta, {
|
|
175
|
-
size: stat.size,
|
|
176
|
-
mtime: stat.mtime.getTime()
|
|
177
|
-
} );
|
|
178
194
|
|
|
179
|
-
|
|
195
|
+
try {
|
|
196
|
+
var stat = fs.statSync( cacheEntry.key );
|
|
197
|
+
var meta = assign( cacheEntry.meta, {
|
|
198
|
+
size: stat.size,
|
|
199
|
+
mtime: stat.mtime.getTime()
|
|
200
|
+
} );
|
|
201
|
+
|
|
202
|
+
cache.setKey( entryName, meta );
|
|
203
|
+
} catch (err) {
|
|
204
|
+
// if the file does not exists we don't save it
|
|
205
|
+
// other errors are just thrown
|
|
206
|
+
if ( err.code !== 'ENOENT' ) {
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
180
210
|
} );
|
|
181
211
|
|
|
182
|
-
cache.save();
|
|
212
|
+
cache.save( true );
|
|
183
213
|
}
|
|
184
214
|
};
|
|
185
215
|
}
|
package/changelog.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
|
|
2
2
|
# file-entry-cache - Changelog
|
|
3
|
+
## v2.0.0
|
|
4
|
+
- **Features**
|
|
5
|
+
- do not persist and prune removed files from cache. Relates to [#2](https://github.com/royriojas/file-entry-cache/issues/2) - [408374d]( https://github.com/royriojas/file-entry-cache/commit/408374d ), [Roy Riojas](https://github.com/Roy Riojas), 16/08/2016 15:47:58
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
## v1.3.1
|
|
9
|
+
- **Build Scripts Changes**
|
|
10
|
+
- remove older node version - [0a26ac4]( https://github.com/royriojas/file-entry-cache/commit/0a26ac4 ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 06:09:17
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## v1.3.0
|
|
14
|
+
- **Features**
|
|
15
|
+
- Add an option to not prune non visited keys. Closes [#2](https://github.com/royriojas/file-entry-cache/issues/2) - [b1a64db]( https://github.com/royriojas/file-entry-cache/commit/b1a64db ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 05:52:12
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## v1.2.4
|
|
19
|
+
- **Enhancements**
|
|
20
|
+
- Expose the flat-cache instance - [f34c557]( https://github.com/royriojas/file-entry-cache/commit/f34c557 ), [royriojas](https://github.com/royriojas), 23/09/2015 20:26:33
|
|
21
|
+
|
|
22
|
+
|
|
3
23
|
## v1.2.3
|
|
4
24
|
- **Build Scripts Changes**
|
|
5
25
|
- update flat-cache dep - [cc7b9ce]( https://github.com/royriojas/file-entry-cache/commit/cc7b9ce ), [royriojas](https://github.com/royriojas), 11/09/2015 18:04:44
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-entry-cache",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Super simple cache for file metadata, useful for process that work o a given series of files and that only need to repeat the job on the changed ones since the previous run of the process",
|
|
5
5
|
"repository": "royriojas/file-entry-cache",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,20 +16,20 @@
|
|
|
16
16
|
"node": ">=0.10.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
|
-
"beautify": "esbeautifier 'cache.js' '
|
|
20
|
-
"beautify-check": "
|
|
19
|
+
"beautify": "esbeautifier 'cache.js' 'test/**/*.js'",
|
|
20
|
+
"beautify-check": "npm run beautify -- -k",
|
|
21
21
|
"eslint": "eslinter 'cache.js' 'specs/**/*.js'",
|
|
22
22
|
"lint": "npm run beautify && npm run eslint",
|
|
23
23
|
"verify": "npm run beautify-check && npm run eslint",
|
|
24
24
|
"install-hooks": "prepush install && changelogx install-hook && precommit install",
|
|
25
25
|
"changelog": "changelogx -f markdown -o ./changelog.md",
|
|
26
26
|
"do-changelog": "npm run changelog && git add ./changelog.md && git commit -m 'DOC: Generate changelog' --no-verify",
|
|
27
|
-
"pre-v": "npm run
|
|
27
|
+
"pre-v": "npm run test",
|
|
28
28
|
"post-v": "npm run do-changelog && git push --no-verify && git push --tags --no-verify",
|
|
29
29
|
"bump-major": "npm run pre-v && npm version major -m 'BLD: Release v%s' && npm run post-v",
|
|
30
30
|
"bump-minor": "npm run pre-v && npm version minor -m 'BLD: Release v%s' && npm run post-v",
|
|
31
31
|
"bump-patch": "npm run pre-v && npm version patch -m 'BLD: Release v%s' && npm run post-v",
|
|
32
|
-
"test": "mocha -R spec test/specs",
|
|
32
|
+
"test": "npm run verify --silent && mocha -R spec test/specs",
|
|
33
33
|
"cover": "istanbul cover test/runner.js html text-summary",
|
|
34
34
|
"watch": "watch-run -i -p 'test/specs/**/*.js' istanbul cover test/runner.js html text-summary"
|
|
35
35
|
},
|
|
@@ -72,14 +72,13 @@
|
|
|
72
72
|
"precommit": "^1.1.5",
|
|
73
73
|
"prepush": "^3.1.4",
|
|
74
74
|
"proxyquire": "^1.3.1",
|
|
75
|
-
"read-file": "^0.2.0",
|
|
76
75
|
"sinon": "^1.12.2",
|
|
77
76
|
"sinon-chai": "^2.7.0",
|
|
78
77
|
"watch-run": "^1.2.1",
|
|
79
|
-
"write": "^0.
|
|
78
|
+
"write": "^0.3.1"
|
|
80
79
|
},
|
|
81
80
|
"dependencies": {
|
|
82
|
-
"flat-cache": "^1.
|
|
81
|
+
"flat-cache": "^1.2.1",
|
|
83
82
|
"object-assign": "^4.0.1"
|
|
84
83
|
}
|
|
85
84
|
}
|