microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
acf08bc211b251f5aae90239291dbef21894989c

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

191lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
2e15926eMeena Kunnathur Balakrishnan10 years ago4import {ChildProcess} from "child_process";
3fb37ad5unknown10 years ago5import {Log} from "./log";
b0061ac6Meena Kunnathur Balakrishnan10 years ago6import {Node} from "./node/node";
7import {ISpawnResult} from "./node/childProcess";
5e5bf86eMeena Kunnathur Balakrishnan10 years ago8import {OutputChannel} from "vscode";
2e15926eMeena Kunnathur Balakrishnan10 years ago9import * as Q from "q";
3fb37ad5unknown10 years ago10
11interface EnvironmentOptions {
12REACT_DEBUGGER?: string;
13}
14
15interface Options {
16env?: EnvironmentOptions;
17}
18
19export class CommandExecutor {
20private currentWorkingDirectory: string;
21
110558c0Jimmy Thomson10 years ago22constructor(currentWorkingDirectory?: string) {
3fb37ad5unknown10 years ago23this.currentWorkingDirectory = currentWorkingDirectory;
24}
25
fb2bae06Daniel10 years ago26public execute(command: string, options: Options = {}): Q.Promise<void> {
bedf110funknown10 years ago27Log.commandStarted(command);
28return new Node.ChildProcess().execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
3fb37ad5unknown10 years ago29.then(stdout => {
bedf110funknown10 years ago30Log.logMessage(stdout);
31Log.commandEnded(command);
3fb37ad5unknown10 years ago32},
bedf110funknown10 years ago33reason => Log.commandFailed(command, reason));
3fb37ad5unknown10 years ago34}
35
45944d15Meena Kunnathur Balakrishnan10 years ago36/**
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*/
5b0582f3digeff10 years ago44public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess {
45return this.spawnChildProcess(command, args, options, outputChannel).spawnedProcess;
45944d15Meena Kunnathur Balakrishnan10 years ago46}
47
48/**
49* Spawns a child process with the params passed
50* This method waits until the spawned process finishes execution
51* {command} - The command to be invoked in the child process
52* {args} - Arguments to be passed to the command
53* {options} - additional options with which the child process needs to be spawned
54* {outputChannel} - optional object of type vscode.OutputChannel where logs need to be printed
55*/
3d69a9b4digeff10 years ago56public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
5b0582f3digeff10 years ago57return this.spawnChildProcess(command, args, options, outputChannel).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago58}
59
5b0582f3digeff10 years ago60public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ChildProcess {
61return this.spawnChildReactCommandProcess(command, args, options, outputChannel).spawnedProcess;
f8d32439dlebu10 years ago62}
63
6126d899Meena Kunnathur Balakrishnan10 years ago64/**
65* Spawns the React Native packager in a child process.
66*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago67public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
68let deferred = Q.defer<ChildProcess>();
6126d899Meena Kunnathur Balakrishnan10 years ago69let command = this.getReactCommandName();
70let runArguments = ["start"];
71
72if (args) {
73runArguments = runArguments.concat(args);
74}
75
76let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
77
c3a987a7Meena Kunnathur Balakrishnan10 years ago78let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions);
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago79result.spawnedProcess.once("error", (error: any) => {
80deferred.reject({ error: error });
81});
82
c3a987a7Meena Kunnathur Balakrishnan10 years ago83result.stderr.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago84if (outputChannel) {
85outputChannel.append(data.toString());
86} else {
87process.stderr.write(data);
88}
89});
90
c3a987a7Meena Kunnathur Balakrishnan10 years ago91result.stdout.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago92if (outputChannel) {
93outputChannel.append(data.toString());
94} else {
95process.stdout.write(data);
96}
97});
98
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago99Q.delay(300).then(() => deferred.resolve(result.spawnedProcess));
100return deferred.promise;
6126d899Meena Kunnathur Balakrishnan10 years ago101}
102
c3a987a7Meena Kunnathur Balakrishnan10 years ago103/**
104* Kills the React Native packager in a child process.
105*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago106public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): void {
c3a987a7Meena Kunnathur Balakrishnan10 years ago107Log.logMessage("Stopping Packager", outputChannel);
108
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago109if (packagerProcess) {
110/* To reliably kill the child process on all versions of Windows,
c3a987a7Meena Kunnathur Balakrishnan10 years ago111* please use taskkill to end the packager process */
112if (process.platform === "win32") {
113new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => {
114Log.logMessage("Packager stopped", outputChannel);
115}, function() {
116Log.logError("Failed to exit the React Native packager", outputChannel);
117});
118} else {
119packagerProcess.kill();
120Log.logMessage("Packager stopped", outputChannel);
121}
122
123packagerProcess = null;
124} else {
125Log.logMessage("Packager not found", outputChannel);
126}
127}
128
129
130
f8d32439dlebu10 years ago131/**
132* Executes a react native command and waits for its completion.
133*/
3d69a9b4digeff10 years ago134public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
5b0582f3digeff10 years ago135return this.spawnChildReactCommandProcess(command, args, options, outputChannel).outcome;
136}
137
138public spawnChildReactCommandProcess(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
f8d32439dlebu10 years ago139let runArguments = [command];
140if (args) {
f7208a21Jimmy Thomson10 years ago141runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago142}
5b0582f3digeff10 years ago143return this.spawnChildProcess(this.getReactCommandName(), runArguments, options, outputChannel);
f8d32439dlebu10 years ago144}
145
146/**
147* Resolves the dev machine, desktop platform.
148*/
149private getReactCommandName() {
150let platform = process.platform;
151switch (platform) {
152case "darwin":
153return "react-native";
154case "win32":
155default:
156return "react-native.cmd";
157}
158}
159
5b0582f3digeff10 years ago160private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): ISpawnResult {
08e81a53unknown10 years ago161let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago162let commandWithArgs = command + " " + args.join(" ");
163
3194e9afMeena Kunnathur Balakrishnan10 years ago164Log.commandStarted(commandWithArgs, outputChannel);
c3a987a7Meena Kunnathur Balakrishnan10 years ago165let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions);
3fb37ad5unknown10 years ago166
167result.stderr.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago168if (outputChannel) {
169outputChannel.append(data.toString());
170} else {
171process.stderr.write(data);
172}
3fb37ad5unknown10 years ago173});
174
175result.stdout.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago176if (outputChannel) {
177outputChannel.append(data.toString());
178} else {
179process.stdout.write(data);
180}
3fb37ad5unknown10 years ago181});
182
5b0582f3digeff10 years ago183result.outcome.done(() => {
184Log.commandEnded(commandWithArgs, outputChannel);
185}, (reason) => {
186Log.commandFailed(commandWithArgs, reason, outputChannel);
187});
188
189return result;
3fb37ad5unknown10 years ago190}
191}