一键导入
file-system
This skill should be used when the user asks to 'scan directories', 'read files', 'parse JSON or Markdown', or 'traverse the file system'. Guides safe file I/O patterns and recursive directory operations.
菜单
This skill should be used when the user asks to 'scan directories', 'read files', 'parse JSON or Markdown', or 'traverse the file system'. Guides safe file I/O patterns and recursive directory operations.
This skill should be used when the user asks to 'create an agent', 'add tool calling', 'build workflows', or 'orchestrate AI tasks'. Guides agentic AI patterns and multi-step reasoning.
This skill should be used when the user asks to 'integrate an API', 'handle streaming responses', 'add authentication', or 'manage API calls'. Covers Claude API integration and streaming patterns.
This skill should be used when the user asks to 'add a command', 'parse arguments', 'improve CLI output', or 'add interactive features'. Covers command routing, argument parsing, and stdio handling.
This skill should be used when the user asks to 'generate Markdown', 'create documentation', 'structure content', or 'format skill definitions'. Covers Markdown generation and structured documentation patterns.
This skill should be used when the user asks to 'create a CLI tool', 'handle file system operations', 'manage processes', or 'configure runtime behavior'. Guides Node.js 20.x patterns and native APIs.
This skill should be used when the user asks to 'add types', 'fix type errors', 'create interfaces', 'improve type safety', or 'configure TypeScript'. Provides TypeScript strict mode patterns and ESM configuration for Node.js projects.
| name | file-system |
| description | This skill should be used when the user asks to 'scan directories', 'read files', 'parse JSON or Markdown', or 'traverse the file system'. Guides safe file I/O patterns and recursive directory operations. |
| version | 1.0.0 |
| metadata | {"internal":false} |
Safe, async file system patterns using Node.js native fs/promises API with ESM imports.
path.resolve())utf-8)import fs from "fs/promises";
import path from "path";
Always use fs/promises for async operations. Never use callback-based fs or synchronous fs.*Sync() methods.
async function scanDirectory(
dir: string,
ignorePatterns: string[] = ["node_modules", ".git", "dist"]
): Promise<string[]> {
const entries = await fs.readdir(dir, { withFileTypes: true });
const files: string[] = [];
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
// Skip ignored patterns
if (ignorePatterns.some((pattern) => entry.name.includes(pattern))) {
continue;
}
if (entry.isDirectory()) {
files.push(...(await scanDirectory(fullPath, ignorePatterns)));
} else {
files.push(fullPath);
}
}
return files;
}
Key Points:
withFileTypes: true to avoid extra stat() callsentry.isDirectory() before recursionnode_modules/, .git/, dist/, hidden files (.dotfile)async function readFileContent(filePath: string): Promise<string | null> {
try {
return await fs.readFile(filePath, "utf-8");
} catch (error) {
console.error(`Failed to read ${filePath}:`, error);
return null;
}
}
Why: File reads can fail (permissions, missing files). Return null on error instead of throwing to allow graceful degradation.
async function readJsonFile<T>(filePath: string): Promise<T | null> {
const content = await readFileContent(filePath);
if (!content) return null;
try {
return JSON.parse(content) as T;
} catch (error) {
console.error(`Invalid JSON in ${filePath}:`, error);
return null;
}
}
Usage:
interface PackageJson {
name: string;
dependencies?: Record<string, string>;
}
const pkg = await readJsonFile<PackageJson>("./package.json");
if (pkg?.dependencies) {
// Type-safe access
}
async function writeFile(filePath: string, content: string): Promise<void> {
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, content, "utf-8");
}
Always create parent directories first with { recursive: true }.
async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}
Don't use fs.existsSync() - keep everything async.
"utf-8" explicitly to readFile()readJsonFile<T>()).js extensions in imports - ESM requires explicit extensions: "./file-system.js"readFileSync() blocks the event loop; use await fs.readFile()writeFile() fails if the directory doesn't existnull instead for optional file reads