microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2ec44b6d62529aa08a88dc1dbe190833e1838036

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/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
4import {ChildProcess} from "child_process";
5import {Log} from "./log";
6import {Node} from "../node/node";
7import {ISpawnResult} from "../node/childProcess";
8import {OutputChannel} from "vscode";
9import * as Q from "q";
10
11interface EnvironmentOptions {
12 REACT_DEBUGGER?: string;
13}
14
15interface Options {
16 env?: EnvironmentOptions;
17}
18
19export class CommandExecutor {
20 private currentWorkingDirectory: string;
21
22 constructor(currentWorkingDirectory?: string) {
23 this.currentWorkingDirectory = currentWorkingDirectory;
24 }
25
26 public execute(command: string, options: Options = {}): Q.Promise<void> {
27 Log.commandStarted(command);
28 return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
29 .then(stdout => {
30 Log.logMessage(stdout);
31 Log.commandEnded(command);
32 },
33 reason => Log.commandFailed(command, reason));
34 }
35
36 /**
37 * Spawns a child process with the params passed and returns promise of the spawned ChildProcess
38 * This method does not wait for the spawned process to finish execution
39 * {command} - The command to be invoked in the child process
40 * {args} - Arguments to be passed to the command
41 * {options} - additional options with which the child process needs to be spawned
42 * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
43 */
44 public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
45 return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
46 let commandWithArgs = command + " " + args.join(" ");
47 spawnResult.outcome.then(() => {
48 Log.commandEnded(commandWithArgs, outputChannel);
49 },
50 (reason) => {
51 Log.commandFailed(commandWithArgs, reason, outputChannel);
52 });
53
54 return Q.resolve(spawnResult.spawnedProcess);
55 });
56 }
57
58 /**
59 * Spawns a child process with the params passed
60 * This method waits until the spawned process finishes execution
61 * {command} - The command to be invoked in the child process
62 * {args} - Arguments to be passed to the command
63 * {options} - additional options with which the child process needs to be spawned
64 * {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
65 */
66 public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
67 return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
68 let commandWithArgs = command + " " + args.join(" ");
69 return spawnResult.outcome.then(() => {
70 Log.commandEnded(commandWithArgs, outputChannel);
71 },
72 (reason) => {
73 Log.commandFailed(commandWithArgs, reason, outputChannel);
74 });
75 });
76 }
77
78 /**
79 * Executes a react native command.
80 */
81 public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
82 let runArguments = [command];
83 if (args) {
84 runArguments.concat(args);
85 }
86 return this.spawn(this.getReactCommandName(), runArguments, options, outputChannel);
87 }
88
89 /**
90 * Executes a react native command and waits for its completion.
91 */
92 public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
93 let runArguments = [command];
94 if (args) {
95 runArguments.concat(args);
96 }
97 return this.spawnAndWaitForCompletion(this.getReactCommandName(), runArguments, options, outputChannel);
98 }
99
100 /**
101 * Resolves the dev machine, desktop platform.
102 */
103 private getReactCommandName() {
104 let platform = process.platform;
105 switch (platform) {
106 case "darwin":
107 return "react-native";
108 case "win32":
109 default:
110 return "react-native.cmd";
111 }
112 }
113
114 private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> {
115 let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
116 let commandWithArgs = command + " " + args.join(" ");
117
118 Log.commandStarted(commandWithArgs, outputChannel);
119 let result = new Node.ChildProcess().spawn(command, args, spawnOptions);
120
121 result.stderr.on("data", (data: Buffer) => {
122 if (outputChannel) {
123 outputChannel.append(data.toString());
124 } else {
125 process.stderr.write(data);
126 }
127 });
128
129 result.stdout.on("data", (data: Buffer) => {
130 if (outputChannel) {
131 outputChannel.append(data.toString());
132 } else {
133 process.stdout.write(data);
134 }
135 });
136
137 return Q.resolve(result);
138 }
139}
140