Skip to content

Commit

Permalink
Replace write with combination of mkdir and writeFile (#49)
Browse files Browse the repository at this point in the history
Node v10 introduced a great "recursive" option for mkdir which allows to
get rid from mkdirp package and easily rewrite "write" package usage
with two function calls.

https://nodejs.org/api/fs.html#fs_fs_mkdir_path_options_callback
  • Loading branch information
TrySound committed Nov 8, 2020
1 parent 45b51ca commit ef48276
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"
- "11"
- "12"
- "14"
12 changes: 3 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -15,7 +15,7 @@
"del.js"
],
"engines": {
"node": ">=4"
"node": ">=10.12.0"
},
"precommit": [
"npm run verify --silent"
Expand Down Expand Up @@ -81,7 +81,6 @@
},
"dependencies": {
"flatted": "^2.0.0",
"rimraf": "^2.6.3",
"write": "^1.0.3"
"rimraf": "^2.6.3"
}
}
11 changes: 8 additions & 3 deletions test/specs/cache.js
Expand Up @@ -6,7 +6,6 @@ describe( 'flat-cache', function () {
var rimraf = require( 'rimraf' ).sync;
var fs = require( 'fs' );
var flatCache = require( '../../cache' );
var write = require( 'write' );

beforeEach( function () {
flatCache.clearAll();
Expand All @@ -22,7 +21,10 @@ describe( 'flat-cache', function () {

it( 'should not crash if the cache file exists but it is an empty string', function () {
var cachePath = path.resolve( __dirname, '../fixtures/.cache2' );
write.sync( path.join( cachePath, 'someId' ), '' );
fs.mkdirSync( cachePath, {
recursive: true
} );
fs.writeFileSync( path.join( cachePath, 'someId' ), '' );

expect( function () {
var cache = flatCache.load( 'someId', cachePath );
Expand All @@ -32,7 +34,10 @@ describe( 'flat-cache', function () {

it( 'should not crash if the cache file exists but it is an invalid JSON string', function () {
var cachePath = path.resolve( __dirname, '../fixtures/.cache2' );
write.sync( path.join( cachePath, 'someId' ), '{ "foo": "fookey", "bar" ' );
fs.mkdirSync( cachePath, {
recursive: true
} );
fs.writeFileSync( path.join( cachePath, 'someId' ), '{ "foo": "fookey", "bar" ' );

expect( function () {
var cache = flatCache.load( 'someId', cachePath );
Expand Down
7 changes: 5 additions & 2 deletions utils.js
@@ -1,5 +1,5 @@
var fs = require( 'fs' );
var write = require( 'write' );
var path = require( 'path' );
var flatted = require( 'flatted' );

module.exports = {
Expand Down Expand Up @@ -34,6 +34,9 @@ module.exports = {
* @param {*} data Object to serialize
*/
writeJSON: function ( filePath, data ) {
write.sync( filePath, flatted.stringify( data ) );
fs.mkdirSync( path.dirname( filePath ), {
recursive: true
} );
fs.writeFileSync( filePath, flatted.stringify( data ) );
}
};

0 comments on commit ef48276

Please sign in to comment.