microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
92ca3e659754cd996ac92b6a0e4b0e9f0a9c1d35

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

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