fix: expose shared structs as well and fix Remote interface

This commit is contained in:
Samuel Attard 2020-04-02 15:10:53 -07:00
Родитель f6633c9961
Коммит e844013010
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: FB94249299E904FE
5 изменённых файлов: 56 добавлений и 34 удалений

Просмотреть файл

@ -3,18 +3,15 @@ declare module 'electron' {
}
declare module 'electron/main' {
const api: Electron.MainInterface
export = api
export = Electron.Main
}
declare module 'electron/common' {
const api: Electron.CommonInterface
export = api
export = Electron.Common
}
declare module 'electron/renderer' {
const api: Electron.RendererInterface
export = api
export = Electron.Renderer
}
interface NodeRequireFunction {

Просмотреть файл

@ -221,6 +221,8 @@ const flushParamInterfaces = (
Object.assign(paramInterfacesToDeclare, nestedInterfacesToDeclare);
}
return Object.keys(declared);
};
export class DynamicParamInterfaces {

Просмотреть файл

@ -28,7 +28,7 @@ const wrapWithHeaderAndFooter = (outputLines: string[], electronVersion: string)
.split(/\r?\n/),
);
outputLines.slice(1).forEach(l => newOutputLines.push(`${_.trimEnd(` ${l}`)}`));
outputLines.slice(0).forEach(l => newOutputLines.push(`${_.trimEnd(` ${l}`)}`));
utils.extendArray(newOutputLines, ['}', '']);
utils.extendArray(
@ -83,10 +83,13 @@ export async function generateDefinitions({ electronApi: API }: GenerateOptions)
};
remapOptionals(API);
generateMasterInterfaces(API, addToOutput);
// generate module declaration for every class, module, structure, element, etc
const declaredStructs: string[] = [];
API.sort((m1, m2) => m1.name.localeCompare(m2.name)).forEach((module, index) => {
if (module.type === 'Structure') {
declaredStructs.push(module.name);
}
generateModuleDeclaration(module, index, API);
});
@ -100,7 +103,8 @@ export async function generateDefinitions({ electronApi: API }: GenerateOptions)
);
});
DynamicParamInterfaces.flushParamInterfaces(API, addToOutput);
const keys = DynamicParamInterfaces.flushParamInterfaces(API, addToOutput);
generateMasterInterfaces(API, [...keys, ...declaredStructs], addToOutput);
const electronOutput = wrapWithHeaderAndFooter(outputLines, API[0].version);

Просмотреть файл

@ -5,15 +5,14 @@ const debug = d('master-interface');
export const generateMasterInterfaces = (
API: ParsedDocumentationResult,
interfaceKeys: string[],
addToOutput: (lines: string[], sep?: string) => void,
) => {
// Generate Main / Renderer process interfaces
const CommonInterface = ['interface CommonInterface {'];
const MainInterface = ['interface MainInterface extends CommonInterface {'];
const RendererInterface = ['interface RendererInterface extends CommonInterface {'];
const ElectronMainAndRendererInterface = [
'interface AllElectron extends MainInterface, RendererInterface {}',
];
const CommonNamespace = ['namespace Common {'];
const MainNamespace = ['namespace Main {'];
const RendererNamespace = ['namespace Renderer {'];
const MainInterfaceForRemote = ['interface RemoteMainInterface {'];
const constDeclarations: string[] = [];
const EMRI: Record<string, boolean> = {};
@ -32,54 +31,74 @@ export const generateMasterInterfaces = (
API.forEach((module, index) => {
if (module.name === 'process') return;
let TargetInterface;
let TargetNamespace;
const isClass =
module.type === 'Class' ||
API.some(
(tModule, tIndex) =>
index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase(),
);
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}${_.upperFirst(
module.name,
)}`;
const moduleString = isClass ? ` class ${_.upperFirst(module.name)} extends Electron.${_.upperFirst(module.name)} {}` : '';
if (module.type === 'Structure') {
// We must be a structure or something
return;
}
const newConstDeclarations: string[] = [];
if (!isClass || module.name !== classify(module.name)) {
if (isClass) {
constDeclarations.push(
newConstDeclarations.push(
`type ${classify(module.name)} = ${_.upperFirst(module.name)};`,
`const ${classify(module.name)}: typeof ${_.upperFirst(module.name)};`,
);
} else {
constDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`);
newConstDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`);
}
}
constDeclarations.push(...newConstDeclarations);
if (module.process.main && module.process.renderer) {
TargetInterface = CommonInterface;
TargetNamespace = CommonNamespace;
} else if (module.process.main) {
TargetInterface = MainInterface;
TargetNamespace = MainNamespace;
} else if (module.process.renderer) {
TargetInterface = RendererInterface;
TargetNamespace = RendererNamespace;
}
if (TargetInterface) {
if (module.process.main && !EMRI[classify(module.name).toLowerCase()]) {
MainInterfaceForRemote.push(` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}${_.upperFirst(
module.name,
)};`)
}
if (TargetNamespace) {
debug(classify(module.name).toLowerCase(), EMRI[classify(module.name).toLowerCase()]);
if (!EMRI[classify(module.name).toLowerCase()]) {
TargetInterface.push(moduleString);
if (moduleString) TargetNamespace.push(moduleString);
}
EMRI[classify(module.name).toLowerCase()] = true;
TargetNamespace.push(...newConstDeclarations.map(s => ` ${s.substr(0, s.length - 1)}`));
}
});
CommonInterface.push('}');
MainInterface.push('}');
RendererInterface.push('}');
addToOutput([
...MainInterfaceForRemote,
'}'
])
for (const interfaceKey of interfaceKeys) {
const alias = ` type ${interfaceKey} = Electron.${interfaceKey}`;
CommonNamespace.push(alias);
MainNamespace.push(alias);
RendererNamespace.push(alias);
}
CommonNamespace.push('}');
MainNamespace.push('}');
RendererNamespace.push('}');
const withSemicolons = (lines: string[]) => {
return lines.map(l => l.endsWith('{') || l.endsWith('}') ? l : `${l};`);
}
addToOutput(['']);
addToOutput(CommonInterface, ';');
addToOutput(MainInterface, ';');
addToOutput(RendererInterface, ';');
addToOutput(ElectronMainAndRendererInterface, ';');
addToOutput(withSemicolons(CommonNamespace));
addToOutput(withSemicolons(MainNamespace));
addToOutput(withSemicolons(RendererNamespace));
addToOutput(constDeclarations);
};

Просмотреть файл

@ -45,7 +45,7 @@ export const generateModuleDeclaration = (
module.name,
)} extends ${module.extends ||
(module.name === 'remote'
? 'MainInterface'
? 'RemoteMainInterface'
: isClass
? 'NodeEventEmitter'
: 'NodeJS.EventEmitter')} {`,