1
0
mirror of https://github.com/mgerb/ps-launcher synced 2026-01-10 18:42:48 +00:00

lots of progress - basic functionality work - bundling working

This commit is contained in:
2017-10-18 23:37:05 -05:00
parent c4e028d102
commit 96f9c4ca5d
27 changed files with 1178 additions and 174 deletions

View File

@@ -1,10 +1,14 @@
import { remote } from 'electron';
import * as _ from 'lodash';
import { action, computed, observable, runInAction } from 'mobx';
import fs from 'fs';
import { persistentStateSeed } from './persistent-state-seed';
export interface ExpansionType {
name: string;
servers: ServerType[];
directory?: string;
selectedServerIndex?: number;
}
export interface ServerType {
@@ -14,47 +18,39 @@ export interface ServerType {
}
export class AppState {
private appPath: string;
private persistentFilePath: string;
constructor() {
this.appPath = remote.app.getPath('appData') + '/' + remote.app.getName();
this.persistentFilePath = this.appPath + '/state.json';
this.bootstrap();
}
@action
private bootstrap(): void {
fs.readFile('./servers.json', (err, data) => {
if (!err) {
runInAction(() => {
this.expansions = JSON.parse(data.toString()) as any;
});
}
});
}
@observable
public expansions: { [key: string]: ExpansionType } = {
vanilla: {
name: 'Vanilla',
servers: [],
directory: '',
},
bc: {
name: 'Burning Crusade',
servers: [],
directory: '',
},
wotlk: {
name: 'Wrath of the Lich King',
servers: [],
directory: '',
},
};
@observable
public selectedExpKey: string = 'vanilla';
@observable public expansions: { [key: string]: ExpansionType };
@observable public isBootstrapped: boolean = false;
@observable public selectedExpKey: string = 'vanilla';
@computed
public get selectedExpansion(): ExpansionType {
return this.expansions[this.selectedExpKey];
return _.get(this.expansions, `[${this.selectedExpKey}]`);
}
@computed
public get selectedServer(): ServerType {
if (this.selectedExpansion.servers.length < 1) {
return null;
}
const index: number = this.selectedExpansion.selectedServerIndex;
return this.selectedExpansion.servers[index];
}
@action
public setSelectedServerIndex(index: number): void {
this.selectedExpansion.selectedServerIndex = index;
this.updateFile(this.expansions);
}
@action
@@ -64,18 +60,47 @@ export class AppState {
@action
public setDirectory(dir: string): void {
this.expansions[this.selectedExpKey].directory = dir;
this.selectedExpansion.directory = dir;
this.updateFile(this.expansions);
}
// bootstrap application
// creates directory and state.json in appData
@action
private bootstrap(): void {
if (!fs.statSync(this.appPath).isDirectory()) {
fs.mkdirSync(this.appPath);
}
fs.stat(this.persistentFilePath, err => {
// create file if not exists
if (err) {
runInAction(() => {
this.expansions = persistentStateSeed();
this.isBootstrapped = true;
});
this.updateFile(this.expansions);
} else {
fs.readFile(this.persistentFilePath, (err, data) => {
if (!err) {
runInAction(() => {
this.expansions = JSON.parse(data.toString()) as any;
this.isBootstrapped = true;
});
}
});
}
});
}
private updateFile(exp: any): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile('./servers.json', JSON.stringify(exp, null, 2), {}, (err) => {
fs.writeFile(this.persistentFilePath, JSON.stringify(exp, null, 2), {}, err => {
err ? reject(err) : resolve();
});
});
}
}
export default new AppState();