File edition helpers working on top of mem-fs
npx @tessl/cli install tessl/npm-mem-fs-editor@11.1.0mem-fs-editor provides comprehensive file system editing utilities that work on top of the mem-fs in-memory file system. It offers a rich API for file operations including reading, writing, copying, moving, and templating files with EJS template support, designed for build tools, scaffolding systems, code generators, and development tools.
npm install mem-fs-editormem-fs ^4.0.0import { create, MemFsEditor } from "mem-fs-editor";
import { create as createMemFs } from "mem-fs";For CommonJS:
const { create } = require("mem-fs-editor");
const { create: createMemFs } = require("mem-fs");State management utilities:
import {
setFileState,
isFileNew,
isFileStateModified,
setModifiedFileState
} from "mem-fs-editor/state";Transform utilities:
import { createCommitTransform } from "mem-fs-editor/transform";import { create as createMemFs } from "mem-fs";
import { create as createEditor } from "mem-fs-editor";
// Initialize the editor
const store = createMemFs();
const fs = createEditor(store);
// Write a file
fs.write("somefile.js", "var a = 1;");
// Read the file
const content = fs.read("somefile.js");
// Copy files with glob pattern
fs.copy("src/**/*.js", "dist/");
// Use templates with context
fs.copyTpl("templates/*.ejs", "output/", { name: "John" });
// Commit changes to disk
await fs.commit();mem-fs-editor is built around several key components:
Core file reading functionality for accessing file contents with support for various data formats.
function read(filepath: string, options?: ReadOptions): string | null;
function readJSON(filepath: string, defaults?: any): any;
function exists(filepath: string): boolean;
interface ReadOptions {
raw?: boolean;
defaults?: string | Buffer | null;
}File writing capabilities including basic write operations, JSON handling, and content appending.
function write(filepath: string, contents: string | Buffer, stat?: FileStats): string;
function writeJSON(filepath: string, contents: any, replacer?: JSONReplacer, space?: string | number): string;
function append(filepath: string, contents: string | Buffer, options?: AppendOptions): void;
interface FileStats {
mode?: number;
}
type JSONReplacer = ((key: string, value: any) => any) | (number | string)[] | null;
interface AppendOptions {
create?: boolean;
trimEnd?: boolean;
separator?: string;
}Comprehensive file copying with glob pattern support, processing functions, and template integration.
function copy(
from: string | string[],
to: string,
options?: CopyOptions,
context?: Record<string, any>,
tplSettings?: EJSOptions
): void;
function copyAsync(
from: string | string[],
to: string,
options?: CopyAsyncOptions,
context?: Record<string, any>,
tplSettings?: EJSOptions
): Promise<void>;
interface CopyOptions {
noGlob?: boolean;
globOptions?: GlobOptions;
ignoreNoMatch?: boolean;
fromBasePath?: string;
processDestinationPath?: (path: string) => string;
append?: boolean;
process?: (contents: string | Buffer, filepath: string, destination: string) => string | Buffer;
}EJS template processing for dynamic file generation with context variables and template options.
function copyTpl(
from: string | string[],
to: string,
context?: Record<string, any>,
tplSettings?: EJSOptions,
options?: CopySingleOptions
): void;
function copyTplAsync(
from: string | string[],
to: string,
context?: Record<string, any>,
tplSettings?: EJSOptions,
options?: CopySingleOptions
): Promise<void>;
function appendTpl(
filepath: string,
contents: string,
context: Record<string, any>,
templateOptions?: EJSOptions,
options?: AppendOptions
): void;
interface EJSOptions {
filename?: string;
cache?: boolean;
[key: string]: any;
}File management operations including moving, deleting, and bulk operations with glob pattern support.
function move(from: string, to: string, options?: CopyOptions): void;
function delete(paths: string | string[], options?: DeleteOptions): void;
interface DeleteOptions {
globOptions?: GlobOptions;
}
interface GlobOptions {
nodir?: boolean;
[key: string]: any;
}Pipeline-based commit system for batch file operations with custom transforms and filtering.
function commit<EditorFile extends MemFsEditorFile>(
options?: PipelineOptions<EditorFile> | FileTransform<EditorFile>,
...transforms: FileTransform<EditorFile>[]
): Promise<void>;
function dump<EditorFile extends MemFsEditorFile>(
cwd?: string,
filter?: string | ((file: EditorFile, cwd: string) => boolean)
): Record<string, MemFsEditorFileDump>;
interface PipelineOptions<T> {
filter?: (file: T) => boolean;
[key: string]: any;
}
interface FileTransform<T> {
(file: T): T | Promise<T>;
}
interface MemFsEditorFileDump {
contents: string | null;
state?: string;
stateCleared?: string;
}Commit and Pipeline Operations
File state management system for tracking file lifecycle states, commit status, and managing pending changes.
function setFileState(file: MemFsEditorFile, state: 'modified' | 'deleted'): void;
function isFileNew(file: MemFsEditorFile): boolean;
function isFileStateModified(file: MemFsEditorFile): boolean;
function setModifiedFileState(file: MemFsEditorFile): void;
function isFilePending(file: MemFsEditorFile): boolean;
function resetFile(file: MemFsEditorFile): void;Stream-based transformation utilities for processing files during commit operations with async support.
function createCommitTransform(): Transform;
interface Transform extends NodeJS.ReadWriteStream {
objectMode: true;
_transform(file: MemFsEditorFile, encoding: string, callback: TransformCallback): void;
}class MemFsEditor<EditorFile extends MemFsEditorFile = VinylMemFsEditorFile> {
store: Store<EditorFile>;
constructor(store: Store<EditorFile>);
}
interface MemFsEditorFile {
path: string;
stat?: { mode?: number } | null;
contents: Buffer | null;
committed?: boolean;
isNew?: boolean;
state?: 'modified' | 'deleted';
stateCleared?: 'modified' | 'deleted';
}
interface VinylMemFsEditorFile extends Omit<Vinyl, 'contents' | 'stat'>, MemFsEditorFile {}
function create<EditorFile extends MemFsEditorFile = VinylMemFsEditorFile>(
store: Store<EditorFile>
): MemFsEditor<EditorFile>;