Skip to content

Commit

Permalink
Support Expo EAS command: open the project page in a web browser (#1959)
Browse files Browse the repository at this point in the history
* Add EAS command: open project page in web browser

* Update

* Update

* Update

* Update
  • Loading branch information
EzioLi01 committed May 26, 2023
1 parent a41b2c1 commit 57fee98
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@
"category": "React Native",
"enablement": "!config.security.workspace.trust.enabled || isWorkspaceTrusted"
},
{
"command": "reactNative.openEASProjectInWebPage",
"title": "%reactNative.command.openEASProjectInWebPage.title%",
"category": "React Native",
"enablement": "!config.security.workspace.trust.enabled || isWorkspaceTrusted"
},
{
"command": "reactNative.showDevMenu",
"title": "%reactNative.command.showDevMenu.title%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"reactNative.command.restartPackager.title": "Restart Packager",
"reactNative.command.publishToExpHost.title": "Publish to Expo",
"reactNative.command.createExpoEASBuildConfigFile.title": "Create EAS config file for Expo",
"reactNative.command.openEASProjectInWebPage.title": "Open the EAS project in a web page",
"reactNative.command.showDevMenu.title": "Show Dev Menu",
"reactNative.command.reloadApp.title": "Reload App",
"reactNative.command.runInspector.title": "Run Element Inspector",
Expand Down
12 changes: 12 additions & 0 deletions src/common/commandExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ export class CommandExecutor {
}
}

public async executeToString(command: string, options: Options = {}): Promise<string> {
try {
const stdout = await this.childProcess.execToString(command, {
cwd: this.currentWorkingDirectory,
env: options.env,
});
return stdout;
} catch (reason) {
return reason;
}
}

/**
* Spawns a child process with the params passed
* This method waits until the spawned process finishes execution
Expand Down
4 changes: 4 additions & 0 deletions src/common/error/errorStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ export const ERROR_STRINGS = {
"FailedToConfigEASBuild",
"Failed to config Expo app with EAS build",
),
[InternalErrorCode.FailedToOpenProjectPage]: localize(
"FailedToOpenProjectPage",
"Failed to open EAS project in web page",
),
[InternalErrorCode.FailedToStartPackager]: localize(
"FailedToStartPackager",
"Failed to start the React Native packager",
Expand Down
1 change: 1 addition & 0 deletions src/common/error/internalErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum InternalErrorCode {
FailedToTestDevEnvironment = 117,
CommandCanceled = 118,
FailedToConfigEASBuild = 119,
FailedToOpenProjectPage = 120,

// Device Deployer errors
IOSDeployNotFound = 201,
Expand Down
1 change: 1 addition & 0 deletions src/extension/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export * from "./stopLogCatMonitor";
export * from "./stopPackager";
export * from "./testDevEnvironment";
export * from "./configEASBuild";
export * from "./openEASProject";
68 changes: 68 additions & 0 deletions src/extension/commands/openEASProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

import * as assert from "assert";
import * as nls from "vscode-nls";
import * as vscode from "vscode";
import { OutputChannelLogger } from "../log/OutputChannelLogger";
import { ErrorHelper } from "../../common/error/errorHelper";
import { InternalErrorCode } from "../../common/error/internalErrorCode";
import { ExponentHelper } from "../exponent/exponentHelper";
import { ReactNativeCommand } from "./util/reactNativeCommand";

nls.config({
messageFormat: nls.MessageFormat.bundle,
bundleFormat: nls.BundleFormat.standalone,
})();
const localize = nls.loadMessageBundle();
const logger = OutputChannelLogger.getMainChannel();

export class OpenEASProject extends ReactNativeCommand {
nodeModulesRoot: string;
codeName = "openEASProjectInWebPage";
label = "Open the eas project in a web page";
error = ErrorHelper.getInternalError(InternalErrorCode.FailedToOpenProjectPage);

async baseFn(): Promise<void> {
assert(this.project);
const projectRootPath = this.project.getWorkspaceFolder().uri.fsPath;
const expoHelper = new ExponentHelper(projectRootPath, projectRootPath);
const isExpo = await expoHelper.isExpoManagedApp(true);

if (isExpo) {
try {
let id = null;
await expoHelper.getExpoEasProjectId().then(result => {
id = result;
});
let owner = null;
await expoHelper.getExpoEasProjectOwner().then(result => {
owner = result;
});
let name = null;
await expoHelper.getExpoEasProjectName().then(result => {
name = result;
});
if (id == null || owner == null) {
const error = localize(
"ExpoProjectNotLinkToEAS",
"Your app not link to EAS project. Please run 'eas init' firstly to bind your app to EAS project.",
);
void vscode.window.showErrorMessage(error);
logger.error(error);
} else if (name != null) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const url = `https://expo.dev/accounts/${owner}/projects/${name}`;
await vscode.env.openExternal(vscode.Uri.parse(url));
}
} catch {
logger.error(
localize(
"NoExistingEASProject",
"Unable to find existing EAS project. Please run 'eas init' firstly to bind your app to EAS project.",
),
);
}
}
}
}
35 changes: 35 additions & 0 deletions src/extension/exponent/exponentHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// <reference path="exponentHelper.d.ts" />

import * as path from "path";
import * as fs from "fs";
import * as semver from "semver";
import * as vscode from "vscode";
import { sync as globSync } from "glob";
Expand Down Expand Up @@ -504,4 +505,38 @@ require('${entryPoint}');`;
});
}
}

public async getExpoEasProjectOwner(): Promise<string | null> {
const appJsonPath = this.pathToFileInWorkspace(APP_JSON);
try {
return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.owner == undefined
? null
: JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.owner;
} catch {
return null;
}
}

public async getExpoEasProjectId(): Promise<string | null> {
const appJsonPath = this.pathToFileInWorkspace(APP_JSON);
try {
return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.extra.eas.projectId ==
undefined
? null
: JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.extra.eas.projectId;
} catch {
return null;
}
}

public async getExpoEasProjectName(): Promise<string | null> {
const appJsonPath = this.pathToFileInWorkspace(APP_JSON);
try {
return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.name == undefined
? null
: JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.name;
} catch {
return null;
}
}
}

0 comments on commit 57fee98

Please sign in to comment.