microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/commandExecutor.ts
139lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as Q from "q"; |
| 5 | import {ChildProcess} from "child_process"; |
| 6 | import {Log} from "./log/log"; |
| 7 | import {Node} from "./node/node"; |
| 8 | import {ISpawnResult} from "./node/childProcess"; |
| 9 | import {HostPlatform, HostPlatformId} from "../common/hostPlatform"; |
| 10 | import {ErrorHelper} from "./error/errorHelper"; |
| 11 | import {InternalErrorCode} from "./error/internalErrorCode"; |
| 12 | |
| 13 | interface EnvironmentOptions { |
| 14 | REACT_DEBUGGER?: string; |
| 15 | } |
| 16 | |
| 17 | interface Options { |
| 18 | env?: EnvironmentOptions; |
| 19 | } |
| 20 | |
| 21 | export enum CommandStatus { |
| 22 | Start = 0, |
| 23 | End = 1 |
| 24 | } |
| 25 | |
| 26 | export class CommandExecutor { |
| 27 | private static ReactNativeCommand = "react-native"; |
| 28 | private currentWorkingDirectory: string; |
| 29 | |
| 30 | constructor(currentWorkingDirectory?: string) { |
| 31 | this.currentWorkingDirectory = currentWorkingDirectory || process.cwd(); |
| 32 | } |
| 33 | |
| 34 | public execute(command: string, options: Options = {}): Q.Promise<void> { |
| 35 | Log.logCommandStatus(command, CommandStatus.Start); |
| 36 | return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env }) |
| 37 | .then(stdout => { |
| 38 | Log.logMessage(stdout); |
| 39 | Log.logCommandStatus(command, CommandStatus.End); |
| 40 | }, |
| 41 | (reason: Error) => |
| 42 | this.generateRejectionForCommand(command, reason)); |
| 43 | } |
| 44 | |
| 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 | */ |
| 52 | public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> { |
| 53 | return this.spawnChildProcess(command, args, true, options).outcome; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Spawns the React Native packager in a child process. |
| 58 | */ |
| 59 | public spawnReactPackager(args?: string[], options: Options = {}): Q.Promise<ISpawnResult> { |
| 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); |
| 69 | return Q.resolve(result); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Kills the React Native packager in a child process. |
| 74 | */ |
| 75 | public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> { |
| 76 | Log.logMessage("Stopping Packager"); |
| 77 | |
| 78 | if (packagerProcess) { |
| 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 | |
| 89 | } else { |
| 90 | Log.logMessage("Packager not found"); |
| 91 | return Q.resolve<void>(void 0); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Executes a react native command and waits for its completion. |
| 97 | */ |
| 98 | public spawnReactCommand(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
| 99 | return this.spawnChildReactCommandProcess(command, args, waitForExit, options); |
| 100 | } |
| 101 | |
| 102 | public spawnChildReactCommandProcess(command: string, args?: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
| 103 | let runArguments = [command]; |
| 104 | if (args) { |
| 105 | runArguments = runArguments.concat(args); |
| 106 | } |
| 107 | |
| 108 | let reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand); |
| 109 | return this.spawnChildProcess(reactCommand, runArguments, waitForExit, options); |
| 110 | } |
| 111 | |
| 112 | private spawnChildProcess(command: string, args: string[], waitForExit: boolean = true, options: Options = {}): ISpawnResult { |
| 113 | let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options); |
| 114 | let commandWithArgs = command + " " + args.join(" "); |
| 115 | let childProcessHelper = new Node.ChildProcess(); |
| 116 | |
| 117 | Log.logCommandStatus(commandWithArgs, CommandStatus.Start); |
| 118 | let result = waitForExit ? childProcessHelper.spawnWaitUntilFinished(command, args, spawnOptions) : childProcessHelper.spawnWaitUntilStarted(command, args, spawnOptions); |
| 119 | |
| 120 | result.stderr.on("data", (data: Buffer) => { |
| 121 | Log.logStreamData(data, process.stderr); |
| 122 | }); |
| 123 | |
| 124 | result.stdout.on("data", (data: Buffer) => { |
| 125 | Log.logStreamData(data, process.stdout); |
| 126 | }); |
| 127 | |
| 128 | result.outcome = result.outcome.then( |
| 129 | () => |
| 130 | Log.logCommandStatus(commandWithArgs, CommandStatus.End), |
| 131 | reason => |
| 132 | this.generateRejectionForCommand(commandWithArgs, reason)); |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> { |
| 137 | return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command)); |
| 138 | } |
| 139 | } |
| 140 | |