|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Updator script triggered from minecraft-data repository |
| 4 | + * This script can be customized to handle updates from minecraft-data |
| 5 | + */ |
| 6 | +const github = require('gh-helpers')() |
| 7 | +const fs = require('fs') |
| 8 | +const cp = require('child_process') |
| 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 | +const triggerBranch = process.env.TRIGGER_SOURCE |
| 14 | +const newVersion = process.env.DATA_VERSION |
| 15 | +const onBehalfOf = process.env.TRIGGER_REASON || 'workflow_dispatch' |
| 16 | +console.log('Trigger reason:', onBehalfOf) |
| 17 | +console.log('New version:', newVersion) |
| 18 | + |
| 19 | +if (!newVersion) { |
| 20 | + console.error('No new version provided. Exiting...') |
| 21 | + process.exit(1) |
| 22 | +} |
| 23 | + |
| 24 | +async function main () { |
| 25 | + const currentSupportedPath = require.resolve('../../src/version.js') |
| 26 | + const readmePath = join(__dirname, '../../docs/README.md') |
| 27 | + const ciPath = join(__dirname, '../../.github/workflows/ci.yml') |
| 28 | + |
| 29 | + // Update the version.js |
| 30 | + const currentSupportedVersion = require('../../src/version.js') |
| 31 | + const currentContents = fs.readFileSync(currentSupportedPath, 'utf8') |
| 32 | + console.log('Current supported version:', currentContents) |
| 33 | + const newContents = currentContents.includes(newVersion) |
| 34 | + ? currentContents |
| 35 | + : currentContents |
| 36 | + .replace(`: '${currentSupportedVersion.defaultVersion}'`, `: '${newVersion}'`) |
| 37 | + .replace(`, '${currentSupportedVersion.defaultVersion}'`, `, '${currentSupportedVersion.defaultVersion}', '${newVersion}'`) |
| 38 | + |
| 39 | + // Update the README.md |
| 40 | + const currentContentsReadme = fs.readFileSync(readmePath, 'utf8') |
| 41 | + if (!currentContentsReadme.includes(newVersion)) { |
| 42 | + const newReadmeContents = currentContentsReadme.replace(' <!-- NEXT_VERSION -->', `, ${newVersion} <!-- NEXT_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', ` |
| 53 | + run: npm install |
| 54 | + - 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 |
| 55 | + - 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 |
| 56 | +`) |
| 57 | + fs.writeFileSync(ciPath, newCIContents) |
| 58 | + console.log('Updated CI workflow with new version:', newVersion) |
| 59 | + } |
| 60 | + |
| 61 | + const branchName = 'pc' + newVersion.replace(/[^a-zA-Z0-9_]/g, '.') |
| 62 | + exec(`git checkout -b ${branchName}`) |
| 63 | + exec('git add --all') |
| 64 | + exec(`git commit -m "Update to version ${newVersion}"`) |
| 65 | + exec(`git push origin ${branchName}`) |
| 66 | + // createPullRequest(title: string, body: string, fromBranch: string, intoBranch?: string): Promise<{ number: number, url: string }>; |
| 67 | + const pr = await github.createPullRequest( |
| 68 | + `${newVersion} updates`, |
| 69 | + `Automatically generated PR for Minecraft version ${newVersion}.\n\nRef: ${onBehalfOf}`, |
| 70 | + branchName, |
| 71 | + 'master' |
| 72 | + ) |
| 73 | + console.log(`Pull request created: ${pr.url} (PR #${pr.number})`) |
| 74 | + console.log('Update process completed successfully!') |
| 75 | +} |
| 76 | + |
| 77 | +main().catch(err => { |
| 78 | + console.error('Error during update process:', err) |
| 79 | + process.exit(1) |
| 80 | +}) |
0 commit comments