Created
August 16, 2022 16:53
-
-
Save rcoedo/66a2e78652d897d799a5011e4b17f71d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This gist contains the examples for the article "Mastering the Node.js REPL, Part Two". | |
- https://medium.com/trabe/mastering-the-node-js-repl-part-2-365c52a5203d | |
- https://rcoedo.com/blog/2018/08/06/mastering-the-node-js-repl-part-two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Repl = require("repl"); | |
// Print the welcome message | |
console.log(` | |
Hello, ${process.env.USER}! | |
You're running the Node.js REPL in ${process.cwd()}. | |
`); | |
// Start the REPL | |
Repl.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Repl = require("repl"); | |
// Color functions | |
const colors = { RED: "31", GREEN: "32", YELLOW: "33", BLUE: "34", MAGENTA: "35" }; | |
const colorize = (color, s) => `\x1b[${color}m${s}\x1b[0m`; | |
// Some useful stuff | |
const user = colorize(colors.MAGENTA, process.env.USER); | |
const cwd = colorize(colors.YELLOW, process.cwd()); | |
const say = message => () => console.log(message); | |
const sayWelcome = say(` | |
Hello, ${user}! | |
You're running the Node.js REPL in ${cwd}. | |
`); | |
// Print the welcome message | |
sayWelcome(); | |
// Start the REPL | |
Repl.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nodeVersion = colorize(colors.GREEN, `${process.title} ${process.version}`); | |
const prompt = `${nodeVersion} → `; | |
// Start the REPL | |
Repl.start({ prompt }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Our goodbye function | |
const sayBye = say(` | |
Goodbye, ${user}! | |
`); | |
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
// Listen for the exit event | |
repl.on("exit", sayBye); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require("util"); | |
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
repl.context.noop = () => {}; | |
repl.context.identity = x => x; | |
repl.context.isString = x => typeof x === "string" || x instanceof String; | |
repl.context.timeout = util.promisify(setTimeout); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function that takes an object o1 and returns another function | |
// that takes an object o2 to extend it with the o1 properties as | |
// read-only | |
const extendWith = properties => context => { | |
Object.entries(properties).forEach(([k, v]) => { | |
Object.defineProperty(context, k, { | |
configurable: false, | |
enumerable: true, | |
value: v, | |
}); | |
}); | |
}; | |
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
// Extend the REPL context as read-only | |
extendWith({ | |
noop: () => {}, | |
identity: x => x, | |
isString: x => typeof x === "string" || x instanceof String, | |
timeout: util.promisify(setTimeout), | |
})(repl.context); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Context initializer | |
const initializeContext = extendWith({ | |
utils: { | |
noop: () => {}, | |
identity: x => x, | |
isString: x => typeof x === "string" || x instanceof String, | |
timeout: util.promisify(setTimeout), | |
}, | |
}); | |
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
// Initialize our context | |
initializeContext(repl.context); | |
// Listen for the reset event | |
repl.on("reset", initializeContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
// Define our custom commands | |
repl.defineCommand("welcome", { | |
help: "Prints the welcome message again", | |
action() { | |
this.clearBufferedCommand(); | |
sayWelcome(); | |
this.displayPrompt(); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
Object.defineProperty(repl.context, "noop", { | |
configurable: false, | |
enumerable: true, | |
value: () => {}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Repl = require("repl"); | |
const util = require("util"); | |
// Color functions | |
const colors = { RED: "31", GREEN: "32", YELLOW: "33", BLUE: "34", MAGENTA: "35" }; | |
const colorize = (color, s) => `\x1b[${color}m${s}\x1b[0m`; | |
// Function that takes an object o1 and returns another function | |
// that takes an object o2 to extend it with the o1 properties as | |
// read-only | |
const extendWith = properties => context => { | |
Object.entries(properties).forEach(([k, v]) => { | |
Object.defineProperty(context, k, { | |
configurable: false, | |
enumerable: true, | |
value: v, | |
}); | |
}); | |
}; | |
// Function that takes an object o1 with shape { key: command } and | |
// returns another function that takes the repl and defines the commands | |
// in it | |
const defineCommands = commands => repl => { | |
Object.entries(commands).forEach(([k, v]) => { | |
repl.defineCommand(k, v); | |
}); | |
}; | |
// Context initializer | |
const initializeContext = extendWith({ | |
noop: () => {}, | |
identity: x => x, | |
isString: x => typeof x === "string" || x instanceof String, | |
timeout: util.promisify(setTimeout), | |
}); | |
// Some useful stuff | |
const user = colorize(colors.MAGENTA, process.env.USER); | |
const cwd = colorize(colors.YELLOW, process.cwd()); | |
const say = message => () => console.log(message); | |
const sayWelcome = say(` | |
Hello, ${user}! | |
You're running the Node.js REPL in ${cwd}. | |
`); | |
const sayBye = say(` | |
Goodbye, ${user}! | |
`); | |
const nodeVersion = colorize(colors.GREEN, `${process.title} ${process.version}`); | |
const prompt = `${nodeVersion} → `; | |
// Print the welcome message | |
sayWelcome(); | |
// Start the REPL | |
const repl = Repl.start({ prompt }); | |
// Initialize our context | |
initializeContext(repl.context); | |
// Define our custom commands | |
defineCommands({ | |
welcome: { | |
help: "Prints the welcome message", | |
action() { | |
this.clearBufferedCommand(); | |
sayWelcome(); | |
this.displayPrompt(); | |
}, | |
}, | |
})(repl); | |
// Listen for the server events | |
repl.on("reset", initializeContext); | |
repl.on("exit", sayBye); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Repl = require("repl"); | |
global.noop = () => {}; | |
global.identity = x => x; | |
global.isString = x => typeof x === "string" || x instanceof String; | |
const repl = Repl.start({ useGlobal: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment