2024-07-23 17:16:26 -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-07-23 17:16:26 -04:00
|
|
|
async join(dir: string, path: string): Promise<string> {
|
2024-07-26 15:52:35 -04:00
|
|
|
return Promise.resolve(window.electron.path.join(dir, path))
|
2024-07-23 17:16:26 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 11:45:40 -04:00
|
|
|
async readFile(path: string): Promise<Uint8Array | void> {
|
2024-07-23 17:16:26 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-07-24 21:37:29 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:16:26 -04:00
|
|
|
return this.join(this.dir, path)
|
2024-02-12 12:18:37 -08:00
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error reading file: ${error}`))
|
2024-02-12 12:18:37 -08:00
|
|
|
})
|
2024-07-31 16:54:32 -04:00
|
|
|
.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-07-23 17:16:26 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-07-24 21:37:29 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-12 12:18:37 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:16:26 -04:00
|
|
|
return this.join(this.dir, path)
|
2024-02-12 12:18:37 -08:00
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error checking file exists: ${error}`))
|
2024-02-12 12:18:37 -08:00
|
|
|
})
|
2024-07-26 15:52:35 -04:00
|
|
|
.then(async (file) => {
|
2024-07-31 15:31:55 -04:00
|
|
|
try {
|
|
|
|
await window.electron.stat(file)
|
|
|
|
} catch (e) {
|
2024-07-26 15:52:35 -04:00
|
|
|
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-07-23 17:16:26 -04:00
|
|
|
// Using local file system only works from desktop.
|
|
|
|
if (!isDesktop()) {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(
|
2024-07-24 21:37:29 -04:00
|
|
|
new Error(
|
|
|
|
'This function can only be called from the desktop application'
|
|
|
|
)
|
2024-02-19 12:33:16 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:16:26 -04:00
|
|
|
return this.join(this.dir, path)
|
2024-02-19 12:33:16 -08:00
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error joining dir: ${error}`))
|
2024-02-19 12:33:16 -08:00
|
|
|
})
|
2024-07-23 17:16:26 -04:00
|
|
|
.then((filepath) => {
|
2024-07-31 15:31:55 -04:00
|
|
|
return window.electron
|
|
|
|
.readdir(filepath)
|
2024-02-19 12:33:16 -08:00
|
|
|
.catch((error) => {
|
2024-06-24 11:45:40 -04:00
|
|
|
return Promise.reject(new Error(`Error reading dir: ${error}`))
|
2024-02-19 12:33:16 -08:00
|
|
|
})
|
|
|
|
.then((files) => {
|
|
|
|
return files.map((file) => file.path)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-02-12 12:18:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const fileSystemManager = new FileSystemManager()
|