CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mem-fs-editor

File edition helpers working on top of mem-fs

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

file-copy.mddocs/

File Copy Operations

Comprehensive file copying with glob pattern support, processing functions, and template integration for duplicating and transforming files during copy operations.

Capabilities

Basic File Copy

Copy files from source to destination with support for glob patterns and batch operations.

/**
 * Copy files with glob pattern support
 * @param from - Source path(s) or glob pattern
 * @param to - Destination path
 * @param options - Copy configuration options
 * @param context - Template context (if using templates)
 * @param tplSettings - EJS template settings
 */
function copy(
  from: string | string[],
  to: string,
  options?: CopyOptions,
  context?: Record<string, any>,
  tplSettings?: EJSOptions
): void;

interface CopyOptions {
  /** Disable glob pattern matching (default: false) */
  noGlob?: boolean;
  /** Options passed to glob library */
  globOptions?: GlobOptions;
  /** Don't throw error if no files match pattern (default: false) */
  ignoreNoMatch?: boolean;
  /** Base path for relative source paths */
  fromBasePath?: string;
  /** Transform destination paths */
  processDestinationPath?: (path: string) => string;
  /** Append to destination files instead of overwriting */
  append?: boolean;
  /** Process file contents during copy */
  process?: (contents: string | Buffer, filepath: string, destination: string) => string | Buffer;
}

interface GlobOptions {
  /** Exclude directories from results */
  nodir?: boolean;
  /** Additional glob options */
  [key: string]: any;
}

interface EJSOptions {
  /** Template filename for error reporting */
  filename?: string;
  /** Enable template caching */
  cache?: boolean;
  /** Additional EJS options */
  [key: string]: any;
}

interface CopySingleOptions {
  /** Append to destination files instead of overwriting */
  append?: boolean;
  /** Process file contents during copy */
  process?: (contents: string | Buffer, filepath: string, destination: string) => string | Buffer;
}

Usage Examples:

import { create as createMemFs } from "mem-fs";
import { create as createEditor } from "mem-fs-editor";

const store = createMemFs();  
const fs = createEditor(store);

// Copy single file
fs.copy("src/index.js", "dist/index.js");

// Copy with glob pattern
fs.copy("src/**/*.js", "dist/");

// Copy multiple specific files
fs.copy(["src/main.js", "src/utils.js"], "dist/");

// Copy all files from directory
fs.copy("templates/*", "output/");

// Copy with custom destination processing
fs.copy("src/**/*.ts", "dist/", {
  processDestinationPath: (path) => path.replace(/\.ts$/, ".js")
});

Async File Copy

Asynchronous file copying that reads directly from disk and supports background processing.

/**
 * Async version of copy with direct disk access
 * @param from - Source path(s) or glob pattern
 * @param to - Destination path
 * @param options - Copy configuration options
 * @param context - Template context (if using templates)
 * @param tplSettings - EJS template settings
 * @returns Promise that resolves when copy is complete
 */
function copyAsync(
  from: string | string[],
  to: string,
  options?: CopyAsyncOptions,
  context?: Record<string, any>,
  tplSettings?: EJSOptions
): Promise<void>;

interface CopyAsyncOptions extends CopySingleAsyncOptions {
  /** Options passed to glob library */
  globOptions?: GlobOptions;
  /** Transform destination paths */
  processDestinationPath?: (path: string) => string;
  /** Don't throw error if no files match pattern (default: false) */
  ignoreNoMatch?: boolean;
}

interface CopySingleAsyncOptions extends AppendOptions, CopySingleOptions {
  /** Append to destination files instead of overwriting */
  append?: boolean;
  /** Process individual file paths during async copy */
  processFile?: (this: MemFsEditor, filepath: string) => string | Promise<string | Buffer>;
}

Usage Examples:

// Async copy with await
await fs.copyAsync("src/**/*.js", "dist/");

// Async copy with file processing
await fs.copyAsync("images/**/*.jpg", "optimized/", {
  processFile: async (file) => {
    console.log(`Processing ${file}`);
    // Could add image optimization here
  }
});

// Async copy with content transformation
await fs.copyAsync("src/**/*.ts", "dist/", {
  process: (contents, filepath) => {
    // Transform TypeScript to JavaScript during copy
    return contents.toString().replace(/: \w+/g, "");
  }
});

