microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4b124a08cf4726fc4b6b1f843dcd24e31f33db5e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

151lines · 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
4import * as Q from "q";
5import {ChildProcess} from "child_process";
6import {Log} from "./log/log";
7import {Node} from "./node/node";
8import {ISpawnResult} from "./node/childProcess";
9import {HostPlatform, HostPlatformId} from "../common/hostPlatform";
10import {ErrorHelper} from "./error/errorHelper";
11import {InternalErrorCode} from "./error/internalErrorCode";
12
13interface EnvironmentOptions {
14 REACT_DEBUGGER?: string;
15}
16
17interface Options {
18 env?: EnvironmentOptions;
19}
20
21export enum CommandStatus {
22 Start = 0,
23 End = 1
24}
25
26export class CommandExecutor {
27 private static ReactNativeCommand = "react-native";
28 private static ReactNativeVersionCommand = "-v";
29 private currentWorkingDirectory: string;
30 private childProcess = new Node.ChildProcess();
31
32 constructor(currentWorkingDirectory?: string) {
33 this.currentWorkingDirectory = currentWorkingDirectory || process.cwd();
34 }
35
36 public execute(command: string, options: Options = {}): Q.Promise<void> {
37 Log.logCommandStatus(command, CommandStatus.Start);
38 return this.childProcess.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
39 .then(stdout => {
40 Log.logMessage(stdout);
41 Log.logCommandStatus(command, CommandStatus.End);
42 },
43 (reason: Error) =>
44 this.generateRejectionForCommand(command, reason));
45 }
46
47 /**
48 * Spawns a child process with the params passed
49 * This method waits until the spawned process finishes execution
50 * {command} - The command to be invoked in the child process
51 * {args} - Arguments to be passed to the command
52 * {options} - additional options with which the child process needs to be spawned
53 */
54 public spawn(command: string, args: string[], options: Options = {}): Q.Promise<any> {
55 return this.spawnChildProcess(command, args, options).outcome;
56 }
57
58 /**
59 * Spawns the React Native packager in a child process.
60 */
61 public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
62 return this.spawnReactCommand("start", args, options);
63 }
64
65 /**
66 * Uses the `react-native -v` command to get the version used on the project.
67 * Returns null if the workspace is not a react native project
68 */
69 public getReactNativeVersion(): Q.Promise<string> {
70 let deferred = Q.defer<string>();
71 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
72 let output = "";
73
74 const result = this.childProcess.spawn(reactCommand,
75 [CommandExecutor.ReactNativeVersionCommand],
76 { cwd: this.currentWorkingDirectory });
77
78 result.stdout.on("data", (data: Buffer) => {
79 output += data.toString();
80 });
81
82 result.stdout.on("end", () => {
83 const match = output.match(/react-native: ([\d\.]+)/);
84 deferred.resolve(match && match[1]);
85 });
86
87 return deferred.promise;
88 }
89
90 /**
91 * Kills the React Native packager in a child process.
92 */
93 public killReactPackager(packagerProcess: ChildProcess): Q.Promise<void> {
94 Log.logMessage("Stopping Packager");
95
96 if (packagerProcess) {
97 return Q({}).then(() => {
98 if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
99 return this.childProcess.exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome;
100 } else {
101 packagerProcess.kill();
102 }
103 }).then(() => {
104 Log.logMessage("Packager stopped");
105 });
106
107 } else {
108 Log.logMessage("Packager not found");
109 return Q.resolve<void>(void 0);
110 }
111 }
112
113 /**
114 * Executes a react native command and waits for its completion.
115 */
116 public spawnReactCommand(command: string, args?: string[], options: Options = {}): ISpawnResult {
117 const reactCommand = HostPlatform.getNpmCliCommand(CommandExecutor.ReactNativeCommand);
118 return this.spawnChildProcess(reactCommand, this.combineArguments(command, args), options);
119 }
120
121 private spawnChildProcess(command: string, args: string[], options: Options = {}): ISpawnResult {
122 const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
123 const commandWithArgs = command + " " + args.join(" ");
124
125 Log.logCommandStatus(commandWithArgs, CommandStatus.Start);
126 const result = this.childProcess.spawn(command, args, spawnOptions);
127
128 result.stderr.on("data", (data: Buffer) => {
129 Log.logStreamData(data, process.stderr);
130 });
131
132 result.stdout.on("data", (data: Buffer) => {
133 Log.logStreamData(data, process.stdout);
134 });
135
136 result.outcome = result.outcome.then(
137 () =>
138 Log.logCommandStatus(commandWithArgs, CommandStatus.End),
139 reason =>
140 this.generateRejectionForCommand(commandWithArgs, reason));
141 return result;
142 }
143
144 private generateRejectionForCommand(command: string, reason: any): Q.Promise<void> {
145 return Q.reject<void>(ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command));
146 }
147
148 private combineArguments(firstArgument: string, otherArguments: string[] = []) {
149 return [firstArgument].concat(otherArguments);
150 }
151}
152