-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
81 lines (70 loc) · 1.79 KB
/
commands.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
var fs = require('fs');
var request = require('request');
function pwd(stdin, filename, done) {
done(process.cwd() + '\n');
}
function date(stdin, filename, done) {
done(Date().toString() + '\n');
}
function ls(stdin, filename, done) {
var output = '';
fs.readdir('.', function(err, files) {
if (err) throw err;
files.forEach(function(file) {
output += file.toString() + '\n';
});
done(output);
});
}
function echo(stdin, filename, done) {
if (filename[0] === '$PATH')
done(process.env.PATH + ' ' + filename.slice(1).join(' ') + '\n');
else
done(filename.join(' ') + '\n');
}
function cat(stdin, filename, done) {
filename.forEach(function(file) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err;
else done(data);
});
});
}
function head(stdin, filename, done) {
var _numLines = 5;
filename.forEach(function(file) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err;
else done(data.split('\n').slice(0, _numLines).join('\n') + '\n');
});
});
}
function tail(stdin, filename, done) {
var _numLines = -5;
filename.forEach(function(file) {
fs.readFile(file, 'utf8', function(err, data) {
if (err) throw err;
else done(data.split('\n').slice(_numLines).join('\n') + '\n');
});
});
}
function curl(stdin, filename, done) {
request(filename[0], function (error, response, body) {
done('error:' + error + '\n' +
// Print the error if one occurred
// Print the response status code if a response was received
'statusCode:' + response && response.statusCode + '\n' +
// Print the HTML
'body:' + body + '\n');
});
}
module.exports = {
pwd: pwd,
date: date,
ls: ls,
echo: echo,
cat: cat,
head: head,
tail: tail,
curl: curl
};