Merge pull request #566 from silentrald/feat/560

[feat-560] ask user for installation folder/path
This commit is contained in:
MathieuG-P
2024-09-11 12:45:37 +02:00
committed by GitHub
24 changed files with 400 additions and 65 deletions
+21 -3
View File
@@ -1,5 +1,7 @@
import ElectronStore from "electron-store";
import fs from "fs-extra";
import { InstallationLocationService } from "./installation-location.service";
import { CustomError } from "shared/models/exceptions/custom-error.class";
export class ConfigurationService {
private static instance: ConfigurationService;
@@ -13,17 +15,23 @@ export class ConfigurationService {
private readonly locations: InstallationLocationService;
private contentPath: string;
private store: ElectronStore;
private constructor() {
this.locations = InstallationLocationService.getInstance();
this.initStore();
this.initStore(false);
this.locations.onInstallLocationUpdate(() => { this.initStore() });
this.locations.onInstallLocationUpdate(() => this.initStore(true));
}
private async initStore() {
private async initStore(createFolder: boolean) {
const contentPath = this.locations.installationDirectory();
if (!createFolder && !fs.pathExistsSync(contentPath)) {
return;
}
this.contentPath = contentPath;
this.store = new ElectronStore({
cwd: contentPath,
name: "config",
@@ -32,15 +40,25 @@ export class ConfigurationService {
});
}
private checkStore(): void {
// Can be null if config.cfg does not exist or corrupted
if (!this.store) {
throw CustomError.fromError(new Error(`Can't read config.cfg on ${this.contentPath}`));
}
}
public set(key: string, value: unknown): void {
this.checkStore();
this.store.set(key, value);
}
public get<T>(key: string): T {
this.checkStore();
return this.store.get(key) as T;
}
public delete(key: string): void {
this.checkStore();
this.store.delete(key);
}
}
@@ -40,18 +40,22 @@ export class InstallationLocationService {
this.updateListeners.forEach(listener => listener());
}
public async setInstallationDirectory(newDir: string): Promise<string> {
/**
* @param move - if true, move the old installation path to the path param
*/
public async setInstallationDirectory(newDir: string, move: boolean): Promise<string> {
newDir = path.basename(newDir) === this.INSTALLATION_FOLDER ? path.join(newDir, "..") : newDir;
const oldDir = this.installationDirectory();
await ensureFolderExist(oldDir);
await copyDirectoryWithJunctions(oldDir, path.join(newDir, this.INSTALLATION_FOLDER), { overwrite: true });
if (move) {
const oldDir = this.installationDirectory();
await ensureFolderExist(oldDir);
await copyDirectoryWithJunctions(oldDir, path.join(newDir, this.INSTALLATION_FOLDER), { overwrite: true });
deleteFolder(oldDir);
}
this._installationDirectory = newDir;
this.staticConfig.set(this.STORE_INSTALLATION_PATH_KEY, newDir);
deleteFolder(oldDir);
return this.installationDirectory();
}
@@ -59,6 +63,12 @@ export class InstallationLocationService {
this.updateListeners.add(fn);
}
public defaultInstallationDirectory(): string {
const { result: oldPath } = tryit(() => path.join(app.getPath("documents"), this.INSTALLATION_FOLDER));
const installationDirectory = (oldPath && pathExistsSync(oldPath)) ? app.getPath("documents") : app.getPath("home");
return path.join(installationDirectory, this.INSTALLATION_FOLDER);
}
public installationDirectory(): string {
const installParentPath = () => {