microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/commands/enableExpoHermes.ts
62lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import assert = require("assert"); |
| 5 | import * as path from "path"; |
| 6 | import * as fs from "fs"; |
| 7 | import * as vscode from "vscode"; |
| 8 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 9 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 10 | import { FileSystem } from "../../common/node/fileSystem"; |
| 11 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 12 | import { Command } from "./util/command"; |
| 13 | |
| 14 | const logger = OutputChannelLogger.getMainChannel(); |
| 15 | |
| 16 | export class EnableExpoHermes extends Command { |
| 17 | codeName = "expoHermesEnable"; |
| 18 | label = "Enable Expo Hermes"; |
| 19 | error = ErrorHelper.getInternalError(InternalErrorCode.FailedToEnableExpoHermes); |
| 20 | private nodeFileSystem = new FileSystem(); |
| 21 | |
| 22 | async baseFn(): Promise<void> { |
| 23 | assert(this.project); |
| 24 | const platform = await vscode.window.showQuickPick(["Expo", "Android", "iOS"], { |
| 25 | placeHolder: "Select platform", |
| 26 | }); |
| 27 | const jsEngine = await vscode.window.showQuickPick(["hermes", "jsc"], { |
| 28 | placeHolder: "Select JavaScript engine", |
| 29 | }); |
| 30 | |
| 31 | if (!platform || !jsEngine) { |
| 32 | return; |
| 33 | } |
| 34 | const projectRoot = this.project.getPackager().getProjectPath(); |
| 35 | const appJsonPath = path.join(projectRoot, "app.json"); |
| 36 | if (!fs.existsSync(appJsonPath)) { |
| 37 | logger.warning("app.json not found"); |
| 38 | return; |
| 39 | } |
| 40 | const appJson = fs.readFileSync(appJsonPath, "utf-8"); |
| 41 | const regex = new RegExp( |
| 42 | `"${platform?.toLocaleLowerCase()}":\\s*{[^{}]*"jsEngine":\\s*"[^"]*"`, |
| 43 | ); |
| 44 | const allMatches = appJson.match(regex); |
| 45 | |
| 46 | if (allMatches) { |
| 47 | const updatedJsEngine = appJson.replace( |
| 48 | /"jsEngine":\s*"[^"]*/, |
| 49 | `"jsEngine": "${jsEngine}`, |
| 50 | ); |
| 51 | await this.nodeFileSystem.writeFile(appJsonPath, updatedJsEngine); |
| 52 | } else { |
| 53 | const appJsonObj = JSON.parse(appJson); |
| 54 | if (platform === "Expo") { |
| 55 | appJsonObj.expo.jsEngine = jsEngine; |
| 56 | } else { |
| 57 | appJsonObj.expo[platform.toLocaleLowerCase()].jsEngine = jsEngine; |
| 58 | } |
| 59 | await this.nodeFileSystem.writeFile(appJsonPath, JSON.stringify(appJsonObj, null, 2)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |