document Resource
This commit is contained in:
parent
bd5d747a72
commit
566bc90aab
4 changed files with 94 additions and 20 deletions
|
@ -1,7 +0,0 @@
|
|||
[**packwizjs**](../README.md)
|
||||
|
||||
***
|
||||
|
||||
[packwizjs](../modules.md) / index
|
||||
|
||||
# index
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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<boolean> {
|
||||
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<string> {
|
||||
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);
|
||||
|
|
Reference in a new issue