Fixes #50. They're right, this should follow OS default behavior.
This commit is contained in:
parent
f8ed81ad5a
commit
04c8fe0239
2 changed files with 32 additions and 13 deletions
|
|
@ -160,16 +160,20 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
|
||||||
*
|
*
|
||||||
* Recursively dives through a directory and moves all its files to a new location. This is a
|
* Recursively dives through a directory and moves all its files to a new location. This is a
|
||||||
* Synchronous function, which blocks things until it's done. If you need/want to do this in
|
* Synchronous function, which blocks things until it's done. If you need/want to do this in
|
||||||
* an Asynchronous manner, look at wrench.copyDirRecursively() below.
|
* an Asynchronous manner, look at wrench.copyDirRecursively() below. Specify forceDelete to force directory overwrite.
|
||||||
*
|
*
|
||||||
* Note: Directories should be passed to this function without a trailing slash.
|
* Note: Directories should be passed to this function without a trailing slash.
|
||||||
*/
|
*/
|
||||||
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
|
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
|
||||||
if (!opts || !opts.preserve) {
|
try {
|
||||||
try {
|
if(fs.statSync(newDirLocation).isDirectory()) {
|
||||||
if(fs.statSync(newDirLocation).isDirectory()) exports.rmdirSyncRecursive(newDirLocation);
|
if(typeof opts !== 'undefined' && opts.forceDelete) {
|
||||||
} catch(e) { }
|
exports.rmdirSyncRecursive(newDirLocation);
|
||||||
}
|
} else {
|
||||||
|
return new Error('You are trying to delete a directory that already exists. Specify forceDelete in the opts argument to override this. Bailing~');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(e) { }
|
||||||
|
|
||||||
/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
|
/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
|
||||||
var checkDir = fs.statSync(sourceDir);
|
var checkDir = fs.statSync(sourceDir);
|
||||||
|
|
@ -306,18 +310,23 @@ exports.rmdirRecursive = function rmdirRecursive(dir, clbk){
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/* wrench.copyDirRecursive("directory_to_copy", "new_location", callback);
|
/* wrench.copyDirRecursive("directory_to_copy", "new_location", {forceDelete: bool}, callback);
|
||||||
*
|
*
|
||||||
* Recursively dives through a directory and moves all its files to a new
|
* Recursively dives through a directory and moves all its files to a new
|
||||||
* location.
|
* location. Specify forceDelete to force directory overwrite.
|
||||||
*
|
*
|
||||||
* Note: Directories should be passed to this function without a trailing slash.
|
* Note: Directories should be passed to this function without a trailing slash.
|
||||||
*/
|
*/
|
||||||
exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {
|
exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk) {
|
||||||
fs.stat(newDir, function(err, newDirStat){
|
fs.stat(newDir, function(err, newDirStat){
|
||||||
if (!err) return exports.rmdirRecursive(newDir, function(err){
|
if(!err) {
|
||||||
copyDirRecursive(srcDir, newDir, clbk);
|
if(typeof opts !== 'undefined' && typeof opts !== 'function' && opts.forceDelete)
|
||||||
});
|
return exports.rmdirRecursive(newDir, function(err){
|
||||||
|
copyDirRecursive.apply(this, arguments);
|
||||||
|
});
|
||||||
|
else
|
||||||
|
return clbk(new Error('You are trying to delete a directory that already exists. Specify forceDelete in an options object to override this.'));
|
||||||
|
}
|
||||||
|
|
||||||
fs.stat(srcDir, function(err, srcDirStat){
|
fs.stat(srcDir, function(err, srcDirStat){
|
||||||
if (err) return clbk(err);
|
if (err) return clbk(err);
|
||||||
|
|
|
||||||
12
readme.md
12
readme.md
|
|
@ -38,7 +38,14 @@ wrench.chmodSyncRecursive('my_directory_name', 0755);
|
||||||
wrench.chownSyncRecursive("directory", uid, gid);
|
wrench.chownSyncRecursive("directory", uid, gid);
|
||||||
|
|
||||||
// Deep-copy an existing directory
|
// Deep-copy an existing directory
|
||||||
wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up');
|
wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up', {
|
||||||
|
forceDelete: bool, // Whether to overwrite existing directory or not
|
||||||
|
excludeHiddenUnix: bool, // Whether to copy hidden Unix files or not (preceding .)
|
||||||
|
preserveFiles: bool, // If we're overwriting something and the file already exists, keep the existing
|
||||||
|
inflateSymlinks: bool, // Whether to follow symlinks or not when copying files
|
||||||
|
filter: regexp, // A filter to match files against; if matches, do nothing (exclude).
|
||||||
|
whitelist: bool, // if true every file or directory which doesn't match filter will be ignored
|
||||||
|
});
|
||||||
|
|
||||||
// Read lines in from a file until you hit the end
|
// Read lines in from a file until you hit the end
|
||||||
var f = new wrench.LineReader('x.txt');
|
var f = new wrench.LineReader('x.txt');
|
||||||
|
|
@ -58,6 +65,9 @@ var files = [];
|
||||||
wrench.readdirRecursive('my_directory_name', function(error, curFiles) {
|
wrench.readdirRecursive('my_directory_name', function(error, curFiles) {
|
||||||
// curFiles is what you want
|
// curFiles is what you want
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If you're feeling somewhat masochistic
|
||||||
|
wrench.copyDirRecursive(srcDir, newDir, {forceDelete: bool /* See sync version */}, callbackfn);
|
||||||
```
|
```
|
||||||
|
|
||||||
Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath)
|
Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath)
|
||||||
|
|
|
||||||
Reference in a new issue