|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import gitCommitInfo from 'git-commit-info'; |
| 3 | +import getCommitRange from 'git-commit-range'; |
| 4 | +import moment from 'moment'; |
| 5 | +import path from 'path'; |
| 6 | +import prependFile from 'prepend-file'; |
| 7 | +import taggedCommits from 'tagged-git-commits'; |
| 8 | +import { |
| 9 | + header, |
| 10 | + body, |
| 11 | +} from './changelogParts'; |
| 12 | + |
| 13 | +const writeBackup = () => { |
| 14 | + const cwd = process.cwd(); |
| 15 | + |
| 16 | + fs.renameSync( |
| 17 | + path.join(cwd, 'CHANGELOG.md'), |
| 18 | + path.join(cwd, '.sgr_backup', `CHANGELOG.${moment().unix()}.bak.md`), |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +const backupChangelog = () => { |
| 23 | + const cwd = process.cwd(); |
| 24 | + |
| 25 | + if (fs.existsSync(path.join(cwd, '.sgr_backup'))) { |
| 26 | + return writeBackup(); |
| 27 | + } |
| 28 | + |
| 29 | + fs.mkdirSync(path.join(cwd, '.sgr_backup')); |
| 30 | + |
| 31 | + return writeBackup(); |
| 32 | +}; |
| 33 | + |
| 34 | +const getAllTags = () => { |
| 35 | + const cwd = process.cwd(); |
| 36 | + const tags = taggedCommits({ |
| 37 | + path: cwd, |
| 38 | + lookBehind: Number.POSITIVE_INFINITY, |
| 39 | + }); |
| 40 | + |
| 41 | + return tags; |
| 42 | +}; |
| 43 | + |
| 44 | +const writeToFile = (tags, exists, backup) => { |
| 45 | + const cwd = process.cwd(); |
| 46 | + let changelogData = ''; |
| 47 | + let commits = []; |
| 48 | + let tagDate = ''; |
| 49 | + |
| 50 | + if (exists && backup) { |
| 51 | + backupChangelog(); |
| 52 | + } else if (exists) { |
| 53 | + fs.truncateSync(path.join(cwd, 'CHANGELOG.md'), 0); |
| 54 | + } |
| 55 | + |
| 56 | + tags.forEach((tag, idx) => { |
| 57 | + if (idx === 0) { |
| 58 | + commits = getCommitRange({ |
| 59 | + path: cwd, |
| 60 | + }); |
| 61 | + commits = getCommitRange({ |
| 62 | + path: cwd, |
| 63 | + from: commits[commits.length - 1], |
| 64 | + to: tag.hash, |
| 65 | + }); |
| 66 | + } else { |
| 67 | + commits = getCommitRange({ |
| 68 | + path: cwd, |
| 69 | + from: tags[idx - 1].hash, |
| 70 | + to: tag.hash, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + tagDate = gitCommitInfo({ |
| 75 | + cwd, |
| 76 | + commit: tag.hash, |
| 77 | + }).date; |
| 78 | + |
| 79 | + const version = tag.version.slice(1, tag.version.length); |
| 80 | + |
| 81 | + changelogData = `${header(version, tagDate)}\n\n${body(commits, version)}`; |
| 82 | + |
| 83 | + prependFile.sync(path.join(cwd, 'CHANGELOG.md'), changelogData); |
| 84 | + }); |
| 85 | +}; |
| 86 | + |
| 87 | +const generateCompleteChangelog = (backup) => { |
| 88 | + const cwd = process.cwd(); |
| 89 | + |
| 90 | + try { |
| 91 | + const exists = fs.existsSync(path.join(cwd, 'CHANGELOG.md')); |
| 92 | + |
| 93 | + if (!exists) { |
| 94 | + fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), ''); |
| 95 | + } |
| 96 | + |
| 97 | + return writeToFile(getAllTags(), exists, backup); |
| 98 | + } catch (err) { |
| 99 | + return false; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +export default generateCompleteChangelog; |
0 commit comments