-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathfile-operations.ts
More file actions
159 lines (138 loc) · 4.84 KB
/
Copy pathfile-operations.ts
File metadata and controls
159 lines (138 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { promises as fs } from 'fs'
import path from 'path'
import { logger } from '../utils/logger'
import { getReposPath } from '@opencode-manager/shared/config/env'
export async function readFileContent(filePath: string): Promise<string> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
return await fs.readFile(fullPath, 'utf8')
} catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error}`)
}
}
export async function readFileAsBase64(filePath: string): Promise<string> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
const buffer = await fs.readFile(fullPath)
return buffer.toString('base64')
} catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error}`)
}
}
export async function writeFileContent(
filePath: string,
content: string | Buffer
): Promise<void> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
await fs.mkdir(path.dirname(fullPath), { recursive: true })
await fs.writeFile(fullPath, Buffer.isBuffer(content) ? content : Buffer.from(content, 'utf8'))
logger.info(`Wrote file to: ${fullPath}`)
} catch (error) {
throw new Error(`Failed to write file ${filePath}: ${error}`)
}
}
export async function ensureDirectoryExists(dirPath: string): Promise<void> {
try {
const fullPath = path.isAbsolute(dirPath) ? dirPath : path.resolve(dirPath)
await fs.mkdir(fullPath, { recursive: true })
} catch (error) {
throw new Error(`Failed to create directory ${dirPath}: ${error}`)
}
}
export async function fileExists(filePath: string): Promise<boolean> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
await fs.access(fullPath)
return true
} catch {
return false
}
}
export async function deletePath(filePath: string): Promise<void> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
const stats = await fs.stat(fullPath)
if (stats.isDirectory()) {
await fs.rm(fullPath, { recursive: true, force: true })
} else {
await fs.unlink(fullPath)
}
} catch (error) {
throw new Error(`Failed to delete path ${filePath}: ${error}`)
}
}
export async function getFileStats(filePath: string): Promise<{ size: number; lastModified: Date; isDirectory: boolean }> {
try {
const fullPath = path.isAbsolute(filePath) ? filePath : path.join(getReposPath(), filePath)
const stats = await fs.stat(fullPath)
return {
size: stats.size,
lastModified: stats.mtime,
isDirectory: stats.isDirectory()
}
} catch (error) {
throw new Error(`Failed to get stats for ${filePath}: ${error}`)
}
}
export async function listDirectory(dirPath: string): Promise<Array<{
name: string
path: string
isDirectory: boolean
size: number
lastModified: Date
}>> {
try {
const fullPath = path.isAbsolute(dirPath) ? dirPath : path.join(getReposPath(), dirPath)
const entries = await fs.readdir(fullPath, { withFileTypes: true })
const result = []
for (const entry of entries) {
if (entry.name === '.' || entry.name === '..') continue
const entryPath = path.join(fullPath, entry.name)
const stats = await fs.stat(entryPath)
result.push({
name: entry.name,
path: entryPath,
isDirectory: entry.isDirectory(),
size: entry.isDirectory() ? 0 : stats.size,
lastModified: stats.mtime
})
}
return result
} catch (error) {
throw new Error(`Failed to list directory ${dirPath}: ${error}`)
}
}
export async function directoryExists(dirPath: string): Promise<boolean> {
try {
const fullPath = path.isAbsolute(dirPath) ? dirPath : path.join(getReposPath(), dirPath)
const stats = await fs.stat(fullPath)
return stats.isDirectory()
} catch {
return false
}
}
export async function removeDirectory(dirPath: string): Promise<void> {
try {
const fullPath = path.isAbsolute(dirPath) ? dirPath : path.join(getReposPath(), dirPath)
await fs.rm(fullPath, { recursive: true, force: true })
} catch (error) {
throw new Error(`Failed to remove directory ${dirPath}: ${error}`)
}
}
export async function listDirectoryNames(dirPath: string): Promise<string[]> {
try {
const fullPath = path.isAbsolute(dirPath) ? dirPath : path.join(getReposPath(), dirPath)
const entries = await fs.readdir(fullPath, { withFileTypes: true })
const directories: string[] = []
for (const entry of entries) {
if (entry.isDirectory()) {
directories.push(entry.name)
}
}
return directories
} catch (error) {
logger.error(`Failed to list directory names for ${dirPath}:`, error)
return []
}
}