microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e034b2f8ed9e645a9f8ffe314b40f4e0b988e473

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

140lines · 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*/
2e15926eMeena 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*/
82public spawnReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ChildProcess> {
83let runArguments = [command];
84if (args) {
f7208a21Jimmy Thomson10 years ago85runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago86}
87return this.spawn(this.getReactCommandName(), runArguments, options, outputChannel);
88}
89
90/**
91* Executes a react native command and waits for its completion.
92*/
93public spawnAndWaitReactCommand(command: string, args?: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<void> {
94let runArguments = [command];
95if (args) {
f7208a21Jimmy Thomson10 years ago96runArguments = runArguments.concat(args);
f8d32439dlebu10 years ago97}
98return this.spawnAndWaitForCompletion(this.getReactCommandName(), runArguments, options, outputChannel);
99}
100
101/**
102* Resolves the dev machine, desktop platform.
103*/
104private getReactCommandName() {
105let platform = process.platform;
106switch (platform) {
107case "darwin":
108return "react-native";
109case "win32":
110default:
111return "react-native.cmd";
112}
113}
114
45944d15Meena Kunnathur Balakrishnan10 years ago115private spawnChildProcess(command: string, args: string[], options: Options = {}, outputChannel?: OutputChannel): Q.Promise<ISpawnResult> {
08e81a53unknown10 years ago116let spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
3fb37ad5unknown10 years ago117let commandWithArgs = command + " " + args.join(" ");
118
3194e9afMeena Kunnathur Balakrishnan10 years ago119Log.commandStarted(commandWithArgs, outputChannel);
3fb37ad5unknown10 years ago120let result = new Node.ChildProcess().spawn(command, args, spawnOptions);
121
122result.stderr.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago123if (outputChannel) {
124outputChannel.append(data.toString());
125} else {
126process.stderr.write(data);
127}
3fb37ad5unknown10 years ago128});
129
130result.stdout.on("data", (data: Buffer) => {
5e5bf86eMeena Kunnathur Balakrishnan10 years ago131if (outputChannel) {
132outputChannel.append(data.toString());
133} else {
134process.stdout.write(data);
135}
3fb37ad5unknown10 years ago136});
137
45944d15Meena Kunnathur Balakrishnan10 years ago138return Q.resolve(result);
3fb37ad5unknown10 years ago139}
140}