From 566bc90aab3181b78eff24728f774f5601975a9e Mon Sep 17 00:00:00 2001 From: cswimr Date: Sun, 9 Feb 2025 10:10:56 -0600 Subject: [PATCH] document Resource --- docs/index/README.md | 7 ---- docs/modules.md | 1 - docs/resource/classes/Resource.md | 64 +++++++++++++++++++++++++------ src/resource.ts | 42 +++++++++++++++++++- 4 files changed, 94 insertions(+), 20 deletions(-) delete mode 100644 docs/index/README.md diff --git a/docs/index/README.md b/docs/index/README.md deleted file mode 100644 index e421cd8..0000000 --- a/docs/index/README.md +++ /dev/null @@ -1,7 +0,0 @@ -[**packwizjs**](../README.md) - -*** - -[packwizjs](../modules.md) / index - -# index diff --git a/docs/modules.md b/docs/modules.md index 7549eb4..d2e051a 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -8,7 +8,6 @@ - [enums/hash-format](enums/hash-format/README.md) - [enums/side](enums/side/README.md) -- [index](index/README.md) - [parser](parser/README.md) - [provider](provider/README.md) - [resource](resource/README.md) diff --git a/docs/resource/classes/Resource.md b/docs/resource/classes/Resource.md index 9409ab3..9b5bbba 100644 --- a/docs/resource/classes/Resource.md +++ b/docs/resource/classes/Resource.md @@ -6,7 +6,9 @@ # Class: Resource -Defined in: resource.ts:5 +Defined in: resource.ts:8 + +Resource class for handling file and URL paths. ## Constructors @@ -14,7 +16,9 @@ Defined in: resource.ts:5 > **new Resource**(`path`): [`Resource`](Resource.md) -Defined in: resource.ts:6 +Defined in: resource.ts:13 + +Creates a new Resource instance. #### Parameters @@ -22,6 +26,8 @@ Defined in: resource.ts:6 `string` +The file or URL path. + #### Returns [`Resource`](Resource.md) @@ -32,7 +38,9 @@ Defined in: resource.ts:6 > `readonly` **path**: `string` -Defined in: resource.ts:6 +Defined in: resource.ts:13 + +The file or URL path. ## Accessors @@ -42,12 +50,16 @@ Defined in: resource.ts:6 > **get** **ext**(): `string` -Defined in: resource.ts:36 +Defined in: resource.ts:63 + +Gets the file extension of the file or URL. ##### Returns `string` +The file extension of the file or URL. + *** ### isUrl @@ -56,12 +68,16 @@ Defined in: resource.ts:36 > **get** **isUrl**(): `boolean` -Defined in: resource.ts:12 +Defined in: resource.ts:27 + +Checks if the path is a valid URL. ##### Returns `boolean` +True if the path is a valid URL, false otherwise. + *** ### name @@ -70,12 +86,16 @@ Defined in: resource.ts:12 > **get** **name**(): `string` -Defined in: resource.ts:21 +Defined in: resource.ts:40 + +Gets the name of the file or URL. ##### Returns `string` +The name of the file or URL. + *** ### parent @@ -84,43 +104,57 @@ Defined in: resource.ts:21 > **get** **parent**(): [`Resource`](Resource.md) -Defined in: resource.ts:27 +Defined in: resource.ts:50 + +Gets the parent directory of the file or URL. ##### Returns [`Resource`](Resource.md) +The parent directory of the file or URL. + ## Methods ### exists() > **exists**(): `Promise`\<`boolean`\> -Defined in: resource.ts:42 +Defined in: resource.ts:73 + +Checks if the file or URL exists. #### Returns `Promise`\<`boolean`\> +True if the file or URL exists, false otherwise. + *** ### fetchContents() > **fetchContents**(): `Promise`\<`string`\> -Defined in: resource.ts:52 +Defined in: resource.ts:87 + +Fetches the contents of the file or URL. #### Returns `Promise`\<`string`\> +The contents of file the Resource points to. + *** ### join() > **join**(...`segments`): [`Resource`](Resource.md) -Defined in: resource.ts:62 +Defined in: resource.ts:102 + +Joins the Resource with other segments to create a new Resource. #### Parameters @@ -128,18 +162,26 @@ Defined in: resource.ts:62 ...`string`[] +The segments to join with the Resource. + #### Returns [`Resource`](Resource.md) +A new Resource instance with the joined path. + *** ### toString() > **toString**(): `string` -Defined in: resource.ts:8 +Defined in: resource.ts:19 + +Returns the string representation of the Resource instance. #### Returns `string` + +The path of the Resource. diff --git a/src/resource.ts b/src/resource.ts index 9b89079..65820b5 100644 --- a/src/resource.ts +++ b/src/resource.ts @@ -2,13 +2,28 @@ import * as fs from "fs/promises"; import { URL } from "url"; import * as path from "path"; +/** + * Resource class for handling file and URL paths. + */ export class Resource { - constructor(public readonly path: string) { } + /** + * Creates a new Resource instance. + * @param path - The file or URL path. + */ + constructor(public readonly path: string) {} + /** + * Returns the string representation of the Resource instance. + * @returns The path of the Resource. + */ toString(): string { return this.path; } + /** + * Checks if the path is a valid URL. + * @return True if the path is a valid URL, false otherwise. + */ get isUrl(): boolean { try { new URL(this.path); @@ -18,12 +33,20 @@ export class Resource { } } + /** + * Gets the name of the file or URL. + * @return The name of the file or URL. + */ get name(): string { return this.isUrl ? new URL(this.path).pathname.split("/").pop() || "" : path.basename(this.path); } + /** + * Gets the parent directory of the file or URL. + * @return The parent directory of the file or URL. + */ get parent(): Resource { if (this.isUrl) { const url = new URL(this.path); @@ -33,12 +56,20 @@ export class Resource { return new Resource(path.dirname(this.path)); } + /** + * Gets the file extension of the file or URL. + * @return The file extension of the file or URL. + */ get ext(): string { return this.isUrl ? path.extname(new URL(this.path).pathname) : path.extname(this.path); } + /** + * Checks if the file or URL exists. + * @return True if the file or URL exists, false otherwise. + */ async exists(): Promise { if (this.isUrl) { const response = await fetch(this.path); @@ -49,6 +80,10 @@ export class Resource { } } + /** + * Fetches the contents of the file or URL. + * @return The contents of file the Resource points to. + */ async fetchContents(): Promise { if (this.isUrl) { const response = await fetch(this.path); @@ -59,6 +94,11 @@ export class Resource { return fs.readFile(this.path, { encoding: "utf8" }); } + /** + * Joins the Resource with other segments to create a new Resource. + * @param segments - The segments to join with the Resource. + * @return A new Resource instance with the joined path. + */ join(...segments: string[]) { if (this.isUrl) { const url = new URL(this.path);