2024-08-16 07:15:42 -04:00
|
|
|
import { isDesktop } from 'lib/isDesktop'
|
2024-02-12 12:18:37 -08:00
|
|
|
|
|
|
|
/// FileSystemManager is a class that provides a way to read files from the local file system.
|
|
|
|
/// It assumes that you are in a project since it is solely used by the std lib
|
|
|
|
/// when executing code.
|
|
|
|
class FileSystemManager {
|
|
|
|
private _dir: string | null = null
|
|
|
|
|
|
|
|
get dir() {
|
2024-06-24 11:45:40 -04:00
|
|
|
return this._dir ?? ''
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
set dir(dir: string) {
|
|
|
|
this._dir = dir
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
async join(dir: string, path: string): Promise<string> {
|
|
|
|
return Promise.resolve(window.electron.path.join(dir, path))
|
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async readFile(path: string): Promise<Uint8Array | void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-08-16 07:15:42 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
return this.join(this.dir, path).then((filePath) => {
|
|
|
|
return window.electron.readFile(filePath)
|
|
|
|
})
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async exists(path: string): Promise<boolean | void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-08-16 07:15:42 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
return this.join(this.dir, path).then(async (file) => {
|
|
|
|
try {
|
|
|
|
await window.electron.stat(file)
|
|
|
|
} catch (e) {
|
|
|
|
if (e === 'ENOENT') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
2024-02-19 12:33:16 -08:00
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async getAllFiles(path: string): Promise<string[] | void> {
|
2024-08-16 07:15:42 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-08-16 07:15:42 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-19 12:33:16 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-16 07:15:42 -04:00
|
|
|
return this.join(this.dir, path).then((filepath) => {
|
|
|
|
return window.electron
|
|
|
|
.readdir(filepath)
|
|
|
|
.catch((error: Error) => {
|
|
|
|
return Promise.reject(new Error(`Error reading dir: ${error}`))
|
|
|
|
})
|
|
|
|
.then((files: string[]) => {
|
|
|
|
return files.map((filePath) => filePath)
|
|
|
|
})
|
|
|
|
})
|
2024-02-19 12:33:16 -08:00
|
|
|
}
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const fileSystemManager = new FileSystemManager()
|