Skip to content

Commit 4cdd7e1

Browse files
committed
refactor: improve fs utilities and update entry point
- Enhance file system utilities - Minor updates to entry point Signed-off-by: leocavalcante <[email protected]>
1 parent ea23fbb commit 4cdd7e1

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/fs.ts

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const OPENCODE_DIR = ".opencode"
1111
const OPENCODER_SUBDIR = "opencoder"
1212

1313
/**
14-
* Initialize all workspace paths for a project
14+
* Initialize all workspace paths for a project.
15+
* @param projectDir - The root directory of the project
16+
* @returns Paths object containing all workspace file and directory paths
1517
*/
1618
export function initializePaths(projectDir: string): Paths {
1719
const opencoderDir = join(resolve(projectDir), OPENCODE_DIR, OPENCODER_SUBDIR)
@@ -30,7 +32,9 @@ export function initializePaths(projectDir: string): Paths {
3032
}
3133

3234
/**
33-
* Ensure all required directories exist
35+
* Ensure all required directories exist.
36+
* Creates the opencoder workspace directories if they don't exist.
37+
* @param paths - Paths object from initializePaths
3438
*/
3539
export function ensureDirectories(paths: Paths): void {
3640
const directories = [
@@ -49,7 +53,9 @@ export function ensureDirectories(paths: Paths): void {
4953
}
5054

5155
/**
52-
* Read a file, returning null if it doesn't exist
56+
* Read a file, returning null if it doesn't exist.
57+
* @param path - Absolute path to the file
58+
* @returns File contents as string, or null if file doesn't exist or read fails
5359
*/
5460
export async function readFileOrNull(path: string): Promise<string | null> {
5561
try {
@@ -60,14 +66,19 @@ export async function readFileOrNull(path: string): Promise<string | null> {
6066
}
6167

6268
/**
63-
* Write content to a file, creating parent directories if needed
69+
* Write content to a file, creating parent directories if needed.
70+
* @param path - Absolute path to the file
71+
* @param content - Content to write
6472
*/
6573
export async function writeFile(path: string, content: string): Promise<void> {
6674
await Bun.write(path, content)
6775
}
6876

6977
/**
70-
* Append content to a file
78+
* Append content to a file.
79+
* Creates the file if it doesn't exist.
80+
* @param path - Absolute path to the file
81+
* @param content - Content to append
7182
*/
7283
export function appendToFile(path: string, content: string): void {
7384
// Bun.write doesn't support append, so use node:fs for sync operation
@@ -76,14 +87,19 @@ export function appendToFile(path: string, content: string): void {
7687
}
7788

7889
/**
79-
* Check if a path exists
90+
* Check if a path exists.
91+
* @param path - Path to check (file or directory)
92+
* @returns True if the path exists, false otherwise
8093
*/
8194
export function pathExists(path: string): boolean {
8295
return existsSync(path)
8396
}
8497

8598
/**
86-
* List files in a directory with optional extension filter
99+
* List files in a directory with optional extension filter.
100+
* @param dirPath - Path to the directory
101+
* @param extension - Optional file extension to filter by (e.g., ".md")
102+
* @returns Array of filenames (not full paths), or empty array if directory doesn't exist
87103
*/
88104
export function listFiles(dirPath: string, extension?: string): string[] {
89105
if (!existsSync(dirPath)) {
@@ -100,7 +116,9 @@ export function listFiles(dirPath: string, extension?: string): string[] {
100116
}
101117

102118
/**
103-
* Get file modification time
119+
* Get file modification time.
120+
* @param path - Path to the file
121+
* @returns Modification time in milliseconds since epoch, or 0 if file doesn't exist
104122
*/
105123
export function getFileModTime(path: string): number {
106124
try {
@@ -112,7 +130,9 @@ export function getFileModTime(path: string): number {
112130
}
113131

114132
/**
115-
* Delete a file if it exists
133+
* Delete a file if it exists.
134+
* @param path - Path to the file to delete
135+
* @returns True if file was deleted, false if it didn't exist or deletion failed
116136
*/
117137
export function deleteFile(path: string): boolean {
118138
try {
@@ -127,7 +147,10 @@ export function deleteFile(path: string): boolean {
127147
}
128148

129149
/**
130-
* Rename/move a file
150+
* Rename or move a file.
151+
* @param oldPath - Current path of the file
152+
* @param newPath - New path for the file
153+
* @returns True if rename succeeded, false otherwise
131154
*/
132155
export function renameFile(oldPath: string, newPath: string): boolean {
133156
try {
@@ -139,7 +162,11 @@ export function renameFile(oldPath: string, newPath: string): boolean {
139162
}
140163

141164
/**
142-
* Clean up old files in a directory based on age
165+
* Clean up old files in a directory based on age.
166+
* Deletes files with modification time older than the specified number of days.
167+
* @param dirPath - Path to the directory to clean
168+
* @param maxAgeDays - Maximum age in days; files older than this will be deleted
169+
* @returns Number of files deleted
143170
*/
144171
export function cleanupOldFiles(dirPath: string, maxAgeDays: number): number {
145172
if (!existsSync(dirPath)) {
@@ -167,15 +194,17 @@ export function cleanupOldFiles(dirPath: string, maxAgeDays: number): number {
167194
}
168195

169196
/**
170-
* Generate a timestamp string for filenames (YYYYMMDD_HHMMSS)
197+
* Generate a timestamp string for filenames.
198+
* @returns Timestamp in YYYYMMDD_HHMMSS format (e.g., "20240115_143025")
171199
*/
172200
export function getTimestampForFilename(): string {
173201
const now = new Date()
174202
return now.toISOString().replace(/[-:]/g, "").replace("T", "_").slice(0, 15)
175203
}
176204

177205
/**
178-
* Generate an ISO timestamp string
206+
* Generate an ISO timestamp string.
207+
* @returns Current time in ISO 8601 format (e.g., "2024-01-15T14:30:25.123Z")
179208
*/
180209
export function getISOTimestamp(): string {
181210
return new Date().toISOString()

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bun
22
/**
3-
* Opencoder - Autonomous development loop powered by OpenCode
3+
* OpenCoder - Autonomous development loop powered by OpenCode
44
*
55
* Entry point for the CLI application.
66
*/

0 commit comments

Comments
 (0)