-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from mailjet/fix_standart_version
Fix standard version
- Loading branch information
Showing
5 changed files
with
225 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module.exports = { | ||
commitUrlFormat: 'https://github.com/mailjet/mailjet-apiv3-nodejs/commits/{{hash}}', | ||
compareUrlFormat: 'https://github.com/mailjet/mailjet-apiv3-nodejs/compare/{{previousTag}}...{{currentTag}}', | ||
bumpFiles: [ | ||
{ | ||
filename: 'package.json', | ||
type: 'json', | ||
}, | ||
{ | ||
filename: 'package-lock.json', | ||
type: 'json', | ||
}, | ||
{ | ||
filename: 'README.md', | ||
updater: './scripts/standardVersionUpdater.js', | ||
}, | ||
], | ||
types: [ | ||
{ | ||
type: 'breaking', | ||
section: 'Breaking changes', | ||
}, | ||
{ | ||
type: 'security', | ||
section: 'Dependency changes for security', | ||
}, | ||
{ | ||
type: 'feature', | ||
section: 'Added features', | ||
}, | ||
{ | ||
type: 'deprecate', | ||
section: 'Deprecated features', | ||
}, | ||
{ | ||
type: 'remove', | ||
section: 'Removed features', | ||
}, | ||
{ | ||
type: 'fix', | ||
section: 'Bug Fixes', | ||
}, | ||
{ | ||
type: 'test', | ||
section: 'Tests', | ||
}, | ||
{ | ||
type: 'build', | ||
section: 'Build changes', | ||
}, | ||
{ | ||
type: 'docs', | ||
section: 'Docs changes', | ||
}, | ||
{ | ||
type: 'other', | ||
section: 'Other changes', | ||
}, | ||
{ | ||
type: 'chore', | ||
hidden: true, | ||
}, | ||
], | ||
scripts: { | ||
prerelease: 'npm run pkg:precommit', | ||
prebump: 'node ./scripts/VersionBump.js', | ||
postchangelog: 'npm run build && npm run docs', | ||
precommit: 'git add -A', | ||
posttag: 'git push && git push --tags', | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -121,60 +121,5 @@ | |
"Arnaud Breton <[email protected]> (https://github.com/arnaudbreton)", | ||
"Nicholas Smith <[email protected]> (https://github.com/safani)", | ||
"Jérémie Parker <[email protected]> (https://github.com/p-j)" | ||
], | ||
"standard-version": { | ||
"commitUrlFormat": "https://github.com/mailjet/mailjet-apiv3-nodejs/commits/{{hash}}", | ||
"compareUrlFormat": "https://github.com/mailjet/mailjet-apiv3-nodejs/compare/{{previousTag}}...{{currentTag}}", | ||
"types": [ | ||
{ | ||
"type": "breaking", | ||
"section": "Breaking changes" | ||
}, | ||
{ | ||
"type": "security", | ||
"section": "Dependency changes for security" | ||
}, | ||
{ | ||
"type": "feature", | ||
"section": "Added features" | ||
}, | ||
{ | ||
"type": "deprecate", | ||
"section": "Deprecated features" | ||
}, | ||
{ | ||
"type": "remove", | ||
"section": "Removed features" | ||
}, | ||
{ | ||
"type": "fix", | ||
"section": "Bug Fixes" | ||
}, | ||
{ | ||
"type": "test", | ||
"section": "Tests" | ||
}, | ||
{ | ||
"type": "build", | ||
"section": "Build changes" | ||
}, | ||
{ | ||
"type": "docs", | ||
"section": "Docs changes" | ||
}, | ||
{ | ||
"type": "other", | ||
"section": "Other changes" | ||
}, | ||
{ | ||
"type": "chore", | ||
"hidden": true | ||
} | ||
], | ||
"scripts": { | ||
"prerelease": "npm run build:release", | ||
"postchangelog": "npm run build:prepublish && git add -A dist", | ||
"posttag": "git push && git push --tags" | ||
} | ||
} | ||
] | ||
} |
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,131 @@ | ||
const childProcess = require('child_process'); | ||
const packageJSON = require('../package.json'); | ||
|
||
const VERSION_NUMBER = { | ||
MAJOR: 'major', | ||
MINOR: 'minor', | ||
PATCH: 'patch', | ||
}; | ||
|
||
const VERSION_NUMBER_IMPORTANCE = { | ||
[VERSION_NUMBER.MAJOR]: 3, | ||
[VERSION_NUMBER.MINOR]: 2, | ||
[VERSION_NUMBER.PATCH]: 1, | ||
}; | ||
|
||
const VERSION_NUMBER_BUMP = { | ||
[VERSION_NUMBER.MAJOR]: [1, 0, 0], | ||
[VERSION_NUMBER.MINOR]: [0, 1, 0], | ||
[VERSION_NUMBER.PATCH]: [0, 0, 1], | ||
}; | ||
|
||
const TYPE_WITH_VERSION_NUMBER = { | ||
breaking: VERSION_NUMBER.MAJOR, | ||
remove: VERSION_NUMBER.MAJOR, | ||
feature: VERSION_NUMBER.MINOR, | ||
deprecate: VERSION_NUMBER.MINOR, | ||
fix: VERSION_NUMBER.PATCH, | ||
security: VERSION_NUMBER.PATCH, // or manual | ||
test: VERSION_NUMBER.PATCH, | ||
build: VERSION_NUMBER.PATCH, | ||
docs: VERSION_NUMBER.PATCH, | ||
other: VERSION_NUMBER.PATCH, // or manual | ||
}; | ||
|
||
function evaluateCommits(packageVersion) { | ||
const commitTypes = Object.keys(TYPE_WITH_VERSION_NUMBER); | ||
const rawCommits = childProcess.execSync(`git log --pretty=format:"%s" v${packageVersion}...HEAD`, { encoding: 'utf-8' }); | ||
|
||
let isUsedBreakingChangesFlag = false; | ||
const types = rawCommits | ||
.split('\n') | ||
.map((commit) => { | ||
const message = commit.trim(); | ||
let type = message.slice(0, message.indexOf(':')); | ||
|
||
if (type.endsWith('!')) { | ||
isUsedBreakingChangesFlag = true; | ||
type = type.slice(0, type.length - 1); | ||
} | ||
|
||
if (type.endsWith(')')) { | ||
type = type.slice(0, type.indexOf('(')); | ||
} | ||
|
||
return type; | ||
}) | ||
.filter((commitType) => commitTypes.includes(commitType)); | ||
|
||
return { | ||
isUsedBreakingChangesFlag, | ||
types, | ||
}; | ||
} | ||
|
||
function increaseVersion(parsedVersion, versionBumps) { | ||
const result = []; | ||
|
||
/*eslint no-continue: 0*/ | ||
for (let index = 0; index < versionBumps.length; index += 1) { | ||
const versionBump = versionBumps[index]; | ||
if (versionBump !== 1) { | ||
result.push(parsedVersion[index]); | ||
continue; | ||
} | ||
|
||
const currentVersion = parsedVersion[index]; | ||
result.push(currentVersion + versionBump); | ||
|
||
if (result.length < 3) { | ||
result.push( | ||
...Array(versionBumps.length - result.length).fill(0), | ||
); | ||
} | ||
|
||
break; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function receiveVersionNumber(commitTypes) { | ||
let importance = 0; | ||
|
||
commitTypes.forEach((commitType) => { | ||
const versionNumber = TYPE_WITH_VERSION_NUMBER[commitType]; | ||
const versionImportance = VERSION_NUMBER_IMPORTANCE[versionNumber]; | ||
|
||
if (importance < versionImportance) { | ||
importance = versionImportance; | ||
} | ||
}); | ||
|
||
return Object | ||
.entries(VERSION_NUMBER_IMPORTANCE) | ||
.find(([, versionImportance]) => versionImportance === importance)[0]; | ||
} | ||
|
||
function calculateVersionBump(packageVersion, isUsedBreakingChangesFlag, commitTypes) { | ||
const parsedVersion = packageVersion.split('.').map((v) => Number(v)); | ||
|
||
if (isUsedBreakingChangesFlag) { | ||
return increaseVersion(parsedVersion, VERSION_NUMBER_BUMP[VERSION_NUMBER.MAJOR]); | ||
} | ||
|
||
const versionNumber = receiveVersionNumber(commitTypes); | ||
return increaseVersion(parsedVersion, VERSION_NUMBER_BUMP[versionNumber]); | ||
} | ||
|
||
function main() { | ||
const oldVersion = packageJSON.version; | ||
|
||
const { isUsedBreakingChangesFlag, types } = evaluateCommits(oldVersion); | ||
if (types.length === 0) { | ||
throw new Error('Not found commits with type!'); | ||
} | ||
|
||
const newVersion = calculateVersionBump(oldVersion, isUsedBreakingChangesFlag, types); | ||
process.stdout.write(newVersion.join('.')); | ||
} | ||
|
||
main(); |
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,22 @@ | ||
const START_KEY_SENTENCE = 'https://img.shields.io/badge/version-'; | ||
const END_KEY_SENTENCE = '-green.svg'; | ||
|
||
function getVersion(content) { | ||
const startIndex = content.indexOf(START_KEY_SENTENCE) + START_KEY_SENTENCE.length; | ||
const endIndex = content.indexOf(END_KEY_SENTENCE); | ||
|
||
return content.slice(startIndex, endIndex); | ||
} | ||
|
||
module.exports.readVersion = function (content) { | ||
return getVersion(content); | ||
}; | ||
|
||
module.exports.writeVersion = function (content, newVersion) { | ||
const oldVersion = getVersion(content); | ||
|
||
return content.replace( | ||
`${START_KEY_SENTENCE}${oldVersion}${END_KEY_SENTENCE}`, | ||
`${START_KEY_SENTENCE}${newVersion}${END_KEY_SENTENCE}`, | ||
); | ||
}; |