Skip to content

Commit

Permalink
Move cascadeDelete to common.js
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski committed May 19, 2015
1 parent cd30607 commit bb3afae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
31 changes: 31 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require('core-js/es6/string');

var asp = require('rsvp').denodeify;
var fs = require('graceful-fs');
var path = require('path');
var config = require('./config');

// the opposite of extend
Expand Down Expand Up @@ -137,3 +138,33 @@ exports.stringify = function (subject) {
// replace the LF used by `JSON.stringify` with the preferred new line
return JSON.stringify(subject, null, 2).replace(/\n/g, config.newLine);
};

/* Recursively remove directory, all those above it, if they are empty.
* Takes optional `stopDir` to terminate at. */
function cascadeDelete(dir, stopDir) {
if (dir && dir !== stopDir) {
return new Promise(function(resolve) {
fs.exists(dir, resolve);
})
.then(function(exists) {
if (!exists)
return true;

return asp(fs.readdir)(dir)
.then(function(paths) {
if (paths.length === 0) {
return asp(fs.rmdir)(dir)
.then(function() {
return true;
});
}
});
})
.then(function(cont) {
if (cont)
return cascadeDelete(path.dirname(dir));
});
}
}

exports.cascadeDelete = cascadeDelete;
31 changes: 2 additions & 29 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ var globalConfig = require('./global-config');

var rimraf = require('rimraf');

var readJSON = require('./common').readJSON;
var alphabetize = require('./common').alphabetize;
var cascadeDelete = require('./common').cascadeDelete;
var hasProperties = require('./common').hasProperties;
var readJSON = require('./common').readJSON;

var fs = require('graceful-fs');

Expand Down Expand Up @@ -869,34 +870,6 @@ function showVersions(forks) {
}
exports.showVersions = showVersions;

/* Recursively remove directory, all those above it, if they are empty.
* Takes optional `stopDir` to terminate at. */
function cascadeDelete(dir, stopDir) {
if (dir && dir !== stopDir) {
return new Promise(function(resolve) {
fs.exists(dir, resolve);
})
.then(function(exists) {
if (!exists)
return true;

return asp(fs.readdir)(dir)
.then(function(paths) {
if (paths.length === 0) {
return asp(fs.rmdir)(dir)
.then(function() {
return true;
});
}
});
})
.then(function(cont) {
if (cont)
return cascadeDelete(path.dirname(dir));
});
}
}

/*
* Configuration cleaning
*
Expand Down

0 comments on commit bb3afae

Please sign in to comment.