-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Configuration to a separate class
- Loading branch information
Showing
9 changed files
with
73 additions
and
53 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
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
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
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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# Changelog | ||
### v1.0.1 | ||
|
||
- Refactor Configuration to a separate class | ||
|
||
### v1.0.0 | ||
|
||
- Add example to readme | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 path from 'path'; | ||
import * as vscode from "vscode"; | ||
|
||
export default class Configuration { | ||
readonly namespace: string; | ||
readonly logger?: (msg: string) => void; | ||
|
||
constructor(namespace: string, logger: (msg: string) => void) { | ||
this.namespace = namespace; | ||
this.logger = logger; | ||
} | ||
|
||
getDirectoryPath() { | ||
let directory = vscode.workspace.getConfiguration(this.namespace).get<string>("directory") || process.env["XDG_CONFIG_HOME"] || path.join(process.env["HOME"]!, ".config"); | ||
if (directory.endsWith("/")) { | ||
return directory.slice(directory.length - 1); | ||
} | ||
return directory; | ||
} | ||
|
||
getFiles() { | ||
return vscode.workspace.getConfiguration(this.namespace).get<{ [key: string]: string; }>("files", {}); | ||
} | ||
|
||
async setFiles(files: { [key: string]: string; }) { | ||
try { | ||
await vscode.workspace.getConfiguration(this.namespace).update("files", files, vscode.ConfigurationTarget.Global); | ||
} catch (err) { | ||
this.logger?.(`${new Date().toLocaleString()}: failed to update files: ${err}`); | ||
vscode.window.showErrorMessage("Unable to update dotfiles.files: " + err); | ||
} | ||
} | ||
|
||
shouldAutoUpdate() { | ||
return vscode.workspace.getConfiguration(this.namespace).get<boolean>("autoUpdate", false); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import * as assert from 'assert'; | ||
import * as assert from "assert"; | ||
import * as vscode from "vscode"; | ||
import Configuration from "../Configuration"; | ||
|
||
// You can import and use all API from the 'vscode' module | ||
// as well as import your extension to test it | ||
import * as vscode from 'vscode'; | ||
// import * as myExtension from '../../extension'; | ||
|
||
suite('Extension Test Suite', () => { | ||
vscode.window.showInformationMessage('Start all tests.'); | ||
|
||
test('Sample test', () => { | ||
assert.strictEqual(-1, [1, 2, 3].indexOf(5)); | ||
assert.strictEqual(-1, [1, 2, 3].indexOf(0)); | ||
suite("Extension", () => { | ||
suiteSetup(() => { | ||
vscode.extensions.getExtension("grg.dotfiles")!.activate(); | ||
}); | ||
test("setFiles is idempotent", () => { | ||
const testNamespace = "dotfiles"; | ||
const configuration = new Configuration(testNamespace, () => { }); | ||
const files = configuration.getFiles(); | ||
configuration.setFiles(files); | ||
configuration.setFiles(files); | ||
assert.deepStrictEqual(configuration.getFiles(), files); | ||
}); | ||
}); |