-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
103 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters