Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | import fs from 'fs'; import os from 'os'; import path from 'path'; import { ChangelogFiles, ChangelogFileData, ComponentData } from './types'; import Logger from './Logger'; interface ChangelogArguments { changelogFileName: string; pathToGlobalChangelog?: string; globalChangelogFormat?: string; } export default class ChangelogModule { changelogFileName: string = 'CHANGELOG.md'; globalChangelogFormat: string; pathToGlobalChangelog?: string; files: ChangelogFiles = {}; globalChangelog?: ChangelogFileData; constructor( { changelogFileName, pathToGlobalChangelog, globalChangelogFormat = '- [%name%@%version%]: %msg%', }: ChangelogArguments, logger: Logger ) { 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.resolve(pathToGlobalChangelog)); } } isset(path: string): Boolean { return fs.existsSync(`${path}/${this.changelogFileName}`); } parse(path: string): ChangelogFileData { const md: string = fs.readFileSync(path, 'utf8'); const rows: string[] = md.split(/\r?\n/g); return { lines: rows, unrealised: [], unrealisedLineNumber: 0 }; } add(path: string): ChangelogFileData { this.files[path] = this.parse(`${path}/${this.changelogFileName}`); return this.files[path]; } get(path: string): ChangelogFileData { return this.files[path]; } isUnrealized(path: string): Boolean { const changelog: ChangelogFileData = this.get(path); return !!(changelog && changelog.unrealised.length); } read(pathOrChangelog: string | ChangelogFileData): ChangelogFileData { const changelog: ChangelogFileData = typeof pathOrChangelog === 'string' ? this.add(pathOrChangelog) : pathOrChangelog; // Разбор changelog let isUnrelised = false; for (const lineNumber in changelog.lines) { const line: string = 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: string, version: string): Boolean { const changelog: ChangelogFileData = this.get(path); if (!changelog) { return false; } const headLine: string = `## [${version}] - ${this.getDateString()}`; changelog.lines.splice(changelog.unrealisedLineNumber + 1, 0, '', '---', headLine); const lines: string[] = changelog.lines; fs.writeFileSync(`${path}/${this.changelogFileName}`, lines.join(os.EOL), 'utf8'); return true; } getDateString(): string { const date: Date = new Date(); const day: string = `0${date.getDate()}`.slice(-2); const month: string = `0${date.getMonth() + 1}`.slice(-2); return `${date.getFullYear()}-${month}-${day}`; } writeGlobalChangelog(path: string, component: ComponentData) { if (!this.globalChangelog) { return false; } const changelog: ChangelogFileData = this.get(path); if (!changelog) { return false; } const changedMark: string = '### Changed'; let index: number = 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: string = 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: string[] = this.globalChangelog.lines; fs.writeFileSync(`${this.pathToGlobalChangelog}`, globalLines.join(os.EOL), 'utf8'); } indexOfMarkInUnrelized(changelog: ChangelogFileData, mark: string): number { for (let i = changelog.unrealisedLineNumber + 1; changelog.lines.length > i; i++) { const row: string = changelog.lines[i]; if (row.match(/^## (.*)/i)) { return -1; } if (row.includes(mark)) { return i; } } return -1; } } |