/********************************************************************************
 * Copyright (C) 2017 TypeFox and others.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the Eclipse
 * Public License v. 2.0 are satisfied: GNU General Public License, version 2
 * with the GNU Classpath Exception which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 ********************************************************************************/ import * as os from 'os' ; import * as fs from 'fs-extra' ; import { ApplicationPackage } from '@theia/application-package' ; export interface GeneratorOptions { mode ? : 'development' | 'production' ; splitFrontend ? : boolean ; } export abstract class AbstractGenerator { constructor ( protected readonly pck , protected options = { } ) { } protected compileFrontendModuleImports ( modules ) { const splitFrontend = this . options . splitFrontend ?? this . options . mode !== 'production' ; return this . compileModuleImports ( modules , splitFrontend ? 'import' : 'require' ) ; } protected compileBackendModuleImports ( modules ) { return this . compileModuleImports ( modules , 'require' ) ; } protected compileElectronMainModuleImports ( modules ? ) { return modules && this . compileModuleImports ( modules , 'require' ) || '' ; } protected compileModuleImports ( modules , fn ) { 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 => `    .then(function () { return ${ statement }.then(load) })` ) ; return os . EOL + lines . join ( os . EOL ) ; } protected ifBrowser ( value , defaultValue = '' ) { return this . pck . ifBrowser ( value , defaultValue ) ; } protected ifElectron ( value , defaultValue = '' ) { return this . pck . ifElectron ( value , defaultValue ) ; } protected async write ( path , content ) { await fs . ensureFile ( path ) ; await fs . writeFile ( path , content ) ; } protected ifMonaco ( value , defaultValue = ( ) => '' ) { return ( this . pck . extensionPackages . some ( e => e . name === '@theia/monaco' ) ? value : defaultValue ) ( ) ; } protected prettyStringify ( object ) { return JSON . stringify ( object , null , 4 ) ; } }