如何启动可以访问 node.js 中的局部变量的 REPL?
How can I start a REPL that can access local variables in node.js?
就像 from IPython import embed; embed()
但 node
.
我想以编程方式打开 REPL shell 并且至少能够读取局部变量。能够改变它们也是一个加号。
对于deno
(标题说Node.js,标记deno)你可以使用Deno.run
执行deno
并写入stdin
并从[读取=16=].
执行以下操作:
const p = Deno.run({
cmd: ["deno"],
stdin: "piped",
stdout: "piped",
stderr: "piped"
});
async function read(waitForMessage) {
const reader = Deno.iter(p.stdout)
let res = '';
for await(const chunk of reader) {
res += new TextDecoder().decode(chunk);
console.log('Chunk', res, '---')
// improve this, you should wait until the last chunk
// is read in case of a command resulting in a big output
if(!waitForMessage)
return res;
else if(res.includes(waitForMessage))
return res;
}
}
async function writeCommand(command) {
const msg = new TextEncoder().encode(command + '\n');
console.log('Command: ', command)
const readPromise = read();
// write command
await p.stdin.write(msg);
// Wait for output
const value = await readPromise
return value;
}
// Wait for initial output:
// Deno 1.0.0
// exit using ctrl+d or close()
await read('ctrl+d or close()');
await writeCommand('let x = 5;')
let value = await writeCommand('x') // read x
console.log('Value: ', value)
await writeCommand('x = 6;')
value = await writeCommand('x') // read x
console.log('Value: ', value)
如果您 运行 该片段,输出将是:
Command: let x = 5;
Command: x
Value: 5
Command: x = 6;
Command: x
Value: 6
需要进行一些改进,例如处理 stderr
但您明白了。
此功能目前不存在,但在 https://github.com/denoland/deno/issues/7938 中提出。
愿景与
类似
Deno.eval("Deno.repl()")
您可以构建类似于内置 Deno REPL 的 REPL,并使用 dangerous eval
function. Through it you'll be able to access local variables and other things (e.g. window
).
计算表达式
repl.ts
import { readLines, writeAll } from "https://deno.land/std@0.106.0/io/mod.ts";
export default async function repl(evaluate: (x: string) => unknown) {
await writeOutput("exit using ctrl+d or close()\n");
await writeOutput("> ");
for await (const input of readInputs()) {
try {
const value = evaluate(input);
const output = `${Deno.inspect(value, { colors: !Deno.noColor })}\n`;
await writeOutput(output);
await writeOutput("> ");
} catch (error) {
await writeError(error);
}
}
}
async function* readInputs(): AsyncIterableIterator<string> {
yield* readLines(Deno.stdin);
}
async function writeOutput(output: string) {
await writeAll(Deno.stdout, new TextEncoder().encode(output));
}
async function writeError(error: unknown) {
await writeAll(Deno.stderr, new TextEncoder().encode(`Uncaught ${error}\n`));
}
repl_demo.ts
import repl from "./repl.ts";
let a = 1;
let b = 2;
let c = 3;
await repl((x) => eval(x));
用法示例
% deno run repl_demo.ts
exit using ctrl+d or close()
> a
1
> a = 40
40
> a + b
42
据我所知,最接近的方法是使用 repl
内置模块(由 node inspect
本身使用):
// ... your code you want to debug
const repl = require("repl");
const replServer = repl.start({
prompt: "Your Own Repl > ",
useGlobal: true
});
// Expose variables
const localVar = 42
replServer.context.localVar = localVar;
通过 运行 node index.js
(假设您将上述内容保存在 index.js
中)我们可以访问此自定义回复:
$ node index.js
Your Own Repl > localVar
42
Your Own Repl >
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
Your Own Repl >
但是,这不像调试器工具那样工作,实际上,它只是一个 REPL。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//to be sure this context is here
const ev = eval.bind(this);
function ask() {
rl.question('>', (code) => {
console.log(ev(code));
ask();
});
}
ask();
此代码使用 readLine 模块请求输入,每次提供响应时都会执行代码并请求新的输入
就像 from IPython import embed; embed()
但 node
.
我想以编程方式打开 REPL shell 并且至少能够读取局部变量。能够改变它们也是一个加号。
对于deno
(标题说Node.js,标记deno)你可以使用Deno.run
执行deno
并写入stdin
并从[读取=16=].
执行以下操作:
const p = Deno.run({
cmd: ["deno"],
stdin: "piped",
stdout: "piped",
stderr: "piped"
});
async function read(waitForMessage) {
const reader = Deno.iter(p.stdout)
let res = '';
for await(const chunk of reader) {
res += new TextDecoder().decode(chunk);
console.log('Chunk', res, '---')
// improve this, you should wait until the last chunk
// is read in case of a command resulting in a big output
if(!waitForMessage)
return res;
else if(res.includes(waitForMessage))
return res;
}
}
async function writeCommand(command) {
const msg = new TextEncoder().encode(command + '\n');
console.log('Command: ', command)
const readPromise = read();
// write command
await p.stdin.write(msg);
// Wait for output
const value = await readPromise
return value;
}
// Wait for initial output:
// Deno 1.0.0
// exit using ctrl+d or close()
await read('ctrl+d or close()');
await writeCommand('let x = 5;')
let value = await writeCommand('x') // read x
console.log('Value: ', value)
await writeCommand('x = 6;')
value = await writeCommand('x') // read x
console.log('Value: ', value)
如果您 运行 该片段,输出将是:
Command: let x = 5;
Command: x
Value: 5
Command: x = 6;
Command: x
Value: 6
需要进行一些改进,例如处理 stderr
但您明白了。
此功能目前不存在,但在 https://github.com/denoland/deno/issues/7938 中提出。
愿景与
类似Deno.eval("Deno.repl()")
您可以构建类似于内置 Deno REPL 的 REPL,并使用 dangerous eval
function. Through it you'll be able to access local variables and other things (e.g. window
).
repl.ts
import { readLines, writeAll } from "https://deno.land/std@0.106.0/io/mod.ts";
export default async function repl(evaluate: (x: string) => unknown) {
await writeOutput("exit using ctrl+d or close()\n");
await writeOutput("> ");
for await (const input of readInputs()) {
try {
const value = evaluate(input);
const output = `${Deno.inspect(value, { colors: !Deno.noColor })}\n`;
await writeOutput(output);
await writeOutput("> ");
} catch (error) {
await writeError(error);
}
}
}
async function* readInputs(): AsyncIterableIterator<string> {
yield* readLines(Deno.stdin);
}
async function writeOutput(output: string) {
await writeAll(Deno.stdout, new TextEncoder().encode(output));
}
async function writeError(error: unknown) {
await writeAll(Deno.stderr, new TextEncoder().encode(`Uncaught ${error}\n`));
}
repl_demo.ts
import repl from "./repl.ts";
let a = 1;
let b = 2;
let c = 3;
await repl((x) => eval(x));
用法示例
% deno run repl_demo.ts
exit using ctrl+d or close()
> a
1
> a = 40
40
> a + b
42
据我所知,最接近的方法是使用 repl
内置模块(由 node inspect
本身使用):
// ... your code you want to debug
const repl = require("repl");
const replServer = repl.start({
prompt: "Your Own Repl > ",
useGlobal: true
});
// Expose variables
const localVar = 42
replServer.context.localVar = localVar;
通过 运行 node index.js
(假设您将上述内容保存在 index.js
中)我们可以访问此自定义回复:
$ node index.js
Your Own Repl > localVar
42
Your Own Repl >
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
Your Own Repl >
但是,这不像调试器工具那样工作,实际上,它只是一个 REPL。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
//to be sure this context is here
const ev = eval.bind(this);
function ask() {
rl.question('>', (code) => {
console.log(ev(code));
ask();
});
}
ask();
此代码使用 readLine 模块请求输入,每次提供响应时都会执行代码并请求新的输入