microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
183lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as Q from "q"; |
| 5 | import {ChildProcess} from "child_process"; |
| 6 | import {Log} from "./log/log"; |
| 7 | import {Node} from "./node/node"; |
| 8 | import {ISpawnResult, IExecRejection} from "./node/childProcess"; |
| 9 | import {OutputChannel} from "vscode"; |
| 10 | import {ErrorHelper} from "./error/errorHelper"; |
| 11 | import {InternalErrorCode} from "./error/internalErrorCode"; |
| 12 | |
| 13 | interface EnvironmentOptions { |
| 14 | REACT_DEBUGGER?: string; |
| 15 | } |
| 16 | |
| 17 | interface Options { |
| 18 | env?: EnvironmentOptions; |
| 19 | } |
| 20 | |
| 21 | export enum CommandStatus { |
| 22 | Start = 0, |
| 23 | End = 1 |
| 24 | } |
| 25 | |
| 26 | export class CommandExecutor { |
| 27 | private currentWorkingDirectory: string; |
| 28 | |
| 29 | constructor(currentWorkingDirectory?: string) { |
| 30 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 31 | } |
| 32 | |
| 33 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
| 34 | Log.logCommandStatus(command, CommandStatus.Start); |
| 35 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
| 36 | .then(stdout => { |
| 37 | Log.logMessage(stdout); |
| 38 | Log.logCommandStatus(command, CommandStatus.End); |
| 39 | }, |
| 40 | (reason: IExecRejection) => |
| 41 | this.generateRejectionForCommand(command, reason.error)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Spawns a child process with the params passed and returns promise of the spawned ChildProcess |
| 46 | * This method does not wait for the spawned process to finish execution |
| 47 | * {command} - The command to be invoked in the child process |
| 48 | * {args} - Arguments to be passed to the command |
| 49 | * {options} - additional options with which the child process needs to be spawned |
| 50 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed |
| 51 | */ |
| 52 | public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess { |
| 53 | return this.spawnChildProcess(command, args, options, outputChannel).spawnedProcess; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Spawns a child process with the params passed |
| 58 | * This method waits until the spawned process finishes execution |
| 59 | * {command} - The command to be invoked in the child process |
| 60 | * {args} - Arguments to be passed to the command |
| 61 | * {options} - additional options with which the child process needs to be spawned |
| 62 | * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed |
| 63 | */ |
| 64 | public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { |
| 65 | return this.spawnChildProcess(command, args, options, outputChannel).outcome; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Spawns the React Native packager in a child process. |
| 70 | */ |
| 71 | public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> { |
| 72 | let deferred = Q.defer<ChildProcess>(); |
| 73 | let command = this.getReactCommandName(); |
| 74 | let runArguments = ["start"]; |
| 75 | |
| 76 | if (args) { |
| 77 | runArguments = runArguments.concat(args); |
| 78 | } |
| 79 | |
| 80 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 81 | |
| 82 | let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions); |
| 83 | result.spawnedProcess.once("error", (error: any) => { |
| 84 | deferred.reject({ error: error }); |
| 85 | }); |
| 86 | |
| 87 | result.stderr.on("data", (data: Buffer) => { |
| 88 | Log.logMessage(data.toString(), outputChannel || process.stderr, /*formatMessage*/false); |
| 89 | }); |
| 90 | |
| 91 | result.stdout.on("data", (data: Buffer) => { |
| 92 | Log.logMessage(data.toString(), outputChannel || process.stdout, /*formatMessage*/false); |
| 93 | }); |
| 94 | |
| 95 | // TODO #83 - PROMISE: We need to consume result.outcome here |
| 96 | Q.delay(300).done(() => deferred.resolve(result.spawnedProcess)); |
| 97 | return deferred.promise; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Kills the React Native packager in a child process. |
| 102 | */ |
| 103 | public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): Q.Promise<void> { |
| 104 | let targetLogChannel = outputChannel || console; |
| 105 | |
| 106 | Log.logMessage("Stopping Packager", targetLogChannel); |
| 107 | if (packagerProcess) { |
| 108 | /* To reliably kill the child process on all versions of Windows, |
| 109 | * please use taskkill to end the packager process */ |
| 110 | if (process.platform === "win32") { |
| 111 | return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => { |
| 112 | Log.logMessage("Packager stopped", targetLogChannel); |
| 113 | }); |
| 114 | } else { |
| 115 | packagerProcess.kill(); |
| 116 | Log.logMessage("Packager stopped", targetLogChannel); |
| 117 | return Q.resolve<void>(void 0); |
| 118 | } |
| 119 | } else { |
| 120 | Log.logMessage("Packager not found", targetLogChannel); |
| 121 | return Q.resolve<void>(void 0); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Executes a react native command and waits for its completion. |
| 129 | */ |
| 130 | public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> { |
| 131 | return this.spawnChildReactCommandProcess(command, args, options, outputChannel).outcome; |
| 132 | } |
| 133 | |
| 134 | public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult { |
| 135 | let runArguments = [command]; |
| 136 | if (args) { |
| 137 | runArguments = runArguments.concat(args); |
| 138 | } |
| 139 | return this.spawnChildProcess(this.getReactCommandName(), runArguments, options, outputChannel); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Resolves the dev machine, desktop platform. |
| 144 | */ |
| 145 | private getReactCommandName() { |
| 146 | let platform = process.platform; |
| 147 | switch (platform) { |
| 148 | case "darwin": |
| 149 | return "react-native"; |
| 150 | case "win32": |
| 151 | default: |
| 152 | return "react-native.cmd"; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult { |
| 157 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 158 | let commandWithArgs = command + " " + args.join(" "); |
| 159 | |
| 160 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start, outputChannel || console); |
| 161 | let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions); |
| 162 | |
| 163 | result.stderr.on("data", (data: Buffer) => { |
| 164 | Log.logMessage(data.toString(), outputChannel || process.stderr, /*formatMessage*/ false); |
| 165 | }); |
| 166 | |
| 167 | result.stdout.on("data", (data: Buffer) => { |
| 168 | Log.logMessage(data.toString(), outputChannel || process.stdout, /*formatMessage*/ false); |
| 169 | }); |
| 170 | |
| 171 | result.outcome = result.outcome.then( |
| 172 | () => |
| 173 | Log.logCommandStatus(commandWithArgs, CommandStatus.End, outputChannel || console), |
| 174 | reason => |
| 175 | this.generateRejectionForCommand(command, reason)); |
| 176 | |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
| 181 | return Q.reject<void>(ErrorHelper.getInternalError(InternalErrorCode.CommandExecutionFailed, command, reason)); |
| 182 | } |
| 183 | } |
| 184 | |