UNPKG

extract-changelog-release

Version:

Extract release notes from latest entry in standard-version changelog

60 lines (59 loc) 2.97 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractLog = void 0; const path_1 = __importDefault(require("path")); const fs = __importStar(require("fs")); const minimist_1 = __importDefault(require("minimist")); /* ****************************************************************************************************************** * * Config * ****************************************************************************************************************** */ const headerMatchRegex = /^#+[^\S\r\n]+\[\d+\.\d+\.\d+\S*?]\(.+?\)$|^#+[^\S\r\n]+\d+\.\d+\.\d+\S*?[^\S\r\n]+\(.+?\)$/gm; /* ****************************************************************************************************************** * * Script * ****************************************************************************************************************** */ function extractLog(changeLogPath) { var _a; changeLogPath = !changeLogPath ? path_1.default.resolve(process.cwd(), 'CHANGELOG.md') : path_1.default.isAbsolute(changeLogPath) ? changeLogPath : path_1.default.resolve(process.cwd(), changeLogPath); if (!fs.existsSync(changeLogPath)) throw new Error(`Cannot resolve file: ${changeLogPath}`); const fileData = fs.readFileSync(changeLogPath, 'utf-8'); headerMatchRegex.lastIndex = void 0; const startMatch = headerMatchRegex.exec(fileData); if (!startMatch) throw new Error(`Could not find matching header in changelog!`); const endMatch = (_a = headerMatchRegex.exec(fileData)) !== null && _a !== void 0 ? _a : { 0: '', index: fileData.length }; return fileData .slice(startMatch.index, endMatch.index) .replace(/((?:\r?\n){2})(?:\r?\n)+/g, '$1') .trim(); } exports.extractLog = extractLog; if (require.main === module) { const res = extractLog(minimist_1.default(process.argv.slice(2))._[0]); process.stdout.write(res); }