就像from IPython import embed; embed()
但对于node
.
我想以编程方式打开一个 REPL shell 并至少能够读取局部变量。能够改变它们也是一个优点。
就像from IPython import embed; embed()
但对于node
.
我想以编程方式打开一个 REPL shell 并至少能够读取局部变量。能够改变它们也是一个优点。
据我所知,你能得到的最接近的是使用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
),我们可以访问此自定义 repl:
$ 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。
对于deno
(标题说 Node.js,标记 deno),您可以使用它Deno.run
来执行deno
和写入stdin
和读取stdout
.
以下将做:
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
但你明白了。
您可以构建一个类似于内置 Deno REPL 的 REPL,并使用危险 eval
函数评估表达式。通过它,您将能够访问局部变量和其他东西(例如window
)。
复制品
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
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 模块询问输入,每次提供响应时,都会执行代码并且新输入是 askef