UNPKG

@codesandbox/sdk

Version:
117 lines (116 loc) 3.38 kB
import { type IAgentClient } from "../agent-client-interface"; import { Disposable } from "../utils/disposable"; import { type Event } from "../utils/event"; import { Tracer } from "@opentelemetry/api"; export type FSStatResult = { type: "file" | "directory"; isSymlink: boolean; size: number; mtime: number; ctime: number; atime: number; }; export type WriteFileOpts = { create?: boolean; overwrite?: boolean; }; export type ReaddirEntry = { name: string; type: "file" | "directory"; isSymlink: boolean; }; export type WatchOpts = { readonly recursive?: boolean; readonly excludes?: readonly string[]; }; export type WatchEvent = { paths: string[]; type: "add" | "change" | "remove"; }; export type Watcher = { dispose(): void; onEvent: Event<WatchEvent>; }; export type BatchWriteFile = { path: string; content: string | Uint8Array; }; export declare class FileSystem { private agentClient; private username?; private disposable; private tracer?; constructor(sessionDisposable: Disposable, agentClient: IAgentClient, username?: string | undefined, tracer?: Tracer); private withSpan; /** * Write a file. */ writeFile(path: string, content: Uint8Array, opts?: WriteFileOpts): Promise<void>; /** * Write a file as a string. */ writeTextFile(path: string, content: string, opts?: WriteFileOpts): Promise<void>; /** * Batch write multiple files by zipping them, uploading the zip, and extracting it on the sandbox. * This is more efficient than writing many files individually. * Files will be created/overwritten as needed. */ batchWrite(files: BatchWriteFile[]): Promise<void>; /** * Create a directory. */ mkdir(path: string, recursive?: boolean): Promise<void>; /** * Read a directory. */ readdir(path: string): Promise<ReaddirEntry[]>; /** * Read a file */ readFile(path: string): Promise<Uint8Array>; /** * Read a file as a string. */ readTextFile(path: string): Promise<string>; /** * Get the stat of a file or directory. */ stat(path: string): Promise<FSStatResult>; /** * Copy a file or directory. */ copy(from: string, to: string, recursive?: boolean, overwrite?: boolean): Promise<void>; /** * Rename a file or directory. */ rename(from: string, to: string, overwrite?: boolean): Promise<void>; /** * Remove a file or directory. */ remove(path: string, recursive?: boolean): Promise<void>; /** * Watch for changes in the filesystem. * * ```ts * const watcher = await sandbox.fs.watch("/path/to/watch"); * watcher.onEvent((event) => { * console.log(event); * }); * * // When done * watcher.dispose(); * ``` * * @param path - The path to watch. * @param options - The options for the watch. * @returns A watcher that can be disposed to stop the watch. */ watch(path: string, options?: WatchOpts): Promise<Watcher>; /** * Download a file or folder from the filesystem, can only be used to download * from within the workspace directory. A download URL that's valid for 5 minutes. */ download(path: string): Promise<{ downloadUrl: string; }>; }