mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-03-28 03:29:25 +01:00
107 lines
2.7 KiB
TypeScript
107 lines
2.7 KiB
TypeScript
import { mkdir, readdir, readFile, stat, unlink } from 'fs/promises'
|
|
import { join } from 'path'
|
|
import { FileEntry } from '../../types/files.js'
|
|
import { createReadStream } from 'fs'
|
|
|
|
export async function listDirectoryContents(path: string): Promise<FileEntry[]> {
|
|
const entries = await readdir(path, { withFileTypes: true })
|
|
const results: FileEntry[] = []
|
|
for (const entry of entries) {
|
|
if (entry.isFile()) {
|
|
results.push({
|
|
type: 'file',
|
|
key: join(path, entry.name),
|
|
name: entry.name,
|
|
})
|
|
} else if (entry.isDirectory()) {
|
|
results.push({
|
|
type: 'directory',
|
|
prefix: join(path, entry.name),
|
|
name: entry.name,
|
|
})
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
export async function listDirectoryContentsRecursive(path: string): Promise<FileEntry[]> {
|
|
let results: FileEntry[] = []
|
|
const entries = await readdir(path, { withFileTypes: true })
|
|
for (const entry of entries) {
|
|
const fullPath = join(path, entry.name)
|
|
if (entry.isDirectory()) {
|
|
const subdirectoryContents = await listDirectoryContentsRecursive(fullPath)
|
|
results = results.concat(subdirectoryContents)
|
|
} else {
|
|
results.push({
|
|
type: 'file',
|
|
key: fullPath,
|
|
name: entry.name,
|
|
})
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
export async function ensureDirectoryExists(path: string): Promise<void> {
|
|
try {
|
|
await stat(path)
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
await mkdir(path, { recursive: true })
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function getFile(path: string, returnType: 'buffer'): Promise<Buffer | null>
|
|
export async function getFile(
|
|
path: string,
|
|
returnType: 'stream'
|
|
): Promise<NodeJS.ReadableStream | null>
|
|
export async function getFile(path: string, returnType: 'string'): Promise<string | null>
|
|
export async function getFile(
|
|
path: string,
|
|
returnType: 'buffer' | 'string' | 'stream' = 'buffer'
|
|
): Promise<Buffer | string | NodeJS.ReadableStream | null> {
|
|
try {
|
|
if (returnType === 'string') {
|
|
return await readFile(path, 'utf-8')
|
|
} else if (returnType === 'stream') {
|
|
return createReadStream(path)
|
|
}
|
|
return await readFile(path)
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
return null
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function getFileStatsIfExists(
|
|
path: string
|
|
): Promise<{ size: number; modifiedTime: Date } | null> {
|
|
try {
|
|
const stats = await stat(path)
|
|
return {
|
|
size: stats.size,
|
|
modifiedTime: stats.mtime,
|
|
}
|
|
} catch (error) {
|
|
if (error.code === 'ENOENT') {
|
|
return null
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function deleteFileIfExists(path: string): Promise<void> {
|
|
try {
|
|
await unlink(path)
|
|
} catch (error) {
|
|
if (error.code !== 'ENOENT') {
|
|
throw error
|
|
}
|
|
}
|
|
}
|