Skip to content

Commit

Permalink
Use EOL in code generators
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Sep 11, 2023
1 parent 16034c2 commit aa50408
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { EOL } from 'os';
import { AbstractGenerator } from './abstract-generator';

export class BackendGenerator extends AbstractGenerator {
Expand Down Expand Up @@ -87,7 +88,7 @@ async function start() {
module.exports = async () => {
try {
${Array.from(electronMainModules?.values() ?? [], jsModulePath => `\
await load(container, import('${jsModulePath}'));`).join('\n')}
await load(container, import('${jsModulePath}'));`).join(EOL)}
await start();
} catch (reason) {
console.error('Failed to start the electron application.');
Expand Down Expand Up @@ -147,7 +148,7 @@ async function start(port, host, argv = process.argv) {
module.exports = async (port, host, argv) => {
try {
${Array.from(backendModules.values(), jsModulePath => `\
await load(require('${jsModulePath}'));`).join('\n')}
await load(require('${jsModulePath}'));`).join(EOL)}
return await start(port, host, argv);
} catch (error) {
console.error('Failed to start the backend application:');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/* eslint-disable @typescript-eslint/indent */

import { EOL } from 'os';
import { AbstractGenerator, GeneratorOptions } from './abstract-generator';
import { existsSync, readFileSync } from 'fs';

Expand Down Expand Up @@ -96,7 +97,7 @@ async function preload(parent) {
container.parent = parent;
try {
${Array.from(frontendPreloadModules.values(), jsModulePath => `\
await load(container, import('${jsModulePath}'));`).join('\n')}
await load(container, import('${jsModulePath}'));`).join(EOL)}
const { Preloader } = require('@theia/core/lib/browser/preload/preloader');
const preloader = container.get(Preloader);
await preloader.initialize();
Expand Down Expand Up @@ -124,7 +125,7 @@ module.exports = (async () => {
try {
${Array.from(frontendModules.values(), jsModulePath => `\
await load(container, import('${jsModulePath}'));`).join('\n')}
await load(container, import('${jsModulePath}'));`).join(EOL)}
await start();
} catch (reason) {
console.error('Failed to start the frontend application.');
Expand Down Expand Up @@ -187,16 +188,7 @@ ${Array.from(frontendModules.values(), jsModulePath => `\
</html>`;
}

protected compileSecondaryModuleImports(secondaryWindowModules: Map<string, string>): string {
const lines = Array.from(secondaryWindowModules.entries())
.map(([moduleName, path]) => ` container.load(require('${path}').default);`);
return '\n' + lines.join('\n');
}

protected compileSecondaryIndexJs(secondaryWindowModules: Map<string, string>): string {
const compiledModuleImports = this.compileSecondaryModuleImports(secondaryWindowModules)
// fix the generated indentation
.replace(/^ /g, ' ');
return `\
// @ts-check
require('reflect-metadata');
Expand All @@ -206,19 +198,16 @@ module.exports = Promise.resolve().then(() => {
const { frontendApplicationModule } = require('@theia/core/lib/browser/frontend-application-module');
const container = new Container();
container.load(frontendApplicationModule);
${compiledModuleImports}
${Array.from(secondaryWindowModules.values(), jsModulePath => `\
container.load(require('${jsModulePath}').default);`).join(EOL)}
});
`;
}

compilePreloadJs(): string {
const lines = Array.from(this.pck.preloadModules)
.map(([moduleName, path]) => `require('${path}').preload();`);
const imports = '\n' + lines.join('\n');

return `\
// @ts-check
${imports}
${Array.from(this.pck.preloadModules.values(), path => `require('${path}').preload();`).join(EOL)}
`;
}
}

0 comments on commit aa50408

Please sign in to comment.