microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
216lines · 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 | |
831f4a85Patricio Beltran9 years ago | 13 | export enum CommandVerbosity { |
| 14 | OUTPUT, | |
| 15 | SILENT, | |
| 16 | PROGRESS, | |
| 17 | } | |
| 18 | | |
3fb37ad5unknown10 years ago | 19 | interface EnvironmentOptions { |
| 20 | REACT_DEBUGGER?: string; | |
| 21 | } | |
| 22 | | |
| 23 | interface Options { | |
| 24 | env?: EnvironmentOptions; | |
831f4a85Patricio Beltran9 years ago | 25 | verbosity?: CommandVerbosity; |
0d827d9bJimmy Thomson9 years ago | 26 | cwd?: string; |
3fb37ad5unknown10 years ago | 27 | } |
| 28 | | |
17161993Meena Kunnathur Balakrishnan10 years ago | 29 | export enum CommandStatus { |
| 30 | Start = 0, | |
| 31 | End = 1 | |
| 32 | } | |
| 33 | | |
3fb37ad5unknown10 years ago | 34 | export class CommandExecutor { |
bbdbbb3bdlebu10 years ago | 35 | private static ReactNativeCommand = "react-native"; |
b8a56999Patricio Beltran9 years ago | 36 | private static ReactNativeVersionCommand = "-v"; |
3fb37ad5unknown10 years ago | 37 | private currentWorkingDirectory: string; |
9596aa53digeff10 years ago | 38 | private childProcess = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 39 | |
110558c0Jimmy Thomson10 years ago | 40 | constructor(currentWorkingDirectory?: string) { |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 41 | this.currentWorkingDirectory = currentWorkingDirectory || process.cwd(); |
3fb37ad5unknown10 years ago | 42 | } |
| 43 | | |
fb2bae06Daniel10 years ago | 44 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
831f4a85Patricio Beltran9 years ago | 45 | Log.logCommandStatus(command, CommandStatus.Start); |
9596aa53digeff10 years ago | 46 | return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
3fb37ad5unknown10 years ago | 47 | .then(stdout => { |
831f4a85Patricio Beltran9 years ago | 48 | Log.logMessage(stdout); |
| 49 | Log.logCommandStatus(command, CommandStatus.End); | |
3fb37ad5unknown10 years ago | 50 | }, |
cb6d0922digeff10 years ago | 51 | (reason: Error) => |
| 52 | this.generateRejectionForCommand(command, reason)); | |
3fb37ad5unknown10 years ago | 53 | } |
| 54 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 55 | /** |
| 56 | * Spawns a child process with the params passed | |
| 57 | * This method waits until the spawned process finishes execution | |
| 58 | * {command} - The command to be invoked in the child process | |
| 59 | * {args} - Arguments to be passed to the command | |
| 60 | * {options} - additional options with which the child process needs to be spawned | |
| 61 | */ | |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 62 | public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> { |
9596aa53digeff10 years ago | 63 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 64 | } |
| 65 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 66 | /** |
| 67 | * Spawns the React Native packager in a child process. | |
| 68 | */ | |
9596aa53digeff10 years ago | 69 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 70 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 71 | } |
| 72 | | |
b8a56999Patricio Beltran9 years ago | 73 | /** |
| 74 | * Uses the `react-native -v` command to get the version used on the project. | |
| 75 | * Returns null if the workspace is not a react native project | |
| 76 | */ | |
| 77 | public getReactNativeVersion(): Q.Promise<string> { | |
| 78 | let deferred = Q.defer<string>(); | |
| 79 | const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); | |
| 80 | let output = ""; | |
| 81 | | |
| 82 | const result = this.childProcess.spawn(reactCommand, | |
| 83 | [CommandExecutor.ReactNativeVersionCommand], | |
| 84 | { cwd: this.currentWorkingDirectory }); | |
| 85 | | |
| 86 | result.stdout.on("data", (data: Buffer) => { | |
| 87 | output += data.toString(); | |
| 88 | }); | |
| 89 | | |
| 90 | result.stdout.on("end", () => { | |
| 91 | const match = output.match(/react-native: ([\d\.]+)/); | |
| 92 | deferred.resolve(match && match[1]); | |
| 93 | }); | |
| 94 | | |
| 95 | return deferred.promise; | |
| 96 | } | |
| 97 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 98 | /** |
| 99 | * Kills the React Native packager in a child process. | |
| 100 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 101 | public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> { |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 102 | if (packagerProcess) { |
a1005420dlebu10 years ago | 103 | return Q({}).then(() => { |
| 104 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { | |
9596aa53digeff10 years ago | 105 | return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome; |
a1005420dlebu10 years ago | 106 | } else { |
| 107 | packagerProcess.kill(); | |
| 108 | } | |
| 109 | }).then(() => { | |
| 110 | Log.logMessage("Packager stopped"); | |
| 111 | }); | |
| 112 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 113 | } else { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 114 | Log.logMessage("Packager not found"); |
10873e11digeff10 years ago | 115 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 116 | } |
| 117 | } | |
| 118 | | |
f8d32439dlebu10 years ago | 119 | /** |
| 120 | * Executes a react native command and waits for its completion. | |
| 121 | */ | |
9596aa53digeff10 years ago | 122 | public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult { |
| 123 | const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); | |
| 124 | return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options); | |
5b0582f3digeff10 years ago | 125 | } |
| 126 | | |
831f4a85Patricio Beltran9 years ago | 127 | /** |
| 128 | * Spawns a child process with the params passed | |
| 129 | * This method has logic to do while the command is executing | |
| 130 | * {command} - The command to be invoked in the child process | |
| 131 | * {args} - Arguments to be passed to the command | |
| 132 | * {options} - additional options with which the child process needs to be spawned | |
| 133 | */ | |
| 134 | public spawnWithProgress(command: string, args: string[], options: Options = { verbosity: CommandVerbosity.OUTPUT }): Q.Promise<void> { | |
| 135 | let deferred = Q.defer<void>(); | |
| 136 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 137 | const commandWithArgs = command + " " + args.join(" "); | |
| 138 | const timeBetweenDots = 1500; | |
b0af599cJimmy Thomson9 years ago | 139 | let lastDotTime = 0; |
831f4a85Patricio Beltran9 years ago | 140 | |
| 141 | const printDot = () => { | |
b0af599cJimmy Thomson9 years ago | 142 | const now = Date.now(); |
| 143 | if (now - lastDotTime > timeBetweenDots) { | |
| 144 | lastDotTime = now; | |
| 145 | Log.logString("."); | |
| 146 | } | |
831f4a85Patricio Beltran9 years ago | 147 | }; |
| 148 | | |
| 149 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
| 150 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); | |
| 151 | } | |
| 152 | | |
| 153 | const result = this.childProcess.spawn(command, args, spawnOptions); | |
| 154 | | |
| 155 | result.stdout.on("data", (data: Buffer) => { | |
| 156 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
| 157 | Log.logStreamData(data, process.stdout); | |
b0af599cJimmy Thomson9 years ago | 158 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 159 | printDot(); |
| 160 | } | |
| 161 | }); | |
| 162 | | |
| 163 | result.stderr.on("data", (data: Buffer) => { | |
| 164 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
| 165 | Log.logStreamData(data, process.stderr); | |
b0af599cJimmy Thomson9 years ago | 166 | } else if (options.verbosity === CommandVerbosity.PROGRESS) { |
831f4a85Patricio Beltran9 years ago | 167 | printDot(); |
| 168 | } | |
| 169 | }); | |
| 170 | | |
| 171 | result.outcome = result.outcome.then( | |
| 172 | () => { | |
| 173 | if (options.verbosity === CommandVerbosity.OUTPUT) { | |
| 174 | Log.logCommandStatus(commandWithArgs, CommandStatus.End); | |
| 175 | } | |
| 176 | Log.logString("\n"); | |
| 177 | deferred.resolve(void 0); | |
| 178 | }, | |
| 179 | reason => { | |
0d827d9bJimmy Thomson9 years ago | 180 | deferred.reject(reason); |
831f4a85Patricio Beltran9 years ago | 181 | return this.generateRejectionForCommand(commandWithArgs, reason); |
| 182 | }); | |
| 183 | return deferred.promise; | |
| 184 | } | |
| 185 | | |
9596aa53digeff10 years ago | 186 | private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult { |
| 187 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 188 | const commandWithArgs = command + " " + args.join(" "); | |
3fb37ad5unknown10 years ago | 189 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 190 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); |
9596aa53digeff10 years ago | 191 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 192 | |
| 193 | result.stderr.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 194 | Log.logStreamData(data, process.stderr); |
3fb37ad5unknown10 years ago | 195 | }); |
| 196 | | |
| 197 | result.stdout.on("data", (data: Buffer) => { | |
831f4a85Patricio Beltran9 years ago | 198 | Log.logStreamData(data, process.stdout); |
3fb37ad5unknown10 years ago | 199 | }); |
| 200 | | |
10873e11digeff10 years ago | 201 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 202 | () => |
| 203 | Log.logCommandStatus(commandWithArgs, CommandStatus.End), | |
10873e11digeff10 years ago | 204 | reason => |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 205 | this.generateRejectionForCommand(commandWithArgs, reason)); |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 206 | return result; |
3fb37ad5unknown10 years ago | 207 | } |
10873e11digeff10 years ago | 208 | |
3677173cdigeff10 years ago | 209 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 210 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
10873e11digeff10 years ago | 211 | } |
9596aa53digeff10 years ago | 212 | |
| 213 | private combineArguments(firstArgument: string, otherArguments: string[] = []) { | |
| 214 | return [firstArgument].concat(otherArguments); | |
| 215 | } | |
3fb37ad5unknown10 years ago | 216 | } |