-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
102 lines (93 loc) · 2.58 KB
/
command.js
File metadata and controls
102 lines (93 loc) · 2.58 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
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
var exports = module.exports;
var fs = require('fs');
var pwd = function(){
var workDir = process.cwd();
process.stdout.write( workDir );
process.stdout.write('\nprompt > ');
}
var date = function(){
var date = new Date().toString();
process.stdout.write( date );
process.stdout.write('\nprompt > ');
}
var ls = function(){
fs.readdir('.', function(err, files) {
if (err) throw err;
files.forEach(function(file) {
process.stdout.write(file.toString() + "\n");
})
process.stdout.write("prompt > ");
});
}
var echo = function(cmdLine) {
var string = cmdLine.slice(1).join(' ') || null;
if(string) process.stdout.write( string );
else {
process.stdout.write( "Error: echo takes string as an argument" );
}
process.stdout.write('\nprompt > ');
};
var cat = function(cmdLine) {
cmdLine.forEach((el,index) => {
if (index !== 0) {
fs.readFile(el, 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
if(index == cmdLine.length - 1) {
process.stdout.write('\nprompt > ');
}
});
}
});
};
var head = function(cmdLine) {
var hasNotCount = isNaN(cmdLine[1]);
var count = hasNotCount ? 5 : Number(cmdLine[1]);
var startPoint = hasNotCount ? 0 : 1;
cmdLine.forEach((el,index) => {
if (index > startPoint) {
fs.readFile(el, 'utf8', (err, data) => {
if (err) throw err;
var data = data.split("\n").slice(0, count).join('\n');
console.log(data);
if(index === cmdLine.length -1) {
process.stdout.write('prompt > ');
}
});
}
});
};
var tail = function(cmdLine) {
var hasNotCount = isNaN(cmdLine[1]);
var count = hasNotCount ? 5 : Number(cmdLine[1]);
var startPoint = hasNotCount ? 0 : 1;
cmdLine.forEach((el,index) => {
if (index > startPoint) {
fs.readFile(el, 'utf8', (err, data) => {
if (err) throw err;
var data = data.split("\n").reverse().slice(0, count).reverse().join('\n');
console.log(data);
if(index === cmdLine.length -1) {
process.stdout.write('prompt > ');
} else {
process.stdout.write('\n');
}
});
}
});
};
exports.pwd = pwd;
exports.date = date;
exports.ls = ls;
exports.echo = echo;
exports.cat = cat;
exports.head = head;
exports.tail = tail;
// if(cmd === "date") {
// var date = new Date().toString();
// process.stdout.write( date );
// process.stdout.write('\nprompt > ');
// } else {
// process.stdout.write('You typed: ' + cmd);
// process.stdout.write('\nprompt > ');
// }