microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
125lines · 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; |
9596aa53digeff10 years ago | 29 | private childProcess = new Node.ChildProcess(); |
3fb37ad5unknown10 years ago | 30 | |
110558c0Jimmy Thomson10 years ago | 31 | constructor(currentWorkingDirectory?: string) { |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 32 | this.currentWorkingDirectory = currentWorkingDirectory || process.cwd(); |
3fb37ad5unknown10 years ago | 33 | } |
| 34 | | |
fb2bae06Daniel10 years ago | 35 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
17161993Meena Kunnathur Balakrishnan10 years ago | 36 | Log.logCommandStatus(command, CommandStatus.Start); |
9596aa53digeff10 years ago | 37 | return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
3fb37ad5unknown10 years ago | 38 | .then(stdout => { |
bedf110funknown10 years ago | 39 | Log.logMessage(stdout); |
17161993Meena Kunnathur Balakrishnan10 years ago | 40 | Log.logCommandStatus(command, CommandStatus.End); |
3fb37ad5unknown10 years ago | 41 | }, |
cb6d0922digeff10 years ago | 42 | (reason: Error) => |
| 43 | this.generateRejectionForCommand(command, reason)); | |
3fb37ad5unknown10 years ago | 44 | } |
| 45 | | |
45944d15Meena Kunnathur Balakrishnan10 years ago | 46 | /** |
| 47 | * Spawns a child process with the params passed | |
| 48 | * This method waits until the spawned process finishes execution | |
| 49 | * {command} - The command to be invoked in the child process | |
| 50 | * {args} - Arguments to be passed to the command | |
| 51 | * {options} - additional options with which the child process needs to be spawned | |
| 52 | */ | |
323a3cc0Meena Kunnathur Balakrishnan10 years ago | 53 | public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> { |
9596aa53digeff10 years ago | 54 | return this.spawnChildProcess(command, args, options).outcome; |
45944d15Meena Kunnathur Balakrishnan10 years ago | 55 | } |
| 56 | | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 57 | /** |
| 58 | * Spawns the React Native packager in a child process. | |
| 59 | */ | |
9596aa53digeff10 years ago | 60 | public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult { |
| 61 | return this.spawnReactCommand("start", args, options); | |
6126d899Meena Kunnathur Balakrishnan10 years ago | 62 | } |
| 63 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 64 | /** |
| 65 | * Kills the React Native packager in a child process. | |
| 66 | */ | |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 67 | public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 68 | Log.logMessage("Stopping Packager"); |
a1005420dlebu10 years ago | 69 | |
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago | 70 | if (packagerProcess) { |
a1005420dlebu10 years ago | 71 | return Q({}).then(() => { |
| 72 | if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) { | |
9596aa53digeff10 years ago | 73 | return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome; |
a1005420dlebu10 years ago | 74 | } else { |
| 75 | packagerProcess.kill(); | |
| 76 | } | |
| 77 | }).then(() => { | |
| 78 | Log.logMessage("Packager stopped"); | |
| 79 | }); | |
| 80 | | |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 81 | } else { |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 82 | Log.logMessage("Packager not found"); |
10873e11digeff10 years ago | 83 | return Q.resolve<void>(void 0); |
c3a987a7Meena Kunnathur Balakrishnan10 years ago | 84 | } |
| 85 | } | |
| 86 | | |
f8d32439dlebu10 years ago | 87 | /** |
| 88 | * Executes a react native command and waits for its completion. | |
| 89 | */ | |
9596aa53digeff10 years ago | 90 | public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult { |
| 91 | const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); | |
| 92 | return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options); | |
5b0582f3digeff10 years ago | 93 | } |
| 94 | | |
9596aa53digeff10 years ago | 95 | private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult { |
| 96 | const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); | |
| 97 | const commandWithArgs = command + " " + args.join(" "); | |
3fb37ad5unknown10 years ago | 98 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 99 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); |
9596aa53digeff10 years ago | 100 | const result = this.childProcess.spawn(command, args, spawnOptions); |
3fb37ad5unknown10 years ago | 101 | |
| 102 | result.stderr.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 103 | Log.logStreamData(data, process.stderr); |
3fb37ad5unknown10 years ago | 104 | }); |
| 105 | | |
| 106 | result.stdout.on("data", (data: Buffer) => { | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 107 | Log.logStreamData(data, process.stdout); |
3fb37ad5unknown10 years ago | 108 | }); |
| 109 | | |
10873e11digeff10 years ago | 110 | result.outcome = result.outcome.then( |
9596aa53digeff10 years ago | 111 | () => |
| 112 | Log.logCommandStatus(commandWithArgs, CommandStatus.End), | |
10873e11digeff10 years ago | 113 | reason => |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 114 | this.generateRejectionForCommand(commandWithArgs, reason)); |
efa076b0Meena Kunnathur Balakrishnan10 years ago | 115 | return result; |
3fb37ad5unknown10 years ago | 116 | } |
10873e11digeff10 years ago | 117 | |
3677173cdigeff10 years ago | 118 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
4f7c7447Meena Kunnathur Balakrishnan10 years ago | 119 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
10873e11digeff10 years ago | 120 | } |
9596aa53digeff10 years ago | 121 | |
| 122 | private combineArguments(firstArgument: string, otherArguments: string[] = []) { | |
| 123 | return [firstArgument].concat(otherArguments); | |
| 124 | } | |
3fb37ad5unknown10 years ago | 125 | } |