Skip to content

Commit

Permalink
Merge f761c29 into 4838eb1
Browse files Browse the repository at this point in the history
  • Loading branch information
olivereisenhut committed May 1, 2018
2 parents 4838eb1 + f761c29 commit 5b52c67
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 23 deletions.
50 changes: 50 additions & 0 deletions src/tests/integration/config-loader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as fs from "fs";
import { ConfigLoader } from "../../ts/config-loader";
import { WebSearch } from "../../ts/web-search";

const defaultConfigMock = {
autoStartApp: true,
maxSearchResultCount: 10,
rescanInterval: 30,
searchOperatinSystemSettings: false,
showHiddenFiles: true,
version: "Some otherrr stuff",
webSearches: new Array<WebSearch>(),
windowWith: 860,
};

const buggyConfig = {
nothing: null,
};

const fakeFilePath = "./fakeFile";
const buggyFilePath = "./buggy";

describe(ConfigLoader.name, (): void => {
it("loads config form file", () => {
const configLoader = new ConfigLoader(defaultConfigMock, "./fakeFile");
const config = configLoader.loadConigFromConfigFile();
expect(config.version).toBe(defaultConfigMock.version);
expect(config.webSearches.length).toBe(0);
});

it("loads default config when error is thrown", () => {
fs.writeFileSync("./buggy", buggyConfig);
const configLoader = new ConfigLoader(defaultConfigMock, "./buggy");
const config = configLoader.loadConigFromConfigFile();
expect(config.version).toBe(defaultConfigMock.version);
expect(config.webSearches.length).toBe(0);
});

it("loads default path when not passed", () => {
const configLoader = new ConfigLoader(defaultConfigMock);
const config = configLoader.loadConigFromConfigFile();
expect(config.version).not.toBe(defaultConfigMock.version);
expect(config.webSearches.length).toBeGreaterThan(0);
});

afterAll(() => {
fs.unlink(fakeFilePath);
fs.unlink(buggyFilePath);
});
});
37 changes: 37 additions & 0 deletions src/ts/config-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { ConfigOptions } from "./config";

export class ConfigLoader {

private configFilePath = path.join(os.homedir(), "ueli.config.json");
private defaultConfig: ConfigOptions;
public constructor(defaultConfig: ConfigOptions, configFilePath?: string) {
if (configFilePath !== undefined) {
this.configFilePath = configFilePath;
}
this.defaultConfig = defaultConfig;
}

public loadConigFromConfigFile(): ConfigOptions {
try {
const fileContent = fs.readFileSync(this.configFilePath, "utf-8");
const parsed = JSON.parse(fileContent) as ConfigOptions;
if (parsed.version === undefined || !parsed.version.startsWith("3")) {
this.writeDefaultConfigToConfigFile();
return this.defaultConfig;
} else {
return parsed;
}
} catch (err) {
this.writeDefaultConfigToConfigFile();
return this.defaultConfig;
}
}

private writeDefaultConfigToConfigFile(): void {
const stringifiedConfig = JSON.stringify(this.defaultConfig);
fs.writeFileSync(this.configFilePath, stringifiedConfig, "utf-8");
}
}
39 changes: 16 additions & 23 deletions src/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ import { WebSearchExecutor } from "./executors/web-search-executor";
import { WebUrlExecutor } from "./executors/web-url-executor";
import { InputValidationService } from "./input-validation-service";
import { WebSearch } from "./web-search";
import { ConfigLoader } from "./config-loader";

// tslint:disable-next-line:no-var-requires because there is no other way to get package.json, or is there?
const pkg = require("../../package.json");
const productName = pkg.productName;
const version = pkg.version;
const appName = pkg.productName;
const defaultConfig = {
export interface ConfigOptions {
autoStartApp: boolean;
maxSearchResultCount: number;
rescanInterval: number;
searchOperatinSystemSettings: boolean;
showHiddenFiles: boolean;
version: string;
webSearches: WebSearch[];
windowWith: number;
searchWindows10Apps?: boolean;
}

const defaultConfig: ConfigOptions = {
autoStartApp: true,
maxSearchResultCount: 8,
rescanInterval: 30,
Expand Down Expand Up @@ -82,28 +95,8 @@ const defaultConfig = {
};

const configFilePath = path.join(os.homedir(), "ueli.config.json");
const config = loadConigFromConfigFile();

function loadConigFromConfigFile(): any {
try {
const fileContent = fs.readFileSync(configFilePath, "utf-8");
const parsed = JSON.parse(fileContent);
if (parsed.version === undefined || !parsed.version.startsWith("3")) {
writeDefaultConfigToConfigFile();
return defaultConfig;
} else {
return parsed;
}
} catch (err) {
writeDefaultConfigToConfigFile();
return defaultConfig;
}
}

function writeDefaultConfigToConfigFile(): void {
const stringifiedConfig = JSON.stringify(defaultConfig);
fs.writeFileSync(configFilePath, stringifiedConfig, "utf-8");
}
const configLoader = new ConfigLoader(defaultConfig, configFilePath);
const config = configLoader.loadConigFromConfigFile();

export class Config {
public static readonly productName = productName;
Expand Down

0 comments on commit 5b52c67

Please sign in to comment.