/********************************************************************************
 * Copyright (C) 2021 Ericsson 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 cp from 'child_process' ; import * as fs from '@theia/core/shared/fs-extra' ; import { injectable } from '@theia/core/shared/inversify' ; import { OS } from '@theia/core/lib/common/os' ; import { FileUri } from '@theia/core/lib/node/file-uri' ; import { ExternalTerminalService , ExternalTerminalConfiguration } from '../common/external-terminal' ; @ injectable ( ) export class LinuxExternalTerminalService implements ExternalTerminalService { protected DEFAULT_TERMINAL_LINUX_READY : Promise < string > ; async openTerminal ( configuration , cwd ) { await this . spawnTerminal ( configuration , FileUri . fsPath ( cwd ) ) ; } async getDefaultExec ( ) { return this . getDefaultTerminalLinux ( ) ; } param configuration param cwd protected async spawnTerminal ( configuration , cwd ? ) { const terminalConfig = configuration [ 'terminal.external.linuxExec' ] ; const execPromise = terminalConfig ? Promise . resolve ( terminalConfig ) : this . getDefaultTerminalLinux ( ) ; return new Promise < void > ( ( resolve , reject ) => { execPromise . then ( exec => { const env = cwd ? { cwd } : undefined ; const child = cp . spawn ( exec , [ ] , env ) ; child . on ( 'error' , reject ) ; child . on ( 'exit' , resolve ) ; } ) ; } ) ; } returns protected async getDefaultTerminalLinux ( ) { if ( ! this . DEFAULT_TERMINAL_LINUX_READY ) { this . DEFAULT_TERMINAL_LINUX_READY = new Promise ( async ( resolve ) => { if ( OS . type ( ) === OS . Type . Linux ) { const isDebian = await fs . pathExists ( '/etc/debian_version' ) ; if ( isDebian ) { resolve ( 'x-terminal-emulator' ) ; } else if ( process . env . DESKTOP_SESSION === 'gnome' || process . env . DESKTOP_SESSION === 'gnome-classic' ) { resolve ( 'gnome-terminal' ) ; } else if ( process . env . DESKTOP_SESSION === 'kde-plasma' ) { resolve ( 'konsole' ) ; } else if ( process . env . COLORTERM ) { resolve ( process . env . COLORTERM ) ; } else if ( process . env . TERM ) { resolve ( process . env . TERM ) ; } else { resolve ( 'xterm' ) ; } } else { resolve ( 'xterm' ) ; } } ) ; } return this . DEFAULT_TERMINAL_LINUX_READY ; } }