/********************************************************************************
 * 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 path from 'path' ; import { injectable } from '@theia/core/shared/inversify' ; import { FileUri } from '@theia/core/lib/node/file-uri' ; import { ExternalTerminalService , ExternalTerminalConfiguration } from '../common/external-terminal' ; @ injectable ( ) export class WindowsExternalTerminalService implements ExternalTerminalService { protected readonly CMD = 'cmd.exe' ; protected DEFAULT_TERMINAL_WINDOWS : string ; async openTerminal ( configuration , cwd ) { await this . spawnTerminal ( configuration , FileUri . fsPath ( cwd ) ) ; } async getDefaultExec ( ) { return this . getDefaultTerminalWindows ( ) ; } param configuration param cwd protected async spawnTerminal ( configuration , cwd ? ) { const terminalConfig = configuration [ 'terminal.external.windowsExec' ] ; const exec = terminalConfig || this . getDefaultTerminalWindows ( ) ; if ( cwd && cwd [ 1 ] === ':' ) { cwd = cwd [ 0 ] . toUpperCase ( ) + cwd . substr ( 1 ) ; } const basename = path . basename ( exec ) . toLowerCase ( ) ; if ( basename === 'cmder' || basename === 'cmder.exe' ) { cp . spawn ( exec , cwd ? [ cwd ] : undefined ) ; return ; } const cmdArgs = [ '/c' , 'start' , '/wait' ] ; if ( exec . indexOf ( ' ' ) >= 0 ) { cmdArgs . push ( '""' ) ; } cmdArgs . push ( exec ) ; if ( basename === 'wt' || basename === 'wt.exe' ) { cmdArgs . push ( '-d .' ) ; } return new Promise < void > ( async ( resolve , reject ) => { const env = cwd ? { cwd } : undefined ; const command = this . getWindowsShell ( ) ; const child = cp . spawn ( command , cmdArgs , env ) ; child . on ( 'error' , reject ) ; child . on ( 'exit' , resolve ) ; } ) ; } returns protected getDefaultTerminalWindows ( ) { if ( ! this . DEFAULT_TERMINAL_WINDOWS ) { const isWoW64 = ! ! process . env . hasOwnProperty ( 'PROCESSOR_ARCHITEW6432' ) ; this . DEFAULT_TERMINAL_WINDOWS = `${ process . env . windir ? process . env . windir : 'C:\\Windows' }\\${ isWoW64 ? 'Sysnative' : 'System32' }\\cmd.exe` ; } return this . DEFAULT_TERMINAL_WINDOWS ; } /**
     * Find the Windows Shell process to start up (defaults to cmd.exe).
     */ protected getWindowsShell ( ) { return process . env . compsec || this . CMD ; } }