Skip to content

Commit 4844494

Browse files
Merge pull request #67 from MichaelCurrin/refactor-extension.ts
Refactor extension.ts
2 parents eef2ea0 + 684b37f commit 4844494

1 file changed

Lines changed: 48 additions & 26 deletions

File tree

src/extension.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,40 @@ import { API } from "./api/git";
99
import { makeAndFillCommitMsg } from "./autofill";
1010
import { getGitExtension } from "./gitExtension";
1111

12+
function _validateFoundRepos(git: API) {
13+
let msg = "";
14+
15+
if (!git) {
16+
msg = "Unable to load Git Extension";
17+
} else if (git.repositories.length === 0) {
18+
msg =
19+
"No repos found. Please open a repo or run `git init` then try again.";
20+
}
21+
22+
if (msg) {
23+
vscode.window.showErrorMessage(msg);
24+
25+
throw new Error(msg);
26+
}
27+
}
28+
1229
/**
1330
* Run autofill against one of multiples in the workspace.
1431
*
1532
* This is a rare flow.
33+
*
34+
* @param sourceControl Of type `vscode.SourceControl` with public `.rootUri`
35+
* and private `.rootUri`.
1636
*/
17-
async function _handleRepos(git: API, uri: any) {
37+
async function _handleRepos(git: API, sourceControl: any) {
1838
// FIXME: Unfortunately this seems to only pick up the first repo and not find
1939
// second, etc.
2040
const selectedRepository = git.repositories.find(repository => {
21-
return repository.rootUri.path === uri._rootUri.path;
41+
const uri = sourceControl._rootUri;
42+
if (!uri) {
43+
console.warn("_rootUri not set");
44+
}
45+
return repository.rootUri.path === uri.path;
2246
});
2347

2448
if (selectedRepository) {
@@ -37,38 +61,36 @@ async function _handleRepo(git: API) {
3761
}
3862

3963
/**
40-
* Set up the autofill command to run when triggered.
64+
* Choose the relevant repo and apply autofill logic on files there.
4165
*/
42-
export function activate(context: vscode.ExtensionContext) {
43-
const disposable = vscode.commands.registerCommand(
44-
"commitMsg.autofill",
45-
async (uri?) => {
46-
const git = getGitExtension();
66+
async function _chooseRepoForAutofill(uri?: vscode.Uri) {
67+
const git = getGitExtension()!;
68+
_validateFoundRepos(git);
4769

48-
if (!git) {
49-
vscode.window.showErrorMessage("Unable to load Git Extension");
50-
return;
51-
}
70+
vscode.commands.executeCommand("workbench.view.scm");
5271

53-
if (git.repositories.length === 0) {
54-
vscode.window.showErrorMessage(
55-
"No repos found. Please open a repo or run git init then try this extension again."
56-
);
57-
return;
58-
}
59-
60-
vscode.commands.executeCommand("workbench.view.scm");
72+
if (uri) {
73+
_handleRepos(git, uri);
74+
} else {
75+
_handleRepo(git);
76+
}
77+
}
6178

62-
if (uri) {
63-
_handleRepos(git, uri);
64-
} else {
65-
_handleRepo(git);
66-
}
67-
}
79+
/**
80+
* Set up the extension activation.
81+
*
82+
* The autofill command as configured in `package.json` will be triggered
83+
* and run the autofill logic for a repo.
84+
*/
85+
export function activate(context: vscode.ExtensionContext) {
86+
const disposable = vscode.commands.registerCommand(
87+
"commitMsg.autofill",
88+
_chooseRepoForAutofill
6889
);
6990

7091
context.subscriptions.push(disposable);
7192
}
7293

94+
// prettier-ignore
7395
// eslint-disable-next-line @typescript-eslint/no-empty-function
7496
export function deactivate() { }

0 commit comments

Comments
 (0)