component-version-update
Version:
Обновляет версию пакета в файлах package.json, CHANGELOG.md с формированием коммита с изменениями
124 lines • 5.03 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
class ChangelogModule {
constructor({ changelogFileName, pathToGlobalChangelog, globalChangelogFormat = '- [%name%@%version%]: %msg%', }, logger) {
this.changelogFileName = 'CHANGELOG.md';
this.files = {};
this.changelogFileName = changelogFileName || this.changelogFileName;
this.globalChangelogFormat = globalChangelogFormat;
this.pathToGlobalChangelog = pathToGlobalChangelog;
if (pathToGlobalChangelog) {
this.globalChangelog = this.parse(pathToGlobalChangelog);
this.read(this.globalChangelog);
logger.info(`Global changelog found in`, path_1.default.resolve(pathToGlobalChangelog));
}
}
isset(path) {
return fs_1.default.existsSync(`${path}/${this.changelogFileName}`);
}
parse(path) {
const md = fs_1.default.readFileSync(path, 'utf8');
const rows = md.split(/\r?\n/g);
return { lines: rows, unrealised: [], unrealisedLineNumber: 0 };
}
add(path) {
this.files[path] = this.parse(`${path}/${this.changelogFileName}`);
return this.files[path];
}
get(path) {
return this.files[path];
}
isUnrealized(path) {
const changelog = this.get(path);
return !!(changelog && changelog.unrealised.length);
}
read(pathOrChangelog) {
const changelog = typeof pathOrChangelog === 'string' ? this.add(pathOrChangelog) : pathOrChangelog;
// Разбор changelog
let isUnrelised = false;
for (const lineNumber in changelog.lines) {
const line = changelog.lines[lineNumber];
if (isUnrelised) {
const matches = line.match(/^### ([a-z]*)/i);
if (line.match(/^## (.*)/i)) {
isUnrelised = false;
}
else if (!matches && line.length) {
const desc = line.replace(/^([^a-zа-яё])*/i, '');
if (desc.length) {
changelog.unrealised.push(desc);
}
}
}
else if (line.toLowerCase().includes('unreleased')) {
changelog.unrealisedLineNumber = +lineNumber;
isUnrelised = true;
}
}
return changelog;
}
upVersion(path, version) {
const changelog = this.get(path);
if (!changelog) {
return false;
}
const headLine = `## [${version}] - ${this.getDateString()}`;
changelog.lines.splice(changelog.unrealisedLineNumber + 1, 0, '', '---', headLine);
const lines = changelog.lines;
fs_1.default.writeFileSync(`${path}/${this.changelogFileName}`, lines.join(os_1.default.EOL), 'utf8');
return true;
}
getDateString() {
const date = new Date();
const day = `0${date.getDate()}`.slice(-2);
const month = `0${date.getMonth() + 1}`.slice(-2);
return `${date.getFullYear()}-${month}-${day}`;
}
writeGlobalChangelog(path, component) {
if (!this.globalChangelog) {
return false;
}
const changelog = this.get(path);
if (!changelog) {
return false;
}
const changedMark = '### Changed';
let index = this.indexOfMarkInUnrelized(this.globalChangelog, changedMark);
if (index < 0) {
this.globalChangelog.lines.splice(this.globalChangelog.unrealisedLineNumber + 1, 0, '', changedMark);
index = this.globalChangelog.unrealisedLineNumber + 2;
}
else {
index++;
}
const line = this.globalChangelogFormat
.replace(/%name%/g, component.data.name)
.replace(/%version%/g, component.data.version)
.replace(/%data%/g, this.getDateString())
.replace(/%link%/g, `/${path}/${this.changelogFileName}`)
.replace(/%msg%/g, changelog.unrealised.join(', '));
this.globalChangelog.lines.splice(index + 1, 0, line);
const globalLines = this.globalChangelog.lines;
fs_1.default.writeFileSync(`${this.pathToGlobalChangelog}`, globalLines.join(os_1.default.EOL), 'utf8');
}
indexOfMarkInUnrelized(changelog, mark) {
for (let i = changelog.unrealisedLineNumber + 1; changelog.lines.length > i; i++) {
const row = changelog.lines[i];
if (row.match(/^## (.*)/i)) {
return -1;
}
if (row.includes(mark)) {
return i;
}
}
return -1;
}
}
exports.default = ChangelogModule;
//# sourceMappingURL=ChangelogModule.js.map