{"size": 1643, "ext": "js", "max_stars_repo_path": "lib/CoreUtil/mv.js", "max_stars_repo_name": "samloh84/node-file-utils", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/CoreUtil/mv.js", "max_issues_repo_name": "samloh84/node-file-utils", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/CoreUtil/mv.js", "max_forks_repo_name": "samloh84/node-file-utils", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.3833333333, "max_line_length": 104, "alphanum_fraction": 0.6007303713}
const _ = require('lodash');
const _path = require('path');
const _walk = require('./_walk');
const stat = require("../FileUtil/stat");
const rename = require("../FileUtil/rename");
const mkdir = require("./mkdir");

/**
 * @memberOf CoreUtil
 * @function mv
 *
 * @param options
 * @param options.source
 * @param options.destination
 *
 * @returns {*}
 * @see {@link https://www.gnu.org/software/coreutils/manual/html_node/mv-invocation.html#mv-invocation}
 */
var mv = function (options) {

    var source = _.get(options, 'source');
    var destination = _.get(options, 'destination');

    if (_.isString(source) || _.isBuffer(source)) {
        source = _path.resolve(process.cwd(), source);
    }

    if (_.isString(destination) || _.isBuffer(destination)) {
        destination = _path.resolve(process.cwd(), destination);
    }

    var move = function (sourcePath, destinationPath) {
        return rename({oldPath: sourcePath, newPath: destinationPath});
    };

    return stat({path: destination})
        .catch(function (err) {
            if (err.code !== 'ENOENT') {
                throw err;
            }
            return null;
        })
        .then(function (destinationStats) {
            var destinationIsDirectory = !_.isNil(destinationStats) && destinationStats.isDirectory();

            var destinationPath;
            if (destinationIsDirectory) {
                destinationPath = _path.resolve(destination, _path.basename(source));
            } else {
                destinationPath = destination;
            }

            return move(source, destinationPath);
        });


};

module.exports = mv;
