#Get Path relative to main module

20 messages Β· Page 1 of 1 (latest)

chilly shoal
#

Is there any way to resolve a path relative to the main module, that also works if run inside a self contained executable?

I know there is Deno.mainModule and with the std/path the problem seems quite easy to solve, but Deno.mainModule
returns the path of the main module during build time.
There is also Deno.execPath for getting the path of the executable, so the problem would be solvable with a combination of those two APIs, except I don't know how to check if the code is running in a self contained executable.

Any suggestions?

Thanks a lot in advance πŸ˜„

tawdry ruin
#

can't find any runtime api that can tell the difference. there's this one way i could think of right now but it's kinda hacky...

function checkCompiled() {
  const f = Deno.openSync(Deno.execPath());
  f.seekSync(-24, Deno.SeekMode.End);
  const data = new Uint8Array(8);
  f.readSync(data);
  f.close();
  return new TextDecoder().decode(data) == "d3n0l4nd";
}
quaint socket
#

Deno.execPath return deno path when not compiled but it returns a different path when compiled

#

for your first question maybe try import.meta.main

drowsy scaffold
#

.main returns true or false

quaint socket
#

meta.url *

tawdry ruin
chilly shoal
tawdry ruin
#

right

chilly shoal
#

πŸ˜•

tawdry ruin
#

execPath also needs read permission fyi

quaint socket
#

well for an approximation I think is good enough

!deno.execpath().endswith("/deno")
drowsy scaffold
#

--allow-read="$(which deno)" would work in a cross platform way

tawdry ruin
#

not when you compile

drowsy scaffold
#

Oh right

chilly shoal
#

is there actually no runtime api to check whether the JS is running in a self contained executable πŸ˜…

#

if that's actually the case I'll open a feature request πŸ™ˆ

drowsy scaffold
#

I think there's an issue open already, not sure

chilly shoal
#

Not really happy with the solution (if you can call it that) but thanks a lot for the help guys! πŸ€—

chilly shoal