49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
import Store from 'store'
|
|
|
|
const noWorkingDirWarning = "Please open a directory to allow file saving."
|
|
|
|
export let cwd
|
|
export let store
|
|
|
|
export async function Start() {
|
|
store = await Store.Open('device', 'handles')
|
|
cwd = await store.Get('directory')
|
|
}
|
|
|
|
export async function cd() {
|
|
cwd = await window.showDirectoryPicker()
|
|
await store.Set('directory', cwd)
|
|
}
|
|
|
|
export async function Entries(handle = cwd) {
|
|
let pool = new Map()
|
|
|
|
for await (let [ , e ] of handle.entries()) {
|
|
pool.set(e.name, e)
|
|
}
|
|
|
|
return pool
|
|
}
|
|
|
|
export async function Create(sPath) {
|
|
return cwd.getFileHandle(sPath, { create: true })
|
|
}
|
|
|
|
export async function Open(hFile) {
|
|
if(!cwd) {
|
|
return new Blob([ noWorkingDirWarning ], { type: "text/plain" })
|
|
}
|
|
|
|
return hFile.getFile()
|
|
}
|
|
|
|
export async function Write(hFile, sContent) {
|
|
if(!cwd) {
|
|
throw new Error("Unable to save file: no working directory")
|
|
}
|
|
|
|
let s = await hFile.createWritable()
|
|
.catch(console.error)
|
|
await s.write(sContent)
|
|
await s.close()
|
|
} |