Skip to content

Commit 004b860

Browse files
hramosfacebook-github-bot
authored andcommitted
Hermes: Add scripts to package, remove shelljs
Summary: The new Hermes scripts need to be included in the `react-native` npm. The `shelljs` dependency that was used by the Hermes scripts is a dev dependency, so instead of adding to the `react-native` npm size, we refactored its use out of hermes-utils.js. Changelog: [General][Added] - Add Hermes scripts to package Reviewed By: cortinico Differential Revision: D36387135 fbshipit-source-id: 12d0bc29d365c4cb18d33a0d390e6e7d34864b7a
1 parent 55133ea commit 004b860

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
"scripts/codegen/codegen-utils.js",
4444
"scripts/codegen/generate-artifacts-executor.js",
4545
"scripts/codegen/generate-specs-cli-executor.js",
46+
"scripts/hermes/hermes-utils.js",
47+
"scripts/hermes/prepare-hermes-for-build.js",
4648
"scripts/ios-configure-glog.sh",
4749
"scripts/xcode/with-environment.sh",
4850
"scripts/launchPackager.bat",

scripts/__tests__/hermes-utils-test.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ const MemoryFs = require('metro-memory-fs');
3030

3131
let execCalls;
3232
let fs;
33-
let shelljs;
3433

35-
jest.mock('shelljs', () => ({
36-
echo: jest.fn(),
37-
exec: jest.fn(command => {
34+
jest.mock('child_process', () => ({
35+
execSync: jest.fn(command => {
3836
if (command.startsWith('curl')) {
3937
fs.writeFileSync(
4038
path.join(SDKS_DIR, 'download', `hermes-${hermesTagSha}.tgz`),
@@ -58,7 +56,6 @@ jest.mock('shelljs', () => ({
5856
return {code: 0};
5957
}
6058
}),
61-
exit: jest.fn(),
6259
}));
6360

6461
function populateMockFilesystem() {
@@ -118,12 +115,17 @@ describe('hermes-utils', () => {
118115
populateMockFilesystem();
119116

120117
execCalls = Object.create(null);
121-
shelljs = require('shelljs');
122118
});
123119
describe('readHermesTag', () => {
124120
it('should return main if .hermesversion does not exist', () => {
125121
expect(readHermesTag()).toEqual('main');
126122
});
123+
it('should fail if hermes tag is empty', () => {
124+
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), '');
125+
expect(() => {
126+
readHermesTag();
127+
}).toThrow('[Hermes] .hermesversion file is empty.');
128+
});
127129
it('should return tag from .hermesversion if file exists', () => {
128130
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag);
129131
expect(readHermesTag()).toEqual(hermesTag);
@@ -152,6 +154,7 @@ describe('hermes-utils', () => {
152154
});
153155
describe('downloadHermesTarball', () => {
154156
it('should download Hermes tarball to download dir', () => {
157+
fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag);
155158
downloadHermesTarball();
156159
expect(execCalls.curl).toBeTruthy();
157160
expect(
@@ -188,9 +191,10 @@ describe('hermes-utils', () => {
188191
expect(fs.existsSync(path.join(SDKS_DIR, 'hermes'))).toBeTruthy();
189192
});
190193
it('should fail if Hermes tarball does not exist', () => {
191-
expandHermesTarball();
194+
expect(() => {
195+
expandHermesTarball();
196+
}).toThrow('[Hermes] Failed to expand Hermes tarball.');
192197
expect(execCalls.tar).toBeUndefined();
193-
expect(shelljs.exit.mock.calls.length).toBeGreaterThan(0);
194198
});
195199
});
196200
describe('copyBuildScripts', () => {

scripts/hermes/hermes-utils.js

+42-34
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const fs = require('fs');
1313
const path = require('path');
14-
const {echo, exec, exit} = require('shelljs');
14+
const {execSync} = require('child_process');
1515

1616
const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks'));
1717
const HERMES_DIR = path.join(SDKS_DIR, 'hermes');
@@ -41,14 +41,21 @@ function prepareFileSystem() {
4141

4242
function readHermesTag() {
4343
if (fs.existsSync(HERMES_TAG_FILE_PATH)) {
44-
const data = fs.readFileSync(HERMES_TAG_FILE_PATH, {
45-
encoding: 'utf8',
46-
flag: 'r',
47-
});
48-
return data.trim();
49-
} else {
50-
return 'main';
44+
const data = fs
45+
.readFileSync(HERMES_TAG_FILE_PATH, {
46+
encoding: 'utf8',
47+
flag: 'r',
48+
})
49+
.trim();
50+
51+
if (data.length > 0) {
52+
return data;
53+
} else {
54+
throw new Error('[Hermes] .hermesversion file is empty.');
55+
}
5156
}
57+
58+
return 'main';
5259
}
5360

5461
function setHermesTag(hermesTag) {
@@ -64,10 +71,11 @@ function setHermesTag(hermesTag) {
6471
}
6572

6673
function getHermesTagSHA(hermesTag) {
67-
return exec(
74+
return execSync(
6875
`git ls-remote https://github.com/facebook/hermes ${hermesTag} | cut -f 1`,
69-
{silent: true},
70-
).trim();
76+
)
77+
.toString()
78+
.trim();
7179
}
7280

7381
function getHermesTarballDownloadPath(hermesTag) {
@@ -87,11 +95,13 @@ function downloadHermesTarball() {
8795
return;
8896
}
8997

90-
echo(`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`);
91-
if (exec(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`).code) {
92-
echo('[Hermes] Failed to download Hermes tarball.');
93-
exit(1);
94-
return;
98+
console.info(
99+
`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`,
100+
);
101+
try {
102+
execSync(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`);
103+
} catch (error) {
104+
throw new Error(`[Hermes] Failed to download Hermes tarball. ${error}`);
95105
}
96106
}
97107

@@ -103,32 +113,24 @@ function expandHermesTarball() {
103113
prepareFileSystem();
104114

105115
if (!fs.existsSync(hermesTarballDownloadPath)) {
106-
echo(
107-
`[Hermes] Failed to expand Hermes tarball, no file found at ${hermesTarballDownloadPath}.`,
108-
);
109-
exit(1);
110-
return;
116+
throw new Error(`[Hermes] Failed to expand Hermes tarball.`);
111117
}
112118

113-
echo(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
114-
if (
115-
exec(
119+
console.info(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`);
120+
try {
121+
execSync(
116122
`tar -zxf ${hermesTarballDownloadPath} --strip-components=1 --directory ${HERMES_DIR}`,
117-
).code
118-
) {
119-
echo('[Hermes] Failed to expand Hermes tarball.');
120-
exit(1);
121-
return;
123+
);
124+
} catch (error) {
125+
throw new Error('[Hermes] Failed to expand Hermes tarball.');
122126
}
123127
}
124128

125129
function copyBuildScripts() {
126130
if (!fs.existsSync(HERMES_DIR)) {
127-
echo(
131+
throw new Error(
128132
'[Hermes] Failed to copy Hermes build scripts, no Hermes source directory found.',
129133
);
130-
exit(1);
131-
return;
132134
}
133135

134136
fs.copyFileSync(
@@ -163,8 +165,14 @@ set_target_properties(native-hermesc PROPERTIES
163165
IMPORTED_LOCATION "${MACOS_HERMESC_PATH}"
164166
)`;
165167

166-
fs.mkdirSync(MACOS_BIN_DIR, {recursive: true});
167-
fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE);
168+
try {
169+
fs.mkdirSync(MACOS_BIN_DIR, {recursive: true});
170+
fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE);
171+
} catch (error) {
172+
console.warn(
173+
`[Hermes] Re-compiling hermesc. Unable to configure make: ${error}`,
174+
);
175+
}
168176
}
169177

170178
module.exports = {

0 commit comments

Comments
 (0)