-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: recover the complete changelog.md (#10)
* Style: add red color to warnings * Feat: recover the complete changelog.md * Test: for new feature complete changelog.md * Feat: cli support for recover complete changelog.md * Chore: add new dependecies * Docs: add description for new feat * Fix: problem with babel * CI: tmp fix, downgrade to [email protected] on windows * Refactor: remove unnecessary try catch * Test: update for better coverage * Test: remove Object.keys * Test: fix tests * Refactor: create changelog parts * Test: update for new feat and update to fs-extra * Style: remove index
- Loading branch information
Showing
12 changed files
with
698 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import moment from 'moment'; | ||
import gitCommitInfo from 'git-commit-info'; | ||
|
||
const header = (version, date = '') => { | ||
if (date === '') { | ||
return `${version} - ${moment().format('MMMM, DD YYYY')}`; | ||
} | ||
|
||
return `${version} - ${moment(date, 'ddd MMM D HH:mm:ss YYYY Z').format('MMMM, DD YYYY')}`; | ||
}; | ||
|
||
const oneCommit = (commitInfo) => ( | ||
`* ${commitInfo.shortHash} ${commitInfo.message.split('\n')[0]} (${commitInfo.author})` | ||
); | ||
|
||
const body = (commits, version) => { | ||
const cwd = process.cwd(); | ||
let changelogData = ''; | ||
|
||
commits.forEach((commit, i) => { | ||
const commitInfo = gitCommitInfo({ commit, cwd }); | ||
|
||
/* istanbul ignore next */ | ||
if (!commitInfo.shortHash || | ||
!commitInfo.author || | ||
!commitInfo.message || | ||
commitInfo.message.split('\n')[0] === version) { | ||
return; | ||
} | ||
|
||
changelogData = `${changelogData} ${oneCommit(commitInfo)}\n`; | ||
|
||
if (commits.length - 1 === i) { | ||
changelogData = `${changelogData}\n`; | ||
} | ||
}); | ||
|
||
return changelogData; | ||
}; | ||
|
||
export { | ||
header, | ||
body, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import fs from 'fs-extra'; | ||
import gitCommitInfo from 'git-commit-info'; | ||
import getCommitRange from 'git-commit-range'; | ||
import moment from 'moment'; | ||
import path from 'path'; | ||
import prependFile from 'prepend-file'; | ||
import taggedCommits from 'tagged-git-commits'; | ||
import { | ||
header, | ||
body, | ||
} from './changelogParts'; | ||
|
||
const writeBackup = () => { | ||
const cwd = process.cwd(); | ||
|
||
fs.renameSync( | ||
path.join(cwd, 'CHANGELOG.md'), | ||
path.join(cwd, '.sgr_backup', `CHANGELOG.${moment().unix()}.bak.md`), | ||
); | ||
}; | ||
|
||
const backupChangelog = () => { | ||
const cwd = process.cwd(); | ||
|
||
if (fs.existsSync(path.join(cwd, '.sgr_backup'))) { | ||
return writeBackup(); | ||
} | ||
|
||
fs.mkdirSync(path.join(cwd, '.sgr_backup')); | ||
|
||
return writeBackup(); | ||
}; | ||
|
||
const getAllTags = () => { | ||
const cwd = process.cwd(); | ||
const tags = taggedCommits({ | ||
path: cwd, | ||
lookBehind: Number.POSITIVE_INFINITY, | ||
}); | ||
|
||
return tags; | ||
}; | ||
|
||
const writeToFile = (tags, exists, backup) => { | ||
const cwd = process.cwd(); | ||
let changelogData = ''; | ||
let commits = []; | ||
let tagDate = ''; | ||
|
||
if (exists && backup) { | ||
backupChangelog(); | ||
} else if (exists) { | ||
fs.truncateSync(path.join(cwd, 'CHANGELOG.md'), 0); | ||
} | ||
|
||
tags.forEach((tag, idx) => { | ||
if (idx === 0) { | ||
commits = getCommitRange({ | ||
path: cwd, | ||
}); | ||
commits = getCommitRange({ | ||
path: cwd, | ||
from: commits[commits.length - 1], | ||
to: tag.hash, | ||
}); | ||
} else { | ||
commits = getCommitRange({ | ||
path: cwd, | ||
from: tags[idx - 1].hash, | ||
to: tag.hash, | ||
}); | ||
} | ||
|
||
tagDate = gitCommitInfo({ | ||
cwd, | ||
commit: tag.hash, | ||
}).date; | ||
|
||
const version = tag.version.slice(1, tag.version.length); | ||
|
||
changelogData = `${header(version, tagDate)}\n\n${body(commits, version)}`; | ||
|
||
prependFile.sync(path.join(cwd, 'CHANGELOG.md'), changelogData); | ||
}); | ||
}; | ||
|
||
const generateCompleteChangelog = (backup) => { | ||
const cwd = process.cwd(); | ||
|
||
try { | ||
const exists = fs.existsSync(path.join(cwd, 'CHANGELOG.md')); | ||
|
||
if (!exists) { | ||
fs.writeFileSync(path.join(cwd, 'CHANGELOG.md'), ''); | ||
} | ||
|
||
return writeToFile(getAllTags(), exists, backup); | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
|
||
export default generateCompleteChangelog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Listr from 'listr'; | ||
|
||
import generateCompleteChangelog from '../helpers/generateCompleteChangelog'; | ||
|
||
const recoverTasks = (backup) => ( | ||
new Listr([ | ||
{ | ||
title: 'Recover the complete CHANGELOG.md', | ||
task: () => { | ||
generateCompleteChangelog(backup); | ||
}, | ||
}, | ||
]) | ||
); | ||
|
||
export default recoverTasks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.