-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.js
61 lines (50 loc) · 1.33 KB
/
command.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
var fs = require('fs');
var request = require('request');
var commands = {
getPWD: function (stdin, args, done){
done(process.cwd());
},
ls : function (stdin, args, done){
fs.readdir (process.cwd(), function (err, files) {
if (err) throw err;
var stringOutput = '';
files.forEach (function (file) {
stringOutput += file.toString() + '\n';
});
done(stringOutput);
});
},
getDate: function (stdin, args, done){
done(new Date().toString());
},
cat: function (stdin, args, done){
fs.readFile(args[0], (err,data) =>{
if(err) throw err;
done(data.toString());
});
},
head: function (stdin, args, done){
fs.readFile(args[0], (err,data) =>{
if(err) throw err;
var sentences = data.toString().split('\n');
done(sentences.slice(0,11).join('\n'));
});
},
tail: function (stdin, args, done){
fs.readFile(args[0], (err,data) =>{
if(err) throw err;
var sentences = data.toString().split('\n');
done(sentences.slice(sentences.length -10).join('\n'));
});
},
curl: function (stdin, args, done){
request(args[0], function (error, response, body){
if (error){
console.error('error');
} else {
done(body.toString());
}
});
}
};
module.exports = commands;