microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a7ecc7013b94dd7e69bb3ff00806822aa8875357

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

255lines · 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
af1474acRedMickey6 years ago4import * as path from "path";
ce5e88eeYuri Skorokhodov5 years ago5import * as cp from "child_process";
34472878RedMickey5 years ago6import { ILogger } from "../extension/log/LogHelper";
7import { NullLogger } from "../extension/log/NullLogger";
8import { ProjectVersionHelper } from "../common/projectVersionHelper";
9import { ISpawnResult } from "./node/childProcess";
10import { HostPlatform, HostPlatformId } from "./hostPlatform";
11import { ErrorHelper } from "./error/errorHelper";
12import { InternalErrorCode } from "./error/internalErrorCode";
e2644f37Yuri Skorokhodov7 years ago13import * as nls from "vscode-nls";
ce5e88eeYuri Skorokhodov5 years ago14import { Node } from "./node/node";
34472878RedMickey5 years ago15nls.config({
16messageFormat: nls.MessageFormat.bundle,
17bundleFormat: nls.BundleFormat.standalone,
18})();
e2644f37Yuri Skorokhodov7 years ago19const localize = nls.loadMessageBundle();
3fb37ad5unknown10 years ago20
831f4a85Patricio Beltran9 years ago21export enum CommandVerbosity {
22OUTPUT,
23SILENT,
24PROGRESS,
25}
26
3fb37ad5unknown10 years ago27interface EnvironmentOptions {
28REACT_DEBUGGER?: string;
29}
30
31interface Options {
32env?: EnvironmentOptions;
831f4a85Patricio Beltran9 years ago33verbosity?: CommandVerbosity;
0d827d9bJimmy Thomson9 years ago34cwd?: string;
3fb37ad5unknown10 years ago35}
36
17161993Meena Kunnathur Balakrishnan10 years ago37export enum CommandStatus {
38Start = 0,
27710197Vladimir Kotikov8 years ago39End = 1,
17161993Meena Kunnathur Balakrishnan10 years ago40}
41
3fb37ad5unknown10 years ago42export class CommandExecutor {
af1474acRedMickey6 years ago43public static ReactNativeCommand: string | null;
9596aa53digeff10 years ago44private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago45
0a68f8dbArtem Egorov8 years ago46constructor(
4dfb1c4cetatanova5 years ago47private nodeModulesRoot: string,
0a68f8dbArtem Egorov8 years ago48private currentWorkingDirectory: string = process.cwd(),
34472878RedMickey5 years ago49private logger: ILogger = new NullLogger(),
50) {}
3fb37ad5unknown10 years ago51
ce5e88eeYuri Skorokhodov5 years ago52public execute(command: string, options: Options = {}): Promise<void> {
0a68f8dbArtem Egorov8 years ago53this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
34472878RedMickey5 years ago54return this.childProcess
55.execToString(command, { cwd: this.currentWorkingDirectory, env: options.env })
56.then(
57stdout => {
58this.logger.info(stdout);
59this.logger.debug(
60CommandExecutor.getCommandStatusString(command, CommandStatus.End),
61);
62},
63(reason: Error) => this.generateRejectionForCommand(command, reason),
64);
3fb37ad5unknown10 years ago65}
66
45944d15Meena Kunnathur Balakrishnan10 years ago67/**
68* Spawns a child process with the params passed
69* This method waits until the spawned process finishes execution
70* {command} - The command to be invoked in the child process
71* {args} - Arguments to be passed to the command
72* {options} - additional options with which the child process needs to be spawned
73*/
ce5e88eeYuri Skorokhodov5 years ago74public spawn(command: string, args: string[], options: Options = {}): Promise<any> {
9596aa53digeff10 years ago75return this.spawnChildProcess(command, args, options).outcome;
45944d15Meena Kunnathur Balakrishnan10 years ago76}
77
6126d899Meena Kunnathur Balakrishnan10 years ago78/**
79* Spawns the React Native packager in a child process.
80*/
9596aa53digeff10 years ago81public spawnReactPackager(args: string[], options: Options = {}): ISpawnResult {
82return this.spawnReactCommand("start", args, options);
6126d899Meena Kunnathur Balakrishnan10 years ago83}
84
ce5e88eeYuri Skorokhodov5 years ago85public getReactNativeVersion(): Promise<string> {
34472878RedMickey5 years ago86return ProjectVersionHelper.getReactNativeVersions(this.currentWorkingDirectory).then(
87versions => versions.reactNativeVersion,
88);
b8a56999Patricio Beltran10 years ago89}
90
c3a987a7Meena Kunnathur Balakrishnan10 years ago91/**
92* Kills the React Native packager in a child process.
93*/
ce5e88eeYuri Skorokhodov5 years ago94public killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago95if (packagerProcess) {
34472878RedMickey5 years ago96return new Promise(resolve => {
a1005420dlebu10 years ago97if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
34472878RedMickey5 years ago98return resolve(
99this.childProcess
100.exec("taskkill /pid " + packagerProcess.pid + " /T /F")
101.then(res => res.outcome),
102);
a1005420dlebu10 years ago103} else {
104packagerProcess.kill();
4dfb1c4cetatanova5 years ago105return resolve(void 0);
a1005420dlebu10 years ago106}
107}).then(() => {
e2644f37Yuri Skorokhodov7 years ago108this.logger.info(localize("PackagerStopped", "Packager stopped"));
a1005420dlebu10 years ago109});
c3a987a7Meena Kunnathur Balakrishnan10 years ago110} else {
e2644f37Yuri Skorokhodov7 years ago111this.logger.warning(localize("PackagerNotFound", "Packager not found"));
ce5e88eeYuri Skorokhodov5 years ago112return Promise.resolve();
c3a987a7Meena Kunnathur Balakrishnan10 years ago113}
114}
115
f8d32439dlebu10 years ago116/**
117* Executes a react native command and waits for its completion.
118*/
34472878RedMickey5 years ago119public spawnReactCommand(
120command: string,
121args: string[] = [],
122options: Options = {},
123): ISpawnResult {
af1474acRedMickey6 years ago124const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI());
94cd5149Artem Egorov8 years ago125return this.spawnChildProcess(reactCommand, [command, ...args], options);
5b0582f3digeff10 years ago126}
127
831f4a85Patricio Beltran9 years ago128/**
129* Spawns a child process with the params passed
130* This method has logic to do while the command is executing
131* {command} - The command to be invoked in the child process
132* {args} - Arguments to be passed to the command
133* {options} - additional options with which the child process needs to be spawned
134*/
34472878RedMickey5 years ago135public spawnWithProgress(
136command: string,
137args: string[],
138options: Options = { verbosity: CommandVerbosity.OUTPUT },
139): Promise<void> {
831f4a85Patricio Beltran9 years ago140const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
141const commandWithArgs = command + " " + args.join(" ");
142const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago143let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago144
145const printDot = () => {
b0af599cJimmy Thomson9 years ago146const now = Date.now();
147if (now - lastDotTime > timeBetweenDots) {
148lastDotTime = now;
0a68f8dbArtem Egorov8 years ago149this.logger.logStream(".", process.stdout);
b0af599cJimmy Thomson9 years ago150}
831f4a85Patricio Beltran9 years ago151};
152
153if (options.verbosity === CommandVerbosity.OUTPUT) {
34472878RedMickey5 years ago154this.logger.debug(
155CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
156);
831f4a85Patricio Beltran9 years ago157}
158
159const result = this.childProcess.spawn(command, args, spawnOptions);
160
161result.stdout.on("data", (data: Buffer) => {
162if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago163this.logger.logStream(data, process.stdout);
b0af599cJimmy Thomson9 years ago164} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago165printDot();
166}
167});
168
169result.stderr.on("data", (data: Buffer) => {
170if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago171this.logger.logStream(data, process.stderr);
b0af599cJimmy Thomson9 years ago172} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago173printDot();
174}
175});
176
ce5e88eeYuri Skorokhodov5 years ago177return new Promise((resolve, reject) => {
178result.outcome = result.outcome.then(
179() => {
180if (options.verbosity === CommandVerbosity.OUTPUT) {
34472878RedMickey5 years ago181this.logger.debug(
182CommandExecutor.getCommandStatusString(
183commandWithArgs,
184CommandStatus.End,
185),
186);
ce5e88eeYuri Skorokhodov5 years ago187}
188this.logger.logStream("\n", process.stdout);
189resolve();
190},
191reason => {
192reject(reason);
193return this.generateRejectionForCommand(commandWithArgs, reason);
34472878RedMickey5 years ago194},
195);
ce5e88eeYuri Skorokhodov5 years ago196});
831f4a85Patricio Beltran9 years ago197}
198
af1474acRedMickey6 years ago199public selectReactNativeCLI(): string {
34472878RedMickey5 years ago200return (
201CommandExecutor.ReactNativeCommand ||
4dfb1c4cetatanova5 years ago202path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native")
34472878RedMickey5 years ago203);
af1474acRedMickey6 years ago204}
205
34472878RedMickey5 years ago206private spawnChildProcess(
207command: string,
208args: string[],
209options: Options = {},
210): ISpawnResult {
9596aa53digeff10 years ago211const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
212const commandWithArgs = command + " " + args.join(" ");
3fb37ad5unknown10 years ago213
34472878RedMickey5 years ago214this.logger.debug(
215CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
216);
9596aa53digeff10 years ago217const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago218
219result.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago220this.logger.logStream(data, process.stderr);
3fb37ad5unknown10 years ago221});
222
223result.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago224this.logger.logStream(data, process.stdout);
3fb37ad5unknown10 years ago225});
226
10873e11digeff10 years ago227result.outcome = result.outcome.then(
9596aa53digeff10 years ago228() =>
34472878RedMickey5 years ago229this.logger.debug(
230CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
231),
232reason => this.generateRejectionForCommand(commandWithArgs, reason),
233);
efa076b0Meena Kunnathur Balakrishnan10 years ago234return result;
3fb37ad5unknown10 years ago235}
10873e11digeff10 years ago236
ce5e88eeYuri Skorokhodov5 years ago237private generateRejectionForCommand(command: string, reason: any): Promise<void> {
34472878RedMickey5 years ago238return Promise.reject<void>(
60ad4ec0JiglioNero5 years ago239reason.errorCode === InternalErrorCode.CommandFailed
240? reason
241: ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command),
34472878RedMickey5 years ago242);
10873e11digeff10 years ago243}
0a68f8dbArtem Egorov8 years ago244
245private static getCommandStatusString(command: string, status: CommandStatus) {
246switch (status) {
247case CommandStatus.Start:
fc602bb6Yuri Skorokhodov7 years ago248return `Executing command: ${command}`;
0a68f8dbArtem Egorov8 years ago249case CommandStatus.End:
fc602bb6Yuri Skorokhodov7 years ago250return `Finished executing: ${command}`;
0a68f8dbArtem Egorov8 years ago251default:
fc602bb6Yuri Skorokhodov7 years ago252throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
0a68f8dbArtem Egorov8 years ago253}
254}
3fb37ad5unknown10 years ago255}