.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / node_modules / file-entry-cache / cache.js
1 var path = require( 'path' );
2
3 module.exports = {
4   createFromFile: function ( filePath ) {
5     var fname = path.basename( filePath );
6     var dir = path.dirname( filePath );
7     return this.create( fname, dir );
8   },
9
10   create: function ( cacheId, _path ) {
11     var fs = require( 'fs' );
12     var flatCache = require( 'flat-cache' );
13     var cache = flatCache.load( cacheId, _path );
14     var assign = require( 'object-assign' );
15     var normalizedEntries = { };
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
33     return {
34       /**
35        * the flat cache storage used to persist the metadata of the `files
36        * @type {Object}
37        */
38       cache: cache,
39       /**
40        * Return whether or not a file has changed since last time reconcile was called.
41        * @method hasFileChanged
42        * @param  {String}  file  the filepath to check
43        * @return {Boolean}       wheter or not the file has changed
44        */
45       hasFileChanged: function ( file ) {
46         return this.getFileDescriptor( file ).changed;
47       },
48
49       /**
50        * given an array of file paths it return and object with three arrays:
51        *  - changedFiles: Files that changed since previous run
52        *  - notChangedFiles: Files that haven't change
53        *  - notFoundFiles: Files that were not found, probably deleted
54        *
55        * @param  {Array} files the files to analyze and compare to the previous seen files
56        * @return {[type]}       [description]
57        */
58       analyzeFiles: function ( files ) {
59         var me = this;
60         files = files || [ ];
61
62         var res = {
63           changedFiles: [],
64           notFoundFiles: [],
65           notChangedFiles: []
66         };
67
68         me.normalizeEntries( files ).forEach( function ( entry ) {
69           if ( entry.changed ) {
70             res.changedFiles.push( entry.key );
71             return;
72           }
73           if ( entry.notFound ) {
74             res.notFoundFiles.push( entry.key );
75             return;
76           }
77           res.notChangedFiles.push( entry.key );
78         } );
79         return res;
80       },
81
82       getFileDescriptor: function ( file ) {
83         var meta = cache.getKey( file );
84         var cacheExists = !!meta;
85         var fstat;
86         var me = this;
87
88         try {
89           fstat = fs.statSync( file );
90         } catch (ex) {
91           me.removeEntry( file );
92           return { key: file, notFound: true, err: ex };
93         }
94
95         var cSize = fstat.size;
96         var cTime = fstat.mtime.getTime();
97
98         if ( !meta ) {
99           meta = { size: cSize, mtime: cTime };
100         } else {
101           var isDifferentDate = cTime !== meta.mtime;
102           var isDifferentSize = cSize !== meta.size;
103         }
104
105         var nEntry = normalizedEntries[ file ] = {
106           key: file,
107           changed: !cacheExists || isDifferentDate || isDifferentSize,
108           meta: meta
109         };
110
111         return nEntry;
112       },
113
114       /**
115        * Return the list o the files that changed compared
116        * against the ones stored in the cache
117        *
118        * @method getUpdated
119        * @param files {Array} the array of files to compare against the ones in the cache
120        * @returns {Array}
121        */
122       getUpdatedFiles: function ( files ) {
123         var me = this;
124         files = files || [ ];
125
126         return me.normalizeEntries( files ).filter( function ( entry ) {
127           return entry.changed;
128         } ).map( function ( entry ) {
129           return entry.key;
130         } );
131       },
132
133       /**
134        * return the list of files
135        * @method normalizeEntries
136        * @param files
137        * @returns {*}
138        */
139       normalizeEntries: function ( files ) {
140         files = files || [ ];
141
142         var me = this;
143         var nEntries = files.map( function ( file ) {
144           return me.getFileDescriptor( file );
145         } );
146
147         //normalizeEntries = nEntries;
148         return nEntries;
149       },
150
151       /**
152        * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
153        * modified the next time the process is run
154        *
155        * @method removeEntry
156        * @param entryName
157        */
158       removeEntry: function ( entryName ) {
159         delete normalizedEntries[ entryName ];
160         cache.removeKey( entryName );
161       },
162
163       /**
164        * Delete the cache file from the disk
165        * @method deleteCacheFile
166        */
167       deleteCacheFile: function () {
168         cache.removeCacheFile();
169       },
170
171       /**
172        * remove the cache from the file and clear the memory cache
173        */
174       destroy: function () {
175         normalizedEntries = { };
176         cache.destroy();
177       },
178       /**
179        * Sync the files and persist them to the cache
180        * @method reconcile
181        */
182       reconcile: function () {
183         removeNotFoundFiles();
184
185         var entries = normalizedEntries;
186         var keys = Object.keys( entries );
187
188         if ( keys.length === 0 ) {
189           return;
190         }
191
192         keys.forEach( function ( entryName ) {
193           var cacheEntry = entries[ entryName ];
194
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           }
210         } );
211
212         cache.save( true );
213       }
214     };
215   }
216 };