Skip to content

Commit

Permalink
v0.1.2: Save dataDir config to ~/.config/fuji/config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons committed May 15, 2020
1 parent ef3c528 commit a342250
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuji",
"version": "0.1.1",
"version": "0.1.2",
"private": true,
"repository": "https://github.com/nonbili/fuji",
"author": "Nonbili",
Expand Down
7 changes: 4 additions & 3 deletions src/App/Eval.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/FFI/Tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 3 additions & 5 deletions src/FFI/Tauri.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions src/js/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};

0 comments on commit a342250

Please sign in to comment.