Blame view

node_modules/shelljs/src/touch.js 2.63 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  var common = require('./common');
  var fs = require('fs');
  
  common.register('touch', _touch, {
    cmdOptions: {
      'a': 'atime_only',
      'c': 'no_create',
      'd': 'date',
      'm': 'mtime_only',
      'r': 'reference',
    },
  });
  
  //@
  //@ ### touch([options,] file [, file ...])
  //@ ### touch([options,] file_array)
  //@ Available options:
  //@
  //@ + `-a`: Change only the access time
  //@ + `-c`: Do not create any files
  //@ + `-m`: Change only the modification time
  //@ + `-d DATE`: Parse DATE and use it instead of current time
  //@ + `-r FILE`: Use FILE's times instead of current time
  //@
  //@ Examples:
  //@
  //@ ```javascript
  //@ touch('source.js');
  //@ touch('-c', '/path/to/some/dir/source.js');
  //@ touch({ '-r': FILE }, '/path/to/some/dir/source.js');
  //@ ```
  //@
  //@ Update the access and modification times of each FILE to the current time.
  //@ A FILE argument that does not exist is created empty, unless -c is supplied.
  //@ This is a partial implementation of *[touch(1)](http://linux.die.net/man/1/touch)*.
  function _touch(opts, files) {
    if (!files) {
      common.error('no files given');
    } else if (typeof files === 'string') {
      files = [].slice.call(arguments, 1);
    } else {
      common.error('file arg should be a string file path or an Array of string file paths');
    }
  
    files.forEach(function (f) {
      touchFile(opts, f);
    });
    return '';
  }
  
  function touchFile(opts, file) {
    var stat = tryStatFile(file);
  
    if (stat && stat.isDirectory()) {
      // don't error just exit
      return;
    }
  
    // if the file doesn't already exist and the user has specified --no-create then
    // this script is finished
    if (!stat && opts.no_create) {
      return;
    }
  
    // open the file and then close it. this will create it if it doesn't exist but will
    // not truncate the file
    fs.closeSync(fs.openSync(file, 'a'));
  
    //
    // Set timestamps
    //
  
    // setup some defaults
    var now = new Date();
    var mtime = opts.date || now;
    var atime = opts.date || now;
  
    // use reference file
    if (opts.reference) {
      var refStat = tryStatFile(opts.reference);
      if (!refStat) {
        common.error('failed to get attributess of ' + opts.reference);
      }
      mtime = refStat.mtime;
      atime = refStat.atime;
    } else if (opts.date) {
      mtime = opts.date;
      atime = opts.date;
    }
  
    if (opts.atime_only && opts.mtime_only) {
      // keep the new values of mtime and atime like GNU
    } else if (opts.atime_only) {
      mtime = stat.mtime;
    } else if (opts.mtime_only) {
      atime = stat.atime;
    }
  
    fs.utimesSync(file, atime, mtime);
  }
  
  module.exports = _touch;
  
  function tryStatFile(filePath) {
    try {
      return fs.statSync(filePath);
    } catch (e) {
      return null;
    }
  }