Skip to content

Commit 05a11a3

Browse files
committed
Added a simple shell using node core.
1 parent 2ffeaf6 commit 05a11a3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/shell.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var path = require('path');
2+
var fs = require('fs');
3+
var stdin = process.openStdin();
4+
5+
var commands = {
6+
'ls': function (args) {
7+
fs.readdir(args[0] || process.cwd(), function (err, entries) {
8+
entries.forEach(function (e) {
9+
console.log(e);
10+
});
11+
});
12+
},
13+
'pwd': function () {
14+
console.log(process.cwd());
15+
},
16+
'cd': function (args) {
17+
process.chdir(path.resolve(process.cwd(), args[0]));
18+
},
19+
'tail': function (args) {
20+
21+
}
22+
};
23+
24+
stdin.on('data', function (d) {
25+
var matches = d.toString().match(/(\w+)(.*)/i);
26+
var command = matches[1].toLowerCase();
27+
var args = matches[2].trim().split(/\s/);
28+
29+
commands[command](args);
30+
});

0 commit comments

Comments
 (0)