-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_util.js
More file actions
76 lines (73 loc) · 2.06 KB
/
node_util.js
File metadata and controls
76 lines (73 loc) · 2.06 KB
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
(function() {
var fs, get, log, path, post, put, request, rmdirRecursiveSync, run, spawn;
var __slice = Array.prototype.slice;
spawn = require('child_process').spawn;
fs = require('fs');
path = require('path');
request = require('request');
log = console.log;
run = function(cmd) {
var args, proc, _ref;
log("" + cmd);
_ref = cmd.split(' '), cmd = _ref[0], args = 2 <= _ref.length ? __slice.call(_ref, 1) : [];
proc = spawn(cmd, args);
proc.stdin.end();
proc.stdout.on('data', function(data) {
return log(data.toString());
});
return proc.stderr.on('data', function(data) {
return log(data.toString());
});
};
post = request.post;
get = request.get;
put = request.put;
rmdirRecursiveSync = function(dirPath) {
var currFile, currFilePath, file, _i, _len, _ref;
if (!fs.existsSync(dirPath)) {
return true;
}
_ref = fs.readdirSync(dirPath);
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
currFilePath = dirPath + "/" + file;
currFile = fs.statSync(currFilePath);
if (currFile.isDirectory()) {
rmdirRecursiveSync(currFilePath);
} else if (currFile.isSymbolicLink()) {
fs.unlinkSync(currFilePath);
} else {
fs.unlinkSync(currFilePath);
}
}
return fs.rmdirSync(dirPath);
};
/*
The way you would use this module is as follows:
In JS,
node_util = require('node_util').sync()
read = node_util.read
mv = node_util.mv
In Coffee, destructuring makes usage even more pleasant
{mv, read, rm_r} = require('node_util').sync()
*/
exports.sync = function() {
return {
mv: fs.renameSync,
read: fs.readFileSync,
cat: fs.readFileSync,
write: fs.writeFileSync,
mkdir: fs.mkdirSync,
rm: fs.unlinkSync,
remove: fs.unlinkSync,
rm_r: rmdirRecursiveSync,
rmdirRecursiveSync: rmdirRecursiveSync,
join: path.join,
exec: run,
ls: fs.readdirSync,
exists: fs.existsSync,
post: post,
get: get
};
};
}).call(this);