Skip to content

Commit

Permalink
feat: PathRef - support creating via import.meta and file url str…
Browse files Browse the repository at this point in the history
…ings (#113)
  • Loading branch information
dsherret authored Feb 22, 2023
1 parent f69085e commit 24a7548
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ export interface $BuiltInProperties<TExtras extends ExtrasObject = {}> {
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`)
Expand Down
6 changes: 6 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
22 changes: 19 additions & 3 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 */
Expand All @@ -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));
Expand Down

0 comments on commit 24a7548

Please sign in to comment.