Skip to content

Commit 0257d19

Browse files
committed
Updated shell
1 parent a1d9e9c commit 0257d19

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

_posts/2012-11-2-shell.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ The output of your program might look something like this:
4040

4141
{% highlight javascript %}
4242
foo
43-
<Buffer 64>
43+
<Buffer 61 73 64 0a>
4444
bar
45-
<Buffer 64>
45+
<Buffer 62 61 72 0a>
4646
{% endhighlight %}
4747

4848
You can see that we're printing out a `Buffer` object. That's because the `input` variable passed in to our `function (input) { ... }` callback does not contain the string value of your input directly.
@@ -70,6 +70,8 @@ command [args...]
7070
We can use a handy regular expression to separate the arguments from the command: `/(\w+)(.*)/`. We'll then parse our arguments by splitting on white space.
7171

7272
{% highlight javascript %}
73+
var stdin = process.openStdin();
74+
7375
stdin.on('data', function (input) {
7476
var matches = input.toString().match(/(\w+)(.*)/);
7577
var command = matches[1].toLowerCase();
@@ -124,7 +126,7 @@ To clarify what's happening above, here's sample output of executing the regular
124126
input: 'cmd_name arg1 arg2'] // matches[4]
125127
{% endhighlight %}
126128

127-
We are accessing ```matches[1]``` because it's the first group (groups are specified with the parenthesis). If you are unfamiliar with regular expressions, a good source to learn more is at [http://regular-expressions.info](http://www.regular-expressions.info/), in spite of (because of?) the awesome color scheme.
129+
We are accessing ```matches[1]``` because it's the first group (groups are specified with the parenthesis). If you are unfamiliar with regular expressions, a good source to learn more is at [http://regular-expressions.info](http://www.regular-expressions.info/).
128130

129131
Now, jump back to your terminal and give our shell a try!
130132

@@ -197,6 +199,26 @@ stdin.on('data', function (input) {
197199
});
198200
{% endhighlight %}
199201

202+
#### As a side note about our commands object
203+
204+
The commands object can get a bit hairy when we're nesting so many brackets deep. We can instead define our commands object like so:
205+
206+
{% highlight javascript %}
207+
var commands = {};
208+
209+
commands['pwd'] = function () {
210+
console.log(process.cwd());
211+
};
212+
213+
commands['ls'] = function (args) {
214+
fs.readdir(args[0] || process.cwd(), function (err, entries) {
215+
entries.forEach(function (e) {
216+
console.log(e);
217+
});
218+
});
219+
};
220+
{% endhighlight %}
221+
200222
## Interacting with HTTP: Downloading a File
201223

202224
Similarly to the `fs` module, Node.js also contains a core `http` module which can be used to act as a HTTP client or server. In the next lab you'll be creating a HTTP server, but for now, we'll focus on creating a simple `wget` command to download a file.
@@ -229,7 +251,7 @@ var commands = {
229251
};
230252
{% endhighlight %}
231253

232-
Let's talk about what's happening in that callback.
254+
Let's talk about what's happening in the callback provided to `http.get` callback.
233255

234256
First, we're creating a [writable stream](http://nodejs.org/api/stream.html#stream_class_stream_writable) to our file system using [fs.createWriteStream](http://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options), named `file` (the second argument, or `'download'` by default). This will create or overwrite the file.
235257

0 commit comments

Comments
 (0)