Skip to content

Instantly share code, notes, and snippets.

@TheBrenny
Last active March 2, 2023 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBrenny/ef256cb1edc9a74c0e8279a56fb13e9d to your computer and use it in GitHub Desktop.
Save TheBrenny/ef256cb1edc9a74c0e8279a56fb13e9d to your computer and use it in GitHub Desktop.
A set of global variables to help in a NodeJS REPL.

Save the below nodeRepl.js file to somewhere special (like ~/.whatever). To take advantage of it in a Node REPL, you need to require it before, or when in, the REPL. An easy way to do this is by running node.exe -r ~/.whatever/nodeRepl.js, so it can have all the globals prepared for you.

Also, there's no notable lag hit, which is a big win!

I've also added my Windows Terminal profiles which I use to help me win!

ENJOY!

const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const inspector = require("inspector");
let config = fs.readFileSync(path.join(__dirname, 'config.json')) || "{}";
config = JSON.parse(config);
let inspectorSession = null;
Object.defineProperties(globalThis, {
clear: {
get: console.clear
},
exit: {
get: process.exit
},
pwd: {
get: process.cwd,
},
cd: {
value: (dir) => (typeof dir === "undefined" ? process.cwd() : process.chdir(dir), process.cwd()),
},
ls: {
get: () => {
let objects = readdir(() => true);
console.log(objects.length > 0 ? objects.join("\n") : " Nothing here...");
return objects.length;
}
},
lsf: {
get: () => {
let objects = readdir(stats => !stats.isDirectory());
console.log(objects.length > 0 ? objects.join("\n") : " No files...");
return objects.length;
}
},
lsd: {
get: () => {
let objects = readdir(stats => stats.isDirectory());
console.log(objects.length > 0 ? objects.join("\n") : " No directories...");
return objects.length;
}
},
inspect: {
get: () => {
let host = config.inspect.host;
let port = Math.floor(Math.random() * (65535 - 1024) + 1024);
let cmd = config.inspect.cmd.replace("${target}", `${host}:${port}`);
let terminal = config.inspect.console.replace("${cmd}", cmd);
console.log(terminal);
inspector.open(port, host, false);
child_process.exec(terminal);
inspector.waitForDebugger();
}
},
requireReload: {
value: function (v) {
const reqCache = Object.keys(require.cache);
const deleteCache = (c) => (delete require.cache[c]);
if (v === undefined) {
let ret = 0;
for (const c of reqCache) {
deleteCache(reqCache[c]);
if (fs.existsSync(c)) {
require(path.resolve(c));
ret++;
}
}
return ret;
} else {
if (v.startsWith("./")) {
try {
v = fs.realpathSync(path.resolve(pwd, v));
if (path.extname(v) == "") v += ".js";
} catch (e) { }
}
deleteCache(v);
return require(v);
}
}
}
});
// globalThis.cd._ = () => globalThis.cd("..");
Object.defineProperties(globalThis.cd, {
"_": {
get: () => globalThis.cd("..")
}
});
let fixReplCounter = 0;
let fixRepl = () => {
try {
repl.writer.options.depth = Infinity;
} catch(e) {
fixReplCounter++;
if(fixReplCounter < 200) setTimeout(fixRepl, 300);
else console.warn("Couldn't reset the repl.");
}
};
fixRepl();
let readdir = (predicate) => {
let objects = fs.readdirSync(process.cwd()) || [];
let bigSize = 0;
objects = objects.map((o, i) => {
let ret = [];
let t = "";
let stats = fs.statSync(path.join(process.cwd(), o));
if (!predicate(stats)) {
objects[i] = null;
return;
}
// Filetype
// chmod
let mode = stats.mode;
t = stats.isFile() ? "-" : stats.isDirectory() ? "D" : stats.isSymbolicLink() ? "L" : "?";
t += (mode >> 8 & 0b1) == 1 ? "r" : "-";
t += (mode >> 7 & 0b1) == 1 ? "w" : "-";
t += (mode >> 6 & 0b1) == 1 ? "x" : "-";
t += (mode >> 5 & 0b1) == 1 ? "r" : "-";
t += (mode >> 4 & 0b1) == 1 ? "w" : "-";
t += (mode >> 3 & 0b1) == 1 ? "x" : "-";
t += (mode >> 2 & 0b1) == 1 ? "r" : "-";
t += (mode >> 1 & 0b1) == 1 ? "w" : "-";
t += (mode >> 0 & 0b1) == 1 ? "x" : "-";
ret.push(t);
// last modified
let mtime = new Date(Math.max(stats.mtime, stats.ctime));
t = "";
t += (mtime.getDate() + "").padStart(2, "0") + " ";
t += mtime.toLocaleString('default', {
month: 'long'
}).substr(0, 3) + " ";
t += (mtime.getFullYear() + "").substr(2) + " ";
t += (mtime.getHours() + "").padStart(2, "0") + ":";
t += (mtime.getMinutes() + "").padStart(2, "0") + ":";
t += (mtime.getSeconds() + "").padStart(2, "0");
ret.push(t);
// Filesize
t = stats.isFile() ? stats.size + "b" : "--";
ret.push(t);
bigSize = Math.max(bigSize, t.length);
// The actual name
ret.push(o);
return ret;
}).filter(e => e != null).map((e) => {
e[0] = " " + e[0];
e[2] = ("" + e[2]).padStart(bigSize, " ");
return e.join(" ");
});
return objects;
};
{
"acrylicOpacity": 0.10000000000000001,
"backgroundImage": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs-bg.png",
"backgroundImageOpacity": 0.31,
"commandline": "node.exe --trace-deprecation --trace-warnings --experimental-repl-await -r \"C:/Users/TheBrenny/My Space/Util/nodeRepl.js\" -i",
"guid": "{b2d5980e-b1a0-4f0c-b8db-14f3183613c3}",
"hidden": false,
"icon": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs.ico",
"name": "NodeJS",
"useAcrylic": false
},
{
"acrylicOpacity": 0.10000000000000001,
"backgroundImage": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs-bg.png",
"backgroundImageOpacity": 0.31,
"commandline": "nvs.cmd && node.exe --trace-deprecation --trace-warnings --experimental-repl-await -r \"C:/Users/TheBrenny/My Space/Util/nodeRepl.js\" -i",
"guid": "{44fcbcd7-4860-4efa-a1bc-178535a00e62}",
"icon": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs.ico",
"name": "NVS",
"useAcrylic": false
},
{
"acrylicOpacity": 0.10000000000000001,
"backgroundImage": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs-inspector-bg.png",
"backgroundImageAlignment": "topRight",
"backgroundImageOpacity": 0.31,
"commandline": "cmd /C echo Use 'inspect' from the NodeJS term && pause",
"hidden": false,
"icon": "C:\\Users\\TheBrenny\\My Space\\Util\\Windows Terminal Settings\\nodejs-inspector.png",
"name": "NodeJS Inspector",
"useAcrylic": false,
"guid": "{2f26f98c-ba5b-b87d-4d3f-f1b871d403c5}"
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment