|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Updator script triggered from minecraft-data repository to auto generate PR |
| 4 | + */ |
| 5 | +const fs = require('fs') |
| 6 | +const cp = require('child_process') |
| 7 | +const assert = require('assert') |
| 8 | +const github = require('gh-helpers')() |
| 9 | +const { join } = require('path') |
| 10 | +const exec = (cmd) => github.mock ? console.log('> ', cmd) : (console.log('> ', cmd), cp.execSync(cmd, { stdio: 'inherit' })) |
| 11 | + |
| 12 | +console.log('Starting update process...') |
| 13 | +// Sanitize and validate environment variables all non alpha numeric / underscore / dot |
| 14 | +const newVersion = process.env.NEW_MC_VERSION?.replace(/[^a-zA-Z0-9_.]/g, '_') |
| 15 | +const triggerBranch = process.env.MCDATA_BRANCH?.replace(/[^a-zA-Z0-9_.]/g, '_') |
| 16 | +const mcdataPrURL = process.env.MCDATA_PR_URL |
| 17 | +console.log({ newVersion, triggerBranch, mcdataPrURL }) |
| 18 | + |
| 19 | +assert(newVersion) |
| 20 | +assert(triggerBranch) |
| 21 | + |
| 22 | +async function main () { |
| 23 | + const currentSupportedPath = require.resolve('../../lib/version.js') |
| 24 | + const readmePath = join(__dirname, '../../docs/README.md') |
| 25 | + const ciPath = join(__dirname, '../../.github/workflows/ci.yml') |
| 26 | + |
| 27 | + // Update the version.js |
| 28 | + const currentSupportedVersion = require('../../lib/version.js') |
| 29 | + const currentContents = fs.readFileSync(currentSupportedPath, 'utf8') |
| 30 | + console.log('Current supported version:', currentContents) |
| 31 | + const latestV = currentSupportedVersion.testedVersions.at(-1) |
| 32 | + const newContents = currentContents.includes(newVersion) |
| 33 | + ? currentContents |
| 34 | + : currentContents |
| 35 | + .replace(`, '${latestV}'`, `, '${latestV}', '${newVersion}'`) |
| 36 | + |
| 37 | + // Update the README.md |
| 38 | + const currentContentsReadme = fs.readFileSync(readmePath, 'utf8') |
| 39 | + if (!currentContentsReadme.includes(newVersion)) { |
| 40 | + const newReadmeContents = currentContentsReadme |
| 41 | + .replace(/Minecraft 1\.8 to [0-9A-Za-z._-]+ \(/, `Minecraft 1.8 to ${newVersion} (`) |
| 42 | + .replace(') <!--version-->', `, ${newVersion}) <!--version-->`) |
| 43 | + fs.writeFileSync(readmePath, newReadmeContents) |
| 44 | + console.log('Updated README with new version:', newVersion) |
| 45 | + } |
| 46 | + fs.writeFileSync(currentSupportedPath, newContents) |
| 47 | + |
| 48 | + // Update the CI workflow |
| 49 | + const currentContentsCI = fs.readFileSync(ciPath, 'utf8') |
| 50 | + if (!currentContentsCI.includes(newVersion)) { |
| 51 | + const newCIContents = currentContentsCI.replace( |
| 52 | + 'run: npm install', `run: npm install |
| 53 | + - run: cd node_modules && cd minecraft-data && mv minecraft-data minecraft-data-old && git clone -b ${triggerBranch} https://github.com/PrismarineJS/minecraft-data --depth 1 && node bin/generate_data.js |
| 54 | + - run: curl -o node_modules/protodef/src/serializer.js https://raw.githubusercontent.com/extremeheat/node-protodef/refs/heads/dlog/src/serializer.js && curl -o node_modules/protodef/src/compiler.js https://raw.githubusercontent.com/extremeheat/node-protodef/refs/heads/dlog/src/compiler.js |
| 55 | +`) |
| 56 | + fs.writeFileSync(ciPath, newCIContents) |
| 57 | + console.log('Updated CI workflow with new version:', newVersion) |
| 58 | + } |
| 59 | + |
| 60 | + const branchName = 'pc' + newVersion.replace(/[^a-zA-Z0-9_]/g, '_') |
| 61 | + exec(`git checkout -b ${branchName}`) |
| 62 | + exec('git config user.name "github-actions[bot]"') |
| 63 | + exec('git config user.email "41898282+github-actions[bot]@users.noreply.github.com"') |
| 64 | + exec('git add --all') |
| 65 | + exec(`git commit -m "Update to version ${newVersion}"`) |
| 66 | + exec(`git push origin ${branchName} --force`) |
| 67 | + // createPullRequest(title: string, body: string, fromBranch: string, intoBranch?: string): Promise<{ number: number, url: string }>; |
| 68 | + const pr = await github.createPullRequest( |
| 69 | + `🎈 ${newVersion}`, |
| 70 | + `This automated PR sets up the relevant boilerplate for Minecraft version ${newVersion}. |
| 71 | +
|
| 72 | +Ref: ${mcdataPrURL} |
| 73 | +
|
| 74 | +* You can help contribute to this PR by opening a PR against this <code branch>${branchName}</code> branch instead of <code>master</code>. |
| 75 | + `, |
| 76 | + branchName, |
| 77 | + 'master' |
| 78 | + ) |
| 79 | + console.log(`Pull request created`, pr) |
| 80 | +} |
| 81 | + |
| 82 | +main().catch(err => { |
| 83 | + console.error('Error during update process:', err) |
| 84 | + process.exit(1) |
| 85 | +}) |
0 commit comments