First swing at implementing mv
This commit is contained in:
parent
db4274f1d2
commit
db350bd43a
|
@ -4,6 +4,7 @@ var Environment = require('./environment.js');
|
|||
var async = require('../../lib/async.js');
|
||||
var Encoding = require('../encoding.js');
|
||||
var minimatch = require('minimatch');
|
||||
var ROOT_DIRECTORY_NAME = require('src/constants').ROOT_DIRECTORY_NAME;
|
||||
|
||||
function Shell(fs, options) {
|
||||
options = options || {};
|
||||
|
@ -427,6 +428,78 @@ Shell.prototype.mkdirp = function(path, callback) {
|
|||
_mkdirp(path, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Renames `src` to `dest`, or moves `src` to `dest` if `dest` is
|
||||
* a directory. For moving multiple files, provide an array of paths
|
||||
* to be moved as `src`.
|
||||
*/
|
||||
Shell.prototype.mv = function(src, dest, callback) {
|
||||
var sh = this;
|
||||
var fs = sh.fs;
|
||||
var srcPath;
|
||||
if(typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
}
|
||||
options = options || {};
|
||||
callback = callback || function(){};
|
||||
|
||||
if(!src) {
|
||||
callback(new Errors.EINVAL('Missing src argument'));
|
||||
return;
|
||||
}
|
||||
|
||||
if(src === ROOT_DIRECTORY_NAME) {
|
||||
callback(new Errors.EINVAL('Root directory is not a valid path'));
|
||||
return;
|
||||
}
|
||||
|
||||
if(!dest) {
|
||||
callback(new Errors.EINVAL('Missing dest argument'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Is there a single source path?
|
||||
if(!Array.isArray(src)) {
|
||||
src = Path.resolve(src);
|
||||
if(src === '') {
|
||||
callback(new Errors.EINVAL('Path cannot be an empty string'));
|
||||
return;
|
||||
}
|
||||
|
||||
move(src, function(err, data) {
|
||||
if(err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
callback();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < src.length; i++) {
|
||||
src[i] = Path.resolve(src[i]);
|
||||
if(src[i] === '') {
|
||||
callback(new Errors.EINVAL('Paths cannot be an empty string'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async.eachSeries(src, move, function(error) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
return;
|
||||
}
|
||||
callback();
|
||||
});
|
||||
|
||||
// xxx - Look at "fs.rename"'s callback arguments. There's a bug here!
|
||||
function move(src, cb) {
|
||||
fs.rename(src, dest, cb);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Recursively walk a directory tree, reporting back all paths
|
||||
* that were found along the way. The `path` must be a dir.
|
||||
|
|
Loading…
Reference in New Issue