From 3a448e975b1a7fbb56c44afba6a8ed7b04d7e2bf Mon Sep 17 00:00:00 2001 From: Jon Church Date: Mon, 18 Mar 2019 13:16:05 -0400 Subject: [PATCH 1/4] update code fences --- .../REPL/how-to-create-a-custom-repl.md | 120 ++++++++++-------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md index 8cf757f03b2bd..7fef762691843 100644 --- a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md +++ b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md @@ -8,39 +8,43 @@ difficulty: 2 layout: knowledge-post.hbs --- -Node allows users to create their own REPLs with the [repl module](https://nodejs.org/docs/v0.4.10/api/repl.html). Its basic use looks like this: +Node allows users to create their own REPLs with the [repl module](https://nodejs.org/api/repl.html). Its basic use looks like this: - repl.start(prompt, stream); +```js +repl.start(prompt, stream); +``` `prompt` is a string that's used for the prompt of your REPL and defaults to "> ". `stream` is the stream that the repl listens on and defaults to `process.stdin`. When you run `node` from the command prompt, what it's doing in the background is running `repl.start()` to give you the standard REPL. However, the repl is pretty flexible. Here's an example that shows this off: - #!/usr/bin/env node +```js +#!/usr/bin/env node - var net = require("net"), - repl = require("repl"); +var net = require("net"), + repl = require("repl"); - var mood = function () { - var m = [ "^__^", "-___-;", ">.<", "<_>" ]; - return m[Math.floor(Math.random()*m.length)]; - }; +var mood = function () { + var m = [ "^__^", "-___-;", ">.<", "<_>" ]; + return m[Math.floor(Math.random()*m.length)]; +}; - //A remote node repl that you can telnet to! - net.createServer(function (socket) { - var remote = repl.start("node::remote> ", socket); - //Adding "mood" and "bonus" to the remote REPL's context. - remote.context.mood = mood; - remote.context.bonus = "UNLOCKED"; - }).listen(5001); +//A remote node repl that you can telnet to! +net.createServer(function (socket) { + var remote = repl.start("node::remote> ", socket); + //Adding "mood" and "bonus" to the remote REPL's context. + remote.context.mood = mood; + remote.context.bonus = "UNLOCKED"; +}).listen(5001); - console.log("Remote REPL started on port 5001."); +console.log("Remote REPL started on port 5001."); - //A "local" node repl with a custom prompt - var local = repl.start("node::local> "); +//A "local" node repl with a custom prompt +var local = repl.start("node::local> "); - // Exposing the function "mood" to the local REPL's context. - local.context.mood = mood; +// Exposing the function "mood" to the local REPL's context. +local.context.mood = mood; +``` This script creates *two* REPLs: One is normal excepting for its custom prompt, but the *other* is exposed via the net module so I can telnet to it! In addition, it uses the `context` property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other *doesn't really work*. @@ -48,53 +52,61 @@ In addition, all objects in the global scope will also be accessible to your REP Here's what happens when I run the script: - $ node repl.js - Remote REPL started on port 5001. - node::local> .exit - ^Cjosh@pidgey:/tmp/telnet$ node repl.js - Remote REPL started on port 5001. - node::local> mood() - '^__^' - node::local> bonus - ReferenceError: bonus is not defined - at [object Context]:1:1 - at Interface. (repl.js:171:22) - at Interface.emit (events.js:64:17) - at Interface._onLine (readline.js:153:10) - at Interface._line (readline.js:408:8) - at Interface._ttyWrite (readline.js:585:14) - at ReadStream. (readline.js:73:12) - at ReadStream.emit (events.js:81:20) - at ReadStream._emitKey (tty_posix.js:307:10) - at ReadStream.onData (tty_posix.js:70:12) +```shell +$ node repl.js +Remote REPL started on port 5001. +node::local> .exit +^Cjosh@pidgey:/tmp/telnet$ node repl.js +Remote REPL started on port 5001. +node::local> mood() +'^__^' +node::local> bonus +ReferenceError: bonus is not defined + at [object Context]:1:1 + at Interface. (repl.js:171:22) + at Interface.emit (events.js:64:17) + at Interface._onLine (readline.js:153:10) + at Interface._line (readline.js:408:8) + at Interface._ttyWrite (readline.js:585:14) + at ReadStream. (readline.js:73:12) + at ReadStream.emit (events.js:81:20) + at ReadStream._emitKey (tty_posix.js:307:10) + at ReadStream.onData (tty_posix.js:70:12) +``` As may be seen, the `mood` function is usable within the local REPL, but the `bonus` string is not. This is as expected. Now, here's what happens when I try to telnet to port 5001: - josh@pidgey:/tmp/telnet$ telnet localhost 5001 - Trying ::1... - Trying 127.0.0.1... - Connected to localhost. - Escape character is '^]'. - node::remote> mood() - '>.<' - node::remote> bonus - 'UNLOCKED' +```shell +$ telnet localhost 5001 +Trying ::1... +Trying 127.0.0.1... +Connected to localhost. +Escape character is '^]'. +node::remote> mood() +'>.<' +node::remote> bonus +'UNLOCKED' +``` As you can see, the `mood` function is *also* available over telnet! In addition, so is "bonus". As an interesting consequence of my actions, bonus is now also defined on the local REPL: - node::local> bonus - 'UNLOCKED' +```shell +node::local> bonus +'UNLOCKED' +``` It seems we "unlocked" the `bonus` string on the local REPL as well. As it turns out, any variables created in one REPL are also available to the other: - node::local> var node = "AWESOME!" +```shell +node::local> var node = "AWESOME!" - node::remote> node - 'AWESOME!' +node::remote> node +'AWESOME!' +``` As you can see, the node REPL is powerful and flexible. From 913144357698675878d4cfa096368f6b9da8eab3 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Mon, 18 Mar 2019 13:27:51 -0400 Subject: [PATCH 2/4] update content, remove first person "I" --- .../knowledge/REPL/how-to-create-a-custom-repl.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md index 7fef762691843..8a52e1f42c577 100644 --- a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md +++ b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md @@ -11,18 +11,20 @@ layout: knowledge-post.hbs Node allows users to create their own REPLs with the [repl module](https://nodejs.org/api/repl.html). Its basic use looks like this: ```js +var repl = require('repl') + repl.start(prompt, stream); ``` -`prompt` is a string that's used for the prompt of your REPL and defaults to "> ". `stream` is the stream that the repl listens on and defaults to `process.stdin`. When you run `node` from the command prompt, what it's doing in the background is running `repl.start()` to give you the standard REPL. +Above, `prompt` is a string that's used for the prompt of your REPL (which defaults to "> ") and `stream` is the stream that the repl listens on, defaulting to `process.stdin`. When you run the standalone `node` REPL from the command prompt, what it's doing in the background is running `repl.start()` to give you the standard REPL. However, the repl is pretty flexible. Here's an example that shows this off: ```js #!/usr/bin/env node -var net = require("net"), - repl = require("repl"); +var net = require("net"); +var repl = require("repl"); var mood = function () { var m = [ "^__^", "-___-;", ">.<", "<_>" ]; @@ -46,11 +48,11 @@ var local = repl.start("node::local> "); local.context.mood = mood; ``` -This script creates *two* REPLs: One is normal excepting for its custom prompt, but the *other* is exposed via the net module so I can telnet to it! In addition, it uses the `context` property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other *doesn't really work*. +This script creates *two* REPLs: One is normal excepting for its custom prompt, but the *other* is exposed via the net module so you can telnet to it! In addition, it uses the `context` property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other *doesn't really work*. In addition, all objects in the global scope will also be accessible to your REPLs. -Here's what happens when I run the script: +Here's what happens when you run the script: ```shell $ node repl.js @@ -77,7 +79,7 @@ ReferenceError: bonus is not defined As may be seen, the `mood` function is usable within the local REPL, but the `bonus` string is not. This is as expected. -Now, here's what happens when I try to telnet to port 5001: +Now, here's what happens when you try to telnet to port 5001: ```shell $ telnet localhost 5001 From b284be301907de9eda347d54c21416794c9ee16e Mon Sep 17 00:00:00 2001 From: Jon Church Date: Mon, 18 Mar 2019 13:39:26 -0400 Subject: [PATCH 3/4] test script, update example error message --- .../knowledge/REPL/how-to-create-a-custom-repl.md | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md index 8a52e1f42c577..6db5607393bf9 100644 --- a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md +++ b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md @@ -58,22 +58,14 @@ Here's what happens when you run the script: $ node repl.js Remote REPL started on port 5001. node::local> .exit -^Cjosh@pidgey:/tmp/telnet$ node repl.js +# -C + +$ node repl.js Remote REPL started on port 5001. node::local> mood() '^__^' node::local> bonus ReferenceError: bonus is not defined - at [object Context]:1:1 - at Interface. (repl.js:171:22) - at Interface.emit (events.js:64:17) - at Interface._onLine (readline.js:153:10) - at Interface._line (readline.js:408:8) - at Interface._ttyWrite (readline.js:585:14) - at ReadStream. (readline.js:73:12) - at ReadStream.emit (events.js:81:20) - at ReadStream._emitKey (tty_posix.js:307:10) - at ReadStream.onData (tty_posix.js:70:12) ``` As may be seen, the `mood` function is usable within the local REPL, but the From 352ec9365701b38c36d6c56c8eea3d6bd3edde60 Mon Sep 17 00:00:00 2001 From: Maledong Date: Tue, 26 Mar 2019 18:36:55 +0800 Subject: [PATCH 4/4] change the title change from "Node" to "Node.js" --- locale/en/knowledge/REPL/how-to-create-a-custom-repl.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md index 6db5607393bf9..2d6fe0ad6cc45 100644 --- a/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md +++ b/locale/en/knowledge/REPL/how-to-create-a-custom-repl.md @@ -8,7 +8,7 @@ difficulty: 2 layout: knowledge-post.hbs --- -Node allows users to create their own REPLs with the [repl module](https://nodejs.org/api/repl.html). Its basic use looks like this: +Node.js allows users to create their own REPLs with the [repl module](https://nodejs.org/api/repl.html). Its basic use looks like this: ```js var repl = require('repl')