UNPKG

@neodx/vfs

Version:

Simple virtual file system - working dir context, lazy changes, different modes, integrations and moreover

206 lines (200 loc) 5.71 kB
import { V as VfsContentLike, a as VfsContext, P as PublicVfs, B as BaseVfs, b as VfsLogger, c as VfsLogMethod, d as VfsBackend, A as Asyncable, e as VfsDirent } from './_internal/types-BlKafAog.js'; export { C as CreateVfsContextParams, g as VfsFileAction, h as VfsFileDelete, i as VfsFileMeta, j as VfsFileUpdate, k as VfsFileWrite, m as VfsPlugin, f as createVfsContext, l as createVfsPlugin } from './_internal/types-BlKafAog.js'; import * as fs from 'fs'; import { PackageJsonPluginApi } from './plugins/package-json.js'; import { PrettierPluginParams, PrettierPluginApi } from './plugins/prettier.js'; import { EsLintPluginParams, EsLintPluginApi } from './plugins/eslint.js'; import { GlobPluginApi } from './plugins/glob.js'; import { ScanPluginApi } from './plugins/scan.js'; import { JsonPluginApi } from './plugins/json.js'; interface VirtualInitializer { [path: string]: string | VirtualInitializer; } /** * In-memory VFS backend. * Useful for testing, emulating file system, etc. */ declare function createInMemoryBackend( root?: string, initializer?: VirtualInitializer ): { read(path: string): Buffer | null; isFile: (path: string) => boolean; isDir: (path: string) => boolean; exists(path: string): boolean; readDir(path: string): { isFile: () => boolean; isDirectory: () => boolean; isSymbolicLink: () => boolean; name: string; }[]; write(path: string, content: VfsContentLike): void; delete(path: string): void; __: { kind: string; getStore: () => Map<string, Buffer>; getDeleted: () => Set<string>; }; }; /** * @param initializer Virtual files tree * @param base Root path * @example * ```ts * createInMemoryFilesRecord({ * "package.json": "{...}", * "src": { * "foo": { * "bar.ts": "export const a = 1" * "baz.ts": "export const b = 2" * }, * "index.ts": "export const c = 3" * } * }); * // { * // "package.json": "{...}", * // "src/foo/bar.ts": "export const a = 1", * // "src/foo/baz.ts": "export const b = 2", * // "src/index.ts": "export const c = 3" * // } * ``` */ declare function createInMemoryFilesRecord( initializer: VirtualInitializer, base?: string ): Record<string, string>; declare function createNodeFsBackend(): { read(path: string): Promise<Buffer | null>; write(path: string, content: VfsContentLike): Promise<void>; delete(path: string): Promise<void>; exists(path: string): Promise<boolean>; readDir(path: string): Promise<fs.Dirent[]>; isDir(path: string): Promise<boolean>; isFile(path: string): Promise<boolean>; __: { kind: string; }; }; declare function createBaseVfs(ctx: VfsContext): PublicVfs<BaseVfs>; interface CreateVfsParams extends CreateHeadlessVfsParams { /** * Params for the `prettier` plugin. * Pass `false` or `{ auto: false }` to disable auto formatting. * @default true */ prettier?: boolean | PrettierPluginParams; /** * Params for the `eslint` plugin. * Pass `false` or `{ auto: false }` to disable auto fixing. * @default true */ eslint?: boolean | EsLintPluginParams; } interface CreateHeadlessVfsParams extends CreateDefaultVfsBackendParams { /** @see @neodx/log */ log?: VfsLogger | VfsLogMethod | 'silent'; /** Pass your own vfs backend. */ backend?: VfsBackend; } interface CreateDefaultVfsBackendParams { /** If not specified, will use `node:fs` backend. */ virtual?: boolean | VirtualInitializer; /** If true, all operations will be read-only (if you didn't pass your own backend). */ readonly?: boolean; } type Vfs = ReturnType<typeof createVfs>; declare function createVfs( path: string, { eslint: eslintParams, prettier: prettierParams, ...params }?: CreateVfsParams ): PublicVfs< BaseVfs & JsonPluginApi & ScanPluginApi & GlobPluginApi & EsLintPluginApi & PrettierPluginApi & PackageJsonPluginApi >; declare function createHeadlessVfs( path: string, { log, virtual, readonly, backend }?: CreateHeadlessVfsParams ): PublicVfs<BaseVfs>; declare function createDefaultVfsBackend( path: string, { virtual, readonly }: CreateDefaultVfsBackendParams ): | { read(path: string): Buffer | null; isFile: (path: string) => boolean; isDir: (path: string) => boolean; exists(path: string): boolean; readDir(path: string): { isFile: () => boolean; isDirectory: () => boolean; isSymbolicLink: () => boolean; name: string; }[]; write(path: string, content: VfsContentLike): void; delete(path: string): void; __: { kind: string; getStore: () => Map<string, Buffer>; getDeleted: () => Set<string>; }; } | { read: (path: string) => Asyncable<Buffer | null>; exists: (path: string) => Asyncable<boolean>; isFile: (path: string) => Asyncable<boolean>; isDir: (path: string) => Asyncable<boolean>; write: (path: string, content: VfsContentLike) => void /** * Params for the `prettier` plugin. * Pass `false` or `{ auto: false }` to disable auto formatting. * @default true */; delete: (path: string) => void; readDir(path: string): Promise<VfsDirent[]>; __: { kind: string; }; }; export { BaseVfs, type CreateDefaultVfsBackendParams, type CreateHeadlessVfsParams, type CreateVfsParams, type Vfs, VfsBackend, VfsContentLike, VfsContext, VfsLogMethod, VfsLogger, type VirtualInitializer, createBaseVfs, createDefaultVfsBackend, createHeadlessVfs, createInMemoryBackend, createInMemoryFilesRecord, createNodeFsBackend, createVfs };