From a342250b222ae6ab51e5585e5e1d48bfaabc1c25 Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Fri, 15 May 2020 14:52:12 +0900 Subject: [PATCH] v0.1.2: Save dataDir config to ~/.config/fuji/config.json --- package.json | 2 +- src/App/Eval.purs | 7 ++++--- src/FFI/Tauri.js | 12 +++++++++--- src/FFI/Tauri.purs | 8 +++----- src/js/tauri.js | 26 ++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 18f109c..7818941 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fuji", - "version": "0.1.1", + "version": "0.1.2", "private": true, "repository": "https://github.com/nonbili/fuji", "author": "Nonbili", diff --git a/src/App/Eval.purs b/src/App/Eval.purs index fcbb535..6cc747c 100644 --- a/src/App/Eval.purs +++ b/src/App/Eval.purs @@ -3,15 +3,16 @@ module App.Eval where import Fuji.Prelude import App.Types (DSL) +import Effect.Aff as Aff import FFI.Tauri as Tauri import Halogen as H import Model.Store as Store init :: DSL Unit init = do - liftEffect Tauri.getDataDir >>= case _ of - Nothing -> H.modify_ $ _ { isInitModalOpen = true } - Just _ -> load + liftAff (Aff.attempt Tauri.getDataDir) >>= case _ of + Left _ -> H.modify_ $ _ { isInitModalOpen = true } + Right _ -> load load :: DSL Unit load = do diff --git a/src/FFI/Tauri.js b/src/FFI/Tauri.js index 4bd598a..f284f3c 100644 --- a/src/FFI/Tauri.js +++ b/src/FFI/Tauri.js @@ -5,19 +5,25 @@ const { readFile, writeFile, removeFile, + readConfig, + writeConfig, tauriOn } = require("../../src/js/tauri"); -let dataDir = tauriOn ? localStorage.getItem("fuji") : "fuji"; +let dataDir; const getFilePath = name => dataDir + "/" + name; -exports.getDataDir_ = () => dataDir; +exports.getDataDir_ = () => + readConfig().then(config => { + dataDir = config.dataDir; + return dataDir; + }); exports.setDataDir = dir => () => { dataDir = dir; TauriFS.createDir(getFilePath("notes")); - localStorage.setItem("fuji", dir); + writeConfig({ dataDir }); }; exports.readFile_ = file => () => readFile(getFilePath(file)); diff --git a/src/FFI/Tauri.purs b/src/FFI/Tauri.purs index 7facf18..668bb96 100644 --- a/src/FFI/Tauri.purs +++ b/src/FFI/Tauri.purs @@ -12,14 +12,12 @@ import Fuji.Prelude import Control.Promise (Promise) import Control.Promise as Promise -import Data.Nullable (Nullable) -import Data.Nullable as Nullable newtype FileName = FileName String -foreign import getDataDir_ :: Effect (Nullable String) -getDataDir ::Effect (Maybe String) -getDataDir = Nullable.toMaybe <$> getDataDir_ +foreign import getDataDir_ :: Effect (Promise String) +getDataDir ::Aff String +getDataDir = Promise.toAffE getDataDir_ foreign import setDataDir :: String -> Effect Unit diff --git a/src/js/tauri.js b/src/js/tauri.js index cfe9bef..a83f63a 100644 --- a/src/js/tauri.js +++ b/src/js/tauri.js @@ -31,3 +31,29 @@ export const removeFile = file => tauriOn ? TauriFS.removeFile(file) : Promise.resolve(localStorage.removeItem(file)); + +if (tauriOn) { + // Init fuji filder inside the system config folder. e.g. `~/.config/fuji` on + // Linux. + const configDirOption = { dir: TauriFS.Dir.Config }; + TauriFS.readDir("fuji", configDirOption).then( + () => {}, + () => { + TauriFS.createDir("fuji", configDirOption); + } + ); +} + +const appDirOption = { dir: TauriFS.Dir.App }; + +export const readConfig = () => + tauriOn + ? TauriFS.readTextFile("config.json", appDirOption).then(JSON.parse) + : Promise.resolve({ dataDir: "fuji" }); + +export const writeConfig = config => { + const contents = JSON.stringify(config); + tauriOn + ? TauriFS.writeFile({ file: "config.json", contents }, appDirOption) + : Promise.resolve(localStorage.setItem("fuji", contents)); +};