Skip to content

Commit

Permalink
Simplify generator infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jun 20, 2023
1 parent 8bb6955 commit 29550b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import * as os from 'os';
import * as fs from 'fs-extra';
import { ApplicationPackage } from '@theia/application-package';

Expand All @@ -30,33 +29,6 @@ export abstract class AbstractGenerator {
protected options: GeneratorOptions = {}
) { }

protected compileFrontendModuleImports(modules: Map<string, string>, indentation = 1): string {
const splitFrontend = this.options.splitFrontend ?? this.options.mode !== 'production';
return this.compileModuleImports(modules, splitFrontend ? 'import' : 'require', indentation);
}

protected compileBackendModuleImports(modules: Map<string, string>): string {
return this.compileModuleImports(modules, 'require');
}

protected compileElectronMainModuleImports(modules?: Map<string, string>): string {
return modules && this.compileModuleImports(modules, 'require') || '';
}

protected compileModuleImports(modules: Map<string, string>, fn: 'import' | 'require', indentation = 1): string {
if (modules.size === 0) {
return '';
}
const lines = Array.from(modules.keys()).map(moduleName => {
const invocation = `${fn}('${modules.get(moduleName)}')`;
if (fn === 'require') {
return `Promise.resolve(${invocation})`;
}
return invocation;
}).map(statement => `${' '.repeat(indentation * 4)}.then(function () { return ${statement}.then(load) })`);
return os.EOL + lines.join(os.EOL);
}

protected ifBrowser(value: string, defaultValue: string = ''): string {
return this.pck.ifBrowser(value, defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ async function start() {
await application.start(config);
}
module.exports = Promise.resolve()${this.compileElectronMainModuleImports(electronMainModules)}
.then(start).catch(reason => {
module.exports = async () => {
try {
${Array.from(electronMainModules?.values() ?? [], jsModulePath => `\
await load(container, import('${jsModulePath}'));`).join('\n')}
await start();
} catch (reason) {
console.error('Failed to start the electron application.');
if (reason) {
console.error(reason);
Expand Down

0 comments on commit 29550b2

Please sign in to comment.