frontend/infra/utils/fs-enhance/README.md
Enhanced file system utilities for improved developer experience
@coze-arch/fs-enhance is a lightweight TypeScript utility library that provides enhanced file system operations with modern async/await API. It offers convenient wrappers around Node.js file system operations with built-in support for JSON5 and YAML parsing, making it easier to work with configuration files and common file operations in your projects.
Since this is a workspace package, install it using the workspace protocol:
# Add to your package.json dependencies
"@coze-arch/fs-enhance": "workspace:*"
# Then run rush update
rush update
import {
isFileExists,
isDirExists,
readJsonFile,
readYamlFile,
writeJsonFile,
ensureDir,
readFileLineCount
} from '@coze-arch/fs-enhance';
// Check if file exists
const exists = await isFileExists('./config.json');
// Read JSON with type safety
interface Config {
name: string;
version: string;
}
const config = await readJsonFile<Config>('./config.json');
// Create directory if it doesn't exist
await ensureDir('./dist/output');
isFileExists(file: string): Promise<boolean>Checks if a file exists and is actually a file (not a directory).
const exists = await isFileExists('./package.json');
if (exists) {
console.log('Package.json found!');
}
isDirExists(file: string): Promise<boolean>Checks if a directory exists and is actually a directory (not a file).
const exists = await isDirExists('./src');
if (exists) {
console.log('Source directory found!');
}
readJsonFile<T>(file: string): Promise<T>Reads and parses a JSON file with JSON5 support (allows comments and relaxed syntax). Returns a typed result.
interface PackageJson {
name: string;
version: string;
dependencies?: Record<string, string>;
}
const pkg = await readJsonFile<PackageJson>('./package.json');
console.log(`Package: ${pkg.name}@${pkg.version}`);
readYamlFile<T extends object>(filePath: string): Promise<T>Reads and parses a YAML file. Returns a typed result.
interface DockerCompose {
version: string;
services: Record<string, any>;
}
const compose = await readYamlFile<DockerCompose>('./docker-compose.yml');
console.log(`Docker Compose version: ${compose.version}`);
readFileLineCount(file: string): Promise<number>Counts the number of lines in a text file.
const lineCount = await readFileLineCount('./src/index.ts');
console.log(`File has ${lineCount} lines`);
writeJsonFile(file: string, content: unknown): Promise<void>Writes an object to a JSON file with pretty formatting (2-space indentation).
const config = {
name: 'my-app',
version: '1.0.0',
features: ['json5', 'yaml']
};
await writeJsonFile('./config.json', config);
ensureDir(dir: string): Promise<void>Creates a directory and any necessary parent directories if they don't exist. Does nothing if the directory already exists.
// Creates ./dist/assets/images and any missing parent directories
await ensureDir('./dist/assets/images');
// Safe to call multiple times
await ensureDir('./dist/assets/images'); // No error, does nothing
fs-enhance/
├── src/
│ └── index.ts # Main implementation
├── __tests__/
│ └── file-enhance.test.ts # Test suite
├── package.json
├── tsconfig.json
└── README.md
# Run tests
rush test --to @coze-arch/fs-enhance
# Run tests with coverage
rush test:cov --to @coze-arch/fs-enhance
# Type check
rush build --to @coze-arch/fs-enhance
# Lint code
rush lint --to @coze-arch/fs-enhance
All functions handle errors gracefully:
isFileExists, isDirExists) return false instead of throwing when files don't existensureDir safely handles existing directories without errorsThis package is written in TypeScript and provides full type definitions. Generic types are supported for parsing operations:
// Strongly typed configuration
interface AppConfig {
database: {
host: string;
port: number;
};
features: string[];
}
const config = await readJsonFile<AppConfig>('./app.config.json');
// config is fully typed as AppConfig
Apache-2.0
Note: This package was extracted from rush-x utilities to provide reusable file system enhancements across the monorepo.