doc: add esm examples to node:repl · nodejs/node@b1cec2c
@@ -10,7 +10,11 @@ The `node:repl` module provides a Read-Eval-Print-Loop (REPL) implementation
1010that is available both as a standalone program or includible in other
1111applications. It can be accessed using:
121213-```js
13+```mjs
14+import repl from 'node:repl';
15+```
16+17+```cjs
1418const repl = require('node:repl');
1519```
1620@@ -106,7 +110,14 @@ The default evaluator provides access to any variables that exist in the global
106110scope. It is possible to expose a variable to the REPL explicitly by assigning
107111it to the `context` object associated with each `REPLServer`:
108112109-```js
113+```mjs
114+import repl from 'node:repl';
115+const msg = 'message';
116+117+repl.start('> ').context.m = msg;
118+```
119+120+```cjs
110121const repl = require('node:repl');
111122const msg = 'message';
112123@@ -124,7 +135,19 @@ $ node repl_test.js
124135Context properties are not read-only by default. To specify read-only globals,
125136context properties must be defined using `Object.defineProperty()`:
126137127-```js
138+```mjs
139+import repl from 'node:repl';
140+const msg = 'message';
141+142+const r = repl.start('> ');
143+Object.defineProperty(r.context, 'm', {
144+ configurable: false,
145+ enumerable: true,
146+ value: msg,
147+});
148+```
149+150+```cjs
128151const repl = require('node:repl');
129152const msg = 'message';
130153@@ -280,20 +303,34 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
280303provided. This can be used, for instance, to implement fully customized REPL
281304applications.
282305283-The following illustrates a hypothetical example of a REPL that performs
284-translation of text from one language to another:
306+The following illustrates an example of a REPL that squares a given number:
285307286-```js
308+```mjs
309+import repl from 'node:repl';
310+311+function byThePowerOfTwo(number) {
312+return number * number;
313+}
314+315+function myEval(cmd, context, filename, callback) {
316+callback(null, byThePowerOfTwo(cmd));
317+}
318+319+repl.start({ prompt: 'Enter a number: ', eval: myEval });
320+```
321+322+```cjs
287323const repl = require('node:repl');
288-const { Translator } = require('translator');
289324290-const myTranslator = new Translator('en', 'fr');
325+function byThePowerOfTwo(number) {
326+return number * number;
327+}
291328292329function myEval(cmd, context, filename, callback) {
293-callback(null, myTranslator.translate(cmd));
330+callback(null, byThePowerOfTwo(cmd));
294331}
295332296-repl.start({ prompt: '> ', eval: myEval });
333+repl.start({ prompt: 'Enter a number: ', eval: myEval });
297334```
298335299336#### Recoverable errors
@@ -354,7 +391,21 @@ To fully customize the output of a [`repl.REPLServer`][] instance pass in a new
354391function for the `writer` option on construction. The following example, for
355392instance, simply converts any input text to upper case:
356393357-```js
394+```mjs
395+import repl from 'node:repl';
396+397+const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter });
398+399+function myEval(cmd, context, filename, callback) {
400+callback(null, cmd);
401+}
402+403+function myWriter(output) {
404+return output.toUpperCase();
405+}
406+```
407+408+```cjs
358409const repl = require('node:repl');
359410360411const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter });
@@ -380,7 +431,16 @@ added: v0.1.91
380431Instances of `repl.REPLServer` are created using the [`repl.start()`][] method
381432or directly using the JavaScript `new` keyword.
382433383-```js
434+```mjs
435+import repl from 'node:repl';
436+437+const options = { useColors: true };
438+439+const firstInstance = repl.start(options);
440+const secondInstance = new repl.REPLServer(options);
441+```
442+443+```cjs
384444const repl = require('node:repl');
385445386446const options = { useColors: true };
@@ -424,7 +484,20 @@ reference to the `context` object as the only argument.
424484This can be used primarily to re-initialize REPL context to some pre-defined
425485state:
426486427-```js
487+```mjs
488+import repl from 'node:repl';
489+490+function initializeContext(context) {
491+context.m = 'test';
492+}
493+494+const r = repl.start({ prompt: '> ' });
495+initializeContext(r.context);
496+497+r.on('reset', initializeContext);
498+```
499+500+```cjs
428501const repl = require('node:repl');
429502430503function initializeContext(context) {
@@ -475,7 +548,25 @@ properties:
475548476549The following example shows two new commands added to the REPL instance:
477550478-```js
551+```mjs
552+import repl from 'node:repl';
553+554+const replServer = repl.start({ prompt: '> ' });
555+replServer.defineCommand('sayhello', {
556+ help: 'Say hello',
557+action(name) {
558+this.clearBufferedCommand();
559+console.log(`Hello, ${name}!`);
560+this.displayPrompt();
561+ },
562+});
563+replServer.defineCommand('saybye', function saybye() {
564+console.log('Goodbye!');
565+this.close();
566+});
567+```
568+569+```cjs
479570const repl = require('node:repl');
480571481572const replServer = repl.start({ prompt: '> ' });
@@ -637,7 +728,14 @@ The `repl.start()` method creates and starts a [`repl.REPLServer`][] instance.
637728638729If `options` is a string, then it specifies the input prompt:
639730640-```js
731+```mjs
732+import repl from 'node:repl';
733+734+// a Unix style prompt
735+repl.start('$ ');
736+```
737+738+```cjs
641739const repl = require('node:repl');
642740643741// a Unix style prompt
@@ -709,7 +807,43 @@ separate I/O interfaces.
709807The following example, for instance, provides separate REPLs on `stdin`, a Unix
710808socket, and a TCP socket:
711809712-```js
810+```mjs
811+import net from 'node:net';
812+import repl from 'node:repl';
813+import process from 'node:process';
814+815+let connections = 0;
816+817+repl.start({
818+ prompt: 'Node.js via stdin> ',
819+ input: process.stdin,
820+ output: process.stdout,
821+});
822+823+net.createServer((socket) => {
824+ connections += 1;
825+repl.start({
826+ prompt: 'Node.js via Unix socket> ',
827+ input: socket,
828+ output: socket,
829+ }).on('exit', () => {
830+socket.end();
831+ });
832+}).listen('/tmp/node-repl-sock');
833+834+net.createServer((socket) => {
835+ connections += 1;
836+repl.start({
837+ prompt: 'Node.js via TCP socket> ',
838+ input: socket,
839+ output: socket,
840+ }).on('exit', () => {
841+socket.end();
842+ });
843+}).listen(5001);
844+```
845+846+```cjs
713847const net = require('node:net');
714848const repl = require('node:repl');
715849let connections = 0;