1 var path = require( 'path' );
4 createFromFile: function ( filePath ) {
5 var fname = path.basename( filePath );
6 var dir = path.dirname( filePath );
7 return this.create( fname, dir );
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 = { };
17 var removeNotFoundFiles = function removeNotFoundFiles() {
18 const cachedEntries = cache.keys();
19 // remove not found entries
20 cachedEntries.forEach( function remover( fPath ) {
24 if ( err.code === 'ENOENT' ) {
25 cache.removeKey( fPath );
31 removeNotFoundFiles();
35 * the flat cache storage used to persist the metadata of the `files
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
45 hasFileChanged: function ( file ) {
46 return this.getFileDescriptor( file ).changed;
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
55 * @param {Array} files the files to analyze and compare to the previous seen files
56 * @return {[type]} [description]
58 analyzeFiles: function ( files ) {
68 me.normalizeEntries( files ).forEach( function ( entry ) {
69 if ( entry.changed ) {
70 res.changedFiles.push( entry.key );
73 if ( entry.notFound ) {
74 res.notFoundFiles.push( entry.key );
77 res.notChangedFiles.push( entry.key );
82 getFileDescriptor: function ( file ) {
83 var meta = cache.getKey( file );
84 var cacheExists = !!meta;
89 fstat = fs.statSync( file );
91 me.removeEntry( file );
92 return { key: file, notFound: true, err: ex };
95 var cSize = fstat.size;
96 var cTime = fstat.mtime.getTime();
99 meta = { size: cSize, mtime: cTime };
101 var isDifferentDate = cTime !== meta.mtime;
102 var isDifferentSize = cSize !== meta.size;
105 var nEntry = normalizedEntries[ file ] = {
107 changed: !cacheExists || isDifferentDate || isDifferentSize,
115 * Return the list o the files that changed compared
116 * against the ones stored in the cache
119 * @param files {Array} the array of files to compare against the ones in the cache
122 getUpdatedFiles: function ( files ) {
124 files = files || [ ];
126 return me.normalizeEntries( files ).filter( function ( entry ) {
127 return entry.changed;
128 } ).map( function ( entry ) {
134 * return the list of files
135 * @method normalizeEntries
139 normalizeEntries: function ( files ) {
140 files = files || [ ];
143 var nEntries = files.map( function ( file ) {
144 return me.getFileDescriptor( file );
147 //normalizeEntries = nEntries;
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
155 * @method removeEntry
158 removeEntry: function ( entryName ) {
159 delete normalizedEntries[ entryName ];
160 cache.removeKey( entryName );
164 * Delete the cache file from the disk
165 * @method deleteCacheFile
167 deleteCacheFile: function () {
168 cache.removeCacheFile();
172 * remove the cache from the file and clear the memory cache
174 destroy: function () {
175 normalizedEntries = { };
179 * Sync the files and persist them to the cache
182 reconcile: function () {
183 removeNotFoundFiles();
185 var entries = normalizedEntries;
186 var keys = Object.keys( entries );
188 if ( keys.length === 0 ) {
192 keys.forEach( function ( entryName ) {
193 var cacheEntry = entries[ entryName ];
196 var stat = fs.statSync( cacheEntry.key );
197 var meta = assign( cacheEntry.meta, {
199 mtime: stat.mtime.getTime()
202 cache.setKey( entryName, meta );
204 // if the file does not exists we don't save it
205 // other errors are just thrown
206 if ( err.code !== 'ENOENT' ) {