forked from seank-com/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchPath.js
63 lines (50 loc) · 1.38 KB
/
touchPath.js
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
/*jslint indent: 4, node: true, stupid: true*/
/*global require: false, process: false, readFileSync: false, touchPath: true */
var path = require('path'),
fs = require('fs'),
argv = process.argv,
argc = argv.length;
function enumPaths(pathname, num) {
"use strict";
var files = fs.readdirSync(pathname).sort(),
count = files.length,
i = 0,
file = '';
for (i = 0; i < count; i += 1) {
file = path.resolve(pathname, files[i]);
touchPath(file, num);
}
}
function touchPath(pathname, num) {
"use strict";
var sec = num * 24 * 60 * 60,
stat = {};
try {
stat = fs.statSync(pathname);
fs.utimesSync(pathname, (stat.atime / 1000) - sec, (stat.mtime / 1000) - sec);
if (stat.isDirectory()) {
enumPaths(pathname, num);
}
} catch (err) {
console.log(err);
}
}
function main(argc, argv) {
"use strict";
if (argc !== 4) {
console.log('usage: %s %s dir num', argv[0], argv[1]);
} else {
touchPath(argv[2], argv[3]);
}
}
if (!String.prototype.format) {
String.prototype.format = function () {
"use strict";
var args = arguments;
return this.replace(/\{(\d+)\}/g, function (match, number) {
var result = args[number] || match;
return result;
});
};
}
main(argc, argv);