Skip to content

Commit

Permalink
Merge pull request #212 from mailjet/fix_standart_version
Browse files Browse the repository at this point in the history
Fix standard version
  • Loading branch information
scroll17 authored Jun 30, 2022
2 parents 9d2783b + e615ff7 commit e75062a
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 57 deletions.
71 changes: 71 additions & 0 deletions .versionrc.js
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',
},
};
57 changes: 1 addition & 56 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
}
1 change: 0 additions & 1 deletion scripts/PreparePackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function changePackageData(packageData) {
delete packageData.scripts;
delete packageData.directories;
delete packageData.devDependencies;
delete packageData['standard-version'];

packageData.private = false;
packageData.files = ['*'];
Expand Down
131 changes: 131 additions & 0 deletions scripts/VersionBump.js
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();
22 changes: 22 additions & 0 deletions scripts/standardVersionUpdater.js
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}`,
);
};

0 comments on commit e75062a

Please sign in to comment.