1
+ import { Action } from './proxy/actions' ;
2
+
1
3
const lpModule = import ( 'load-plugin' ) ;
2
4
/* eslint-disable @typescript-eslint/no-unused-expressions */
3
5
( 'use strict' ) ;
@@ -8,7 +10,7 @@ const lpModule = import('load-plugin');
8
10
* @param {string } propertyName - The property name to check for. Default is 'isGitProxyPlugin'.
9
11
* @return {boolean } - True if the object or any of its prototypes has the 'isGitProxyPlugin' property set to true, false otherwise.
10
12
*/
11
- function isCompatiblePlugin ( obj , propertyName = 'isGitProxyPlugin' ) {
13
+ function isCompatiblePlugin ( obj : any , propertyName : string = 'isGitProxyPlugin' ) : boolean {
12
14
// loop through the prototype chain to check if the object is a ProxyPlugin
13
15
// valid plugin objects will have the appropriate property set to true
14
16
// if the prototype chain is exhausted, return false
@@ -23,36 +25,24 @@ function isCompatiblePlugin(obj, propertyName = 'isGitProxyPlugin') {
23
25
return false ;
24
26
}
25
27
26
- /**
27
- * @typedef PluginTypeResult
28
- * @property {PushActionPlugin[] } pushAction - List of push action plugins
29
- * @property {PullActionPlugin[] } pullAction - List of pull action plugins
30
- */
28
+ interface PluginTypeResult {
29
+ pushAction : PushActionPlugin [ ] ;
30
+ pullAction : PullActionPlugin [ ] ;
31
+ }
31
32
32
33
/**
33
34
* Registers and loads plugins used by git-proxy
34
35
*/
35
36
class PluginLoader {
36
- constructor ( targets ) {
37
- /**
38
- * List of Node module specifiers to load as plugins. It can be a relative path, an
39
- * absolute path, or a module name (which can include scoped packages like '@bar/baz').
40
- * @type {string[] }
41
- * @public
42
- */
37
+ targets : string [ ] ;
38
+ pushPlugins : PushActionPlugin [ ] ;
39
+ pullPlugins : PullActionPlugin [ ] ;
40
+
41
+ constructor ( targets : string [ ] ) {
43
42
this . targets = targets ;
44
- /**
45
- * List of loaded PushActionPlugin objects.
46
- * @type {PushActionPlugin[] }
47
- * @public
48
- */
49
43
this . pushPlugins = [ ] ;
50
- /**
51
- * List of loaded PullActionPlugin objects.
52
- * @type {PullActionPlugin[] }
53
- * @public
54
- */
55
44
this . pullPlugins = [ ] ;
45
+
56
46
if ( this . targets . length === 0 ) {
57
47
console . log ( 'No plugins configured' ) ; // TODO: log.debug()
58
48
}
@@ -63,7 +53,7 @@ class PluginLoader {
63
53
* can be used to retrieve plugins.
64
54
* @return {Promise<void> } A Promise that resolves when all plugins have been loaded.
65
55
*/
66
- async load ( ) {
56
+ async load ( ) : Promise < void > {
67
57
try {
68
58
const modulePromises = this . targets . map ( target =>
69
59
this . _loadPluginModule ( target ) . catch ( error => {
@@ -74,7 +64,7 @@ class PluginLoader {
74
64
75
65
const moduleResults = await Promise . allSettled ( modulePromises ) ;
76
66
const loadedModules = moduleResults
77
- . filter ( result => result . status === 'fulfilled' && result . value !== null )
67
+ . filter ( ( result ) : result is PromiseFulfilledResult < any > => result . status === 'fulfilled' && result . value !== null )
78
68
. map ( result => result . value ) ;
79
69
80
70
console . log ( `Found ${ loadedModules . length } plugin modules` ) ; // TODO: log.debug()
@@ -91,7 +81,7 @@ class PluginLoader {
91
81
* @type {PluginTypeResult[] } List of resolved PluginTypeResult objects
92
82
*/
93
83
const pluginTypeResults = settledPluginTypeResults
94
- . filter ( result => result . status === 'fulfilled' && result . value !== null )
84
+ . filter ( ( result ) : result is PromiseFulfilledResult < any > => result . status === 'fulfilled' && result . value !== null )
95
85
. map ( result => result . value ) ;
96
86
97
87
for ( const result of pluginTypeResults ) {
@@ -113,7 +103,7 @@ class PluginLoader {
113
103
* @param {string } target The module specifier to load
114
104
* @return {Promise<Module> } A resolved & loaded Module
115
105
*/
116
- async _loadPluginModule ( target ) {
106
+ private async _loadPluginModule ( target : string ) : Promise < any > {
117
107
const lp = await lpModule ;
118
108
const resolvedModuleFile = await lp . resolvePlugin ( target ) ;
119
109
return await lp . loadPlugin ( resolvedModuleFile ) ;
@@ -124,13 +114,13 @@ class PluginLoader {
124
114
* @param {Module } pluginModule The module to extract plugins from
125
115
* @return {Promise<PluginTypeResult> } An object containing the loaded plugins classified by their type.
126
116
*/
127
- async _getPluginObjects ( pluginModule ) {
128
- const plugins = {
117
+ private async _getPluginObjects ( pluginModule : any ) : Promise < PluginTypeResult > {
118
+ const plugins : PluginTypeResult = {
129
119
pushAction : [ ] ,
130
120
pullAction : [ ] ,
131
121
} ;
132
122
133
- function handlePlugin ( potentialModule ) {
123
+ function handlePlugin ( potentialModule : any ) {
134
124
if ( isCompatiblePlugin ( potentialModule , 'isGitProxyPushActionPlugin' ) ) {
135
125
console . log ( 'found push plugin' , potentialModule . constructor . name ) ;
136
126
plugins . pushAction . push ( potentialModule ) ;
@@ -165,6 +155,8 @@ class PluginLoader {
165
155
* ProxyPlugin to be loaded by PluginLoader.
166
156
*/
167
157
class ProxyPlugin {
158
+ isGitProxyPlugin : boolean ;
159
+
168
160
constructor ( ) {
169
161
this . isGitProxyPlugin = true ;
170
162
}
@@ -174,21 +166,24 @@ class ProxyPlugin {
174
166
* A plugin which executes a function when receiving a git push request.
175
167
*/
176
168
class PushActionPlugin extends ProxyPlugin {
177
- /**
178
- * Wrapper class which contains at least one function executed as part of the action chain for git push operations.
179
- * The function must be called `exec` and take in two parameters: an Express Request (req) and the current Action
180
- * executed in the chain (action). This function should return a Promise that resolves to an Action.
181
- *
182
- * Optionally, child classes which extend this can simply define the `exec` function as their own property.
183
- * This is the preferred implementation when a custom plugin (subclass) has its own state or additional methods
184
- * that are required.
185
- *
186
- * @param {function } exec - A function that:
187
- * - Takes in an Express Request object as the first parameter (`req`).
188
- * - Takes in an Action object as the second parameter (`action`).
189
- * - Returns a Promise that resolves to an Action.
190
- */
191
- constructor ( exec ) {
169
+ isGitProxyPushActionPlugin : boolean ;
170
+ exec : ( req : any , action : Action ) => Promise < any > ;
171
+
172
+ /**
173
+ * Wrapper class which contains at least one function executed as part of the action chain for git push operations.
174
+ * The function must be called `exec` and take in two parameters: an Express Request (req) and the current Action
175
+ * executed in the chain (action). This function should return a Promise that resolves to an Action.
176
+ *
177
+ * Optionally, child classes which extend this can simply define the `exec` function as their own property.
178
+ * This is the preferred implementation when a custom plugin (subclass) has its own state or additional methods
179
+ * that are required.
180
+ *
181
+ * @param {function } exec - A function that:
182
+ * - Takes in an Express Request object as the first parameter (`req`).
183
+ * - Takes in an Action object as the second parameter (`action`).
184
+ * - Returns a Promise that resolves to an Action.
185
+ */
186
+ constructor ( exec : ( req : any , action : Action ) => Promise < any > ) {
192
187
super ( ) ;
193
188
this . isGitProxyPushActionPlugin = true ;
194
189
this . exec = exec ;
@@ -199,6 +194,9 @@ class PushActionPlugin extends ProxyPlugin {
199
194
* A plugin which executes a function when receiving a git fetch request.
200
195
*/
201
196
class PullActionPlugin extends ProxyPlugin {
197
+ isGitProxyPullActionPlugin : boolean ;
198
+ exec : ( req : any , action : Action ) => Promise < any > ;
199
+
202
200
/**
203
201
* Wrapper class which contains at least one function executed as part of the action chain for git pull operations.
204
202
* The function must be called `exec` and take in two parameters: an Express Request (req) and the current Action
@@ -213,14 +211,14 @@ class PullActionPlugin extends ProxyPlugin {
213
211
* - Takes in an Action object as the second parameter (`action`).
214
212
* - Returns a Promise that resolves to an Action.
215
213
*/
216
- constructor ( exec ) {
214
+ constructor ( exec : ( req : any , action : Action ) => Promise < any > ) {
217
215
super ( ) ;
218
216
this . isGitProxyPullActionPlugin = true ;
219
217
this . exec = exec ;
220
218
}
221
219
}
222
220
223
- module . exports = {
221
+ export {
224
222
PluginLoader ,
225
223
PushActionPlugin ,
226
224
PullActionPlugin ,
0 commit comments