Hi, I'm trying to do a discord bot that can perform great eval as the one by default does not accept imports.
So I made a code that creates a process executing the code via deno eval, but whatever I do, I can't get the proper output.
This is my code :
const process = Deno.run({
cwd: Deno.cwd(),
cmd: [
'cmd', // I have to this else it says that 'deno' cannot be found ¯\_(ツ)_/¯
'/c',
`deno eval ${code} --ts --print`
],
stdout: 'piped',
stderr: 'piped',
stdin: 'piped'
});
const decoder: TextDecoder = new TextDecoder('utf8');
const output: string = decoder.decode(await process.output());
const errors: string = decoder.decode(await process.stderrOutput());
If I execute this code with code = "\"console.log('test')\"", it's sending me console.log('test') as the output, I do not understand why and I need help 👀
You're passing a literal string to Deno. (The outer pair of quotes is for the cmd string. The inner escaped pair of quotes is for the JS string.)
C:\>deno eval --print "\"I'm a string\""
I'm a string
I have to this else it says that 'deno' cannot be found ¯_(ツ)_/¯
Can you run where deno in cmd.exe? It seems like deno.exe is not in your %path%.
Alternatively you can use Deno.execPath(), e.g.
// hi.ts
await Deno.run({ cmd: [Deno.execPath(), "eval", "console.log('hi')"], stdout: "inherit" }).status();
C:\>C:\directory\that\is\not\in\path\deno.exe run -A hi.ts
hi
where deno is sending me this :
C:\Users\Pierre\.deno\bin\deno.exe
Thanks, I was not knowing the Deno.execPath function it will helps me ^^
It seems like deno.exe is indeed in your %path%.
What command did you use to execute the example code you copy-pasted above? Which shell did you run it in?
I run it via deno run from the webstorm terminal.
Can you try running from default cmd.exe? This might be an issue with the webstorm terminal. Using just "deno" as the first cmd array item should work.
C:\>deno eval "await Deno.run({ cmd: ['deno', '-V'], stdout: 'inherit' }).status();"
deno 1.5.1
Nothing appears in the terminal.
Does the command above work for you in cmd.exe?
deno eval "await Deno.run({ cmd: ['deno', '-V'], stdout: 'inherit' }).status();"
In cmd it's sending me deno 1.5.1, in webstorm terminal it's not sending anything.
Well, no idea what Webstorm is doing, but this doesn't seem to be a Deno issue.
Yeah...
Anyway, my issue was not getting what the code sends to the console from the await process.output().
So what do I do wrong?
@Ayfri call await process.status() before reading from stdio
As I said in my first comment, you were passing a string to Deno eval.
C:\>deno eval "\"I'm a string\""
is equivalent to executing
// string.ts
"I'm a string"
C:\>deno run string.ts
What you want to do is
Deno.run({ cmd: [Deno.execPath(), "eval", code] // ...
You don't need to escape code with extra quotes.
Oh, I see my problem now, thanks for all it works now!
Most helpful comment
Oh, I see my problem now, thanks for all it works now!