| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | const {execSync} = require('child_process'); |
| 9 | const {existsSync, readFileSync} = require('fs'); |
| 10 | const {resolve} = require('path'); |
| 11 | |
| 12 | const GITHUB_URL = 'https://github.com/facebook/react'; |
| 13 | |
| 14 | function getGitCommit() { |
| 15 | try { |
| 16 | return execSync('git show -s --no-show-signature --format=%h') |
| 17 | .toString() |
| 18 | .trim(); |
| 19 | } catch (error) { |
| 20 | // Mozilla runs this command from a git archive. |
| 21 | // In that context, there is no Git context. |
| 22 | // Using the commit hash specified to download-experimental-build.js script as a fallback. |
| 23 | |
| 24 | // Try to read from build/COMMIT_SHA file |
| 25 | const commitShaPath = resolve(__dirname, '..', '..', 'build', 'COMMIT_SHA'); |
| 26 | if (!existsSync(commitShaPath)) { |
| 27 | throw new Error( |
| 28 | 'Could not find build/COMMIT_SHA file. Did you run scripts/release/download-experimental-build.js script?', |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | try { |
| 33 | const commitHash = readFileSync(commitShaPath, 'utf8').trim(); |
| 34 | // Return short hash (first 7 characters) to match abbreviated commit hash format |
| 35 | return commitHash.slice(0, 7); |
| 36 | } catch (readError) { |
| 37 | throw new Error( |
| 38 | `Failed to read build/COMMIT_SHA file: ${readError.message}`, |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function getVersionString(packageVersion = null) { |
| 45 | if (packageVersion == null) { |
| 46 | packageVersion = JSON.parse( |
| 47 | readFileSync( |
| 48 | resolve(__dirname, '..', 'react-devtools-core', './package.json'), |
| 49 | ), |
| 50 | ).version; |
| 51 | } |
| 52 | |
| 53 | const commit = getGitCommit(); |
| 54 | |
| 55 | return `${packageVersion}-${commit}`; |
| 56 | } |
| 57 | |
| 58 | module.exports = { |
| 59 | GITHUB_URL, |
| 60 | getGitCommit, |
| 61 | getVersionString, |
| 62 | }; |