file-manager

A minimal package that abstracts reading files from a source directory and writing processed files to a destination directory with glob pattern support.

View on GitHub

File Manager

A TypeScript package for file operations with glob pattern support. It abstracts reading files from a source directory and writing processed files to a destination directory.

Features:

Installation

npm install @coursebook/file-manager

Usage

A minimal package that abstracts reading files from a source directory and writing processed files to a destination directory.

Basic Example

import {
  FileManagerImpl,
  type FileDataCollection,
} from "@coursebook/file-manager";

const fileManager = new FileManagerImpl();

// Configure
fileManager.setSourceDir("./source-folder");
fileManager.setDestDir("./destination-folder");
fileManager.setShouldClean(true);
fileManager.setIgnorePatterns(["*.tmp", "*.log"]);

// Read files
const files: FileDataCollection = await fileManager.readFiles();

// Process files...

// Write files
await fileManager.writeFiles(files);

API Reference

Source Directory Operations

// Set the source directory for file operations
setSourceDir(dir: string): void

// Get the current source directory
getSourceDir(): string | undefined

Destination Directory Operations

// Set the destination directory for file operations
setDestDir(dir?: string): void

// Get the current destination directory
getDestDir(): string | undefined

Ignore Pattern Operations

// Set patterns to ignore when reading files
setIgnorePatterns(patterns: string[]): void

// Get current ignore patterns
getIgnorePatterns(): string[]

File Operations

// Read all files from the source directory
readFiles(): Promise<ContentSmithFiles>

// Write files to the destination directory
writeFiles(files: ContentSmithFiles): Promise<void>

Configuration

// Set whether to clean the destination directory before builds
setShouldClean(clean: boolean): void

Error Handling

The component uses the FileManagerError class with specific error types:

Cloning the Repository

To make your workflow more organized, it’s a good idea to clone this repository into a directory named file-manager-workspace. This helps differentiate the workspace from the file-manager located in the packages directory.

git clone https://github.com/proj-coursebook/file-manager file-manager-workspace

cd file-manager-workspace

Repository Structure

How to Use This Repo

Using a VSCode Multi-root Workspace

With Visual Studio Code, you can enhance your development experience by using a multi-root workspace to access packages, examples, and playgrounds simultaneously. This approach is more efficient than opening the root directory, or each package or example separately.

To set up a multi-root workspace:

  1. Open Visual Studio Code.
  2. Navigate to File > Open Workspace from File....
  3. Select the file-manager.code-workspace file located at the root of the repository. This action will open all specified folders in one workspace.

The file-manager.code-workspace file can be customized to include different folders or settings. Here’s a typical configuration:

{
  "folders": [
    {
      "path": "packages/file-manager"
    },
    {
      "path": "examples/simple"
    },
    {
      "path": "playgrounds/fast-glob"
    }
  ],
  "settings": {
    // Add any workspace-specific settings here, for example:
    "git.openRepositoryInParentFolders": "always"
  }
}

Developing the Package

Change to the package directory and install dependencies:

cd packages/file-manager
npm install

Package Management

When you are ready to publish your package:

npm run release

This single command will:

[!TIP] For detailed information about package publishing, versioning, and local development workflows, see the NPM Package Management Guide.