|
| 1 | +const path = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const core = require('@actions/core'); |
| 4 | +const semanticRelease = require('semantic-release').default; |
| 5 | +const { request } = require('undici'); |
| 6 | + |
| 7 | +async function run() { |
| 8 | + const mainRepoPath = process.cwd(); |
| 9 | + const pkgInfo = require(`${mainRepoPath}/package.json`); |
| 10 | + const registry = pkgInfo.publishConfig?.registry || 'https://registry.npmjs.org'; |
| 11 | + core.setOutput('name', pkgInfo.name); |
| 12 | + core.setOutput('registry', registry); |
| 13 | + |
| 14 | + try { |
| 15 | + const configFiles = [ |
| 16 | + path.join(__dirname, 'release.config.js'), |
| 17 | + path.join(mainRepoPath, 'release.config.js'), |
| 18 | + ].filter(file => fs.existsSync(file)); |
| 19 | + |
| 20 | + core.info(`Using config files: ${configFiles.join(', ')}`); |
| 21 | + |
| 22 | + const result = await semanticRelease({ |
| 23 | + dryRun: process.env.DRYRUN === 'true', |
| 24 | + extends: configFiles, |
| 25 | + }); |
| 26 | + |
| 27 | + const { nextRelease, lastRelease } = result; |
| 28 | + |
| 29 | + if (!nextRelease) { |
| 30 | + core.notice('No release need to be published.'); |
| 31 | + core.summary.addRaw('No release need to be published.'); |
| 32 | + await core.summary.write(); |
| 33 | + } else { |
| 34 | + core.info(`Published release: ${nextRelease.version}`); |
| 35 | + core.setOutput('release_version', nextRelease.version); |
| 36 | + |
| 37 | + // cnpm sync |
| 38 | + try { |
| 39 | + const res = await request(`https://registry-direct.npmmirror.com/-/package/${pkgInfo.name}/syncs`, { |
| 40 | + method: 'PUT', |
| 41 | + timeout: 30000, |
| 42 | + }); |
| 43 | + const { id } = await res.body.json(); |
| 44 | + const logUrl = `https://registry.npmmirror.com/-/package/${pkgInfo.name}/syncs/${id}/log`; |
| 45 | + core.setOutput('cnpm_sync_url', logUrl); |
| 46 | + core.info(`cnpm sync log url: ${logUrl}`); |
| 47 | + |
| 48 | + // write summary |
| 49 | + core.summary.addRaw(`## [${pkgInfo.name}](https://github.com/${process.env.GITHUB_REPOSITORY})\n`); |
| 50 | + core.summary.addRaw(`- Release: ${lastRelease?.version ?? ''} -> ${nextRelease.version}\n`); |
| 51 | + core.summary.addRaw(`- Registry: ${registry}\n`); |
| 52 | + core.summary.addRaw(`- CNPM Sync: ${logUrl}\n`); |
| 53 | + core.summary.addRaw(`- DryRun: ${process.env.DRYRUN}\n`); |
| 54 | + } catch (err) { |
| 55 | + core.info(`cnpm sync ${pkgInfo.name} fail, ${err.message}`); |
| 56 | + core.summary.addRaw(`- CNPM Sync ${pkgInfo.name} error: ${err.message}\n`); |
| 57 | + } |
| 58 | + core.summary.addRaw(nextRelease.notes); |
| 59 | + await core.summary.write(); |
| 60 | + } |
| 61 | + console.log('Result:', result); |
| 62 | + } catch (error) { |
| 63 | + console.error(error); |
| 64 | + core.setFailed(error); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +run(); |
0 commit comments