From 8e54e66af903b156fe5b2ff3de90eb94eb3d4d02 Mon Sep 17 00:00:00 2001 From: Ezio Li Date: Thu, 8 Jun 2023 11:21:37 +0800 Subject: [PATCH] Add command to quickly revert open module update (#1961) * Add command to quickly revert open module update * Update --- package.json | 6 ++ package.nls.json | 1 + src/common/error/errorStrings.ts | 4 + src/common/error/internalErrorCode.ts | 1 + src/extension/commands/index.ts | 1 + src/extension/commands/revertOpenModule.ts | 100 +++++++++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 src/extension/commands/revertOpenModule.ts diff --git a/package.json b/package.json index 900346e9d..d771f013f 100644 --- a/package.json +++ b/package.json @@ -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%", diff --git a/package.nls.json b/package.nls.json index 37d239e4c..6713f0407 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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", diff --git a/src/common/error/errorStrings.ts b/src/common/error/errorStrings.ts index 45629794d..efd268a23 100644 --- a/src/common/error/errorStrings.ts +++ b/src/common/error/errorStrings.ts @@ -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", diff --git a/src/common/error/internalErrorCode.ts b/src/common/error/internalErrorCode.ts index 43ed4db77..665091a25 100644 --- a/src/common/error/internalErrorCode.ts +++ b/src/common/error/internalErrorCode.ts @@ -23,6 +23,7 @@ export enum InternalErrorCode { CommandCanceled = 118, FailedToConfigEASBuild = 119, FailedToOpenProjectPage = 120, + FailedToRevertOpenModule = 121, // Device Deployer errors IOSDeployNotFound = 201, diff --git a/src/extension/commands/index.ts b/src/extension/commands/index.ts index 4f0c25926..debfefb2a 100644 --- a/src/extension/commands/index.ts +++ b/src/extension/commands/index.ts @@ -23,3 +23,4 @@ export * from "./stopPackager"; export * from "./testDevEnvironment"; export * from "./configEASBuild"; export * from "./openEASProject"; +export * from "./revertOpenModule"; diff --git a/src/extension/commands/revertOpenModule.ts b/src/extension/commands/revertOpenModule.ts new file mode 100644 index 000000000..08412a14a --- /dev/null +++ b/src/extension/commands/revertOpenModule.ts @@ -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 { + 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(>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.", + ), + ); + } + } +}