-
Notifications
You must be signed in to change notification settings - Fork 15
/
___fs++.js
183 lines (151 loc) · 4.31 KB
/
___fs++.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
* Copyright 2011 by the AToMPM team and licensed under the LGPL
* See COPYING.lesser and README.md in the root of this project for full details
*/
/* this file contains platform-oblivious wrappers for various windows and unix
commands... it's purpose is to hide ugly platform-specific details and to
provide higher-level functions that those provided by nodejs' fs module */
var _cp = require('child_process'),
_os = require('os'),
_fs = require('fs');
/* NOTE:: because microsoft has apparently diversified into hiring comedians,
robocopy can succeed with non-0 exit codes */
exports.cp =
function(src,dest,callback)
{
//guard against injection
src = src.split(';')[0];
dest = dest.split(';')[0];
switch(_os.type())
{
case 'Windows_NT' :
_cp.exec('robocopy "'+src+'" "'+dest+'" /e',
function(err,stdout,stderr)
{
if( err && err.code == 1 )
callback(undefined,stdout,stderr);
else
callback(err,stdout,stderr);
});
break;
case 'Linux' :
case 'Darwin' :
_cp.exec('cp -R "'+src+'" "'+dest+'"',callback);
break;
default:
throw 'unsupported OS :: '+_os.type();
}
};
/* NOTE :: in MS-DOS dir, results are absolute windows paths... these must be
converted to unix paths and relativized to the atompm root
NOTE :: in both cases, the return value's last entry is '\n'... we slice it
off to avoid dumb problems when later on when splitting on '\n' */
exports.findfiles =
function(dir,callback)
{
//guard against injection
dir = dir.split(';')[0];
switch(_os.type())
{
case 'Windows_NT' :
_cp.exec('dir /s /b "'+dir+'"',
function(err, stdout, stderr)
{
if( err )
callback(err,stdout,stderr);
else
{
let windir = (dir.charAt(0)=='.' ? dir.substring(1) : dir).
replace(/\//g,'\\'),
paths = stdout.split('\r\n').map(
function(path)
{
let newpath = dir+path.substring(
path.indexOf(windir)+windir.length).
replace(/\\/g,'/');
try {
if (_fs.lstatSync(path).isDirectory()) {
newpath = newpath + '/';
}
} catch (e) {
//console.log("Error!");
//console.log(e);
}
return newpath;
});
paths.pop();
callback(err,paths.join('\n'),stderr);
}
});
break;
case 'Linux' :
case 'Darwin' :
_cp.exec('find "'+dir+'"',
function(err, stdout, stderr)
{
if( err )
callback(err,stdout,stderr);
else {
let paths = stdout.slice(0,-1),
newpaths = paths.split('\n').map(function(path) {
if (_fs.lstatSync(path).isDirectory()) {
return path + "/";
} else return path;
});
callback(err,newpaths.join('\n'),stderr);
}
});
break;
default:
throw 'unsupported OS :: '+_os.type();
}
};
/* NOTE :: in MS-DOS mkdir, trying to make an already existing directory
triggers an error which we ignore */
exports.mkdirs =
function(dir,callback)
{
//guard against injection
dir = dir.split(';')[0];
let split_dir = dir.split('/'),
curr_dir = '';
for (let i in split_dir) {
curr_dir += split_dir[i] + '/';
if (!_fs.existsSync(curr_dir)) {
_fs.mkdir(curr_dir, 484, function(err) {if (err) {if (err.code != 'EEXIST') callback(err);}});
}
}
callback();
};
exports.mv =
function(src,dest,callback)
{
//guard against injection
src = src.split(';')[0];
dest = dest.split(';')[0];
let split_string = src.split('/');
_fs.rename(src, dest + split_string[split_string.length-1],callback);
};
exports.rmdirs =
function(dir,callback)
{
//guard against injection
dir = dir.split(';')[0];
_fs.rmdir(dir, callback)
};
// http://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty
exports.deleteFolderRecursive = function(path) {
//guard against injection
path = path.split(';')[0];
if( _fs.existsSync(path) ) {
_fs.readdirSync(path).forEach(function(file,index){
let curPath = path + "/" + file;
if(_fs.lstatSync(curPath).isDirectory()) { // recurse
exports.deleteFolderRecursive(curPath);
} else { // delete file
_fs.unlinkSync(curPath);
}
});
_fs.rmdirSync(path);
}
};