microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/commandExecutor.ts

241lines · 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";
09f6024fHeniker4 years ago6import * as nls from "vscode-nls";
34472878RedMickey5 years ago7import { ILogger } from "../extension/log/LogHelper";
8import { NullLogger } from "../extension/log/NullLogger";
09f6024fHeniker4 years ago9import { ProjectVersionHelper } from "./projectVersionHelper";
34472878RedMickey5 years ago10import { ISpawnResult } from "./node/childProcess";
11import { HostPlatform, HostPlatformId } from "./hostPlatform";
12import { ErrorHelper } from "./error/errorHelper";
13import { InternalErrorCode } from "./error/internalErrorCode";
ce5e88eeYuri Skorokhodov5 years ago14import { Node } from "./node/node";
09f6024fHeniker4 years ago15
34472878RedMickey5 years ago16nls.config({
17messageFormat: nls.MessageFormat.bundle,
18bundleFormat: nls.BundleFormat.standalone,
19})();
e2644f37Yuri Skorokhodov7 years ago20const localize = nls.loadMessageBundle();
3fb37ad5unknown10 years ago21
831f4a85Patricio Beltran9 years ago22export enum CommandVerbosity {
23OUTPUT,
24SILENT,
25PROGRESS,
26}
27
3fb37ad5unknown10 years ago28interface EnvironmentOptions {
29REACT_DEBUGGER?: string;
30}
31
32interface Options {
33env?: EnvironmentOptions;
831f4a85Patricio Beltran9 years ago34verbosity?: CommandVerbosity;
0d827d9bJimmy Thomson9 years ago35cwd?: string;
3fb37ad5unknown10 years ago36}
37
17161993Meena Kunnathur Balakrishnan10 years ago38export enum CommandStatus {
39Start = 0,
27710197Vladimir Kotikov8 years ago40End = 1,
17161993Meena Kunnathur Balakrishnan10 years ago41}
42
3fb37ad5unknown10 years ago43export class CommandExecutor {
af1474acRedMickey6 years ago44public static ReactNativeCommand: string | null;
9596aa53digeff10 years ago45private childProcess = new Node.ChildProcess();
3fb37ad5unknown10 years ago46
0a68f8dbArtem Egorov8 years ago47constructor(
4dfb1c4cetatanova5 years ago48private nodeModulesRoot: string,
0a68f8dbArtem Egorov8 years ago49private currentWorkingDirectory: string = process.cwd(),
34472878RedMickey5 years ago50private logger: ILogger = new NullLogger(),
51) {}
3fb37ad5unknown10 years ago52
0d77292aJiglioNero4 years ago53public async execute(command: string, options: Options = {}): Promise<void> {
0a68f8dbArtem Egorov8 years ago54this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.Start));
0d77292aJiglioNero4 years ago55try {
56const stdout = await this.childProcess.execToString(command, {
57cwd: this.currentWorkingDirectory,
58env: options.env,
59});
60this.logger.info(stdout);
61this.logger.debug(CommandExecutor.getCommandStatusString(command, CommandStatus.End));
62} catch (reason) {
63return 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
0d77292aJiglioNero4 years ago85public async getReactNativeVersion(): Promise<string> {
86const versions = await ProjectVersionHelper.getReactNativeVersions(
87this.currentWorkingDirectory,
34472878RedMickey5 years ago88);
0d77292aJiglioNero4 years ago89return versions.reactNativeVersion;
b8a56999Patricio Beltran9 years ago90}
91
c3a987a7Meena Kunnathur Balakrishnan10 years ago92/**
93* Kills the React Native packager in a child process.
94*/
0d77292aJiglioNero4 years ago95public async killReactPackager(packagerProcess?: cp.ChildProcess): Promise<void> {
c9b4fa6cMeena Kunnathur Balakrishnan10 years ago96if (packagerProcess) {
0d77292aJiglioNero4 years ago97if (HostPlatform.getPlatformId() === HostPlatformId.WINDOWS) {
98const res = await this.childProcess.exec(
09f6024fHeniker4 years ago99`taskkill /pid ${packagerProcess.pid} /T /F`,
0d77292aJiglioNero4 years ago100);
101await res.outcome;
102} else {
103packagerProcess.kill();
104}
105this.logger.info(localize("PackagerStopped", "Packager stopped"));
c3a987a7Meena Kunnathur Balakrishnan10 years ago106} else {
e2644f37Yuri Skorokhodov7 years ago107this.logger.warning(localize("PackagerNotFound", "Packager not found"));
c3a987a7Meena Kunnathur Balakrishnan10 years ago108}
109}
110
f8d32439dlebu10 years ago111/**
112* Executes a react native command and waits for its completion.
113*/
34472878RedMickey5 years ago114public spawnReactCommand(
115command: string,
116args: string[] = [],
117options: Options = {},
118): ISpawnResult {
af1474acRedMickey6 years ago119const reactCommand = HostPlatform.getNpmCliCommand(this.selectReactNativeCLI());
94cd5149Artem Egorov8 years ago120return this.spawnChildProcess(reactCommand, [command, ...args], options);
5b0582f3digeff10 years ago121}
122
831f4a85Patricio Beltran9 years ago123/**
124* Spawns a child process with the params passed
125* This method has logic to do while the command is executing
126* {command} - The command to be invoked in the child process
127* {args} - Arguments to be passed to the command
128* {options} - additional options with which the child process needs to be spawned
129*/
0d77292aJiglioNero4 years ago130public async spawnWithProgress(
34472878RedMickey5 years ago131command: string,
132args: string[],
133options: Options = { verbosity: CommandVerbosity.OUTPUT },
134): Promise<void> {
831f4a85Patricio Beltran9 years ago135const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
09f6024fHeniker4 years ago136const commandWithArgs = `${command} ${args.join(" ")}`;
831f4a85Patricio Beltran9 years ago137const timeBetweenDots = 1500;
b0af599cJimmy Thomson9 years ago138let lastDotTime = 0;
831f4a85Patricio Beltran9 years ago139
140const printDot = () => {
b0af599cJimmy Thomson9 years ago141const now = Date.now();
142if (now - lastDotTime > timeBetweenDots) {
143lastDotTime = now;
0a68f8dbArtem Egorov8 years ago144this.logger.logStream(".", process.stdout);
b0af599cJimmy Thomson9 years ago145}
831f4a85Patricio Beltran9 years ago146};
147
148if (options.verbosity === CommandVerbosity.OUTPUT) {
34472878RedMickey5 years ago149this.logger.debug(
150CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
151);
831f4a85Patricio Beltran9 years ago152}
153
154const result = this.childProcess.spawn(command, args, spawnOptions);
155
156result.stdout.on("data", (data: Buffer) => {
157if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago158this.logger.logStream(data, process.stdout);
b0af599cJimmy Thomson9 years ago159} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago160printDot();
161}
162});
163
164result.stderr.on("data", (data: Buffer) => {
165if (options.verbosity === CommandVerbosity.OUTPUT) {
0a68f8dbArtem Egorov8 years ago166this.logger.logStream(data, process.stderr);
b0af599cJimmy Thomson9 years ago167} else if (options.verbosity === CommandVerbosity.PROGRESS) {
831f4a85Patricio Beltran9 years ago168printDot();
169}
170});
171
0d77292aJiglioNero4 years ago172try {
173await result.outcome;
174if (options.verbosity === CommandVerbosity.OUTPUT) {
175this.logger.debug(
176CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
177);
178}
179this.logger.logStream("\n", process.stdout);
180} catch (reason) {
181return this.generateRejectionForCommand(commandWithArgs, reason);
182}
831f4a85Patricio Beltran9 years ago183}
184
af1474acRedMickey6 years ago185public selectReactNativeCLI(): string {
34472878RedMickey5 years ago186return (
187CommandExecutor.ReactNativeCommand ||
4dfb1c4cetatanova5 years ago188path.resolve(this.nodeModulesRoot, "node_modules", ".bin", "react-native")
34472878RedMickey5 years ago189);
af1474acRedMickey6 years ago190}
191
34472878RedMickey5 years ago192private spawnChildProcess(
193command: string,
194args: string[],
195options: Options = {},
196): ISpawnResult {
9596aa53digeff10 years ago197const spawnOptions = Object.assign({}, { cwd: this.currentWorkingDirectory }, options);
09f6024fHeniker4 years ago198const commandWithArgs = `${command} ${args.join(" ")}`;
3fb37ad5unknown10 years ago199
34472878RedMickey5 years ago200this.logger.debug(
201CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.Start),
202);
9596aa53digeff10 years ago203const result = this.childProcess.spawn(command, args, spawnOptions);
3fb37ad5unknown10 years ago204
205result.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago206this.logger.logStream(data, process.stderr);
3fb37ad5unknown10 years ago207});
208
209result.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago210this.logger.logStream(data, process.stdout);
3fb37ad5unknown10 years ago211});
212
10873e11digeff10 years ago213result.outcome = result.outcome.then(
9596aa53digeff10 years ago214() =>
34472878RedMickey5 years ago215this.logger.debug(
216CommandExecutor.getCommandStatusString(commandWithArgs, CommandStatus.End),
217),
218reason => this.generateRejectionForCommand(commandWithArgs, reason),
219);
efa076b0Meena Kunnathur Balakrishnan10 years ago220return result;
3fb37ad5unknown10 years ago221}
10873e11digeff10 years ago222
ce5e88eeYuri Skorokhodov5 years ago223private generateRejectionForCommand(command: string, reason: any): Promise<void> {
34472878RedMickey5 years ago224return Promise.reject<void>(
60ad4ec0JiglioNero5 years ago225reason.errorCode === InternalErrorCode.CommandFailed
226? reason
227: ErrorHelper.getNestedError(reason, InternalErrorCode.CommandFailed, command),
34472878RedMickey5 years ago228);
10873e11digeff10 years ago229}
0a68f8dbArtem Egorov8 years ago230
231private static getCommandStatusString(command: string, status: CommandStatus) {
232switch (status) {
233case CommandStatus.Start:
fc602bb6Yuri Skorokhodov7 years ago234return `Executing command: ${command}`;
0a68f8dbArtem Egorov8 years ago235case CommandStatus.End:
fc602bb6Yuri Skorokhodov7 years ago236return `Finished executing: ${command}`;
0a68f8dbArtem Egorov8 years ago237default:
fc602bb6Yuri Skorokhodov7 years ago238throw ErrorHelper.getInternalError(InternalErrorCode.UnsupportedCommandStatus);
0a68f8dbArtem Egorov8 years ago239}
240}
3fb37ad5unknown10 years ago241}