1
1
/**
2
2
* Extension module.
3
3
*
4
- * This sets up the extension's command entry point and applies the prepare commit message module to
5
- * a target branch.
4
+ * This sets up the VS Code extension's command entry- point and applies logic in
5
+ * the prepareCommitMsg module to a target branch.
6
6
*/
7
7
import * as vscode from "vscode" ;
8
8
import { API } from "./api/git" ;
9
9
import { makeAndFillCommitMsg } from "./autofill" ;
10
10
import { getGitExtension } from "./gitExtension" ;
11
11
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
+
12
29
/**
13
- * Flow for multiple repos in workspace and selecting just one. This is a rare flow.
30
+ * Run autofill against one of multiples in the workspace.
31
+ *
32
+ * This is a rare flow.
33
+ *
34
+ * @param sourceControl Of type `vscode.SourceControl` with public `.rootUri`
35
+ * and private `.rootUri`.
14
36
*/
15
- async function _handleRepos ( git : API , uri : any ) {
16
- // FIXME: Unfortunately this seems to only pick up the first repo and not find second etc.
37
+ async function _handleRepos ( git : API , sourceControl : any ) {
38
+ // FIXME: Unfortunately this seems to only pick up the first repo and not find
39
+ // second, etc.
17
40
const selectedRepository = git . repositories . find ( repository => {
18
- 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 ;
19
46
} ) ;
20
47
21
48
if ( selectedRepository ) {
@@ -26,28 +53,19 @@ async function _handleRepos(git: API, uri: any) {
26
53
}
27
54
28
55
/**
29
- * Flow for a single or zero repos in the workspace.
56
+ * Run autofill flow for a single repo in the workspace.
30
57
*/
31
58
async function _handleRepo ( git : API ) {
32
59
const targetRepo = git . repositories [ 0 ] ;
33
-
34
60
await makeAndFillCommitMsg ( targetRepo ) ;
35
61
}
36
62
37
- async function _autofill ( uri ?: string ) {
38
- const git = getGitExtension ( ) ;
39
-
40
- if ( ! git ) {
41
- vscode . window . showErrorMessage ( "Unable to load Git Extension" ) ;
42
- return ;
43
- }
44
-
45
- if ( git . repositories . length === 0 ) {
46
- vscode . window . showErrorMessage (
47
- "No repos found. Please open a repo or run git init then try this extension again."
48
- ) ;
49
- return ;
50
- }
63
+ /**
64
+ * Choose the relevant repo and apply autofill logic on files there.
65
+ */
66
+ async function _chooseRepoForAutofill ( uri ?: vscode . Uri ) {
67
+ const git = getGitExtension ( ) ! ;
68
+ _validateFoundRepos ( git ) ;
51
69
52
70
vscode . commands . executeCommand ( "workbench.view.scm" ) ;
53
71
@@ -59,16 +77,20 @@ async function _autofill(uri?: string) {
59
77
}
60
78
61
79
/**
62
- * Set up this extension's autofill command to run when triggered.
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.
63
84
*/
64
85
export function activate ( context : vscode . ExtensionContext ) {
65
86
const disposable = vscode . commands . registerCommand (
66
87
"commitMsg.autofill" ,
67
- _autofill
88
+ _chooseRepoForAutofill
68
89
) ;
69
90
70
91
context . subscriptions . push ( disposable ) ;
71
92
}
72
93
94
+ // prettier-ignore
73
95
// eslint-disable-next-line @typescript-eslint/no-empty-function
74
- export function deactivate ( ) { }
96
+ export function deactivate ( ) { }
0 commit comments