@chara-codes/shared
v0.1.24
Published
Shared utilities and types for Chara Codes
Maintainers
Readme
@chara-codes/shared
Shared utilities and types for Chara Codes packages.
Overview
This package contains common utilities that are used across multiple Chara Codes packages. The main purpose is to avoid code duplication and provide consistent implementations of shared functionality.
Features
NodeFS - @netlify/build-info FileSystem Implementation
The NodeFS class provides a complete Node.js implementation of the FileSystem interface required by @netlify/build-info. This allows the build-info library to analyze projects in Node.js environments.
Installation
bun add @chara-codes/sharedUsage
NodeFS
import { NodeFS } from "@chara-codes/shared";
import { Project } from "@netlify/build-info";
// Create a NodeFS instance
const fs = new NodeFS();
// Use with @netlify/build-info
const project = new Project(fs, process.cwd())
.setEnvironment(process.env)
.setNodeVersion(process.version);
// Get build settings
const buildSettings = await project.getBuildSettings();
const devCommand = buildSettings[0]?.devCommand;
// Detect frameworks
const frameworks = await project.detectFrameworks();
// Detect package manager
const packageManager = await project.detectPackageManager();Direct File Operations
import { NodeFS } from "@chara-codes/shared";
const fs = new NodeFS();
// Check if file exists
const exists = await fs.fileExists("package.json");
// Read file content
const content = await fs.readFile("package.json");
// Write file content
await fs.writeFile("output.txt", "Hello, World!");
// List directory contents
const files = await fs.readDir("src");
// List directory with file types
const entries = await fs.readDir("src", true);
// Returns: { "file.ts": "file", "components": "directory" }API Reference
NodeFS Class
Methods
Path Operations
isAbsolute(path: string): boolean- Check if path is absolutedirname(path: string): string- Get directory namebasename(path: string): string- Get base nameresolve(...paths: string[]): string- Resolve pathsrelative(from: string, to: string): string- Get relative pathjoin(...segments: string[]): string- Join path segments
File Operations
fileExists(path: string): Promise<boolean>- Check if file existsreadFile(path: string): Promise<string>- Read file content as UTF-8writeFile(path: string, data: string): Promise<void>- Write file contentstat(path: string): Promise<Stats>- Get file/directory stats
Directory Operations
readdir(path: string): Promise<string[]>- Read directory entriesreadDir(path: string): Promise<string[]>- Read directory entries (alias)readDir(path: string, withFileTypes: true): Promise<Record<string, "directory" | "file">>- Read directory with file types
Environment
getEnvironment(): "node"- Get environment typecwd: string- Current working directory
Error Handling
The NodeFS class handles errors gracefully:
fileExists()returnsfalsefor non-existent files instead of throwingreadDir()returns empty array[]or empty object{}for non-existent directories- Other methods throw appropriate errors that can be caught and handled
Testing
The package includes comprehensive tests for all NodeFS functionality:
bun testTests cover:
- Path operations and resolution
- File and directory operations
- Error handling scenarios
- Integration with real filesystem
- Edge cases and permissions
Used By
This package is used by:
@chara-codes/agents- For project analysis and build detection@chara-codes/cli- For development environment initialization
Implementation Details
Why NodeFS?
The @netlify/build-info library requires a FileSystem implementation to work in different environments. By providing a Node.js-specific implementation, we enable:
- Automatic Framework Detection - Detect React, Vue, Next.js, etc.
- Build Command Detection - Find dev, build, and test scripts
- Package Manager Detection - Identify npm, yarn, pnpm, bun
- Workspace Support - Handle monorepos and workspaces
Design Principles
- Type Safety - Full TypeScript support with proper type definitions
- Error Resilience - Graceful handling of filesystem errors
- Performance - Efficient file operations with proper async/await
- Consistency - Uniform behavior across different use cases
Contributing
When adding new shared utilities:
- Add comprehensive tests
- Update this README
- Follow the existing code style
- Ensure backward compatibility
License
MIT