Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add installer config state #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"react-router-dom": "^6.2.2",
"shebang-loader": "^0.0.1",
"sudo-prompt": "^9.2.1",
"tmp-promise": "^3.0.3"
"tmp-promise": "^3.0.3",
"yaml": "^2.1.1"
},
"build": {
"appId": "gg.wagyu.installer",
Expand Down
74 changes: 74 additions & 0 deletions src/electron/InstallerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { open, mkdir, FileHandle } from 'fs/promises';

import path from 'path';
import os from 'os';
import { parse, Document, parseDocument } from 'yaml';

import { doesFileExist } from './BashUtils';

const configDirectoryPath = path.join(os.homedir(), '.config', 'wagyu-installer');
const configFilePath = path.join(configDirectoryPath, 'wagyu-installer.yml')

interface Configuration {
rootDirectory: string;
}

async function getConfigFileContent(): Promise<string | undefined> {
if (!await doesFileExist(configFilePath)) {
return undefined;
}

// Open config file and read the content.
let configFile: FileHandle | undefined = undefined;
let configFileContent: string | undefined = undefined;
try {
configFile = await open(configFilePath, 'r');
configFileContent = await configFile.readFile({ encoding: 'utf8' });
} finally {
await configFile?.close();
}

return configFileContent;
}

export async function initInstallerConfig() {
await mkdir(configDirectoryPath, { recursive: true });
}

export async function setInstallPath(installPath: string) {
const configFileContent = await getConfigFileContent();

let configDocument: Document | undefined = undefined;
if (configFileContent === undefined) {
configDocument = new Document({
rootDirectory: installPath
});
} else {
const yamlContent = parseDocument(configFileContent as string);
yamlContent.set('rootDirectory', installPath);
configDocument = yamlContent;
}

if (configDocument !== undefined) {
const newFileContent = configDocument?.toString();

let configFile: FileHandle | undefined = undefined;
try {
configFile = await open(configFilePath, 'w');
await configFile.write(newFileContent);
} finally {
await configFile?.close();
}
}
}

export async function getInstallPath(): Promise<string | undefined> {
const configFileContent = await getConfigFileContent();
if (configFileContent === undefined) {
return undefined;
}

const yamlContent = parse(configFileContent as string) as Configuration;

return yamlContent.rootDirectory;
}
7 changes: 7 additions & 0 deletions src/electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { doesDirectoryExist, findFirstFile } from './BashUtils';

import { EthDockerInstaller } from './EthDockerInstaller';
import { InstallDetails, OutputLogs } from "./IMultiClientInstaller";
import { initInstallerConfig, getInstallPath, setInstallPath } from "./InstallerConfig";

import { Writable } from 'stream';

Expand Down Expand Up @@ -71,4 +72,10 @@ contextBridge.exposeInMainWorld('ethDocker', {
'postInstall': ethDockerPostInstall,
'startNodes': ethDockerStartNodes,
'stopNodes': ethDockerStopNodes
});

contextBridge.exposeInMainWorld('installerConfig', {
'initInstallerConfig': initInstallerConfig,
'getInstallPath': getInstallPath,
'setInstallPath': setInstallPath
});
9 changes: 8 additions & 1 deletion src/electron/renderer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ export interface IEthDockerAPI {
stopNodes: (network: Network) => Promise<boolean>,
}

export interface IInstallerConfigAPI {
initInstallerConfig: () => Promise<void>,
setInstallPath: (installPath: string) => Promise<void>,
getInstallPath: () => Promise<string | undefined>
}

declare global {
interface Window {
electronAPI: IElectronAPI,
bashUtils: IBashUtilsAPI,
ethDocker: IEthDockerAPI
ethDocker: IEthDockerAPI,
installerConfig: IInstallerConfigAPI
}
}
8 changes: 5 additions & 3 deletions src/react/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const Container = styled.main`
const App: FC = (): ReactElement => {
// const [network, setNetwork] = useState<Network>(Network.PRATER);
const [installationDetails, setInstallationDetails] = useState<InstallDetails>({
consensusClient: ConsensusClient.PRYSM,
executionClient: ExecutionClient.GETH,
network: Network.PRATER
consensusClient: ConsensusClient.PRYSM,
executionClient: ExecutionClient.GETH,
network: Network.PRATER
})

window.installerConfig.initInstallerConfig();

return (
<ThemeProvider theme={theme}>
<CssBaseline />
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5172,6 +5172,7 @@ __metadata:
typescript: ^4.3.5
webpack: ^5.64.3
webpack-cli: ^4.9.1
yaml: ^2.1.1
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -5398,6 +5399,13 @@ __metadata:
languageName: node
linkType: hard

"yaml@npm:^2.1.1":
version: 2.1.1
resolution: "yaml@npm:2.1.1"
checksum: f48bb209918aa57cfaf78ef6448d1a1f8187f45c746f933268b7023dc59e5456004611879126c9bb5ea55b0a2b1c2b392dfde436931ece0c703a3d754562bb96
languageName: node
linkType: hard

"yargs-parser@npm:^21.0.0":
version: 21.0.1
resolution: "yargs-parser@npm:21.0.1"
Expand Down