UNPKG

projex

Version:
166 lines (165 loc) 7.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReleaseUtils = void 0; const _api_1 = require("../../../../api/index"); const _shared_1 = require("../../../../shared/index"); const fs_extra_1 = require("fs-extra"); const changelog_1 = require("./changelog"); const chalk_1 = __importDefault(require("chalk")); class ReleaseUtils { versionFileUtils = new _shared_1.VersionFileUtils(); constructor() { } commit = (tagName, releaseType) => { const beta = releaseType === 'prerelease' || releaseType === 'pre'; const commitIcon = beta ? 'chore(beta): beta release' : 'chore(main): release'; const commitMessage = `${commitIcon} ${tagName}`; let successMessage = `file(s) ${this.versionFileUtils.versionFile} committed`; if ((0, fs_extra_1.existsSync)(this.versionFileUtils.changelogPath)) { successMessage = `files ${this.versionFileUtils.versionFile} ${this.versionFileUtils.changelogPath} committed`; } return (0, _shared_1.runCommand)(`git commit -m "${commitMessage}"`, this.versionFileUtils.root, successMessage, true); }; push = (tagName, noTag) => { return (0, _shared_1.runCommand)((0, _shared_1.pushCommand)(tagName, noTag), this.versionFileUtils.root, `pushed commit ${noTag ? '' : `and tag ${tagName}`}`, true, 2); }; checkNothingToCommit = () => { const response = (0, _shared_1.gitStatus)(this.versionFileUtils.root).toString(); return !response; }; checkIfGitPushWorks = () => { try { (0, _shared_1.runCommand)('git push --force', this.versionFileUtils.root, '', true, 2, true); } catch (e) { _shared_1.log.error(_api_1.Colors.ERROR(`failed pushing to remote. ${e}`)); } }; preRelease = async ({ checkPreRelease, noPreRelease, releaseType, }) => { if (!checkPreRelease) { if (!this.checkNothingToCommit()) { _shared_1.log.warn(chalk_1.default.red('process could not continue because there are uncommitted changes, Please commit your changes before proceeding.')); process.exit(1); } } if (noPreRelease) return; if (releaseType === _shared_1.ReleaseTypeEnums.Prerelease && this.versionFileUtils.findScript('prerelease-beta')) { return await this.versionFileUtils.runFindScript('prerelease-beta', 'beta release'); } if (this.versionFileUtils.findScript('prerelease')) { return await this.versionFileUtils.runFindScript('prerelease', 'pre release'); } }; confirmRelease = async (newVersion) => { const answer = await (0, _api_1.promptConfirm)(chalk_1.default.green(`are you sure you want to release with version ${chalk_1.default.blue(newVersion)}?`)); if (!answer) { _shared_1.log.warn('aborted release.'); return false; } return true; }; postRelease = (noPostRelease) => { if (noPostRelease) return; const msg = 'post release'; if (this.versionFileUtils.findScript('postrelease')) { return this.versionFileUtils.runFindScript('postrelease', msg); } if (this.versionFileUtils.findScript('postreleasy')) { return this.versionFileUtils.runFindScript('postreleasy', msg); } }; updateChangelog = (changelogVersion, changelog) => { if ((0, fs_extra_1.existsSync)(this.versionFileUtils.changelogPath)) { let data; try { data = (0, fs_extra_1.readFileSync)(this.versionFileUtils.changelogPath).toString(); } catch (e) { _shared_1.log.error(`error reading file: ${e}`); process.exit(1); } if (data.indexOf(_shared_1.unreleased) < 0) { _shared_1.log.info(chalk_1.default.red.bold(`i can't update your CHANGELOG. :( \n Make your CHANGELOG great again and follow the CHANGELOG format http://keepachangelog.com/en/1.0.0/`)); } else { const position = data.indexOf(_shared_1.unreleased) + _shared_1.unreleased.length; const bufferedText = Buffer.from(`${changelogVersion}\n${changelog}${data.substring(position)}`); const file = (0, fs_extra_1.openSync)(this.versionFileUtils.changelogPath, 'r+'); try { (0, fs_extra_1.writeSync)(file, bufferedText, 0, bufferedText.length, position); (0, fs_extra_1.close)(file); _shared_1.log.info(`successfully added the new changes to the CHANGELOG.md file.`); } catch (e) { _shared_1.log.error(`error writing file: ${e}`); process.exit(1); } } } }; getChangelogDate = (newVersion) => { const lastTag = (0, _shared_1.getTheLastTag)(this.versionFileUtils.root); const originUrl = (0, _shared_1.getOriginUrl)(this.versionFileUtils.root); let compareTagUrl = ''; if (lastTag) { // Support for Azure DevOps and GitHub only for now if (originUrl.includes('https://dev.azure.com')) { compareTagUrl = `${originUrl}/branchCompare?baseVersion=GT${lastTag?.replace(/\n+$/, '')}&targetVersion=GTv${newVersion}`; } else { compareTagUrl = `${originUrl}/compare/${lastTag?.replace(/\n+$/, '')}...v${newVersion}`; } } else { if (originUrl.includes('https://dev.azure.com')) { compareTagUrl = `${originUrl}?version=GTv${newVersion}`; } else { compareTagUrl = `${originUrl}/releases/tag/v${newVersion}`; } } const [month, day, year] = new Date() .toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', }) .split('/'); return `\n\n## [${newVersion}](${compareTagUrl}) - (${year}-${month}-${day})`; }; getRelease = (tagName) => { let releaseType = tagName !== 'stable' ? 'prerelease' : null; let changelog = ''; if (tagName === 'stable') { const commits = (0, _shared_1.getGitCommits)(this.versionFileUtils.root).toString(); const changelogContent = (0, changelog_1.organizeCommitsToChangelog)(commits, (0, _shared_1.getOriginUrl)(this.versionFileUtils.root)); releaseType = releaseType ? releaseType : changelogContent.releaseType; changelog = changelogContent.changelog; } if (!releaseType) { _shared_1.log.error(_api_1.Colors.ERROR(`invalid release type: ${tagName}`)); process.exit(1); } const oldVersion = this.versionFileUtils.readVersion(); const newVersion = this.versionFileUtils.incrementVersion(oldVersion, releaseType, tagName); // validate version (0, changelog_1.validateVersion)(oldVersion, releaseType, tagName); const tagText = `v${newVersion}`; const changelogVersion = this.getChangelogDate(String(newVersion)); return { releaseType, oldVersion, newVersion: String(newVersion), tagText, changelogVersion, changelog, }; }; } exports.ReleaseUtils = ReleaseUtils;