2023-03-06 07:21:29 -05:00
|
|
|
import { fs } from "@tauri-apps/api"
|
|
|
|
|
import { appConfigDir, join } from '@tauri-apps/api/path'
|
2023-03-06 08:32:00 -05:00
|
|
|
import { State } from '../../../src/state';
|
2023-03-06 07:21:29 -05:00
|
|
|
|
2023-03-06 08:32:00 -05:00
|
|
|
const configFile = appConfigDir().then(c => join(c, 'config.json'));
|
|
|
|
|
const profileDir = appConfigDir().then(c => join(c, 'profile'));
|
2023-03-06 07:21:29 -05:00
|
|
|
|
2023-03-06 08:32:00 -05:00
|
|
|
export type Profile = {
|
|
|
|
|
state: State,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Config = {
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 14:36:06 -05:00
|
|
|
// TODO: import from state.ts.
|
|
|
|
|
const DEFAULT_STATE: State = {
|
|
|
|
|
cacheDir: "./cache",
|
|
|
|
|
fGen: "https://api.imink.app/f",
|
|
|
|
|
fileExportPath: "./export",
|
|
|
|
|
monitorInterval: 500,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultProfile: Profile = {
|
|
|
|
|
state: DEFAULT_STATE,
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-06 08:32:00 -05:00
|
|
|
const defaultConfig: Config = {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function initFiles() {
|
|
|
|
|
await fs.createDir(await profileDir, { recursive: true });
|
|
|
|
|
await configFile;
|
|
|
|
|
}
|
|
|
|
|
initFiles().catch(console.error);
|
|
|
|
|
|
|
|
|
|
export async function getConfig(): Promise<Config> {
|
|
|
|
|
try {
|
2023-03-06 14:36:06 -05:00
|
|
|
const config = await fs.readTextFile(await configFile);
|
2023-03-06 08:32:00 -05:00
|
|
|
return JSON.parse(config);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return defaultConfig;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-06 07:21:29 -05:00
|
|
|
|
2023-03-06 08:32:00 -05:00
|
|
|
export async function setConfig(config: Config) {
|
|
|
|
|
await fs.writeTextFile(await configFile, JSON.stringify(config));
|
2023-03-06 07:21:29 -05:00
|
|
|
}
|
2023-03-06 14:36:06 -05:00
|
|
|
|
|
|
|
|
export async function getProfile(index: number): Promise<Profile> {
|
|
|
|
|
try {
|
|
|
|
|
const profile = await fs.readTextFile(await profileDir.then(c => join(c, `${index}.json`)));
|
|
|
|
|
return JSON.parse(profile);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return defaultProfile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function setProfile(index: number, profile: Profile) {
|
|
|
|
|
await fs.writeTextFile(await profileDir.then(c => join(c, `${index}.json`)), JSON.stringify(profile));
|
|
|
|
|
}
|