Skip to content

Commit

Permalink
Merge pull request #6 from ctinnovation/feat/title-regex-tolerance
Browse files Browse the repository at this point in the history
feat: improve title regex tolerance
  • Loading branch information
Giovanni Bertoncelli authored Sep 24, 2021
2 parents 1323cc8 + 5d3516a commit 5a59f54
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
37 changes: 0 additions & 37 deletions CHANGELOG.md

This file was deleted.

14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ You must provide a Markdown file for each of yours task completed on the current

- Should be named with the code of the task
- Should be splitted in sections with these titles
- \## Changed
- \## Fixed
- \## Removed
- \## Added
- \## Change[d]
- \## [hot]Fix[ed]
- \## Remove[d]
- \## Add[ed]

Each section should list each change prepended with `-`.
- Each section should list each change prepended with `-`.

### Example

Expand All @@ -102,7 +102,9 @@ Each section should list each change prepended with `-`.
- change 1
- change 2

## Fixed
## Fix
- fix 1
- fix 2
```

> The `[notation]` is used to indicate optional group of characters inside the section title. Each possible variation will be standardised to the form: Changed, Added, Removed, Fixed
3 changes: 2 additions & 1 deletion helpers/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
upperFirst,
matchesToArray,
SUBSECTION_POINT_REGEX,
matchSectionTitle,
} = require('./regexprs');

function parseTaskFile(taskCode, fileContent, currentMap = {}) {
Expand All @@ -34,7 +35,7 @@ function parseTaskFile(taskCode, fileContent, currentMap = {}) {
process.exit(1);
}

sectionTitle = upperFirst(sectionTitle);
sectionTitle = matchSectionTitle(sectionTitle);

let subsectionBody = '';
// retrieve sub section
Expand Down
27 changes: 26 additions & 1 deletion helpers/regexprs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
const VERSION_LINE_REGEX = /## \[(\d+\.\d+\.\d+)\]( - \d*-\d*-\d*)?(?:\n|$)/gi;
const UNRELEASED_LINE_REGEX = /## \[Unreleased\]/gi;
const TASK_SECTION_REGEX = /#+ (\w+).*(?:\n|$)*/gi;
const SUBSECTION_TITLE_REGEX = /Changed|Added|Fixed|Removed/gi;
const SUBSECTION_TITLE_REGEX = /changed?|edit(ed)?|add(ed)?|(hot)?fix(ed)?|removed?|cancel(led)?|updated?|created?/gi;
const CHANGED_REGEX = /changed?|edit(ed)?|updated?/gi;
const FIXED_REGEX = /(hot)?fix(ed)?/gi;
const ADDED_REGEX = /add(ed)?|created?/gi;
const REMOVED_REGEX = /removed?|cancel(led)?/gi;
const SUBSECTION_POINT_REGEX = /( )*- .+(?:\n|$)/gi;

function upperFirst(string = '') {
return `${string.charAt(0).toUpperCase()}${string.substr(1).toLowerCase()}`;
}

function matchSectionTitle(currentTitle) {
if (currentTitle.match(CHANGED_REGEX)) {
return 'Changed';
}
if (currentTitle.match(FIXED_REGEX)) {
return 'Fixed';
}
if (currentTitle.match(ADDED_REGEX)) {
return 'Added';
}
if (currentTitle.match(REMOVED_REGEX)) {
return 'Removed';
}
throw new Error(`Critical error: '${currentTitle}' not matched any section title!`);
}

function matchesToArray(regex, string) {
regex.lastIndex = 0;
const result = [];
Expand All @@ -28,4 +48,9 @@ module.exports = {
SUBSECTION_POINT_REGEX,
upperFirst,
matchesToArray,
CHANGED_REGEX,
FIXED_REGEX,
ADDED_REGEX,
REMOVED_REGEX,
matchSectionTitle,
};
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ async function main() {
process.exit(0);
}

const writeStream = fs.createWriteStream(changelogPath);
const tempChangelog = `${changelogPath}-temp-${new Date().getTime()}`;
const writeStream = fs.createWriteStream(tempChangelog);

writeStream.on('error', (e) => {
console.error(e);
Expand Down Expand Up @@ -165,11 +166,21 @@ async function main() {
writeStream.write(commitSummary);
}

writeStream.on('finish', () => {
// write on the final changelog file
fs.copyFile(tempChangelog, changelogPath, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
fs.unlinkSync(tempChangelog);
emptyFolder(releaseFolder);
console.log('CHANGELOG generated 👍');
});
});

writeStream.write(oldChangelogBody);
writeStream.close();
emptyFolder(releaseFolder);

console.log('CHANGELOG generated 👍');
}

main();
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4"
},
"bin": "cli.js"
}
"bin": "cli.js",
"engines": {
"node": ">=10"
}
}

0 comments on commit 5a59f54

Please sign in to comment.