Skip to content

Commit

Permalink
chore: add changelog script.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchoqueux committed Jan 21, 2021
1 parent b56f8b0 commit f7f6c40
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 1 deletion.
153 changes: 153 additions & 0 deletions config/conventionalChangelog/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

var compareFunc = require('compare-func');
var Q = require('q');
var readFile = Q.denodeify(require('fs').readFile);
var resolve = require('path').resolve;
var path = require('path');

var pkgJson = {};
var gufg = require('github-url-from-git');

try {
pkgJson = require(path.resolve(
process.cwd(),
'./package.json',
));
} catch (err) {
console.error('no root package.json found');
}

var parserOpts = {
headerPattern: /^(\w*)(?:\((.*)\))?\: (.*)$/,
headerCorrespondence: [
'type',
'scope',
'subject',
],
noteKeywords: ['BREAKING CHANGE', 'BREAKING CHANGES'],
revertPattern: /^revert:\s([\s\S]*?)\s*This reverts commit (\w*)\./,
revertCorrespondence: ['header', 'hash'],
};

function issueUrl() {
if (pkgJson.repository && pkgJson.repository.url && ~pkgJson.repository.url.indexOf('github.com')) {
var gitUrl = gufg(pkgJson.repository.url);

if (gitUrl) {
return `${gitUrl}/issues/`;
}
}
}

var writerOpts = {
transform(commit) {
var discard = false;
var issues = [];

commit.notes.forEach((note) => {
note.title = 'BREAKING CHANGES';
discard = false;
});

if (['feat', 'features', 'feature'].includes(commit.type)) {
commit.type = 'Features';
} else if (commit.type === 'fix') {
commit.type = 'Bug Fixes';
} else if (commit.type === 'perf') {
commit.type = 'Performance Improvements';
} else if (commit.type === 'revert') {
commit.type = 'Reverts';
} else if (discard) {
return;
} else if (['doc', 'docs'].includes(commit.type)) {
commit.type = 'Documentation';
} else if (commit.type === 'style') {
commit.type = 'Styles';
} else if (['refactor', 'refacto', 'refactoring'].includes(commit.type)) {
commit.type = 'Code Refactoring';
} else if (['test', 'tests'].includes(commit.type)) {
commit.type = 'Tests';
} else if (['chore', 'rename', 'workflow'].includes(commit.type) || (commit.header && commit.header.startsWith('release v'))) {
commit.type = 'Workflow and chores';
} else if (['example', 'examples'].includes(commit.type)) {
commit.type = 'Examples';
} else {
commit.type = 'Others';
}

if (commit.scope === '*') {
commit.scope = '';
}

if (typeof commit.hash === 'string') {
commit.hash = commit.hash.substring(0, 7);
}

if (typeof commit.subject === 'string') {
var url = issueUrl();
if (url) {
// GitHub issue URLs.
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
issues.push(issue);
return `[#${issue}](${url}${issue})`;
});
}
// GitHub user URLs.
commit.subject = commit.subject.replace(/@([a-zA-Z0-9_]+)/g, '[@$1](https://github.com/$1)');
commit.subject = commit.subject;
}

// remove references that already appear in the subject
commit.references = commit.references.filter((reference) => {
if (issues.indexOf(reference.issue) === -1) {
return true;
}

return false;
});

return commit;
},
groupBy: 'type',

commitGroupsSort: (a, b) => {
const commitGroupOrder = [
'BREAKING CHANGES',
'Features',
'Bug Fixes',
'Performance Improvements',
'Examples',
'Code Refactoring',
'Workflow and chores',
'Reverts',
'Documentation',
'Styles',
'Tests',
'Others',
];
const gRankA = commitGroupOrder.indexOf(a.title);
const gRankB = commitGroupOrder.indexOf(b.title);
return gRankA >= gRankB ? 1 : -1;
},
commitsSort: ['scope', 'subject'],
noteGroupsSort: 'title',
notesSort: compareFunc,
};

