Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hermes support to React Native Code Push CLI #627

Merged
merged 3 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion src/commands/codepush/lib/react-native-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import * as fs from "fs";
import * as path from "path";
import chalk from "chalk";
import * as xml2js from "xml2js";
import { out } from "../../../util/interaction";
import { out, isDebug } from "../../../util/interaction";
import { isValidVersion } from "./validation-utils";
import { fileDoesNotExistOrIsDirectory } from "./file-utils";

const plist = require("plist");
const g2js = require("gradle-to-js/lib/parser");
const properties = require("properties");
Expand Down Expand Up @@ -246,6 +247,104 @@ export function runReactNativeBundleCommand(bundleName: string, development: boo
});
}

export function runHermesEmitBinaryCommand(bundleName: string, outputFolder: string, sourcemapOutput: string, extraHermesFlags: string[]): Promise<void> {
const hermesArgs: string[] = [];
const envNodeArgs: string = process.env.CODE_PUSH_NODE_ARGS;

if (typeof envNodeArgs !== "undefined") {
Array.prototype.push.apply(hermesArgs, envNodeArgs.trim().split(/\s+/));
}

Array.prototype.push.apply(hermesArgs, [
"-emit-binary",
"-out", path.join(outputFolder, bundleName + ".hbc"),
path.join(outputFolder, bundleName),
...extraHermesFlags,
]);

if (sourcemapOutput) {
hermesArgs.push("-output-source-map");
}

if (!isDebug()) {
hermesArgs.push("-w");
}

out.text(chalk.cyan("Converting JS bundle to byte code via Hermes, running command:\n"));
const hermesProcess = spawn(path.join("node_modules", "hermesvm", getHermesOSBin(), "hermes"), hermesArgs);
out.text(`${path.join("node_modules", "hermesvm", getHermesOSBin(), "hermes")} ${hermesArgs.join(" ")}`);

return new Promise<void>((resolve, reject) => {
hermesProcess.stdout.on("data", (data: Buffer) => {
out.text(data.toString().trim());
});

hermesProcess.stderr.on("data", (data: Buffer) => {
console.error(data.toString().trim());
});

hermesProcess.on("close", (exitCode: number) => {
if (exitCode) {
reject(new Error(`"hermes" command exited with code ${exitCode}.`));
}
// Copy HBC bundle to overwrite JS bundle
const source = path.join(outputFolder, bundleName + ".hbc");
const destination = path.join(outputFolder, bundleName);
fs.copyFile(source, destination,
(err) => {
if (err) {
console.error(err);
reject(new Error(`Copying file ${source} to ${destination} failed. "hermes" previously exited with code ${exitCode}.`));
}
fs.unlink(source, (err) => {
if (err) {
console.error(err);
reject(err);
}
resolve(null as void);
});
}
);
});
});
}

export function getHermesEnabled(gradleFile: string): boolean {
let buildGradlePath: string = path.join("android", "app");
if (gradleFile) {
buildGradlePath = gradleFile;
}
if (fs.lstatSync(buildGradlePath).isDirectory()) {
buildGradlePath = path.join(buildGradlePath, "build.gradle");
}

if (fileDoesNotExistOrIsDirectory(buildGradlePath)) {
throw new Error(`Unable to find gradle file "${buildGradlePath}".`);
}

return g2js.parseFile(buildGradlePath)
.catch(() => {
throw new Error(`Unable to parse the "${buildGradlePath}" file. Please ensure it is a well-formed Gradle file.`);
})
.then((buildGradle: any) => {
return Array.from(buildGradle["project.ext.react"]).includes("enableHermes: true");
});
}

function getHermesOSBin(): string {
switch (process.platform) {
case "win32":
return "win64-bin";
case "darwin":
return "osx-bin";
case "freebsd":
case "linux":
case "sunos":
default:
return "linux64-bin";
}
}

export function isValidOS(os: string): boolean {
switch (os.toLowerCase()) {
case "android":
Expand Down
20 changes: 18 additions & 2 deletions src/commands/codepush/release-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as path from "path";
import * as mkdirp from "mkdirp";
import { fileDoesNotExistOrIsDirectory, createEmptyTmpReleaseFolder, removeReactTmpDir } from "./lib/file-utils";
import { isValidRange, isValidDeployment } from "./lib/validation-utils";
import { VersionSearchParams, getReactNativeProjectAppVersion, runReactNativeBundleCommand, isValidOS, isValidPlatform, isReactNativeProject } from "./lib/react-native-utils";
import { VersionSearchParams, getReactNativeProjectAppVersion, runReactNativeBundleCommand, runHermesEmitBinaryCommand, getHermesEnabled, isValidOS, isValidPlatform, isReactNativeProject } from "./lib/react-native-utils";

const debug = require("debug")("appcenter-cli:commands:codepush:release-react");

Expand Down Expand Up @@ -77,6 +77,12 @@ export default class CodePushReleaseReactCommand extends CodePushReleaseCommandS
@hasArg
public extraBundlerOptions: string | string[];

@help("Flag that gets passed to Hermes, JavaScript to bytecode compiler. Can be specified multiple times")
@longName("extra-hermes-flag")
@defaultValue([])
@hasArg
public extraHermesFlags: string | string[];

private os: string;

private platform: string;
Expand Down Expand Up @@ -160,11 +166,21 @@ export default class CodePushReleaseReactCommand extends CodePushReleaseCommandS
this.extraBundlerOptions = [this.extraBundlerOptions];
}

if (typeof this.extraHermesFlags === "string") {
this.extraHermesFlags = [this.extraHermesFlags];
}

try {
createEmptyTmpReleaseFolder(this.updateContentsPath);
removeReactTmpDir();
await runReactNativeBundleCommand(this.bundleName, this.development, this.entryFile, this.updateContentsPath, this.os, this.sourcemapOutput, this.extraBundlerOptions);

// Check if we have to run hermes to compile JS to Byte Code if Hermes is enabled in build.gradle and we're releasing an Android build
if (this.os === "android") {
const isHermesEnabled = await getHermesEnabled(this.gradleFile);
if (isHermesEnabled) {
await runHermesEmitBinaryCommand(this.bundleName, this.updateContentsPath, this.sourcemapOutput, this.extraHermesFlags);
}
}
out.text(chalk.cyan("\nReleasing update contents to CodePush:\n"));

return await this.release(client);
Expand Down