diff --git a/package.json b/package.json index ee79dff2b7..a5cd3da8f6 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,8 @@ "scripts/codegen/codegen-utils.js", "scripts/codegen/generate-artifacts-executor.js", "scripts/codegen/generate-specs-cli-executor.js", + "scripts/hermes/hermes-utils.js", + "scripts/hermes/prepare-hermes-for-build.js", "scripts/ios-configure-glog.sh", "scripts/xcode/with-environment.sh", "scripts/launchPackager.bat", diff --git a/scripts/__tests__/hermes-utils-test.js b/scripts/__tests__/hermes-utils-test.js index 2512aebb17..279a4bd5cd 100644 --- a/scripts/__tests__/hermes-utils-test.js +++ b/scripts/__tests__/hermes-utils-test.js @@ -30,11 +30,9 @@ const MemoryFs = require('metro-memory-fs'); let execCalls; let fs; -let shelljs; -jest.mock('shelljs', () => ({ - echo: jest.fn(), - exec: jest.fn(command => { +jest.mock('child_process', () => ({ + execSync: jest.fn(command => { if (command.startsWith('curl')) { fs.writeFileSync( path.join(SDKS_DIR, 'download', `hermes-${hermesTagSha}.tgz`), @@ -58,7 +56,6 @@ jest.mock('shelljs', () => ({ return {code: 0}; } }), - exit: jest.fn(), })); function populateMockFilesystem() { @@ -118,12 +115,17 @@ describe('hermes-utils', () => { populateMockFilesystem(); execCalls = Object.create(null); - shelljs = require('shelljs'); }); describe('readHermesTag', () => { it('should return main if .hermesversion does not exist', () => { expect(readHermesTag()).toEqual('main'); }); + it('should fail if hermes tag is empty', () => { + fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), ''); + expect(() => { + readHermesTag(); + }).toThrow('[Hermes] .hermesversion file is empty.'); + }); it('should return tag from .hermesversion if file exists', () => { fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag); expect(readHermesTag()).toEqual(hermesTag); @@ -152,6 +154,7 @@ describe('hermes-utils', () => { }); describe('downloadHermesTarball', () => { it('should download Hermes tarball to download dir', () => { + fs.writeFileSync(path.join(SDKS_DIR, '.hermesversion'), hermesTag); downloadHermesTarball(); expect(execCalls.curl).toBeTruthy(); expect( @@ -188,9 +191,10 @@ describe('hermes-utils', () => { expect(fs.existsSync(path.join(SDKS_DIR, 'hermes'))).toBeTruthy(); }); it('should fail if Hermes tarball does not exist', () => { - expandHermesTarball(); + expect(() => { + expandHermesTarball(); + }).toThrow('[Hermes] Failed to expand Hermes tarball.'); expect(execCalls.tar).toBeUndefined(); - expect(shelljs.exit.mock.calls.length).toBeGreaterThan(0); }); }); describe('copyBuildScripts', () => { diff --git a/scripts/hermes/hermes-utils.js b/scripts/hermes/hermes-utils.js index 2deb59fbad..c875ecd4ea 100644 --- a/scripts/hermes/hermes-utils.js +++ b/scripts/hermes/hermes-utils.js @@ -11,7 +11,7 @@ const fs = require('fs'); const path = require('path'); -const {echo, exec, exit} = require('shelljs'); +const {execSync} = require('child_process'); const SDKS_DIR = path.normalize(path.join(__dirname, '..', '..', 'sdks')); const HERMES_DIR = path.join(SDKS_DIR, 'hermes'); @@ -41,14 +41,21 @@ function prepareFileSystem() { function readHermesTag() { if (fs.existsSync(HERMES_TAG_FILE_PATH)) { - const data = fs.readFileSync(HERMES_TAG_FILE_PATH, { - encoding: 'utf8', - flag: 'r', - }); - return data.trim(); - } else { - return 'main'; + const data = fs + .readFileSync(HERMES_TAG_FILE_PATH, { + encoding: 'utf8', + flag: 'r', + }) + .trim(); + + if (data.length > 0) { + return data; + } else { + throw new Error('[Hermes] .hermesversion file is empty.'); + } } + + return 'main'; } function setHermesTag(hermesTag) { @@ -64,10 +71,11 @@ function setHermesTag(hermesTag) { } function getHermesTagSHA(hermesTag) { - return exec( + return execSync( `git ls-remote https://github.com/facebook/hermes ${hermesTag} | cut -f 1`, - {silent: true}, - ).trim(); + ) + .toString() + .trim(); } function getHermesTarballDownloadPath(hermesTag) { @@ -87,11 +95,13 @@ function downloadHermesTarball() { return; } - echo(`[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`); - if (exec(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`).code) { - echo('[Hermes] Failed to download Hermes tarball.'); - exit(1); - return; + console.info( + `[Hermes] Downloading Hermes source code for commit ${hermesTagSHA}`, + ); + try { + execSync(`curl ${hermesTarballUrl} -Lo ${hermesTarballDownloadPath}`); + } catch (error) { + throw new Error(`[Hermes] Failed to download Hermes tarball. ${error}`); } } @@ -103,32 +113,24 @@ function expandHermesTarball() { prepareFileSystem(); if (!fs.existsSync(hermesTarballDownloadPath)) { - echo( - `[Hermes] Failed to expand Hermes tarball, no file found at ${hermesTarballDownloadPath}.`, - ); - exit(1); - return; + throw new Error(`[Hermes] Failed to expand Hermes tarball.`); } - echo(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`); - if ( - exec( + console.info(`[Hermes] Expanding Hermes tarball for commit ${hermesTagSHA}`); + try { + execSync( `tar -zxf ${hermesTarballDownloadPath} --strip-components=1 --directory ${HERMES_DIR}`, - ).code - ) { - echo('[Hermes] Failed to expand Hermes tarball.'); - exit(1); - return; + ); + } catch (error) { + throw new Error('[Hermes] Failed to expand Hermes tarball.'); } } function copyBuildScripts() { if (!fs.existsSync(HERMES_DIR)) { - echo( + throw new Error( '[Hermes] Failed to copy Hermes build scripts, no Hermes source directory found.', ); - exit(1); - return; } fs.copyFileSync( @@ -163,8 +165,14 @@ set_target_properties(native-hermesc PROPERTIES IMPORTED_LOCATION "${MACOS_HERMESC_PATH}" )`; - fs.mkdirSync(MACOS_BIN_DIR, {recursive: true}); - fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE); + try { + fs.mkdirSync(MACOS_BIN_DIR, {recursive: true}); + fs.writeFileSync(MACOS_IMPORT_HERMESC_PATH, IMPORT_HERMESC_TEMPLATE); + } catch (error) { + console.warn( + `[Hermes] Re-compiling hermesc. Unable to configure make: ${error}`, + ); + } } module.exports = {