microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
indexed-sourcemap-null-section-issue

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/enableHermes.ts

95lines · modeblame

4a15a560lexie0111 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
176f99c8ConnorQi014 months ago4import assert = require("assert");
4a15a560lexie0111 years ago5import * as path from "path";
6import * as fs from "fs";
b526468dEzio Li1 years ago7import * as vscode from "vscode";
4a15a560lexie0111 years ago8import { ErrorHelper } from "../../common/error/errorHelper";
9import { InternalErrorCode } from "../../common/error/internalErrorCode";
10import { FileSystem } from "../../common/node/fileSystem";
11import { OutputChannelLogger } from "../log/OutputChannelLogger";
12import { CommandExecutor } from "../../common/commandExecutor";
13import { AppLauncher } from "../appLauncher";
b526468dEzio Li1 years ago14import { Command } from "./util/command";
4a15a560lexie0111 years ago15
16const logger = OutputChannelLogger.getMainChannel();
17
18export class EnableHermes extends Command {
19codeName = "hermesEnable";
20label = "Enable Hermes";
21error = ErrorHelper.getInternalError(InternalErrorCode.FailedToEnableHermes);
22private nodeFileSystem = new FileSystem();
23
24async baseFn(): Promise<void> {
25assert(this.project);
26const type = await vscode.window.showQuickPick(["Android", "iOS"], {
27placeHolder: "Select type for mobile OS",
28});
29const isHermesEnabled = await vscode.window.showQuickPick(["true", "false"], {
30placeHolder: "Whether to enable Hermes",
31});
32const projectRoot = this.project.getPackager().getProjectPath();
33
34if (type === undefined || isHermesEnabled === undefined) return;
35if (type === "iOS") {
36const podfilePath = path.join(projectRoot, "ios", "Podfile");
37if (!projectRoot || !fs.existsSync(podfilePath)) {
38logger.warning("Podfile not found");
39return;
40}
41
42const podfileContent = fs.readFileSync(podfilePath, "utf-8");
43const hermesMatches = podfileContent.match(/#?\s*:hermes_enabled\s*=>\s*\w*/);
44const regex = /(use_react_native!\s*\([^)]*?)(\n\s*\))/;
45const rnMatches = podfileContent.match(regex);
46const nodeModulesRoot: string =
47AppLauncher.getNodeModulesRootByProjectPath(projectRoot);
48const commandExecutor = new CommandExecutor(
49nodeModulesRoot,
50`${projectRoot}/ios`,
51logger,
52);
53
54if (hermesMatches && !hermesMatches[0].startsWith("#")) {
55const updatedHermes = podfileContent.replace(
56/:hermes_enabled\s*=>\s*\w+/,
57`:hermes_enabled => ${isHermesEnabled}`,
58);
59await this.nodeFileSystem.writeFile(podfilePath, updatedHermes);
60await commandExecutor.spawn("pod", ["install"]);
b526468dEzio Li1 years ago61} else if (rnMatches) {
62let content = rnMatches[1];
63const closing = rnMatches[2];
4a15a560lexie0111 years ago64
b526468dEzio Li1 years ago65if (!content.trim().endsWith(",")) {
66content += ",";
4a15a560lexie0111 years ago67}
b526468dEzio Li1 years ago68content += `\n :hermes_enabled => ${isHermesEnabled}`;
69const newData = podfileContent.replace(regex, content + closing);
70await this.nodeFileSystem.writeFile(podfilePath, newData);
71await commandExecutor.spawn("pod", ["install"]);
4a15a560lexie0111 years ago72}
73}
74if (type === "Android") {
75const gradleFilePath = path.join(projectRoot, "android", "gradle.properties");
76if (!projectRoot || !fs.existsSync(gradleFilePath)) {
77logger.warning("gradle.properties file not found");
78return;
79}
80
81const gradleFileContent = fs.readFileSync(gradleFilePath, "utf-8");
82const hermesMatches = gradleFileContent.match(/hermesEnabled\s*=\s*\w*/);
83if (hermesMatches && !hermesMatches[0].startsWith("#")) {
84const updatedHermes = gradleFileContent.replace(
85/hermesEnabled\s*=\s*\w*/,
86`hermesEnabled=${isHermesEnabled}`,
87);
88await this.nodeFileSystem.writeFile(gradleFilePath, updatedHermes);
89} else {
90const updatedGradle = `${gradleFileContent} \nhermesEnabled=${isHermesEnabled}`;
91await this.nodeFileSystem.writeFile(gradleFilePath, updatedGradle);
92}
93}
94}
95}