.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / file-entry-cache / README.md
1 # file-entry-cache
2 > Super simple cache for file metadata, useful for process that work o a given series of files
3 > and that only need to repeat the job on the changed ones since the previous run of the process — Edit
4
5 [![NPM Version](http://img.shields.io/npm/v/file-entry-cache.svg?style=flat)](https://npmjs.org/package/file-entry-cache)
6 [![Build Status](http://img.shields.io/travis/royriojas/file-entry-cache.svg?style=flat)](https://travis-ci.org/royriojas/file-entry-cache)
7
8 ## install
9
10 ```bash
11 npm i --save file-entry-cache
12 ```
13
14 ## Usage
15
16 ```js
17 // loads the cache, if one does not exists for the given
18 // Id a new one will be prepared to be created
19 var fileEntryCache = require('file-entry-cache');
20
21 var cache = fileEntryCache.create('testCache');
22
23 var files = expand('../fixtures/*.txt');
24
25 // the first time this method is called, will return all the files
26 var oFiles = cache.getUpdatedFiles(files);
27
28 // this will persist this to disk checking each file stats and
29 // updating the meta attributes `size` and `mtime`.
30 // custom fields could also be added to the meta object and will be persisted
31 // in order to retrieve them later
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 */)
38
39 // on a second run
40 var cache2 = fileEntryCache.create('testCache');
41
42 // will return now only the files that were modified or none
43 // if no files were modified previous to the execution of this function
44 var oFiles = cache.getUpdatedFiles(files);
45
46 // if you want to prevent a file from being considered non modified
47 // something useful if a file failed some sort of validation
48 // you can then remove the entry from the cache doing
49 cache.removeEntry('path/to/file'); // path to file should be the same path of the file received on `getUpdatedFiles`
50 // that will effectively make the file to appear again as modified until the validation is passed. In that
51 // case you should not remove it from the cache
52
53 // if you need all the files, so you can determine what to do with the changed ones
54 // you can call
55 var oFiles = cache.normalizeEntries(files);
56
57 // oFiles will be an array of objects like the following
58 entry = {
59   key: 'some/name/file', the path to the file
60   changed: true, // if the file was changed since previous run
61   meta: {
62     size: 3242, // the size of the file
63     mtime: 231231231, // the modification time of the file
64     data: {} // some extra field stored for this file (useful to save the result of a transformation on the file
65   }
66 }
67
68 ```
69
70 ## Motivation for this module
71
72 I needed a super simple and dumb **in-memory cache** with optional disk persistence (write-back cache) in order to make
73 a script that will beautify files with `esformatter` to execute only on the files that were changed since the last run.
74
75 In doing so the process of beautifying files was reduced from several seconds to a small fraction of a second.
76
77 This module uses [flat-cache](https://www.npmjs.com/package/flat-cache) a super simple `key/value` cache storage with
78 optional file persistance.
79
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
82 the files that were modified. Making the process to end faster.
83
84 This module could also be used by processes that modify the files applying a transform, in that case the result of 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
87 entries with a `changed` field that can be used to determine if the file was changed or not. If it was not changed
88 the transformed stored data could be used instead of actually applying the transformation, saving time in case of only
89 a few files changed.
90
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.
92
93 ## Important notes
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   ```
102
103 ## License
104
105 MIT
106
107