microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
166lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
10873e11digeff10 years ago | 4 | import * as Q from "q"; |
2e15926eMeena Kunnathur Balakrishnan10 years ago | 5 | import {ChildProcess} from "child_process"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 6 | import {Log} from "./log/log"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 7 | import {Node} from "./node/node"; |
cb6d0922digeff10 years ago | 8 | import {ISpawnResult} from "./node/childProcess"; |
a1005420dlebu10 years ago | 9 | import {HostPlatform, HostPlatformId} from "../common/hostPlatform"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 10 | import {ErrorHelper} from "./error/errorHelper"; |
| 11 | import {InternalErrorCode} from "./error/internalErrorCode"; | |
3fb37ad5unknown10 years ago | 12 | |
| 13 | interface EnvironmentOptions { | |
| 14 | REACT_DEBUGGER?: string; | |
| 15 | } | |
| 16 | | |
| 17 | interface Options { | |
| 18 | env?: EnvironmentOptions; | |
| 19 | } | |
| 20 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 21 | export enum CommandStatus { |
| 22 | Start = 0, | |
| 23 | End = 1 | |
| 24 | } | |
| 25 | | |
3fb37ad5unknown10 years ago | 26 | export class CommandExecutor { |
bbdbbb3bdlebu10 years ago | 27 | private static ReactNativeCommand = "react-native"; |
3fb37ad5unknown10 years ago | 28 | private currentWorkingDirectory: string; |
| 29 | | |
110558c0Jimmy Thomson10 years ago | 30 | constructor(currentWorkingDirectory?: string) { |
3fb37ad5unknown10 years ago | 31 | this.currentWorkingDirectory = currentWorkingDirectory; |
| 32 | } | |
| 33 | | |
fb2bae06Daniel10 years ago | 34 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
17161993Meena Kunnathur Balakrishnan10 years ago | 35 | Log.logCommandStatus(command, CommandStatus.Start); |
bedf110funknown10 years ago | 36 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
3fb37ad5unknown10 years ago | 37 | .then(stdout => { |
bedf110funknown10 years ago | 38 | Log.logMessage(stdout); |
17161993Meena Kunnathur Balakrishnan10 years ago | 39 | Log.logCommandStatus(command, CommandStatus.End); |
3fb37ad5unknown10 years ago | 40 | }, |
cb6d0922digeff10 years ago | 41 | (reason: Error) => |
| 42 | this.generateRejectionForCommand(command, reason)); | |
3fb37ad5unknown10 years ago | 43 | } |
| 44 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 45 | /** |
| 46 | * Spawns a child process with the params passed and returns promise of the spawned ChildProcess | |
| 47 | * This method does not wait for the spawned process to finish execution | |
| 48 | * {command} - The command to be invoked in the child process | |
| 49 | * {args} - Arguments to be passed to the command | |
| 50 | * {options} - additional options with which the child process needs to be spawned | |
| 51 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 52 | public spawn(command: string, args: string[], options: Options = {}): ChildProcess { |
| 53 | return this.spawnChildProcess(command, args, options).spawnedProcess; | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 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 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 63 | public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}): Q.Promise<void> { |
| 64 | return this.spawnChildProcess(command, args, options).outcome; | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 65 | } |
| 66 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 67 | /** |
| 68 | * Spawns the React Native packager in a child process. | |
| 69 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 70 | public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ChildProcess> { |
b3ef3553Meena Kunnathur Balakrishnan10 years ago | 71 | let deferred = Q.defer<ChildProcess>(); |
a1005420dlebu10 years ago | 72 | let command = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
6126d899Meena Kunnathur Balakrishnan10 years ago | 73 | let runArguments = ["start"]; |
| 74 | | |
| 75 | if (args) { | |
| 76 | runArguments = runArguments.concat(args); | |
| 77 | } | |
| 78 | | |
| 79 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 80 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 81 | let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 82 | result.spawnedProcess.once("error", (error: any) => { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 83 | deferred.reject(ErrorHelper.getNestedError(error, InternalErrorCode.PackagerStartFailed)); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 84 | }); |
| 85 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 86 | result.stderr.on("data", (data: Buffer) => { |
99e41548Meena Kunnathur Balakrishnan10 years ago | 87 | Log.logStreamData(data, process.stderr); |
6126d899Meena Kunnathur Balakrishnan10 years ago | 88 | }); |
| 89 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 90 | result.stdout.on("data", (data: Buffer) => { |
99e41548Meena Kunnathur Balakrishnan10 years ago | 91 | Log.logStreamData(data, process.stdout); |
6126d899Meena Kunnathur Balakrishnan10 years ago | 92 | }); |
| 93 | | |
65fd8e85digeff10 years ago | 94 | // TODO #83 - PROMISE: We need to consume result.outcome here |
10873e11digeff10 years ago | 95 | Q.delay(300).done(() => deferred.resolve(result.spawnedProcess)); |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 96 | return deferred.promise; |
6126d899Meena Kunnathur Balakrishnan10 years ago | 97 | } |
| 98 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 99 | /** |
| 100 | * Kills the React Native packager in a child process. | |
| 101 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 102 | public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 103 | Log.logMessage("Stopping Packager"); |
a1005420dlebu10 years ago | 104 | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 105 | if (packagerProcess) { |
a1005420dlebu10 years ago | 106 | return Q({}).then(() => { |
| 107 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { | |
| 108 | return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome; | |
| 109 | } else { | |
| 110 | packagerProcess.kill(); | |
| 111 | } | |
| 112 | }).then(() => { | |
| 113 | Log.logMessage("Packager stopped"); | |
| 114 | }); | |
| 115 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 116 | } else { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 117 | Log.logMessage("Packager not found"); |
10873e11digeff10 years ago | 118 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 119 | } |
| 120 | } | |
| 121 | | |
f8d32439dlebu10 years ago | 122 | /** |
| 123 | * Executes a react native command and waits for its completion. | |
| 124 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 125 | public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}): Q.Promise<void> { |
| 126 | return this.spawnChildReactCommandProcess(command, args, options).outcome; | |
5b0582f3digeff10 years ago | 127 | } |
| 128 | | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 129 | public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}): ISpawnResult { |
f8d32439dlebu10 years ago | 130 | let runArguments = [command]; |
| 131 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 132 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 133 | } |
| 134 | | |
a1005420dlebu10 years ago | 135 | let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
445b113fdlebu10 years ago | 136 | return this.spawnChildProcess(reactCommand, runArguments, options); |
f8d32439dlebu10 years ago | 137 | } |
| 138 | | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 139 | private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult { |
08e81a53unknown10 years ago | 140 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 141 | let commandWithArgs = command + " " + args.join(" "); |
| 142 | | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 143 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 144 | let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 145 | |
| 146 | result.stderr.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 147 | Log.logStreamData(data, process.stderr); |
3fb37ad5unknown10 years ago | 148 | }); |
| 149 | | |
| 150 | result.stdout.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 151 | Log.logStreamData(data, process.stdout); |
3fb37ad5unknown10 years ago | 152 | }); |
| 153 | | |
10873e11digeff10 years ago | 154 | result.outcome = result.outcome.then( |
| 155 | () => | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 156 | Log.logCommandStatus(commandWithArgs, CommandStatus.End), |
10873e11digeff10 years ago | 157 | reason => |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 158 | this.generateRejectionForCommand(commandWithArgs, reason)); |
5b0582f3digeff10 years ago | 159 | |
| 160 | return result; | |
3fb37ad5unknown10 years ago | 161 | } |
10873e11digeff10 years ago | 162 | |
3677173cdigeff10 years ago | 163 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 164 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
10873e11digeff10 years ago | 165 | } |
3fb37ad5unknown10 years ago | 166 | } |