From 24a7548e5bd67296aa2ed5891a1a54e65e1c94cb Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 22 Feb 2023 12:21:51 -0500 Subject: [PATCH] feat: `PathRef` - support creating via `import.meta` and file url strings (#113) --- README.md | 10 ++++++++++ mod.ts | 5 ++++- src/path.test.ts | 6 ++++++ src/path.ts | 22 +++++++++++++++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c702355..291450d 100644 --- a/README.md +++ b/README.md @@ -514,6 +514,16 @@ const srcDir = $.path("src").resolve(); await $`echo ${srcDir}`; ``` +`PathRef`s can be created in the following ways: + +```ts +const pathRelative = $.path("./relative"); +const pathAbsolute = $.path("/tmp"); +const pathFileUrl = $.path(new URL("file:///tmp")); // converts to /tmp +const pathStringFileUrl = $.path("file:///tmp"); // converts to /tmp +const pathImportMeta = $.path(import.meta); // the path for the current module +``` + There are a lot of helper methods here, so check the [documentation on PathRef](https://deno.land/x/dax/src/path.ts?s=PathRef) for more details. ## Helper functions diff --git a/mod.ts b/mod.ts index a2e42fd..261e8b0 100644 --- a/mod.ts +++ b/mod.ts @@ -221,7 +221,10 @@ export interface $BuiltInProperties { fs: typeof fs; /** Helper function for creating path references, which provide an easier way for * working with paths, directories, and files on the file system. Also, a re-export - * of deno_std's `path` module as properties on this object. */ + * of deno_std's `path` module as properties on this object. + * + * The function creates a new `PathRef` from a path or URL string, file URL, or for the current module. + */ path: typeof createPathRef & typeof stdPath; /** * Logs with potential indentation (`$.logIndent`) diff --git a/src/path.test.ts b/src/path.test.ts index 2b3f487..55e08e9 100644 --- a/src/path.test.ts +++ b/src/path.test.ts @@ -579,3 +579,9 @@ Deno.test("instanceof check", () => { assert(new OtherPathRef() instanceof PathRef); assert(createPathRef("test") instanceof OtherPathRef); }); + +Deno.test("toFileUrl", () => { + const path = createPathRef(import.meta); + assertEquals(path.toString(), stdPath.fromFileUrl(import.meta.url)); + assertEquals(path.toFileUrl(), new URL(import.meta.url)); +}); diff --git a/src/path.ts b/src/path.ts index 0573a39..81cb87d 100644 --- a/src/path.ts +++ b/src/path.ts @@ -2,7 +2,7 @@ import { fs, path as stdPath, writeAll, writeAllSync } from "./deps.ts"; const PERIOD_CHAR_CODE = ".".charCodeAt(0); -export function createPathRef(path: string | URL): PathRef { +export function createPathRef(path: string | URL | ImportMeta): PathRef { return new PathRef(path); } @@ -27,8 +27,18 @@ export class PathRef { */ private static instanceofSymbol = Symbol.for("dax.PathRef"); - constructor(path: string | URL) { - this.#path = path instanceof URL ? stdPath.fromFileUrl(path) : path; + constructor(path: string | URL | ImportMeta) { + if (path instanceof URL) { + this.#path = stdPath.fromFileUrl(path); + } else if (typeof path === "string") { + if (path.startsWith("file://")) { + this.#path = stdPath.fromFileUrl(path); + } else { + this.#path = path; + } + } else { + this.#path = stdPath.fromFileUrl(path.url); + } } /** @internal */ @@ -42,6 +52,12 @@ export class PathRef { return this.#path; } + /** Resolves the path and gets the file URL. */ + toFileUrl(): URL { + const resolvedPath = this.resolve(); + return stdPath.toFileUrl(resolvedPath.toString()); + } + /** Joins the provided path segments onto this path. */ join(...pathSegments: string[]): PathRef { return new PathRef(stdPath.join(this.#path, ...pathSegments));