parent
6f8618d523
commit
a083b589eb
@ -11,7 +11,7 @@ edition = "2021"
|
|||||||
tauri-build = { version = "1", features = [] }
|
tauri-build = { version = "1", features = [] }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tauri = { version = "1", features = [ "fs-write-file", "fs-read-file", "path-all", "fs-exists", "shell-open"] }
|
tauri = { version = "1", features = [ "fs-read-file", "fs-create-dir", "fs-exists", "fs-write-file", "path-all", "shell-open"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
|
||||||
|
@ -22,10 +22,11 @@
|
|||||||
},
|
},
|
||||||
"fs": {
|
"fs": {
|
||||||
"all": false,
|
"all": false,
|
||||||
|
"createDir": true,
|
||||||
"readFile": true,
|
"readFile": true,
|
||||||
"writeFile": true,
|
"writeFile": true,
|
||||||
"exists": true,
|
"exists": true,
|
||||||
"scope": ["$APPDATA/*"]
|
"scope": ["$APPDATA/*", "$HOME/Library/Application Support/com.KlectrRadio.dev"]
|
||||||
},
|
},
|
||||||
"path": {
|
"path": {
|
||||||
"all": true
|
"all": true
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
import { exists, readTextFile, writeTextFile } from "@tauri-apps/api/fs"
|
import {
|
||||||
|
createDir,
|
||||||
|
exists,
|
||||||
|
readTextFile,
|
||||||
|
writeTextFile,
|
||||||
|
} from "@tauri-apps/api/fs"
|
||||||
import { appDataDir } from "@tauri-apps/api/path"
|
import { appDataDir } from "@tauri-apps/api/path"
|
||||||
import { createStore } from "kaioken"
|
import { createStore } from "kaioken"
|
||||||
import { Station } from "../hooks/stationStores"
|
import { Station } from "../hooks/stationStores"
|
||||||
@ -10,7 +15,11 @@ export const useStorageStore = createStore(
|
|||||||
|
|
||||||
export function useStorage() {
|
export function useStorage() {
|
||||||
async function _createStationsFile(path: string): Promise<Station[]> {
|
async function _createStationsFile(path: string): Promise<Station[]> {
|
||||||
await writeTextFile(path, "[]", { append: false })
|
try {
|
||||||
|
await writeTextFile(path, "[]", { append: false })
|
||||||
|
} catch (err) {
|
||||||
|
console.error("CUSTOM ERROR: ", err)
|
||||||
|
}
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,9 +31,17 @@ export function useStorage() {
|
|||||||
console.error("getStationsFile: ", err)
|
console.error("getStationsFile: ", err)
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dir) return undefined
|
if (!dir) return undefined
|
||||||
|
|
||||||
|
const dirExists = await exists(dir)
|
||||||
|
if (!dirExists) {
|
||||||
|
await createDir(dir)
|
||||||
|
}
|
||||||
|
|
||||||
const path = `${dir}stations.json`
|
const path = `${dir}stations.json`
|
||||||
if (!(await exists(path))) return await _createStationsFile(path)
|
const pathExists = await exists(path)
|
||||||
|
if (!pathExists) return await _createStationsFile(path)
|
||||||
const jsonString = await readTextFile(path)
|
const jsonString = await readTextFile(path)
|
||||||
const json = JSON.parse(jsonString) as Station[]
|
const json = JSON.parse(jsonString) as Station[]
|
||||||
useStorageStore.setState(() => path)
|
useStorageStore.setState(() => path)
|
||||||
|
Loading…
Reference in New Issue
Block a user