Content Processing During Copy

Transform file contents during copy operations using custom processing functions.

/**
 * Copy single file with processing options
 * @param from - Source file path
 * @param to - Destination path
 * @param options - Copy options including processing function
 */
function _copySingle(from: string, to: string, options?: CopySingleOptions): void;

/**
 * Async version of _copySingle for single file copy operations
 * @param from - Source file path
 * @param to - Destination path
 * @param options - Copy options including processing function
 * @returns Promise that resolves when copy is complete
 */
function _copySingleAsync(from: string, to: string, options?: CopySingleOptions): Promise<void>;

interface CopySingleOptions {
  /** Append to destination instead of overwriting */
  append?: boolean;
  /** Process file contents during copy */
  process?: (contents: string | Buffer, filepath: string, destination: string) => string | Buffer;
}

Usage Examples:

// Copy with content transformation
fs.copy("src/config.template.js", "dist/config.js", {
  process: (contents, filepath, destination) => {
    return contents
      .toString()
      .replace("{{VERSION}}", "1.2.3")
      .replace("{{ENV}}", "production");
  }
});

// Copy and minify JavaScript
fs.copy("src/**/*.js", "dist/", {
  process: (contents) => {
    // Simple minification (remove comments and extra whitespace)
    return contents
      .toString()
      .replace(/\/\*[\s\S]*?\*\//g, "")
      .replace(/\/\/.*$/gm, "")
      .replace(/^\s+/gm, "");
  }
});

// Copy with file-specific processing
fs.copy("docs/**/*.md", "output/", {
  process: (contents, filepath) => {
    if (filepath.endsWith("README.md")) {
      return `# Generated Documentation\n\n${contents}`;
    }
    return contents;
  }
});

Advanced Copy Patterns

Directory Structure Copying

// Maintain directory structure
fs.copy("src/**/*", "backup/", {
  fromBasePath: "src"
});

// Flatten directory structure
fs.copy("src/**/*.js", "dist/js/", {
  processDestinationPath: (path) => {
    const filename = path.split("/").pop();
    return `dist/js/${filename}`;
  }
});

// Reorganize by file type
fs.copy("mixed/**/*", "organized/", {
  processDestinationPath: (path) => {
    const ext = path.split(".").pop();
    const filename = path.split("/").pop();
    return `organized/${ext}/${filename}`;
  }
});

Conditional Copying

// Copy only specific file types
fs.copy("src/**/*", "dist/", {
  globOptions: {
    ignore: ["**/*.test.js", "**/*.spec.js"]
  }
});

// Copy with size or date conditions
fs.copy("assets/**/*", "public/", {
  process: (contents, filepath) => {
    // Only copy files smaller than 1MB
    if (contents.length > 1024 * 1024) {
      console.log(`Skipping large file: ${filepath}`);
      return null; // Skip this file
    }
    return contents;
  }
});

Batch Copy Operations

// Copy multiple source patterns
const sources = [
  "src/**/*.js",
  "assets/**/*.css", 
  "public/**/*.html"
];

sources.forEach(pattern => {
  fs.copy(pattern, "dist/");
});

// Copy with different destinations
const copyMap = {
  "src/**/*.js": "dist/js/",
  "styles/**/*.css": "dist/css/",
  "images/**/*.{png,jpg}": "dist/images/"
};

Object.entries(copyMap).forEach(([from, to]) => {
  fs.copy(from, to);
});

Error Handling

// Handle missing source files
fs.copy("optional/**/*", "dist/", {
  ignoreNoMatch: true
});

// Copy with error handling
try {
  fs.copy("src/**/*.js", "dist/");
} catch (error) {
  if (error.message.includes("no files found")) {
    console.log("No JavaScript files to copy");
  } else {
    throw error;
  }
}

// Async copy with error handling
try {
  await fs.copyAsync("large-files/**/*", "backup/");
} catch (error) {
  console.error("Copy failed:", error.message);
}

docs

commit-pipeline.md

file-copy.md

file-management.md

file-reading.md

file-writing.md

index.md

state-management.md

template-processing.md

transform.md

tile.json