-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathn4m.max-fs.js
258 lines (226 loc) · 6.05 KB
/
n4m.max-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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// ---------------------------------------------------------------------------
//
// n4m.max-fs.js
//
// Provide access to the node.js "fs" wrapper functions as well
// as other file system functions.
//
// ---------------------------------------------------------------------------
const fs = require("fs");
const path = require("path");
const util = require("util");
const maxAPI = require("max-api");
const os = require("os");
const access = util.promisify(fs.access);
const copyFile = util.promisify(fs.copyFile);
const lstat = util.promisify(fs.lstat);
const mkdir = util.promisify(fs.mkdir);
const readdir = util.promisify(fs.readdir);
const rmdir = util.promisify(fs.rmdir);
const unlink = util.promisify(fs.unlink);
const utimes = util.promisify(fs.utimes);
const writeFile = util.promisify(fs.writeFile);
let currentFolder = process.cwd();
const decodePaths = (arr) => {
return arr.reduce((a, c) => {
if (typeof (c) === "string") {
return a.concat(c);
}
return a;
}, []);
};
const expandTilde = (p) => {
if (p.startsWith("~")) return os.homedir() + p.substr(1);
return p;
};
async function findRecursiveHelper({ dir, fileFilter, depth }) {
let result = [];
let files;
try {
files = await readdir(dir);
} catch (err) {
maxAPI.post(err.message);
return result;
}
if (!files || !files.length) return result;
for (let i = 0, il = files.length; i < il; i++) {
try {
const fName = files[i];
const fPath = path.join(dir, fName);
const stats = await lstat(fPath);
if (stats.isFile() && fileFilter.test(fName)) {
result.push(fPath);
} else if (stats.isDirectory() && depth !== 0) {
const dirResult = await findRecursiveHelper({
dir: fPath,
fileFilter,
depth: depth - 1
});
result = result.concat(dirResult);
}
} catch (err) {
// simply skip entry
maxAPI.post(err.message);
}
}
return result;
}
const simpleGlobToRegexp = (glob) => {
const regStr = glob
.replace(/\./g, "\\.")
.replace(/\*\*$/g, "(.+)")
.replace(/(?:\*\*\/|\*\*|\*)/g, (match) => {
switch (match) {
case "*": return "([^/]+)";
case "**": return "(.+/)?([^/]+)";
case "**/": return "(.+/)?";
default: return "";
}
});
return new RegExp(regStr);
};
const findRecursive = ({ root, fileGlob, depth }) => {
const fileFilter = simpleGlobToRegexp(fileGlob);
return findRecursiveHelper({
dir: root,
fileFilter,
depth: !depth ? -1 : depth
});
};
const handlers = {
pwd: () => {
maxAPI.outlet(currentFolder);
},
ls: async (...args) => {
const myPaths = decodePaths(args);
try {
let newPath = currentFolder;
if (myPaths.length) newPath = expandTilde(myPaths[0]);
const files = await readdir(newPath);
maxAPI.outlet(files);
} catch (e) {
maxAPI.post("Error: " + e.message);
}
},
cd: (...args) => {
const myPaths = decodePaths(args);
try {
if (myPaths.length) {
let newPath = expandTilde(myPaths[0]);
process.chdir(newPath);
currentFolder = process.cwd();
maxAPI.outlet(currentFolder);
} else {
maxAPI.post("The cd command needs an argument!");
}
} catch (e) {
maxAPI.post("Error: " + e.message);
}
},
mkdir: async (...args) => {
try {
if (args.length) {
let newPath = expandTilde(args[0]);
await mkdir(newPath);
maxAPI.post(`mkdir: ${newPath}`);
} else {
maxAPI.post("The mkdir command needs an argument!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
},
rmdir: async (...args) => {
try {
if (args.length) {
let newPath = expandTilde(args[0]);
await rmdir(newPath);
maxAPI.post(`rmdir: ${newPath}`);
} else {
maxAPI.post("The rmdir command needs an argument!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
},
touch: async (...args) => {
if (args.length) {
let newPath = expandTilde(args[0]);
try {
await access(newPath, fs.constants.F_OK);
let tmpDate = new Date();
await utimes(newPath, tmpDate, tmpDate);
maxAPI.post("touch: time updated");
} catch (access_err) {
try {
await writeFile(newPath, "");
maxAPI.post(`touch: new file ${newPath}`);
} catch (e) {
maxAPI.post("Error " + e.message);
}
}
} else {
maxAPI.post("The touch command needs an argument!");
}
},
rm: async (...args) => {
try {
if (args.length) {
let newPath = expandTilde(args[0]);
await unlink(newPath);
maxAPI.post(`unlink: ${newPath}`);
} else {
maxAPI.post("The rm command needs an argument!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
},
cp: async (...args) => {
try {
if (args.length === 2) {
const fromFile = expandTilde(args[0]);
const toFile = expandTilde(args[1]);
await copyFile(fromFile, toFile, fs.constants.COPYFILE_EXCL);
maxAPI.post(`cp: ${fromFile} to ${toFile}`);
} else {
maxAPI.post("The cp command needs exactly two arguments!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
},
mv: async (...args) => {
try {
if (args.length === 2) {
const fromFile = expandTilde(args[0]);
const toFile = expandTilde(args[1]);
await copyFile(fromFile, toFile, fs.constants.COPYFILE_EXCL);
await unlink(fromFile);
maxAPI.post(`mv: ${fromFile} to ${toFile}`);
} else {
maxAPI.post("The mv command needs exactly two arguments!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
},
locate: async (...args) => {
try {
if (args.length) {
const root = expandTilde(args[0]);
const fileGlob = args.length > 1 ? args[1] : "*";
const depth = args.length > 2 ? args[2] : null;
if (!fs.existsSync(root) || !(await lstat(root)).isDirectory()) throw new Error(`${root} is not a valid directory path`);
if (depth !== null && isNaN(depth) || depth < 0) throw new Error(`${args[2]} is not a valid depth argument`);
const results = await findRecursive({ root, fileGlob, depth });
if (results && results.length) maxAPI.outlet(results);
} else {
maxAPI.post("The locate command requires at least one argument!");
}
} catch (e) {
maxAPI.post("Error " + e.message);
}
}
};
maxAPI.addHandlers(handlers);