~dpm/+junk/navigator

« back to all changes in this revision

Viewing changes to platforms/ubuntu/build/node_modules/shelljs/src/mv.js

  • Committer: David Planella
  • Date: 2015-05-28 07:14:31 UTC
  • Revision ID: david.planella@ubuntu.com-20150528071431-hlvqt5utpe6d1o6o
Added Ubuntu platform

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var fs = require('fs');
 
2
var path = require('path');
 
3
var common = require('./common');
 
4
 
 
5
//@
 
6
//@ ### mv(source [, source ...], dest')
 
7
//@ ### mv(source_array, dest')
 
8
//@ Available options:
 
9
//@
 
10
//@ + `f`: force
 
11
//@
 
12
//@ Examples:
 
13
//@
 
14
//@ ```javascript
 
15
//@ mv('-f', 'file', 'dir/');
 
16
//@ mv('file1', 'file2', 'dir/');
 
17
//@ mv(['file1', 'file2'], 'dir/'); // same as above
 
18
//@ ```
 
19
//@
 
20
//@ Moves files. The wildcard `*` is accepted.
 
21
function _mv(options, sources, dest) {
 
22
  options = common.parseOptions(options, {
 
23
    'f': 'force'
 
24
  });
 
25
 
 
26
  // Get sources, dest
 
27
  if (arguments.length < 3) {
 
28
    common.error('missing <source> and/or <dest>');
 
29
  } else if (arguments.length > 3) {
 
30
    sources = [].slice.call(arguments, 1, arguments.length - 1);
 
31
    dest = arguments[arguments.length - 1];
 
32
  } else if (typeof sources === 'string') {
 
33
    sources = [sources];
 
34
  } else if ('length' in sources) {
 
35
    sources = sources; // no-op for array
 
36
  } else {
 
37
    common.error('invalid arguments');
 
38
  }
 
39
 
 
40
  sources = common.expand(sources);
 
41
 
 
42
  var exists = fs.existsSync(dest),
 
43
      stats = exists && fs.statSync(dest);
 
44
 
 
45
  // Dest is not existing dir, but multiple sources given
 
46
  if ((!exists || !stats.isDirectory()) && sources.length > 1)
 
47
    common.error('dest is not a directory (too many sources)');
 
48
 
 
49
  // Dest is an existing file, but no -f given
 
50
  if (exists && stats.isFile() && !options.force)
 
51
    common.error('dest file already exists: ' + dest);
 
52
 
 
53
  sources.forEach(function(src) {
 
54
    if (!fs.existsSync(src)) {
 
55
      common.error('no such file or directory: '+src, true);
 
56
      return; // skip file
 
57
    }
 
58
 
 
59
    // If here, src exists
 
60
 
 
61
    // When copying to '/path/dir':
 
62
    //    thisDest = '/path/dir/file1'
 
63
    var thisDest = dest;
 
64
    if (fs.existsSync(dest) && fs.statSync(dest).isDirectory())
 
65
      thisDest = path.normalize(dest + '/' + path.basename(src));
 
66
 
 
67
    if (fs.existsSync(thisDest) && !options.force) {
 
68
      common.error('dest file already exists: ' + thisDest, true);
 
69
      return; // skip file
 
70
    }
 
71
 
 
72
    if (path.resolve(src) === path.dirname(path.resolve(thisDest))) {
 
73
      common.error('cannot move to self: '+src, true);
 
74
      return; // skip file
 
75
    }
 
76
 
 
77
    fs.renameSync(src, thisDest);
 
78
  }); // forEach(src)
 
79
} // mv
 
80
module.exports = _mv;