retrieves the current working directory or URL based on the specified RUNTIME enum.

Note

  • the returned directory path may or may not end in a trailing slash. this is intentional, as it is possible for the path to actually point towards a file. (such as in the case of chrome.runtime.getURL(""))
  • however, the returned path will always use posix separators (only forward slashes, no windows backslashes).
  • if you try to query the working directory of a runtime enum that your current environment does not support, then an error will be thrown, because you'll be accessing an undefined object.

depending on the provided runtime_enum, the current working directory is defined as the following:

  • for DENO, BUN, and NODE: the result will be that of the runtime's cwd() method.
  • for CHROMIUM and EXTENSION: the result will be the url string obtained from runtime.getURL("").
  • for WEB and WORKER: a url string will be returned that will vary based on the current_path flag (true by default):
    • if current_path == false: location.origin will be returned (i.e. the root of your webpage, which is your domain-name + subdomain-name).
    • if current_path == true: the directory path of location.href will be returned.

an error is thrown if the runtime associated with the provided enum is undefined, or if an invalid enum is provided.

import { assertEquals } from "jsr:@std/assert"
import process from "node:process" // this works in deno 2.0

assertEquals(getRuntimeCwd(RUNTIME.DENO), getRuntimeCwd(RUNTIME.NODE))
assertEquals(getRuntimeCwd(RUNTIME.DENO), Deno.cwd().replaceAll(/\\\\?/g, "/"))
assertEquals(getRuntimeCwd(RUNTIME.NODE), process.cwd().replaceAll(/\\\\?/g, "/"))
  • Parameters

    • runtime_enum: RUNTIME

      the runtime enum indicating which runtime's working-directory/url-path to retrieve.

    • current_path: boolean = true

      a boolean flag that, when true, returns the full href url of the runtime, rather than the root origin. defaults to true.

    Returns string

    a posix string path of the working-directory/url-path of the specified runtime.