UNPKG

@storm-stack/core

Version:

A build toolkit and runtime used by Storm Software in TypeScript applications

443 lines (440 loc) 18.6 kB
import { Volume } from 'memfs'; import fs, { PathLike, ObjectEncodingOptions, PathOrFileDescriptor, Stats, StatSyncOptions } from 'node:fs'; import { IUnionFs } from 'unionfs'; import { cx as Context, cy as SerializedVirtualFileSystem } from '../../build-B-FJ5_Zf.cjs'; import { VirtualFileSystemInterface, __VFS_CACHE__, __VFS_RESOLVER__, __VFS_VIRTUAL__, __VFS_UNIFIED__, __VFS_INIT__, __VFS_REVERT__, ResolvePathOptions, VirtualRuntimeFile, ResolveFSOptions, MakeDirectoryOptions, WriteFileOptions, WriteRuntimeFileOptions } from '../../types/vfs.cjs'; import '@storm-software/config-tools/types'; import '@stryke/types/base'; import '@stryke/types/configuration'; import 'esbuild'; import 'hookable'; import 'vite'; import 'magic-string'; import '@babel/core'; import '@rspack/core'; import '@storm-software/build-tools/types'; import '@storm-software/config/types'; import '@storm-software/esbuild/types'; import '@storm-software/unbuild/types'; import 'c12'; import 'rolldown'; import 'rollup'; import 'unplugin'; import 'webpack'; import '@babel/types'; import '@storm-stack/core/deepkit/type'; import '@stryke/env/get-env-paths'; import '@stryke/types/package-json'; import 'jest-worker'; import 'jiti'; import 'unimport'; import '@stryke/capnp'; import 'semver'; import '../../types/tsconfig.cjs'; import '@stryke/types/tsconfig'; import 'typescript'; /** * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) * because the buffer-to-string conversion in `fs.readFileSync()` * translates it to FEFF, the UTF-16 BOM. */ declare const RUNTIME_PREFIX = "storm:"; /** * Represents a virtual file system (VFS) that stores files and their associated metadata in virtual memory. * * @remarks * This class provides methods to manage virtual files, check their existence, retrieve their content, and manipulate the virtual file system. It allows for efficient file management and retrieval without relying on the actual file system. */ declare class VirtualFileSystem implements VirtualFileSystemInterface { #private; /** * Exposes the internal VFS map for advanced usage. */ get [__VFS_CACHE__](): Map<string, string>; /** * Exposes the internal VFS resolver cache for advanced usage. */ get [__VFS_RESOLVER__](): Map<string, string | false>; /** * Exposes the internal VFS map for advanced usage. */ get [__VFS_VIRTUAL__](): Volume; /** * Exposes the internal UFS map for advanced usage. */ get [__VFS_UNIFIED__](): IUnionFs; /** * Creates a new instance of the VirtualFileSystem. * * @param context - The context of the virtual file system, typically containing options and logging functions. * @param serialized - A map of files/file contents to populate in cache */ constructor(context: Context, serialized?: SerializedVirtualFileSystem); [__VFS_INIT__](): void; [__VFS_REVERT__](): void; /** * Returns a Map of all runtime file IDs and their corresponding paths in the virtual file system. * * @returns A Map where the keys are runtime file IDs (strings) and the values are their corresponding paths (strings). */ get runtimeIdMap(): Map<string, string>; /** * Lists all runtime IDs in the virtual file system. * * @returns An array of formatted runtime IDs. */ get runtimeIds(): string[]; /** * Checks if a given path or ID corresponds to a runtime file. * * @param pathOrId - The path or ID to check. * @param options - Options for resolving the path, such as paths to check. * @returns `true` if the path or ID corresponds to a runtime file, otherwise `false`. */ isRuntimeFile(pathOrId: string, options?: ResolvePathOptions): boolean; /** * Checks if a provided string is a valid runtime ID (does not need to already be created in the file system). * * @param id - The ID to check. * @returns Whether the ID is a valid runtime ID. */ isValidRuntimeId(id: string): boolean; /** * Check if a path or ID corresponds to a virtual file. * * @param pathOrId - The path or ID to check. * @param options - Options for resolving the path, such as paths to check. * @returns Whether the path or ID corresponds to a virtual file. */ isVirtualFile(pathOrId: string, options?: ResolvePathOptions): boolean; /** * Check if a path exists within one of the directories specified in the tsconfig.json's `path` field. * * @see https://www.typescriptlang.org/tsconfig#paths * * @param pathOrId - The path or ID to check. * @returns Whether the path or ID corresponds to a virtual file. */ isTsconfigPath(pathOrId: string): boolean; /** * Checks if a given ID corresponds to a runtime file path. * * @param id - The unique identifier for the runtime file. * @param pathOrId - The path or ID to check. * @returns `true` if the ID corresponds to the path or ID of a runtime file, otherwise `false`. */ isMatchingRuntimeId(id: string, pathOrId: string): boolean; /** * Lists all runtime files in the virtual file system. * * @returns A promise that resolves to an array of runtime files. */ listRuntimeFiles(): Promise<VirtualRuntimeFile[]>; /** * Lists files in a given path. * * @param path - The path to list files from. * @param options - Options for listing files, such as encoding and recursion. * @returns An array of file names in the specified path. */ readdirSync(path: fs.PathLike, options?: { encoding: BufferEncoding | null; withFileTypes?: false | undefined; recursive?: boolean | undefined; } | BufferEncoding): string[]; /** * Removes a file in the virtual file system (VFS). * * @param path - The path to create the directory at. */ unlinkSync(path: fs.PathLike, options?: ResolveFSOptions): void; /** * Removes a file in the virtual file system (VFS). * * @param path - The path to create the directory at. */ unlink(path: fs.PathLike, options?: ResolveFSOptions): Promise<void>; /** * Removes a directory in the virtual file system (VFS). * * @param path - The path to create the directory at. * @param options - Options for creating the directory. */ rmdirSync(path: fs.PathLike, options?: fs.RmDirOptions & ResolveFSOptions): void; /** * Removes a directory in the virtual file system (VFS). * * @param path - The path to create the directory at. * @param options - Options for creating the directory. * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created. */ rmdir(path: fs.PathLike, options?: fs.RmDirOptions & ResolveFSOptions): Promise<void>; /** * Removes a file in the virtual file system (VFS). * * @param path - The path to the file to remove. * @param options - Options for removing the file. * @returns A promise that resolves when the file is removed. */ rm(path: fs.PathLike, options?: fs.RmOptions & ResolveFSOptions): Promise<void>; /** * Creates a directory in the virtual file system (VFS). * * @param path - The path to create the directory at. * @param options - Options for creating the directory. * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created. */ mkdirSync(path: fs.PathLike, options?: MakeDirectoryOptions): string | undefined; /** * Creates a directory in the virtual file system (VFS). * * @param path - The path to create the directory at. * @param options - Options for creating the directory. * @returns A promise that resolves to the path of the created directory, or undefined if the directory could not be created. */ mkdir(path: fs.PathLike, options?: MakeDirectoryOptions): Promise<string | undefined>; /** * Lists files in a given path. * * @param path - The path to list files from. * @param options - Options for listing files, such as encoding and recursion. * @returns An array of file names in the specified path. */ readdir(path: PathLike, options?: { encoding: BufferEncoding | null; withFileTypes?: false | undefined; recursive?: boolean | undefined; } | BufferEncoding): Promise<string[]>; /** * Asynchronously reads a file from the virtual file system (VFS). * * @param pathOrId - The path or ID of the file to read. * @returns A promise that resolves to the contents of the file as a string, or undefined if the file does not exist. */ readFile(pathOrId: PathLike, options?: (ObjectEncodingOptions & { flag?: string | undefined; }) | BufferEncoding): Promise<string | undefined>; /** * Synchronously reads a file from the virtual file system (VFS). * * @param pathOrId - The path or ID of the file to read. * @returns The contents of the file as a string, or undefined if the file does not exist. */ readFileSync(pathOrId: PathLike, options?: (fs.ObjectEncodingOptions & { flag?: string | undefined; }) | BufferEncoding | null): string | undefined; /** * Writes a file to the virtual file system (VFS). * * @param file - The path to the file. * @param data - The contents of the file. * @param options - Optional parameters for writing the file. * @returns A promise that resolves when the file is written. */ writeFile(file: PathOrFileDescriptor, data?: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): Promise<void>; /** * Synchronously writes a file to the virtual file system (VFS). * * @param file - The file to write. * @param data - The contents of the file. * @param options - Optional parameters for writing the file. */ writeFileSync(file: PathOrFileDescriptor, data?: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): void; /** * Writes a runtime file to the virtual file system (VFS). * * @param id - The unique identifier for the runtime file. * @param path - The path to the runtime file. * @param contents - The contents of the runtime file. * @param options - Optional parameters for writing the runtime file. * @returns A promise that resolves when the file is written. */ writeRuntimeFile(id: string, path: PathLike, contents: string, options?: WriteRuntimeFileOptions): Promise<void>; /** * Adds an entry file to the virtual file system. * * @param name - The file name or absolute path of the entry module. * @param contents - The contents of the entry file. * @param options - Optional parameters for writing the entry file. */ writeEntryFile(name: string, contents: string, options?: WriteRuntimeFileOptions): Promise<void>; /** * Writes a file to disk from the physical file system (on disk). * * @param path - The path to the file to write. * @param contents - The contents of the file to write. * @param options - Optional parameters for writing the file. * @returns A promise that resolves when the file is written. */ writeFileToDisk(path: PathLike, contents: string, options?: WriteRuntimeFileOptions): Promise<void>; /** * Synchronously checks if a file exists in the virtual file system (VFS). * * @param pathOrId - The path or ID of the file to check. * @returns `true` if the file exists, otherwise `false`. */ existsSync(pathOrId: PathLike): boolean; /** * Checks if a file exists in the virtual file system (VFS). * * @remarks * This is a base method used by {@link existsSync} - it does not try to resolve the path prior to checking if it exists or not. * * @param path - The path of the file to check. * @returns `true` if the file exists, otherwise `false`. */ fileExistsSync(path: PathLike): boolean; /** * Checks if a directory exists in the virtual file system (VFS). * * @param path - The path of the directory to check. * @returns `true` if the directory exists, otherwise `false`. */ directoryExistsSync(path: PathLike): boolean; /** * Checks if a path exists in the virtual file system (VFS). * * @param path - The path to check. * @returns `true` if the path exists, otherwise `false`. */ pathExistsSync(path: PathLike): boolean; /** * Retrieves the status of a file in the virtual file system (VFS). * * @param pathOrId - The path or ID of the file to retrieve status for. * @returns A promise that resolves to the file's status information, or false if the file does not exist. */ stat(pathOrId: PathLike, options?: fs.StatOptions & { bigint?: false | undefined; }): Promise<Stats>; /** * Synchronously retrieves the status of a file in the virtual file system (VFS). * * @param pathOrId - The path or ID of the file to retrieve status for. * @returns The file's status information, or false if the file does not exist. */ statSync(pathOrId: PathLike): Stats; /** * Retrieves the status of a symbolic link in the virtual file system (VFS). * * @param pathOrId - The path or ID of the symbolic link to retrieve status for. * @returns A promise that resolves to the symbolic link's status information, or false if the link does not exist. */ lstat(pathOrId: PathLike, options?: fs.StatOptions & { bigint?: false | undefined; }): Promise<Stats>; /** * Synchronously retrieves the status of a symbolic link in the virtual file system (VFS). * * @param pathOrId - The path or ID of the symbolic link to retrieve status for. * @returns The symbolic link's status information, or false if the link does not exist. */ lstatSync(pathOrId: PathLike, options?: StatSyncOptions & { bigint?: false | undefined; throwIfNoEntry: false; }): Stats | undefined; /** * Resolves a path or ID to a runtime file id in the virtual file system. * * @param pathOrId - The path or id of the file to resolve. * @returns The resolved id of the runtime file if it exists, otherwise false. */ resolveId(pathOrId: PathLike): string | false; /** * Resolves a path based on TypeScript's `tsconfig.json` paths. * * @see https://www.typescriptlang.org/tsconfig#paths * * @param path - The path to check. * @returns The resolved file path if it exists, otherwise undefined. */ resolveTsconfigPath(path: string): string | false; /** * Resolves a path based on TypeScript's `tsconfig.json` paths. * * @see https://www.typescriptlang.org/tsconfig#paths * * @param path - The path to check. * @returns The resolved file path if it exists, otherwise undefined. */ resolveTsconfigPathPackage(path: string): string | false; /** * Resolves a path or ID to its real path in the virtual file system (VFS). * * @param pathOrId - The path or ID to resolve. * @returns The resolved real path if it exists, otherwise undefined. */ realpathSync(pathOrId: PathLike): string; /** * Resolves a path or ID parameter to a corresponding virtual file path in the virtual file system (VFS). * * @param pathOrId - The path or ID to resolve. * @param options - Optional parameters for resolving the path, such as whether to include the file extension. * @returns The resolved file path if it exists, otherwise undefined. */ resolvePath(pathOrId: PathLike, options?: ResolvePathOptions): string | false; /** * Formats a file path by removing the runtime prefix and leading null character. * * @param path - The file path to format. * @returns The formatted file path. */ formatFilePath(path: string): string; /** * Converts a relative path to an absolute path based on the workspace and project root. * * @param path - The relative path to convert. * @returns The absolute path. */ formatAbsoluteFilePath: (path: string) => string; /** * Formats a runtime ID by removing the file extension and prepending the runtime prefix. * * @param id - The runtime ID to format. * @returns The formatted runtime ID. */ formatRuntimeId(id: string): string; /** * Resolves a path or ID parameter to a corresponding virtual file path in the virtual file system (VFS). * * @param pathOrId - The path or ID to resolve. * @returns The resolved file path if it exists, otherwise undefined. */ private resolvePathName; private resolveParentPaths; /** * Select the file system module to use for the operation based on the path or URL. * * @param pathOrUrl - The path to perform the file system operation on. * @param options - Options for the operation, such as output mode. * @returns The file system module used for the operation. */ private resolveFS; /** * Select the file system module to use for the operation based on the path or URL. * * @param pathOrUrl - The path to perform the file system operation on. * @param options - Options for the operation, such as output mode. * @returns The file system module used for the operation. */ private resolveOutputMode; /** * Clears the resolver cache for a given path. * * @param path - The path to clear the resolver cache for. */ private clearResolverCache; } /** * Creates a new virtual file system (VFS) using a Map to store {@link Vinyl} instances. * * @returns A virtual file-system containing a map where the keys are file paths and the values are {@link Vinyl} instances. */ declare function createVfs(context: Context): VirtualFileSystem; /** * Creates a new virtual file system (VFS) using a Map to store {@link Vinyl} instances. * * @returns A virtual file-system containing a map where the keys are file paths and the values are {@link Vinyl} instances. */ declare function restoreVfs(context: Context, serialized: SerializedVirtualFileSystem): VirtualFileSystem; export { RUNTIME_PREFIX, VirtualFileSystem, createVfs, restoreVfs };