Skip to content

Commit

Permalink
Add command to quickly revert open module update (#1961)
Browse files Browse the repository at this point in the history
* Add command to quickly revert open module update

* Update
  • Loading branch information
EzioLi01 authored Jun 8, 2023
1 parent 0afa02b commit 8e54e66
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@
"category": "React Native",
"enablement": "!config.security.workspace.trust.enabled || isWorkspaceTrusted"
},
{
"command": "reactNative.revertOpenModule",
"title": "%reactNative.command.revertOpenModule.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 @@ -19,6 +19,7 @@
"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.revertOpenModule.title": "Revert extension input in open package module",
"reactNative.command.showDevMenu.title": "Show Dev Menu",
"reactNative.command.reloadApp.title": "Reload App",
"reactNative.command.runInspector.title": "Run Element Inspector",
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 @@ -146,6 +146,10 @@ export const ERROR_STRINGS = {
"FailedToOpenProjectPage",
"Failed to open EAS project in web page",
),
[InternalErrorCode.FailedToRevertOpenModule]: localize(
"FailedToRevertOpenModule",
"Failed to revert Open module",
),
[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 @@ -23,6 +23,7 @@ export enum InternalErrorCode {
CommandCanceled = 118,
FailedToConfigEASBuild = 119,
FailedToOpenProjectPage = 120,
FailedToRevertOpenModule = 121,

// 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 @@ -23,3 +23,4 @@ export * from "./stopPackager";
export * from "./testDevEnvironment";
export * from "./configEASBuild";
export * from "./openEASProject";
export * from "./revertOpenModule";
100 changes: 100 additions & 0 deletions src/extension/commands/revertOpenModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 path from "path";
import * as fs from "fs";
import * as semver from "semver";
import * as nls from "vscode-nls";
import { OutputChannelLogger } from "../log/OutputChannelLogger";
import { ErrorHelper } from "../../common/error/errorHelper";
import { InternalErrorCode } from "../../common/error/internalErrorCode";
import { ProjectVersionHelper } from "../../common/projectVersionHelper";
import { findFileInFolderHierarchy } from "../../common/extensionHelper";
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 RevertOpenModule extends ReactNativeCommand {
nodeModulesRoot: string;
codeName = "revertOpenModule";
label = "Revert extension input in open package module";
error = ErrorHelper.getInternalError(InternalErrorCode.FailedToRevertOpenModule);

async baseFn(): Promise<void> {
assert(this.project);
const NODE_MODULES_FODLER_NAME = "node_modules";
const projectRootPath = this.project.getWorkspaceFolder().uri.fsPath;

const packageJsonPath = findFileInFolderHierarchy(projectRootPath, "package.json");
const rnVersion = packageJsonPath
? JSON.parse(fs.readFileSync(packageJsonPath, "utf-8")).dependencies["react-native"]
: null;

const OPN_PACKAGE_NAME =
semver.gte(rnVersion, "0.60.0") || ProjectVersionHelper.isCanaryVersion(rnVersion)
? "open"
: "opn";

const openModulePath = path.resolve(
projectRootPath,
NODE_MODULES_FODLER_NAME,
OPN_PACKAGE_NAME,
);

if (fs.existsSync(openModulePath)) {
const mainFilePath = path.resolve(openModulePath, "open-main.js");
if (fs.existsSync(mainFilePath)) {
try {
await fs.unlinkSync(mainFilePath);
} catch {
logger.error(
localize("FailedToDeleteMainFile", "Failed to delete open-main.js file."),
);
}
} else {
logger.info(
localize(
"NotFindMainFile",
"Not find open-main.js file in open module, skip main file deleting.",
),
);
}

const packageFilePath = path.resolve(openModulePath, "package.json");
const packageJson = JSON.parse(fs.readFileSync(packageFilePath, "utf-8"));
if (packageJson.main == "open-main.js") {
try {
delete packageJson.main;
await fs.writeFileSync(
packageFilePath,
JSON.stringify(<Record<string, any>>packageJson),
);
} catch {
logger.error(localize("FailedToDeleteEntry", "Failed to delete main enrty."));
}
} else {
logger.info(
localize(
"NotFindMainEntry",
"Not find main entry in package.json file, skip entry deleting.",
),
);
}
logger.info(localize("CompleteOpenModuleCleaUp", "Open module clean up is complete."));
} else {
logger.error(
localize(
"NotFindOpenModule",
"Unable to find open module in your project. Please check it again.",
),
);
}
}
}

0 comments on commit 8e54e66

Please sign in to comment.