Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
feat(config): create from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dnoishi committed Sep 29, 2022
1 parent fece3dd commit 1dfbb39
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async fn main() -> Result<()> {
utils::open_folder,
utils::get_disk_stats,
utils::get_this_binary,
utils::create_file,
],
#[cfg(target_os = "windows")]
tauri::generate_handler![
Expand Down
16 changes: 16 additions & 0 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::Serialize;
use std::path::PathBuf;
use std::process::Command;
use std::fs::File;
use std::io::prelude::*;
use std::os::unix::fs::PermissionsExt;
use tauri::{api, Env};
use tracing::{debug, error, info};

Expand Down Expand Up @@ -77,3 +80,16 @@ pub(crate) fn custom_log_dir(id: &str) -> PathBuf {

path.expect("log path generation should succeed")
}

// TODO: current implementation works only on unix like systems
#[tauri::command]
pub(crate) fn create_file(path: &str, content: String) {
let mut file = File::create(path).expect("can't create file");
file.write_all(content.as_bytes())
.expect("couldn't write file");
let metadata = file.metadata().expect("can't get metadata");
let mut permissions = metadata.permissions();

permissions.set_mode(0o644); // Read/write for owner and read for others.
assert_eq!(permissions.mode(), 0o644);
}
4 changes: 2 additions & 2 deletions src/boot/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createApi } from '../lib/util';
import AutoLauncher, { MacOSAutoLauncher, LinuxAutoLauncher, WindowsAutoLauncher } from '../lib/autoLauncher';
import Config from '../lib/config';
import { useStore } from '../stores/store';
import { appName, errorLogger, infoLogger } from '../lib/util';
import { appName, errorLogger, infoLogger, writeFile } from '../lib/util';
import * as native from '../lib/native';
import { initUpdater } from '../lib/updater';

Expand All @@ -26,7 +26,7 @@ declare module '@vue/runtime-core' {
export default boot(async ({ app }) => {
// create Config instance and initialize it
const configDir = await tauri.path.configDir();
const config = new Config({ fs: tauri.fs, appName, configDir, errorLogger });
const config = new Config({ fs: tauri.fs, appName, configDir, errorLogger, writeFile });
await config.init();

// set node name from config (empty string is default value)
Expand Down
12 changes: 6 additions & 6 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ interface FilesParams {
configDir: string;
appName: string;
errorLogger: (error: unknown) => Promise<void>;
writeFile: (configFullPath: string, config: IConfig) => Promise<void>;
}

export interface Plot {
location: string
sizeGB: number
}

interface IConfig {
export interface IConfig {
plot: Plot
rewardAddress: string,
launchOnBoot: boolean,
Expand Down Expand Up @@ -46,12 +47,14 @@ class Config {
private configPath: string;
private configFullPath: string;
private errorLogger: (error: unknown) => Promise<void>;
private writeFile: (configFullPath: string, config: IConfig) => Promise<void>;

constructor({ fs, configDir, appName, errorLogger }: FilesParams) {
constructor({ fs, configDir, appName, errorLogger, writeFile }: FilesParams) {
this.fs = fs;
this.configPath = `${configDir}${appName}`;
this.configFullPath = `${this.configPath}/${appName}.cfg`;
this.errorLogger = errorLogger;
this.writeFile = writeFile;
}

/**
Expand Down Expand Up @@ -115,10 +118,7 @@ class Config {
* @param {IConfig} - config object to store as config file
*/
private async write(config: IConfig): Promise<void> {
await this.fs.writeFile({
path: this.configFullPath,
contents: JSON.stringify(config, null, 2)
})
await this.writeFile(this.configFullPath,config)
.catch((error) => this.errorLogger(error));
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export {
errorLogger,
infoLogger,
resetAndClear,
writeFile,
} from './tauri';
12 changes: 11 additions & 1 deletion src/lib/util/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { invoke } from '@tauri-apps/api/tauri';
import { LocalStorage } from 'quasar';

import { IPlotDir } from '../plotDir';
import Config from '../config';
import Config, { IConfig } from '../config';
import { getErrorMessage } from './';

export const appName: string = process.env.APP_NAME || 'subspace-desktop';
Expand Down Expand Up @@ -54,3 +54,13 @@ export async function resetAndClear({
errorLogger(error);
}
}

export async function writeFile(
configFullPath: string,
config: IConfig
): Promise<void> {
await invoke('create_file', {
path: configFullPath,
content: JSON.stringify(config, null, 2)
}).catch((error) => errorLogger(error));
}
1 change: 1 addition & 0 deletions src/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const params = {
appName,
configDir,
errorLogger: jest.fn(),
writeFile: jest.fn(),
};

// TODO:
Expand Down

0 comments on commit 1dfbb39

Please sign in to comment.