diff --git a/README.md b/README.md index d448753..95a774f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ # gitool Gitool helps you always have the right format for your commit message. -Following https://www.conventionalcommits.org/en/v1.0.0-beta.2/ +Following https://www.conventionalcommits.org/en/v1.0.0/ ## alias any gitool command can be called with ```gitool``` or ```gt``` @@ -24,22 +24,20 @@ or ```gitool -cl=3``` ## pretty print -```gitool --prettyPrint``` -```gitool -p``` (groupBy type by default) +```gitool --prettyPrint --pn=ProjectName --v=1.23.2``` +```gitool -p --pn=ProjectName --v=1.23.2``` (groupBy type by default) -```gitool -p groupBy='type'``` +```gitool -p groupBy='type' --pn=ProjectName --v=1.23.2``` -```gitool -pg='type'``` -```gitool -pg='scope'``` +```gitool -pg='type' --pn=ProjectName --v=1.23.2``` +```gitool -pg='scope' --pn=ProjectName --v=1.23.2``` -```gitool -pg='scope' --tag=3``` - -```gitool -pg='type' --tag=3 --filters='feat|fix'``` +```gitool -pg='type' --filters='feat|fix' --pn=ProjectName --v=1.23.2``` ![alt text](https://raw.githubusercontent.com/stefdelec/gitool/master/readme/commitprettier.png) -```gitool -p --tag``` output commits from last tag +```gitool -p --pn=ProjectName --v=1.23.2``` output commits from last tag Output a file: @@ -50,7 +48,7 @@ Output a file: Add this in your package.json ``` - "prepublish": "npm test && npm version minor && node index.js -pao=changelog.md --tag=1 && git push" + "postversion": "gitool -p --pn=ProjectName --v=1.23.2" ``` ## message composer @@ -68,12 +66,12 @@ and ## cane shortcut for ``` -git add . && git commit --amend --no-edit +git add . && git commit -m "release(changelog): Update changelog.md [CI SKIP]" ``` ## force -rebase on develop or branch + push force +rebase on master or branch + push force ```gitool -force``` -```gitool -force -branch=master``` -```gitool -fb=master``` +```gitool -force -branch=develop``` +```gitool -fb=develop``` diff --git a/changelog.md b/changelog.md index 01d1064..8e87254 100644 --- a/changelog.md +++ b/changelog.md @@ -1,15 +1,75 @@ +# gitool # Release notes +## v1.24.0 ( 2021-09-1 ) -**version**: From v1.20.0 to v1.21.0 +### **feat (1):** + - feat(changelog): Append commits in change log file -## **feat (3):** +## v1.22.0 ( 2021-06-19 ) + +### **feat (1):** + - feat(force): git rebase and push + +## v1.21.0 ( 2021-02-19 ) + +### **feat (3):** - feat(cane): short cut for add commit no edit - feat(console): improve console log - feat(readme): improve package.json bump +## v1.20.0 ( 2021-02-19 ) +### **docs (15):** + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs: changelog + - docs(readme): update readme with link to changelog + - docs: changelog + - docs(prettyPrint): update changelog pretty print + - docs(readme): improve readme and add images + - docs(readme): update readme with gitool alias (gt) +### **feat (22):** + - feat(readme): improve readme + - feat(autofetch): auto fetch tags before creating changelog + - feat(changelog): improve changelog title + - feat(changelog): changelog only on last tag + - feat(prettyPrint): can add all or not + - feat(prettyPrint): add changelog in preversion + - feat(prettyPrint): add filters to type of pretty print + - feat(prettyPrint): pretty print from tag + - feat(prettyPrint): quick changelog + - feat(readme): update readme and package.json + - feat(prettyPrint): can pass --tag and output from last tag + - feat(prettyPrint): can output a file + - feat(prettyPrint): pretty print group by type or scope + - feat(messageComposer): can add all modified file before commiting with -am + - feat(bin): commit is not executed if message and type are undefined + - feat(bin): gt is a new alias for gitool + - feat(commitChecker): version bump commit by npm is accepted + - feat: undefined + - feat(bin): add aliases for existing commands + - feat(messageComposer): add functionaly message composer + - feat(readme): create a simple README.md + - feat(bin): bin is packed +### **fix (8):** + - fix(changelog): fix change log tag title + - fix(prettyPrint): version of pretty print was incorrect + - fix(travis): fix travis test + - fix(readme): fix link to changelog + - fix(messageComposer): ex was undefined for git add . + - fix(messageComposer): remove automatic add . + - fix(commitChecker): fix default value of last + - fix(bin): bin command was not gitool - +### **test (1):** + - test(travis): add travis ci diff --git a/index.js b/index.js index 362f74a..133ec85 100755 --- a/index.js +++ b/index.js @@ -3,11 +3,14 @@ const commitMessageChercher = require('./src/commit-message-checker/commit-messa const shortCut = require('./src/gitShortCut'); const messageComposer = require('./src/message-composer/message-composer') const chalk = require('chalk'); +const { existsSync } = require('fs'); const argv = require('yargs') .alias('c', 'checkCommit') .alias('o', 'output') .alias('p', 'prettyPrint') + .alias('pn','projectName') + .alias('v','versionTag') .alias('a', 'addAll') .alias('m', 'messageComposer') .alias('t', 'test') @@ -17,7 +20,7 @@ const argv = require('yargs') .alias('b','branch') .argv -const { prettyPrint, write } = require('./src/commit-prettier/commit-prettier'); +const { prettyPrint } = require('./src/commit-prettier/commit-prettier'); // Check Last commit if (argv.checkCommit) { @@ -40,7 +43,6 @@ if (argv.checkCommit) { console.log(chalk.red('commit message is NOT valid')); throw new Error() } - console.log('') } } @@ -48,27 +50,48 @@ if(argv.bump){ console.log(argv.bump); } if (argv.prettyPrint) { + if(!argv.projectName){ + throw new Error('Project name is mandatory'); + } + + if(!argv.versionTag){ + throw new Error('Version tag is mandatory'); + } console.log('Fetching tags from origin'); shortCut.ex("git fetch origin --tags"); const groupBy = argv.groupBy === 'scope' ? 'scope' : 'type'; - const tags = shortCut.getTagsOrderedByDate().split('\n').reverse(); - - const numOftag=parseInt(argv.tag); + const tags = shortCut.getTagsOrderedByDate().split('\n').filter(t => t !== ''); const getFromTag = (tagNumber) => tags[+tagNumber]; - const commitFrom = argv.tag ? getFromTag(numOftag) : shortCut.firstCommitAllTime().trim(); - - let title = `From ${getFromTag(numOftag)} to ${tags[0]}`; + const firstCommit = shortCut.firstCommitAllTime().trim() const filters = argv.filters ? argv.filters.split('|') : undefined; - prettyPrint(commitFrom, groupBy, argv.output, title, filters); + const path = argv.output || './changelog.md'; + + const getDateOfTag = (tag) => ((shortCut.getDateTimeOfTag(tag).trim().split(' '))[0]); + const dateNow = (now = new Date()) => { + let month = now.getMonth() + 1; + month = month > 9 ? month : `0${month}`; + return `${now.getFullYear()}-${month}-${now.getDate()}`; + } + + if(!existsSync(path)){ + for (let i = 0; i < tags.length; i++) { + const date = getDateOfTag(getFromTag(i)); + prettyPrint(i !== 0 ? getFromTag(i - 1) : firstCommit, getFromTag(i), groupBy, path, argv.projectName || 'untitled', getFromTag(i), filters, date); + } + } + + prettyPrint(tags.length !== 0 ? getFromTag(tags.length - 1) : shortCut.firstCommitAllTime().trim(), 'HEAD', groupBy, path, argv.projectName || 'untitled', argv.versionTag, filters, dateNow()); + + if (argv.addAll) { console.log('Adding files to previous commit'); shortCut.ex('git add .') - shortCut.ex('git commit --amend --no-edit') + shortCut.ex('git commit -m "release(changelog): Update changelog.md [CI SKIP]"') } } @@ -85,11 +108,11 @@ if (argv.messageComposer) { } if(argv.force){ - const branch=argv.branch || 'develop'; - shortCut.ex(`git add . && git commit --amend --no-edit && git fetch && git pull origin ${branch} --rebase && git push origin HEAD --force`); + const branch=argv.branch || 'master'; + shortCut.ex(`git add . && git commit -m "release(changelog): Update changelog.md [CI SKIP]" && git fetch && git pull origin ${branch} --rebase && git push origin HEAD --force`); } if(argv.cane){ shortCut.ex('git add .') - shortCut.ex('git commit --amend --no-edit') + shortCut.ex('git commit -m "release(changelog): Update changelog.md [CI SKIP]"') } diff --git a/package-lock.json b/package-lock.json index 4bd6db3..1e7b846 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "gitool", - "version": "1.21.0", + "version": "1.24.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3258,12 +3258,9 @@ "integrity": "sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=" }, "json5": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.1.tgz", - "integrity": "sha512-l+3HXD0GEI3huGq1njuqtzYK8OYJyXMkOLtQ53pjWh89tvWS2h6l+1zMkYWqlb57+SiQodKZyvMEFb2X+KrFhQ==", - "requires": { - "minimist": "^1.2.0" - } + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==" }, "jsprim": { "version": "1.4.1", diff --git a/package.json b/package.json index 4d3356b..36a4739 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { - "name": "gitool", - "version": "1.21.0", + "name": "@arianee/gitool", + "version": "1.24.0", "description": "", "main": "index.js", "scripts": { "test": "jest", - "prepublish": "npm test && npm version minor && node index.js -pao=changelog.md --tag=1 && git push" + "postversion": "node index.js -po=changelog.md --pn=gitool --v=1.25.0" }, "bin": { "gitool": "index.js", @@ -13,9 +13,10 @@ }, "repository": { "type": "git", - "url": "https://github.com/stefdelec/gitool" + "url": "https://github.com/Arianee/gitool" }, "access": "public", + "private": false, "keywords": [], "author": "", "license": "ISC", diff --git a/src/commit-extractor/commit-extractor.js b/src/commit-extractor/commit-extractor.js index a9326e8..7139957 100644 --- a/src/commit-extractor/commit-extractor.js +++ b/src/commit-extractor/commit-extractor.js @@ -1,5 +1,5 @@ const commitExtractor = (commitMessage) => { - const reg = new RegExp(/(feat|fix|docs|style|refactor|test|chore)(\((\w{1,})\)){0,1}: (.{1,})/) + const reg = new RegExp(/(feat|fix|docs|style|refactor|test|chore|release)(\((\w{1,})\)){0,1}: (.{1,})/) const result = commitMessage.match(reg) if (result) { return { diff --git a/src/commit-message-checker/commit-message-checker.spec.js b/src/commit-message-checker/commit-message-checker.spec.js index be254bc..ea23a2a 100644 --- a/src/commit-message-checker/commit-message-checker.spec.js +++ b/src/commit-message-checker/commit-message-checker.spec.js @@ -10,6 +10,7 @@ describe('commit message checker', () => { 'chore: zefe', '2.0.1', '02.02.001', + 'release(changelog): Update changelog.md [CI SKIP]', ] acceptedCommit.forEach(commitMessage => { diff --git a/src/commit-prettier/commit-prettier.js b/src/commit-prettier/commit-prettier.js index 8e4b723..5a76194 100644 --- a/src/commit-prettier/commit-prettier.js +++ b/src/commit-prettier/commit-prettier.js @@ -1,8 +1,8 @@ -const { allCommitSince ,gitAddAll } = require('../gitShortCut'); +const { commitBetweenTags } = require('../gitShortCut'); const { types } = require('../configuration'); const { commitExtractor } = require('../commit-extractor/commit-extractor'); const Mustache = require('mustache'); -const { readFileSync, writeFileSync } = require("fs"); +const { readFileSync, writeFileSync, existsSync } = require("fs"); const addToAcc = (acc, commitType, value, filters) => { const shouldAdd=!filters || filters.length === 0 || filters.includes(commitType); @@ -49,8 +49,8 @@ const groupByType = (commitLogs, filters) => { }, {}) } -const groupCommits = (commitHash, groupBy,filters) => { - const commitsLog = allCommitSince(commitHash); +const groupCommits = (from, to, groupBy,filters) => { + const commitsLog = commitBetweenTags(from, to); let groupedCommit if (groupBy === 'type') { @@ -69,28 +69,39 @@ const write = (view) => { eval(output); } -const prettyPrint = (commitHash, groupBy, path, title = 'untitled', filters) => { - const groupedCommit = groupCommits(commitHash, groupBy, filters); + +const prettyPrint = (from, to, groupBy, path, projectName, versionTag, filters, date) => { + const groupedCommit = groupCommits(from, to, groupBy, filters); const data = { - title, + projectName, + versionTag: versionTag.replace('v', ''), + date, sections: Object.keys(groupedCommit).map(key => groupedCommit[key]) } const template = readFileSync(__dirname + '/template.terminal.txt').toString(); + const titleTemplate = readFileSync(__dirname + '/template.title.txt').toString(); const fileTemplate = readFileSync(__dirname + '/template.file.txt').toString(); const consoleLogs = Mustache.render(template, data); - - const file = Mustache.render(fileTemplate, data); + const title = Mustache.render(titleTemplate, data); + const changelog = Mustache.render(fileTemplate, data); eval(consoleLogs); if (path) { - console.log('Writing changelog at: ', path); - writeFileSync(path, file); + if (existsSync(path)) { // file exists + console.log('Writing changelog at: ', path); + const data = readFileSync(path, { encoding:'utf8' }); + const newdata = data.replace(title, title + changelog); + writeFileSync(path, newdata); + } else { // file not exists + console.log('Generating and writing changelog at: ', path); + writeFileSync(path, title + changelog); + } } } diff --git a/src/commit-prettier/template.file.txt b/src/commit-prettier/template.file.txt index 16c314a..08e7db4 100644 --- a/src/commit-prettier/template.file.txt +++ b/src/commit-prettier/template.file.txt @@ -1,17 +1,9 @@ -# Release notes - -**version**: {{title}} +## v{{versionTag}} ( {{date}} ) {{#sections}} -## **{{name}} ({{length}}):** +### **{{name}} ({{length}}):** {{#content}} - {{.}} {{/content}} {{/sections}} - - - - - - diff --git a/src/commit-prettier/template.terminal.txt b/src/commit-prettier/template.terminal.txt index 480bdb5..0dfb913 100644 --- a/src/commit-prettier/template.terminal.txt +++ b/src/commit-prettier/template.terminal.txt @@ -1,13 +1,15 @@ var chalk = require('chalk'); -console.log(chalk.bold.blue('version: {{title}}')); +console.log(chalk.bold.blue('### {{projectName}} ###')); +console.log(''); +console.log(chalk.bold.blue('version: v{{versionTag}}')); console.log('') {{#sections}} console.log(chalk.green('##########################')) console.log(chalk.bold.green('### {{name}} ({{length}})')) console.log(chalk.green('##########################')) {{#content}} -console.log(chalk.blue('{{.}}')) +console.log(chalk.blue('{{.}}')) {{/content}} console.log('') diff --git a/src/commit-prettier/template.title.txt b/src/commit-prettier/template.title.txt new file mode 100644 index 0000000..bbe35da --- /dev/null +++ b/src/commit-prettier/template.title.txt @@ -0,0 +1,2 @@ +# {{projectName}} +# Release notes diff --git a/src/configuration.js b/src/configuration.js index 964a837..496e71c 100644 --- a/src/configuration.js +++ b/src/configuration.js @@ -1,5 +1,5 @@ module.exports = { - types: ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore'], + types: ['feat', 'fix', 'docs', 'style', 'refactor', 'test', 'chore', 'release'], examples: { feat: "feature", fix: "bug fix", @@ -7,7 +7,8 @@ module.exports = { style: "formatting, missing semi colons", refactor: 'a refacto of a class', test: "when adding missing tests", - chore: "maintain" + chore: "maintain", + release: "when release", } }; diff --git a/src/gitShortCut.js b/src/gitShortCut.js index 5748f1a..f3ef0e0 100644 --- a/src/gitShortCut.js +++ b/src/gitShortCut.js @@ -6,21 +6,21 @@ const dateOfCommit = (commitHash) => ex(`git show -s --format=%ci ${commitHash}` const hashLastCommit = (number) => ex(`git log --skip ${number - 1} -n 1 --pretty=%H --no-merges`) const messageLastCommit = (number) => ex(`git log --skip ${number - 1} -n 1 --pretty=%B --no-merges`) const lastTag = () => ex(`git describe --abbrev=0 --tags`) -const allCommitSince = (commitHash) => ex(`git log ${commitHash}...HEAD --pretty=format:%s --no-merges`); const firstCommitAllTime = () => ex('git rev-list --max-parents=0 HEAD') -const commitBetweenTags = (tag1, tag2) => ex(`git log --pretty=oneline ${tag1}...${tagB}`) +const commitBetweenTags = (from, to) => ex(`git log ${from}...${to} --pretty=format:%s --no-merges`) const getTagsOrderedByDate = () => ex(`git tag --format='%(refname:strip=2)' --sort=creatordate`) const gitAddAll=()=>ex('git add .') +const getDateTimeOfTag = (tag) => ex(`git log -1 --format=%ai ${tag}`) module.exports = { lastTag, dateOfCommit, messageLastCommit, - allCommitSince, + commitBetweenTags, firstCommitAllTime, hashLastCommit, gitAddAll, getTagsOrderedByDate, - getTagsOrderedByDate, + getDateTimeOfTag, ex };