microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
transitive-dependency-serialize-javascript

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/enableExpoHermes.ts

62lines · modeblame

29b51a5alexie0111 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
4import * as assert from "assert";
5import * as path from "path";
6import * as fs from "fs";
c8ff508eEzio Li1 years ago7import * as vscode from "vscode";
29b51a5alexie0111 years ago8import { ErrorHelper } from "../../common/error/errorHelper";
9import { InternalErrorCode } from "../../common/error/internalErrorCode";
10import { FileSystem } from "../../common/node/fileSystem";
11import { OutputChannelLogger } from "../log/OutputChannelLogger";
c8ff508eEzio Li1 years ago12import { Command } from "./util/command";
29b51a5alexie0111 years ago13
14const logger = OutputChannelLogger.getMainChannel();
15
16export class EnableExpoHermes extends Command {
17codeName = "expoHermesEnable";
18label = "Enable Expo Hermes";
19error = ErrorHelper.getInternalError(InternalErrorCode.FailedToEnableExpoHermes);
20private nodeFileSystem = new FileSystem();
21
22async baseFn(): Promise<void> {
23assert(this.project);
24const platform = await vscode.window.showQuickPick(["Expo", "Android", "iOS"], {
25placeHolder: "Select platform",
26});
27const jsEngine = await vscode.window.showQuickPick(["hermes", "jsc"], {
28placeHolder: "Select JavaScript engine",
29});
30
31if (!platform || !jsEngine) {
32return;
33}
34const projectRoot = this.project.getPackager().getProjectPath();
35const appJsonPath = path.join(projectRoot, "app.json");
36if (!fs.existsSync(appJsonPath)) {
37logger.warning("app.json not found");
38return;
39}
40const appJson = fs.readFileSync(appJsonPath, "utf-8");
41const regex = new RegExp(
42`"${platform?.toLocaleLowerCase()}":\\s*{[^{}]*"jsEngine":\\s*"[^"]*"`,
43);
44const allMatches = appJson.match(regex);
45
46if (allMatches) {
47const updatedJsEngine = appJson.replace(
48/"jsEngine":\s*"[^"]*/,
49`"jsEngine": "${jsEngine}`,
50);
51await this.nodeFileSystem.writeFile(appJsonPath, updatedJsEngine);
52} else {
53const appJsonObj = JSON.parse(appJson);
54if (platform === "Expo") {
55appJsonObj.expo.jsEngine = jsEngine;
56} else {
57appJsonObj.expo[platform.toLocaleLowerCase()].jsEngine = jsEngine;
58}
59await this.nodeFileSystem.writeFile(appJsonPath, JSON.stringify(appJsonObj, null, 2));
60}
61}
62}