Skip to content

Commit

Permalink
Removed the need for del (#33)
Browse files Browse the repository at this point in the history
* Removed the need for del

Removed the need for del as newer versions have broken backwards
compatibility. del mainly uses rimraf for deleting folders
and files, replaceing it with rimraf only is a minimal change.

* Disable glob on rimraf calls

* Added glob disable to wrong call

* Wrapped rimraf to simplify solution
  • Loading branch information
sgilroy85 authored and royriojas committed Nov 13, 2018
1 parent 0ed6bc7 commit c429012
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
14 changes: 4 additions & 10 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var path = require( 'path' );
var fs = require( 'graceful-fs' );
var del = require( 'del' ).sync;
var utils = require( './utils' );
var del = require( './del' );
var writeJSON = utils.writeJSON;

var cache = {
Expand Down Expand Up @@ -125,9 +125,7 @@ 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 );
},
/**
* Destroy the file cache and cache content.
Expand Down Expand Up @@ -185,9 +183,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 );
},
/**
* Remove all cache stored in the cache directory
Expand All @@ -196,8 +192,6 @@ module.exports = {
*/
clearAll: function ( cacheDir ) {
var filePath = cacheDir ? path.resolve( cacheDir ) : path.resolve( __dirname, './.cache/' );
return del( filePath, {
force: true
} ).length > 0;
return del( filePath );
}
};
11 changes: 11 additions & 0 deletions del.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var rimraf = require ( 'rimraf' ).sync;
var fs = require ( 'graceful-fs' );

module.exports = function del (file) {
if ( fs.existsSync ( file ) ) {
//if rimraf doesn't throw then the file has been deleted or didn't exist
rimraf( file, { glob:false });
return true;
}
return false;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
},
"dependencies": {
"circular-json": "^0.3.1",
"del": "^3.0.0",
"graceful-fs": "^4.1.2",
"rimraf": "~2.6.2",
"write": "^0.2.1"
}
}
22 changes: 6 additions & 16 deletions test/specs/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ describe( 'flat-cache', function () {
var expect = require( 'chai' ).expect;
var readJSON = require( '../../utils.js' ).readJSON;
var path = require( 'path' );
var del = require( 'del' ).sync;
var rimraf = require( 'rimraf' ).sync;
var fs = require( 'fs' );
var flatCache = require( '../../cache' );
var write = require( 'write' );

beforeEach( function () {
flatCache.clearAll();
del( path.resolve( __dirname, '../fixtures/.cache/' ), {
force: true
} );
del( path.resolve( __dirname, '../fixtures/.cache2/' ), {
force: true
} );
rimraf( path.resolve( __dirname, '../fixtures/.cache/' ) );
rimraf( path.resolve( __dirname, '../fixtures/.cache2/' ) );
} );

afterEach( function () {
flatCache.clearAll();
del( path.resolve( __dirname, '../fixtures/.cache/' ), {
force: true
} );
del( path.resolve( __dirname, '../fixtures/.cache2/' ), {
force: true
} );
rimraf( path.resolve( __dirname, '../fixtures/.cache/' ) );
rimraf( path.resolve( __dirname, '../fixtures/.cache2/' ) );
} );

it( 'should not crash if the cache file exists but it is an empty string', function () {
Expand Down Expand Up @@ -184,9 +176,7 @@ describe( 'flat-cache', function () {
describe( 'loading a cache using a filePath directly', function () {
var file = path.resolve( __dirname, '../fixtures/.cache2/mycache-file.cache' );
beforeEach( function () {
del( file, {
force: true
} );
rimraf( file );
} );

it( 'should create the file if it does not exists before', function () {
Expand Down

0 comments on commit c429012

Please sign in to comment.