shelljs/src/mv.js
Nate Fischer 4975b54a4f feat: plugin.error() takes an options parameter (#535)
This implements the following options: `continue`, `code`, `prefix`, & `silent`.

Fixes #522, #523
2016-10-23 23:17:42 -07:00

96 lines
2.6 KiB
JavaScript

var fs = require('fs');
var path = require('path');
var common = require('./common');
var cp = require('./cp');
var rm = require('./rm');
common.register('mv', _mv, {
cmdOptions: {
'f': '!no_force',
'n': 'no_force',
},
});
//@
//@ ### mv([options ,] source [, source ...], dest')
//@ ### mv([options ,] source_array, dest')
//@ Available options:
//@
//@ + `-f`: force (default behavior)
//@ + `-n`: no-clobber
//@
//@ Examples:
//@
//@ ```javascript
//@ mv('-n', 'file', 'dir/');
//@ mv('file1', 'file2', 'dir/');
//@ mv(['file1', 'file2'], 'dir/'); // same as above
//@ ```
//@
//@ Moves files.
function _mv(options, sources, dest) {
// Get sources, dest
if (arguments.length < 3) {
common.error('missing <source> and/or <dest>');
} else if (arguments.length > 3) {
sources = [].slice.call(arguments, 1, arguments.length - 1);
dest = arguments[arguments.length - 1];
} else if (typeof sources === 'string') {
sources = [sources];
} else {
common.error('invalid arguments');
}
var exists = fs.existsSync(dest);
var stats = exists && fs.statSync(dest);
// Dest is not existing dir, but multiple sources given
if ((!exists || !stats.isDirectory()) && sources.length > 1) {
common.error('dest is not a directory (too many sources)');
}
// Dest is an existing file, but no -f given
if (exists && stats.isFile() && options.no_force) {
common.error('dest file already exists: ' + dest);
}
sources.forEach(function (src) {
if (!fs.existsSync(src)) {
common.error('no such file or directory: ' + src, { continue: true });
return; // skip file
}
// If here, src exists
// When copying to '/path/dir':
// thisDest = '/path/dir/file1'
var thisDest = dest;
if (fs.existsSync(dest) && fs.statSync(dest).isDirectory()) {
thisDest = path.normalize(dest + '/' + path.basename(src));
}
if (fs.existsSync(thisDest) && options.no_force) {
common.error('dest file already exists: ' + thisDest, { continue: true });
return; // skip file
}
if (path.resolve(src) === path.dirname(path.resolve(thisDest))) {
common.error('cannot move to self: ' + src, { continue: true });
return; // skip file
}
try {
fs.renameSync(src, thisDest);
} catch (e) {
if (e.code === 'EXDEV') { // external partition
// if either of these fails, the appropriate error message will bubble
// up to the top level automatically
cp('-r', src, thisDest);
rm('-rf', src);
}
}
}); // forEach(src)
return '';
} // mv
module.exports = _mv;