or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

commit-pipeline.mdfile-copy.mdfile-management.mdfile-reading.mdfile-writing.mdindex.mdstate-management.mdtemplate-processing.mdtransform.md
tile.json

tessl/npm-mem-fs-editor

File edition helpers working on top of mem-fs

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/mem-fs-editor@11.1.x

To install, run

npx @tessl/cli install tessl/npm-mem-fs-editor@11.1.0

index.mddocs/

mem-fs-editor

mem-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.

Package Information

  • Package Name: mem-fs-editor
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install mem-fs-editor
  • Peer Dependency: mem-fs ^4.0.0

Core Imports

import { 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";

Basic Usage

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();

Architecture

mem-fs-editor is built around several key components:

  • MemFsEditor Class: Main interface providing all file operations as instance methods
  • Action Modules: Individual file operations (read, write, copy, etc.) implemented as separate modules
  • State Management: File state tracking system for managing pending changes and commit operations
  • Template Engine: EJS-based templating system for dynamic file generation
  • Glob Support: Pattern matching for bulk file operations using globby
  • Async Pipeline: Stream-based commit system for batch file operations with transforms

Capabilities

File Reading Operations

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 Reading

File Writing Operations

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;
}

File Writing

File Copy Operations

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;
}

File Copy Operations

Template Processing

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;
}

Template Processing

File Management

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;
}

File Management

Commit and Pipeline Operations

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

State Management

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;

State Management

Transform Operations

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;
}

Transform Operations

Core Types

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>;