diff --git a/src/shell/shell.js b/src/shell/shell.js index 000fc02..8c0d5ff 100644 --- a/src/shell/shell.js +++ b/src/shell/shell.js @@ -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.