@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
99 lines (98 loc) • 4.77 kB
TypeScript
import type { FileInfo, FileSystemOptions, DirEntry, DirTree } from "./types.ts";
import type { CopyOptions, GetDirOptions, GetFileOptions, MkdirOptions, ReadDirOptions, ReadFileOptions, RemoveOptions, StatOptions, WriteFileOptions } from "../fs.ts";
export type { FileSystemOptions, FileInfo, DirEntry, DirTree };
export declare const EOL: "\n" | "\r\n";
/**
* Obtains the directory handle of the given path.
*
* NOTE: This function is only available in the browser.
*
* NOTE: If the `path` is not provided or is empty, the root directory handle
* will be returned.
*
* @example
* ```ts
* // with the default storage
* import { getDirHandle } from "@ayonli/jsext/fs";
*
* const dir = await getDirHandle("/path/to/dir");
* ```
*
* @example
* ```ts
* // with a user-selected directory as root (Chromium only)
* import { getDirHandle } from "@ayonli/jsext/fs";
*
* const root = await window.showDirectoryPicker();
* const dir = await getDirHandle("/path/to/dir", { root });
* ```
*
* @example
* ```ts
* // create the directory if not exist
* import { getDirHandle } from "@ayonli/jsext/fs";
*
* const dir = await getDirHandle("/path/to/dir", { create: true, recursive: true });
* ```
*
* @example
* ```ts
* // return the root directory handle
* import { getDirHandle } from "@ayonli/jsext/fs";
*
* const root = await getDirHandle();
* ```
*/
export declare function getDirHandle(path?: string, options?: GetDirOptions): Promise<FileSystemDirectoryHandle>;
/**
* Obtains the file handle of the given path.
*
* NOTE: This function is only available in the browser.
*
* @example
* ```ts
* // with the default storage
* import { getFileHandle } from "@ayonli/jsext/fs";
*
* const file = await getFileHandle("/path/to/file.txt");
* ```
*
* @example
* ```ts
* // with a user-selected directory as root (Chromium only)
* import { getFileHandle } from "@ayonli/jsext/fs";
*
* const root = await window.showDirectoryPicker();
* const file = await getFileHandle("/path/to/file.txt", { root });
* ```
*
* @example
* ```ts
* // create the file if not exist
* import { getFileHandle } from "@ayonli/jsext/fs";
*
* const file = await getFileHandle("/path/to/file.txt", { create: true });
* ```
*/
export declare function getFileHandle(path: string, options?: GetFileOptions): Promise<FileSystemFileHandle>;
export declare function exists(path: string, options?: FileSystemOptions): Promise<boolean>;
export declare function stat(target: string | FileSystemFileHandle | FileSystemDirectoryHandle, options?: StatOptions): Promise<FileInfo>;
export declare function mkdir(path: string, options?: MkdirOptions): Promise<void>;
export declare function ensureDir(path: string, options?: Omit<MkdirOptions, "recursive">): Promise<void>;
export declare function readDir(target: string | FileSystemDirectoryHandle, options?: ReadDirOptions): AsyncIterableIterator<DirEntry>;
export declare function readTree(target: string | FileSystemDirectoryHandle, options?: FileSystemOptions): Promise<DirTree>;
export declare function readFile(target: string | FileSystemFileHandle, options?: ReadFileOptions): Promise<Uint8Array>;
export declare function readFileAsText(target: string | FileSystemFileHandle, options?: ReadFileOptions & {
encoding?: string;
}): Promise<string>;
export declare function readFileAsFile(target: string | FileSystemFileHandle, options?: ReadFileOptions): Promise<File>;
export declare function writeFile(target: string | FileSystemFileHandle, data: string | ArrayBuffer | ArrayBufferView | ReadableStream<Uint8Array> | Blob, options?: WriteFileOptions): Promise<void>;
export declare function writeLines(target: string | FileSystemFileHandle, lines: string[], options?: WriteFileOptions): Promise<void>;
export declare function truncate(target: string | FileSystemFileHandle, size?: number, options?: FileSystemOptions): Promise<void>;
export declare function remove(path: string, options?: RemoveOptions): Promise<void>;
export declare function rename(oldPath: string, newPath: string, options?: FileSystemOptions): Promise<void>;
export declare function copy(src: string, dest: string, options?: CopyOptions): Promise<void>;
export declare function copy(src: FileSystemFileHandle, dest: FileSystemFileHandle | FileSystemDirectoryHandle): Promise<void>;
export declare function copy(src: FileSystemDirectoryHandle, dest: FileSystemDirectoryHandle, options?: Pick<CopyOptions, "recursive">): Promise<void>;
export declare function createReadableStream(target: string | FileSystemFileHandle, options?: FileSystemOptions): ReadableStream<Uint8Array>;
export declare function createWritableStream(target: string | FileSystemFileHandle, options?: Omit<WriteFileOptions, "signal">): WritableStream<Uint8Array>;