microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

207lines · 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
b3ef3553Meena 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*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago44public spawn(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
45944d15Meena Kunnathur Balakrishnan10 years ago45return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
46let commandWithArgs = command + " " + args.join(" ");
47spawnResult.outcome.then(() => {
48Log.commandEnded(commandWithArgs, outputChannel);
49},
f8d32439dlebu10 years ago50(reason) => {
51Log.commandFailed(commandWithArgs, reason, outputChannel);
52});
45944d15Meena Kunnathur Balakrishnan10 years ago53
54return 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*/
66public spawnAndWaitForCompletion(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
67return this.spawnChildProcess(command, args, options, outputChannel).then(spawnResult => {
68let commandWithArgs = command + " " + args.join(" ");
69return spawnResult.outcome.then(() => {
70Log.commandEnded(commandWithArgs, outputChannel);
71},
f8d32439dlebu10 years ago72(reason) => {
73Log.commandFailed(commandWithArgs, reason, outputChannel);
bc96b26bJimmy Thomson10 years ago74throw reason;
f8d32439dlebu10 years ago75});
45944d15Meena Kunnathur Balakrishnan10 years ago76});
77}
78
f8d32439dlebu10 years ago79/**
80* Executes a react native command.
81*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago82public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
f8d32439dlebu10 years ago83let runArguments = [command];
84if (args) {
f7208a21Jimmy Thomson10 years ago85runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago86}
87return this.spawn(this.getReactCommandName(), runArguments, options, outputChannel);
88}
89
6126d899Meena Kunnathur Balakrishnan10 years ago90/**
91* Spawns the React Native packager in a child process.
92*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago93public spawnReactPackager(args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
94let deferred = Q.defer<ChildProcess>();
6126d899Meena Kunnathur Balakrishnan10 years ago95let command = this.getReactCommandName();
96let runArguments = ["start"];
97
98if (args) {
99runArguments = runArguments.concat(args);
100}
101
102let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
103
c3a987a7Meena Kunnathur Balakrishnan10 years ago104let result = new Node.ChildProcess().spawn(command, runArguments, spawnOptions);
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago105result.spawnedProcess.once("error", (error: any) => {
106deferred.reject({ error: error });
107});
108
c3a987a7Meena Kunnathur Balakrishnan10 years ago109result.stderr.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago110if (outputChannel) {
111outputChannel.append(data.toString());
112} else {
113process.stderr.write(data);
114}
115});
116
c3a987a7Meena Kunnathur Balakrishnan10 years ago117result.stdout.on("data", (data: Buffer) => {
6126d899Meena Kunnathur Balakrishnan10 years ago118if (outputChannel) {
119outputChannel.append(data.toString());
120} else {
121process.stdout.write(data);
122}
123});
124
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago125Q.delay(300).then(() => deferred.resolve(result.spawnedProcess));
126return deferred.promise;
6126d899Meena Kunnathur Balakrishnan10 years ago127}
128
c3a987a7Meena Kunnathur Balakrishnan10 years ago129/**
130* Kills the React Native packager in a child process.
131*/
b3ef3553Meena Kunnathur Balakrishnan10 years ago132public killReactPackager(packagerProcess: ChildProcess, outputChannel?: OutputChannel): void {
c3a987a7Meena Kunnathur Balakrishnan10 years ago133Log.logMessage("Stopping Packager", outputChannel);
134
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago135if (packagerProcess) {
136/* To reliably kill the child process on all versions of Windows,
c3a987a7Meena Kunnathur Balakrishnan10 years ago137* please use taskkill to end the packager process */
138if (process.platform === "win32") {
139new Node.ChildProcess().exec("taskkill /pid " + packagerProcess.pid + " /T /F").outcome.then(() => {
140Log.logMessage("Packager stopped", outputChannel);
141}, function() {
142Log.logError("Failed to exit the React Native packager", outputChannel);
143});
144} else {
145packagerProcess.kill();
146Log.logMessage("Packager stopped", outputChannel);
147}
148
149packagerProcess = null;
150} else {
151Log.logMessage("Packager not found", outputChannel);
152}
153}
154
155
156
f8d32439dlebu10 years ago157/**
158* Executes a react native command and waits for its completion.
159*/
160public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
161let runArguments = [command];
162if (args) {
f7208a21Jimmy Thomson10 years ago163runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago164}
165return this.spawnAndWaitForCompletion(this.getReactCommandName(), runArguments, options, outputChannel);
166}
167
168/**
169* Resolves the dev machine, desktop platform.
170*/
171private getReactCommandName() {
172let platform = process.platform;
173switch (platform) {
174case "darwin":
175return "react-native";
176case "win32":
177default:
178return "react-native.cmd";
179}
180}
181
45944d15Meena Kunnathur Balakrishnan10 years ago182private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> {
08e81a53unknown10 years ago183let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago184let commandWithArgs = command + " " + args.join(" ");
185
3194e9afMeena Kunnathur Balakrishnan10 years ago186Log.commandStarted(commandWithArgs, outputChannel);
c3a987a7Meena Kunnathur Balakrishnan10 years ago187let result = new Node.ChildProcess().spawnWithExitHandler(command, args, spawnOptions);
3fb37ad5unknown10 years ago188
189result.stderr.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago190if (outputChannel) {
191outputChannel.append(data.toString());
192} else {
193process.stderr.write(data);
194}
3fb37ad5unknown10 years ago195});
196
197result.stdout.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago198if (outputChannel) {
199outputChannel.append(data.toString());
200} else {
201process.stdout.write(data);
202}
3fb37ad5unknown10 years ago203});
204
45944d15Meena Kunnathur Balakrishnan10 years ago205return Q.resolve(result);
3fb37ad5unknown10 years ago206}
207}