|
| 1 | +/** |
| 2 | + * code based on appcenter-cli |
| 3 | + */ |
| 4 | + |
| 5 | +const childProcess = require('child_process'); |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const shell = require('shelljs'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Run Hermes compile CLI command |
| 12 | + * |
| 13 | + * @param bundleName {string} JS bundle file name |
| 14 | + * @param outputPath {string} Path to output .hbc file |
| 15 | + * @param sourcemapOutput {string} Path to output sourcemap file (Warning: if sourcemapOutput points to the outputPath, the sourcemap will be included in the CodePush bundle and increase the deployment size) |
| 16 | + * @param extraHermesFlags {string[]} Additional options to pass to `hermesc` command |
| 17 | + * @return {Promise<void>} |
| 18 | + */ |
| 19 | +async function runHermesEmitBinaryCommand( |
| 20 | + bundleName, |
| 21 | + outputPath, |
| 22 | + sourcemapOutput, |
| 23 | + extraHermesFlags = [], |
| 24 | +) { |
| 25 | + /** |
| 26 | + * @type {string[]} |
| 27 | + */ |
| 28 | + const hermesArgs = [ |
| 29 | + '-emit-binary', |
| 30 | + '-out', |
| 31 | + path.join(outputPath, bundleName + '.hbc'), |
| 32 | + path.join(outputPath, bundleName), |
| 33 | + ...extraHermesFlags, |
| 34 | + ]; |
| 35 | + if (sourcemapOutput) { |
| 36 | + hermesArgs.push('-output-source-map'); |
| 37 | + } |
| 38 | + |
| 39 | + console.log('Converting JS bundle to byte code via Hermes, running command:\n'); |
| 40 | + |
| 41 | + return new Promise((resolve, reject) => { |
| 42 | + try { |
| 43 | + const hermesCommand = getHermesCommand(); |
| 44 | + |
| 45 | + const disableAllWarningsArg = '-w'; |
| 46 | + shell.exec(`${hermesCommand} ${hermesArgs.join(' ')} ${disableAllWarningsArg}`); |
| 47 | + |
| 48 | + // Copy HBC bundle to overwrite JS bundle |
| 49 | + const source = path.join(outputPath, bundleName + '.hbc'); |
| 50 | + const destination = path.join(outputPath, bundleName); |
| 51 | + shell.cp(source, destination); |
| 52 | + shell.rm(source); |
| 53 | + resolve(); |
| 54 | + } catch (e) { |
| 55 | + reject(e); |
| 56 | + } |
| 57 | + }).then(() => { |
| 58 | + if (!sourcemapOutput) { |
| 59 | + // skip source map compose if source map is not enabled |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // compose-source-maps.js file path |
| 64 | + const composeSourceMapsPath = getComposeSourceMapsPath(); |
| 65 | + if (composeSourceMapsPath === null) { |
| 66 | + throw new Error('react-native compose-source-maps.js scripts is not found'); |
| 67 | + } |
| 68 | + |
| 69 | + const jsCompilerSourceMapFile = path.join(outputPath, bundleName + '.hbc' + '.map'); |
| 70 | + if (!fs.existsSync(jsCompilerSourceMapFile)) { |
| 71 | + throw new Error(`sourcemap file ${jsCompilerSourceMapFile} is not found`); |
| 72 | + } |
| 73 | + |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + const composeSourceMapsArgs = [ |
| 76 | + composeSourceMapsPath, |
| 77 | + sourcemapOutput, |
| 78 | + jsCompilerSourceMapFile, |
| 79 | + '-o', |
| 80 | + sourcemapOutput, |
| 81 | + ]; |
| 82 | + const composeSourceMapsProcess = childProcess.spawn('node', composeSourceMapsArgs); |
| 83 | + console.log(`${composeSourceMapsPath} ${composeSourceMapsArgs.join(' ')}`); |
| 84 | + |
| 85 | + composeSourceMapsProcess.stdout.on('data', (data) => { |
| 86 | + console.log(data.toString().trim()); |
| 87 | + }); |
| 88 | + |
| 89 | + composeSourceMapsProcess.stderr.on('data', (data) => { |
| 90 | + console.error(data.toString().trim()); |
| 91 | + }); |
| 92 | + |
| 93 | + composeSourceMapsProcess.on('close', (exitCode, signal) => { |
| 94 | + if (exitCode !== 0) { |
| 95 | + reject(new Error(`"compose-source-maps" command failed (exitCode=${exitCode}, signal=${signal}).`)); |
| 96 | + } |
| 97 | + |
| 98 | + // Delete the HBC sourceMap, otherwise it will be included in 'code-push' bundle as well |
| 99 | + fs.unlink(jsCompilerSourceMapFile, (err) => { |
| 100 | + if (err != null) { |
| 101 | + console.error(err); |
| 102 | + reject(err); |
| 103 | + } |
| 104 | + |
| 105 | + resolve(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @return {string} |
| 114 | + */ |
| 115 | +function getHermesCommand() { |
| 116 | + /** |
| 117 | + * @type {(file: string) => boolean} |
| 118 | + */ |
| 119 | + const fileExists = (file) => { |
| 120 | + try { |
| 121 | + return fs.statSync(file).isFile(); |
| 122 | + } catch (e) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + }; |
| 126 | + // Hermes is bundled with react-native since 0.69 |
| 127 | + const bundledHermesEngine = path.join( |
| 128 | + getReactNativePackagePath(), |
| 129 | + 'sdks', |
| 130 | + 'hermesc', |
| 131 | + getHermesOSBin(), |
| 132 | + getHermesOSExe(), |
| 133 | + ); |
| 134 | + if (fileExists(bundledHermesEngine)) { |
| 135 | + return bundledHermesEngine; |
| 136 | + } |
| 137 | + throw new Error('Hermes engine binary not found. Please upgrade to react-native 0.69 or later'); |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * @return {string} |
| 142 | + */ |
| 143 | +function getHermesOSBin() { |
| 144 | + switch (process.platform) { |
| 145 | + case 'win32': |
| 146 | + return 'win64-bin'; |
| 147 | + case 'darwin': |
| 148 | + return 'osx-bin'; |
| 149 | + case 'freebsd': |
| 150 | + case 'linux': |
| 151 | + case 'sunos': |
| 152 | + default: |
| 153 | + return 'linux64-bin'; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * @return {string} |
| 159 | + */ |
| 160 | +function getHermesOSExe() { |
| 161 | + const hermesExecutableName = 'hermesc'; |
| 162 | + switch (process.platform) { |
| 163 | + case 'win32': |
| 164 | + return hermesExecutableName + '.exe'; |
| 165 | + default: |
| 166 | + return hermesExecutableName; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * @return {string | null} |
| 172 | + */ |
| 173 | +function getComposeSourceMapsPath() { |
| 174 | + // detect if compose-source-maps.js script exists |
| 175 | + const composeSourceMaps = path.join(getReactNativePackagePath(), 'scripts', 'compose-source-maps.js'); |
| 176 | + if (fs.existsSync(composeSourceMaps)) { |
| 177 | + return composeSourceMaps; |
| 178 | + } |
| 179 | + return null; |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * @return {string} |
| 184 | + */ |
| 185 | +function getReactNativePackagePath() { |
| 186 | + const result = childProcess.spawnSync('node', [ |
| 187 | + '--print', |
| 188 | + "require.resolve('react-native/package.json')", |
| 189 | + ]); |
| 190 | + const packagePath = path.dirname(result.stdout.toString()); |
| 191 | + if (result.status === 0 && directoryExistsSync(packagePath)) { |
| 192 | + return packagePath; |
| 193 | + } |
| 194 | + |
| 195 | + return path.join('node_modules', 'react-native'); |
| 196 | +} |
| 197 | + |
| 198 | +/** |
| 199 | + * @param dirname {string} |
| 200 | + * @return {boolean} |
| 201 | + */ |
| 202 | +function directoryExistsSync(dirname) { |
| 203 | + try { |
| 204 | + return fs.statSync(dirname).isDirectory(); |
| 205 | + } catch (err) { |
| 206 | + if (err.code !== 'ENOENT') { |
| 207 | + throw err; |
| 208 | + } |
| 209 | + } |
| 210 | + return false; |
| 211 | +} |
| 212 | + |
| 213 | +module.exports = { runHermesEmitBinaryCommand }; |
0 commit comments