Skip to content

Commit e844013

Browse files
fix: expose shared structs as well and fix Remote interface
1 parent f6633c9 commit e844013

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

base/base_footer.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ declare module 'electron' {
33
}
44

55
declare module 'electron/main' {
6-
const api: Electron.MainInterface
7-
export = api
6+
export = Electron.Main
87
}
98

109
declare module 'electron/common' {
11-
const api: Electron.CommonInterface
12-
export = api
10+
export = Electron.Common
1311
}
1412

1513
declare module 'electron/renderer' {
16-
const api: Electron.RendererInterface
17-
export = api
14+
export = Electron.Renderer
1815
}
1916

2017
interface NodeRequireFunction {

src/dynamic-param-interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ const flushParamInterfaces = (
221221

222222
Object.assign(paramInterfacesToDeclare, nestedInterfacesToDeclare);
223223
}
224+
225+
return Object.keys(declared);
224226
};
225227

226228
export class DynamicParamInterfaces {

src/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const wrapWithHeaderAndFooter = (outputLines: string[], electronVersion: string)
2828
.split(/\r?\n/),
2929
);
3030

31-
outputLines.slice(1).forEach(l => newOutputLines.push(`${_.trimEnd(` ${l}`)}`));
31+
outputLines.slice(0).forEach(l => newOutputLines.push(`${_.trimEnd(` ${l}`)}`));
3232
utils.extendArray(newOutputLines, ['}', '']);
3333

3434
utils.extendArray(
@@ -83,10 +83,13 @@ export async function generateDefinitions({ electronApi: API }: GenerateOptions)
8383
};
8484

8585
remapOptionals(API);
86-
generateMasterInterfaces(API, addToOutput);
8786

8887
// generate module declaration for every class, module, structure, element, etc
88+
const declaredStructs: string[] = [];
8989
API.sort((m1, m2) => m1.name.localeCompare(m2.name)).forEach((module, index) => {
90+
if (module.type === 'Structure') {
91+
declaredStructs.push(module.name);
92+
}
9093
generateModuleDeclaration(module, index, API);
9194
});
9295

@@ -100,7 +103,8 @@ export async function generateDefinitions({ electronApi: API }: GenerateOptions)
100103
);
101104
});
102105

103-
DynamicParamInterfaces.flushParamInterfaces(API, addToOutput);
106+
const keys = DynamicParamInterfaces.flushParamInterfaces(API, addToOutput);
107+
generateMasterInterfaces(API, [...keys, ...declaredStructs], addToOutput);
104108

105109
const electronOutput = wrapWithHeaderAndFooter(outputLines, API[0].version);
106110

src/master-interfaces.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ const debug = d('master-interface');
55

66
export const generateMasterInterfaces = (
77
API: ParsedDocumentationResult,
8+
interfaceKeys: string[],
89
addToOutput: (lines: string[], sep?: string) => void,
910
) => {
1011
// 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 {'];
1716
const constDeclarations: string[] = [];
1817
const EMRI: Record<string, boolean> = {};
1918

@@ -32,54 +31,74 @@ export const generateMasterInterfaces = (
3231

3332
API.forEach((module, index) => {
3433
if (module.name === 'process') return;
35-
let TargetInterface;
34+
let TargetNamespace;
3635
const isClass =
3736
module.type === 'Class' ||
3837
API.some(
3938
(tModule, tIndex) =>
4039
index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase(),
4140
);
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)} {}` : '';
4542
if (module.type === 'Structure') {
4643
// We must be a structure or something
4744
return;
4845
}
46+
const newConstDeclarations: string[] = [];
4947
if (!isClass || module.name !== classify(module.name)) {
5048
if (isClass) {
51-
constDeclarations.push(
49+
newConstDeclarations.push(
5250
`type ${classify(module.name)} = ${_.upperFirst(module.name)};`,
5351
`const ${classify(module.name)}: typeof ${_.upperFirst(module.name)};`,
5452
);
5553
} else {
56-
constDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`);
54+
newConstDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`);
5755
}
5856
}
57+
constDeclarations.push(...newConstDeclarations);
5958
if (module.process.main && module.process.renderer) {
60-
TargetInterface = CommonInterface;
59+
TargetNamespace = CommonNamespace;
6160
} else if (module.process.main) {
62-
TargetInterface = MainInterface;
61+
TargetNamespace = MainNamespace;
6362
} else if (module.process.renderer) {
64-
TargetInterface = RendererInterface;
63+
TargetNamespace = RendererNamespace;
6564
}
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) {
6771
debug(classify(module.name).toLowerCase(), EMRI[classify(module.name).toLowerCase()]);
6872
if (!EMRI[classify(module.name).toLowerCase()]) {
69-
TargetInterface.push(moduleString);
73+
if (moduleString) TargetNamespace.push(moduleString);
7074
}
7175
EMRI[classify(module.name).toLowerCase()] = true;
76+
TargetNamespace.push(...newConstDeclarations.map(s => ` ${s.substr(0, s.length - 1)}`));
7277
}
7378
});
7479

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('}');
7895

96+
const withSemicolons = (lines: string[]) => {
97+
return lines.map(l => l.endsWith('{') || l.endsWith('}') ? l : `${l};`);
98+
}
7999
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));
84103
addToOutput(constDeclarations);
85104
};

src/module-declaration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const generateModuleDeclaration = (
4545
module.name,
4646
)} extends ${module.extends ||
4747
(module.name === 'remote'
48-
? 'MainInterface'
48+
? 'RemoteMainInterface'
4949
: isClass
5050
? 'NodeEventEmitter'
5151
: 'NodeJS.EventEmitter')} {`,

0 commit comments

Comments
 (0)