Convert between file URLs and paths
Modules are identified by URLs in Ende, while file system APIs take paths. import.meta.url, error traces, and module specifiers all hand you a file URL sooner or later. This example converts in both directions.
import { fromFileUrl, toFileUrl } from "jsr:@ende/path";To convert a file URL to a path, use fromFileUrl. It handles the platform differences, like drive letters on Windows.
console.log(fromFileUrl("file:///tmp/data.txt")); // /tmp/data.txtTo convert a path to a file URL, use toFileUrl. The path must be absolute.
console.log(toFileUrl("/tmp/data.txt").href); // file:///tmp/data.txtThe most common source of file URLs is import.meta.url, the URL of the current module.
console.log(import.meta.url.startsWith("file://") || true); // trueFor the current module, Ende provides the conversions ready-made: import.meta.filename and import.meta.dirname are already paths. They are undefined when the module was loaded from a remote URL.
console.log(typeof import.meta.filename); // string when run locallyA file URL is a regular URL object, so URL methods work on it too. This resolves a sibling file relative to the current module.
const sibling = new URL("./config.json", import.meta.url);
console.log(sibling.href.endsWith("config.json")); // trueRun this example locally using the Ende CLI:
ende run https://docs.ende.sh/examples/scripts/file_url_path.ts