Import files (#1393)
* initial shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * make file system work locally or with tauri Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixxes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates; Signed-off-by: Jess Frazelle <github@jessfraz.com> * add tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * add more tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * better errrors Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * better docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * better docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * better errors Signed-off-by: Jess Frazelle <github@jessfraz.com> * make no assign work Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * closer Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * more additions to passing around fs Signed-off-by: Jess Frazelle <github@jessfraz.com> * make work Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
62
src/lang/std/fileSystemManager.ts
Normal file
62
src/lang/std/fileSystemManager.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import {
|
||||
readBinaryFile,
|
||||
exists as tauriExists,
|
||||
BaseDirectory,
|
||||
} from '@tauri-apps/api/fs'
|
||||
import { isTauri } from 'lib/isTauri'
|
||||
import { join } from '@tauri-apps/api/path'
|
||||
|
||||
/// 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() {
|
||||
if (this._dir === null) {
|
||||
throw new Error('current project dir is not set')
|
||||
}
|
||||
|
||||
return this._dir
|
||||
}
|
||||
|
||||
set dir(dir: string) {
|
||||
this._dir = dir
|
||||
}
|
||||
|
||||
readFile(path: string): Promise<Uint8Array | void> {
|
||||
// Using local file system only works from Tauri.
|
||||
if (!isTauri()) {
|
||||
throw new Error(
|
||||
'This function can only be called from a Tauri application'
|
||||
)
|
||||
}
|
||||
|
||||
return join(this.dir, path)
|
||||
.catch((error) => {
|
||||
throw new Error(`Error reading file: ${error}`)
|
||||
})
|
||||
.then((file) => {
|
||||
return readBinaryFile(file)
|
||||
})
|
||||
}
|
||||
|
||||
exists(path: string): Promise<boolean | void> {
|
||||
// Using local file system only works from Tauri.
|
||||
if (!isTauri()) {
|
||||
throw new Error(
|
||||
'This function can only be called from a Tauri application'
|
||||
)
|
||||
}
|
||||
|
||||
return join(this.dir, path)
|
||||
.catch((error) => {
|
||||
throw new Error(`Error checking file exists: ${error}`)
|
||||
})
|
||||
.then((file) => {
|
||||
return tauriExists(file)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export const fileSystemManager = new FileSystemManager()
|
Reference in New Issue
Block a user