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