module.exports = Q.all([
readFile(resolve(__dirname, 'templates/template.hbs'), 'utf-8'),
readFile(resolve(__dirname, 'templates/header.hbs'), 'utf-8'),
readFile(resolve(__dirname, 'templates/commit.hbs'), 'utf-8'),
readFile(resolve(__dirname, 'templates/footer.hbs'), 'utf-8'),
])
.spread((template, header, commit, footer) => {
writerOpts.mainTemplate = template;
writerOpts.headerPartial = header;
writerOpts.commitPartial = commit;
writerOpts.footerPartial = footer;

return {
parserOpts,
writerOpts,
};
});
61 changes: 61 additions & 0 deletions config/conventionalChangelog/templates/commit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
*{{#if scope}} **{{scope}}:**
{{~/if}} {{#if subject}}
{{~subject}}
{{~else}}
{{~header}}
{{~/if}}

{{~!-- commit link --}} {{#if @root.linkReferences~}}
([{{hash}}](
{{~#if @root.repository}}
{{~#if @root.host}}
{{~@root.host}}/
{{~/if}}
{{~#if @root.owner}}
{{~@root.owner}}/
{{~/if}}
{{~@root.repository}}
{{~else}}
{{~@root.repoUrl}}
{{~/if}}/
{{~@root.commit}}/{{hash}}))
{{~else}}
{{~hash}}
{{~/if}}

{{~!-- commit references --}}
{{~#if references~}}
, closes
{{~#each references}} {{#if @root.linkReferences~}}
[
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}#{{this.issue}}](
{{~#if @root.repository}}
{{~#if @root.host}}
{{~@root.host}}/
{{~/if}}
{{~#if this.repository}}
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}
{{~else}}
{{~#if @root.owner}}
{{~@root.owner}}/
{{~/if}}
{{~@root.repository}}
{{~/if}}
{{~else}}
{{~@root.repoUrl}}
{{~/if}}/
{{~@root.issue}}/{{this.issue}})
{{~else}}
{{~#if this.owner}}
{{~this.owner}}/
{{~/if}}
{{~this.repository}}#{{this.issue}}
{{~/if}}{{/each}}
{{~/if}}

11 changes: 11 additions & 0 deletions config/conventionalChangelog/templates/footer.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{#if noteGroups}}
{{#each noteGroups}}

### {{title}}

{{#each notes}}
* {{#if commit.scope}}**{{commit.scope}}:** {{/if}}{{text}}
{{/each}}
{{/each}}

{{/if}}
26 changes: 26 additions & 0 deletions config/conventionalChangelog/templates/header.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<a name="{{version}}"></a>
{{#if isPatch~}}
##
{{~else~}}
#
{{~/if}} {{#if @root.linkCompare~}}
[{{version}}](
{{~#if @root.repository~}}
{{~#if @root.host}}
{{~@root.host}}/
{{~/if}}
{{~#if @root.owner}}
{{~@root.owner}}/
{{~/if}}
{{~@root.repository}}
{{~else}}
{{~@root.repoUrl}}
{{~/if~}}
/compare/{{previousTag}}...{{currentTag}})
{{~else}}
{{~version}}
{{~/if}}
{{~#if title}} "{{title}}"
{{~/if}}
{{~#if date}} ({{date}})
{{/if}}
16 changes: 16 additions & 0 deletions config/conventionalChangelog/templates/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{> header}}

{{#each commitGroups}}

{{#if title}}
### {{title}}

{{/if}}
{{#each commits}}
{{> commit root=@root}}
{{/each}}

{{/each}}
{{> footer}}


7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"debug": "npm start -- --env.noInline=true",
"prepublishOnly": "npm run build && npm run transpile",
"prepare": "cross-env NO_UPDATE_NOTIFIER=true node ./config/prepare.js && node ./config/replace.config.js",
"watch": "cross-env BABEL_DISABLE_CACHE=1 babel --watch src --out-dir lib"
"watch": "cross-env BABEL_DISABLE_CACHE=1 babel --watch src --out-dir lib",
"changelog": "conventional-changelog -n ./config/conventionalChangelog/config.js -i changelog.md -s"
},
"nyc": {
"exclude": [
Expand Down Expand Up @@ -74,6 +75,8 @@
"babel-plugin-module-resolver": "^4.0.0",
"chalk": "^4.1.0",
"chart.js": "^2.9.4",
"compare-func": "^2.0.0",
"conventional-changelog-cli": "^2.1.1",
"copyfiles": "^2.4.1",
"core-js": "^3.8.0",
"cross-env": "^7.0.3",
Expand All @@ -82,6 +85,7 @@
"eslint-import-resolver-webpack": "^0.13.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.22.1",
"github-url-from-git": "^1.5.0",
"https-proxy-agent": "^5.0.0",
"jsdoc": "^3.6.6",
"marked": "^1.2.5",
Expand All @@ -90,6 +94,7 @@
"nyc": "^15.1.0",
"proj4": "^2.6.3",
"puppeteer": "^5.5.0",
"q": "^1.5.1",
"replace-in-file": "^6.1.0",
"three": "0.123.0",
"url-polyfill": "^1.1.12",
Expand Down

0 comments on commit f7f6c40

Please sign in to comment.