@@ -5,15 +5,14 @@ const debug = d('master-interface');
5
5
6
6
export const generateMasterInterfaces = (
7
7
API : ParsedDocumentationResult ,
8
+ interfaceKeys : string [ ] ,
8
9
addToOutput : ( lines : string [ ] , sep ?: string ) => void ,
9
10
) => {
10
11
// Generate Main / Renderer process interfaces
11
- const CommonInterface = [ 'interface CommonInterface {' ] ;
12
- const MainInterface = [ 'interface MainInterface extends CommonInterface {' ] ;
13
- const RendererInterface = [ 'interface RendererInterface extends CommonInterface {' ] ;
14
- const ElectronMainAndRendererInterface = [
15
- 'interface AllElectron extends MainInterface, RendererInterface {}' ,
16
- ] ;
12
+ const CommonNamespace = [ 'namespace Common {' ] ;
13
+ const MainNamespace = [ 'namespace Main {' ] ;
14
+ const RendererNamespace = [ 'namespace Renderer {' ] ;
15
+ const MainInterfaceForRemote = [ 'interface RemoteMainInterface {' ] ;
17
16
const constDeclarations : string [ ] = [ ] ;
18
17
const EMRI : Record < string , boolean > = { } ;
19
18
@@ -32,54 +31,74 @@ export const generateMasterInterfaces = (
32
31
33
32
API . forEach ( ( module , index ) => {
34
33
if ( module . name === 'process' ) return ;
35
- let TargetInterface ;
34
+ let TargetNamespace ;
36
35
const isClass =
37
36
module . type === 'Class' ||
38
37
API . some (
39
38
( tModule , tIndex ) =>
40
39
index !== tIndex && tModule . name . toLowerCase ( ) === module . name . toLowerCase ( ) ,
41
40
) ;
42
- const moduleString = ` ${ classify ( module . name ) } : ${ isClass ? 'typeof ' : '' } ${ _ . upperFirst (
43
- module . name ,
44
- ) } `;
41
+ const moduleString = isClass ? ` class ${ _ . upperFirst ( module . name ) } extends Electron.${ _ . upperFirst ( module . name ) } {}` : '' ;
45
42
if ( module . type === 'Structure' ) {
46
43
// We must be a structure or something
47
44
return ;
48
45
}
46
+ const newConstDeclarations : string [ ] = [ ] ;
49
47
if ( ! isClass || module . name !== classify ( module . name ) ) {
50
48
if ( isClass ) {
51
- constDeclarations . push (
49
+ newConstDeclarations . push (
52
50
`type ${ classify ( module . name ) } = ${ _ . upperFirst ( module . name ) } ;` ,
53
51
`const ${ classify ( module . name ) } : typeof ${ _ . upperFirst ( module . name ) } ;` ,
54
52
) ;
55
53
} else {
56
- constDeclarations . push ( `const ${ classify ( module . name ) } : ${ _ . upperFirst ( module . name ) } ;` ) ;
54
+ newConstDeclarations . push ( `const ${ classify ( module . name ) } : ${ _ . upperFirst ( module . name ) } ;` ) ;
57
55
}
58
56
}
57
+ constDeclarations . push ( ...newConstDeclarations ) ;
59
58
if ( module . process . main && module . process . renderer ) {
60
- TargetInterface = CommonInterface ;
59
+ TargetNamespace = CommonNamespace ;
61
60
} else if ( module . process . main ) {
62
- TargetInterface = MainInterface ;
61
+ TargetNamespace = MainNamespace ;
63
62
} else if ( module . process . renderer ) {
64
- TargetInterface = RendererInterface ;
63
+ TargetNamespace = RendererNamespace ;
65
64
}
66
- if ( TargetInterface ) {
65
+ if ( module . process . main && ! EMRI [ classify ( module . name ) . toLowerCase ( ) ] ) {
66
+ MainInterfaceForRemote . push ( ` ${ classify ( module . name ) } : ${ isClass ? 'typeof ' : '' } ${ _ . upperFirst (
67
+ module . name ,
68
+ ) } ;`)
69
+ }
70
+ if ( TargetNamespace ) {
67
71
debug ( classify ( module . name ) . toLowerCase ( ) , EMRI [ classify ( module . name ) . toLowerCase ( ) ] ) ;
68
72
if ( ! EMRI [ classify ( module . name ) . toLowerCase ( ) ] ) {
69
- TargetInterface . push ( moduleString ) ;
73
+ if ( moduleString ) TargetNamespace . push ( moduleString ) ;
70
74
}
71
75
EMRI [ classify ( module . name ) . toLowerCase ( ) ] = true ;
76
+ TargetNamespace . push ( ...newConstDeclarations . map ( s => ` ${ s . substr ( 0 , s . length - 1 ) } ` ) ) ;
72
77
}
73
78
} ) ;
74
79
75
- CommonInterface . push ( '}' ) ;
76
- MainInterface . push ( '}' ) ;
77
- RendererInterface . push ( '}' ) ;
80
+ addToOutput ( [
81
+ ...MainInterfaceForRemote ,
82
+ '}'
83
+ ] )
84
+
85
+ for ( const interfaceKey of interfaceKeys ) {
86
+ const alias = ` type ${ interfaceKey } = Electron.${ interfaceKey } ` ;
87
+ CommonNamespace . push ( alias ) ;
88
+ MainNamespace . push ( alias ) ;
89
+ RendererNamespace . push ( alias ) ;
90
+ }
91
+
92
+ CommonNamespace . push ( '}' ) ;
93
+ MainNamespace . push ( '}' ) ;
94
+ RendererNamespace . push ( '}' ) ;
78
95
96
+ const withSemicolons = ( lines : string [ ] ) => {
97
+ return lines . map ( l => l . endsWith ( '{' ) || l . endsWith ( '}' ) ? l : `${ l } ;` ) ;
98
+ }
79
99
addToOutput ( [ '' ] ) ;
80
- addToOutput ( CommonInterface , ';' ) ;
81
- addToOutput ( MainInterface , ';' ) ;
82
- addToOutput ( RendererInterface , ';' ) ;
83
- addToOutput ( ElectronMainAndRendererInterface , ';' ) ;
100
+ addToOutput ( withSemicolons ( CommonNamespace ) ) ;
101
+ addToOutput ( withSemicolons ( MainNamespace ) ) ;
102
+ addToOutput ( withSemicolons ( RendererNamespace ) ) ;
84
103
addToOutput ( constDeclarations ) ;
85
104
} ;
0 commit comments