|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// USAGE: |
| 4 | +// This will create/update a local stable-sync branch |
| 5 | +// and get it in the state needed for a stable-sync PR |
| 6 | +// Once the script successfully completes, you just |
| 7 | +// need to push the branch to the remote repo. This will |
| 8 | +// likely require a `git push --force` |
| 9 | +// |
| 10 | +// Usage: node stable-sync.js [branch-name] |
| 11 | +// If no branch name is provided, defaults to 'stable-sync' |
| 12 | + |
| 13 | +const { promisify } = require('util'); |
| 14 | +const exec = promisify(require('child_process').exec); |
| 15 | + |
| 16 | +async function runGitCommands() { |
| 17 | + // Get branch name from command line arguments or use default |
| 18 | + const branchName = process.argv[2] || 'stable-main'; |
| 19 | + |
| 20 | + try { |
| 21 | + try { |
| 22 | + // Check if the branch already exists |
| 23 | + const { stdout: branchExists } = await exec( |
| 24 | + `git rev-parse --quiet --verify ${branchName}`, |
| 25 | + ); |
| 26 | + if (branchExists.trim()) { |
| 27 | + // Branch exists, so simply check it out |
| 28 | + await exec(`git checkout ${branchName}`); |
| 29 | + console.log(`Checked out branch: ${branchName}`); |
| 30 | + } else { |
| 31 | + throw new Error( |
| 32 | + 'git rev-parse --quiet --verify failed. Branch hash empty', |
| 33 | + ); |
| 34 | + } |
| 35 | + } catch (error) { |
| 36 | + if (error.stdout === '') { |
| 37 | + console.warn( |
| 38 | + `Branch does not exist, creating new ${branchName} branch.`, |
| 39 | + ); |
| 40 | + |
| 41 | + // Branch does not exist, create and check it out |
| 42 | + await exec(`git checkout -b ${branchName}`); |
| 43 | + console.log(`Created and checked out branch: ${branchName}`); |
| 44 | + } else { |
| 45 | + console.error(`Error: ${error.message}`); |
| 46 | + process.exit(1); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + await exec('git fetch'); |
| 51 | + console.log('Executed: git fetch'); |
| 52 | + |
| 53 | + await exec('git reset --hard origin/stable'); |
| 54 | + console.log('Executed: git reset --hard origin/stable'); |
| 55 | + |
| 56 | + try { |
| 57 | + await exec('git merge origin/main'); |
| 58 | + console.log('Executed: git merge origin/main'); |
| 59 | + } catch (error) { |
| 60 | + // Handle the error but continue script execution |
| 61 | + if ( |
| 62 | + error.stdout.includes( |
| 63 | + 'Automatic merge failed; fix conflicts and then commit the result.', |
| 64 | + ) |
| 65 | + ) { |
| 66 | + console.warn( |
| 67 | + 'Merge conflict encountered. Continuing script execution.', |
| 68 | + ); |
| 69 | + } else { |
| 70 | + console.error(`Error: ${error.message}`); |
| 71 | + process.exit(1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + await exec('git add .'); |
| 76 | + await exec('git restore --source origin/main .'); |
| 77 | + console.log('Executed: it restore --source origin/main .'); |
| 78 | + |
| 79 | + await exec('git checkout origin/main -- .'); |
| 80 | + console.log('Executed: git checkout origin/main -- .'); |
| 81 | + |
| 82 | + await exec('git checkout origin/stable -- CHANGELOG.md'); |
| 83 | + console.log('Executed: git checkout origin/stable -- CHANGELOG.md'); |
| 84 | + |
| 85 | + // Mobile Only |
| 86 | + await exec('git checkout origin/stable -- bitrise.yml'); |
| 87 | + console.log('Executed: git checkout origin/stable -- bitrise.yml'); |
| 88 | + |
| 89 | + // Mobile Only |
| 90 | + await exec('git checkout origin/stable -- android/app/build.gradle'); |
| 91 | + console.log('Executed: git checkout origin/stable -- android/app/build.gradle'); |
| 92 | + |
| 93 | + // Mobile Only |
| 94 | + await exec('git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj'); |
| 95 | + console.log('Executed: git checkout origin/stable -- ios/MetaMask.xcodeproj/project.pbxproj'); |
| 96 | + |
| 97 | + // Mobile Only |
| 98 | + await exec('git checkout origin/stable -- package.json'); |
| 99 | + console.log('Executed: git checkout origin/stable -- package.json'); |
| 100 | + |
| 101 | + // Extension Only |
| 102 | + // const { stdout: packageJsonContent } = await exec( |
| 103 | + // 'git show origin/master:package.json', |
| 104 | + // ); |
| 105 | + // const packageJson = JSON.parse(packageJsonContent); |
| 106 | + // const packageVersion = packageJson.version; |
| 107 | + |
| 108 | + // await exec(`yarn version "${packageVersion}"`); |
| 109 | + // console.log('Executed: yarn version'); |
| 110 | + |
| 111 | + await exec('git add .'); |
| 112 | + console.log('Executed: git add .'); |
| 113 | + |
| 114 | + await exec(`git commit -m "Merge origin/main into ${branchName}" --no-verify`); |
| 115 | + console.log('Executed: git commit'); |
| 116 | + |
| 117 | + console.log(`Your local ${branchName} branch is now ready to become a PR.`); |
| 118 | + console.log('You likely now need to do `git push --force`'); |
| 119 | + } catch (error) { |
| 120 | + console.error(`Error: ${error.message}`); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +runGitCommands(); |
0 commit comments