-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
61 lines (51 loc) · 1.5 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
var fs = require('fs')
exports.pwd = function() {
process.stdout.write( process.argv[1] )
process.stdout.write('\nprompt > ')
}
exports.date = function() {
process.stdout.write( new Date().toLocaleString() )
process.stdout.write('\nprompt > ')
}
exports.ls = function() {
fs.readdir('.', function(err, files) {
if (err) throw err
files.forEach(function(file) {
process.stdout.write(file.toString() + "\n")
})
process.stdout.write('\nprompt > ')
})
}
exports.echo = function(str){
if (str[0] === "$"){
process.stdout.write(`${str}`);
} else {
process.stdout.write(str.trim());
}
process.stdout.write('\nprompt > ')
}
exports.cat = function(fileName){
fs.readFile(fileName, 'utf8', function(err, data){
if (err) throw err;
process.stdout.write('\n' +data);
})
setTimeout(function(){process.stdout.write('\nprompt > ')},50)
}
exports.head = function(fileName){
fs.readFile(fileName, 'utf8', function(err, data){
if (err) throw err;
let head = data.split('\n').slice(0,4).join("\n");
process.stdout.write('\n' + head);
})
setTimeout(function(){process.stdout.write('\nprompt > ')},50)
}
exports.tail = function(fileName){
fs.readFile(fileName, 'utf8', function(err, data){
if (err) throw err;
let tail = data.split('\n');
tail = tail.slice(tail.length-5).join("\n");
process.stdout.write('\n' + tail);
})
setTimeout(function(){process.stdout.write('\nprompt > ')},50)
}
// process.stdout.write( 'Sorry... don\'t know that one.' )