microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
145lines · 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) { |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 31 | this.currentWorkingDirectory = currentWorkingDirectory || process.cwd(); |
3fb37ad5unknown10 years ago | 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 | |
| 47 | * This method waits until the spawned process finishes 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 | */ | |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 52 | public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> { |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 53 | return this.spawnChildProcess(command, args, true, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 54 | } |
| 55 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 56 | /** |
| 57 | * Spawns the React Native packager in a child process. | |
| 58 | */ | |
8269b3b7Meena Kunnathur Balakrishnan10 years ago | 59 | public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ISpawnResult> { |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 60 | let command = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
| 61 | let runArguments = ["start"]; | |
| 62 | | |
| 63 | if (args) { | |
| 64 | runArguments = runArguments.concat(args); | |
| 65 | } | |
| 66 | | |
| 67 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 68 | let result = this.spawnChildProcess(command, runArguments, false, spawnOptions); | |
8269b3b7Meena Kunnathur Balakrishnan10 years ago | 69 | return Q.resolve(result); |
6126d899Meena Kunnathur Balakrishnan10 years ago | 70 | } |
| 71 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 72 | /** |
| 73 | * Kills the React Native packager in a child process. | |
| 74 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 75 | public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 76 | Log.logMessage("Stopping Packager"); |
a1005420dlebu10 years ago | 77 | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 78 | if (packagerProcess) { |
a1005420dlebu10 years ago | 79 | return Q({}).then(() => { |
| 80 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { | |
| 81 | return new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome; | |
| 82 | } else { | |
| 83 | packagerProcess.kill(); | |
| 84 | } | |
| 85 | }).then(() => { | |
| 86 | Log.logMessage("Packager stopped"); | |
| 87 | }); | |
| 88 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 89 | } else { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 90 | Log.logMessage("Packager not found"); |
10873e11digeff10 years ago | 91 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 92 | } |
| 93 | } | |
| 94 | | |
f8d32439dlebu10 years ago | 95 | /** |
| 96 | * Executes a react native command and waits for its completion. | |
| 97 | */ | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 98 | public spawnReactCommand(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 99 | return this.spawnChildReactCommandProcess(command, args, waitForExit, options); |
5b0582f3digeff10 years ago | 100 | } |
| 101 | | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 102 | public spawnChildReactCommandProcess(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
f8d32439dlebu10 years ago | 103 | let runArguments = [command]; |
| 104 | if (args) { | |
f7208a21Jimmy Thomson10 years ago | 105 | runArguments = runArguments.concat(args); |
f8d32439dlebu10 years ago | 106 | } |
| 107 | | |
a1005420dlebu10 years ago | 108 | let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 109 | return this.spawnChildProcess(reactCommand, runArguments, waitForExit, options); |
f8d32439dlebu10 years ago | 110 | } |
| 111 | | |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 112 | private spawnChildProcess(command: string, args: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
08e81a53unknown10 years ago | 113 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
3fb37ad5unknown10 years ago | 114 | let commandWithArgs = command + " " + args.join(" "); |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 115 | let childProcessHelper = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 116 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 117 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); |
88246fe9Meena Kunnathur Balakrishnan10 years ago | 118 | let result = waitForExit ? childProcessHelper.spawnWaitUntilFinished(command, args, spawnOptions) : childProcessHelper.spawnWaitUntilStarted(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 119 | |
| 120 | result.stderr.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 121 | Log.logStreamData(data, process.stderr); |
3fb37ad5unknown10 years ago | 122 | }); |
| 123 | | |
| 124 | result.stdout.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 125 | Log.logStreamData(data, process.stdout); |
3fb37ad5unknown10 years ago | 126 | }); |
| 127 | | |
05604bfbdigeff10 years ago | 128 | // HACK: Remove this and make a better implementation |
| 129 | result.spawnedProcess.once("exit", (code: number) => { | |
| 130 | if (code === 0) { | |
| 131 | Log.logCommandStatus(commandWithArgs, CommandStatus.End); | |
| 132 | } | |
| 133 | }); | |
| 134 | | |
10873e11digeff10 years ago | 135 | result.outcome = result.outcome.then( |
05604bfbdigeff10 years ago | 136 | () => {}, |
10873e11digeff10 years ago | 137 | reason => |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 138 | this.generateRejectionForCommand(commandWithArgs, reason)); |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 139 | return result; |
3fb37ad5unknown10 years ago | 140 | } |
10873e11digeff10 years ago | 141 | |
3677173cdigeff10 years ago | 142 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 143 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
10873e11digeff10 years ago | 144 | } |
3fb37ad5unknown10 years ago | 145 | } |