diff --git a/.travis.yml b/.travis.yml index 3f48ead..46a07c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,9 @@ node_js: - "8" - "6" - "4" -script: npm run test:coveralls \ No newline at end of file +script: npm test +jobs: + include: + - stage: coveralls + node_js: "node" + script: npm run test:coveralls \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 63d075a..7941ef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Support for BitBucket ## [1.0.0] - 2017-12-12 ### Added diff --git a/README.md b/README.md index 24124c8..e58d996 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ # changelog-updater [![npm package](https://img.shields.io/npm/v/changelog-updater.svg?style=flat-square)](https://www.npmjs.org/package/changelog-updater) +[![GitHub license](https://img.shields.io/github/license/nikolajevp/changelog-updater.svg)](https://github.com/nikolajevp/changelog-updater/blob/master/LICENSE) +[![Dependencies](https://img.shields.io/david/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater) [![Build Status](https://travis-ci.org/nikolajevp/changelog-updater.svg?branch=master)](https://travis-ci.org/nikolajevp/changelog-updater) [![Coverage Status](https://coveralls.io/repos/github/nikolajevp/changelog-updater/badge.svg?branch=master)](https://coveralls.io/github/nikolajevp/changelog-updater?branch=master) -[![Dependencies](https://img.shields.io/david/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater) -[![DevDependencies](https://img.shields.io/david/dev/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater?type=dev) -[![GitHub license](https://img.shields.io/github/license/nikolajevp/changelog-updater.svg)](https://github.com/nikolajevp/changelog-updater/blob/master/LICENSE) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) Update [Unreleased] in CHANGELOG.md to current package version. diff --git a/lib/index.js b/lib/index.js index d71ece5..3efcd31 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,39 +5,71 @@ const version = process.env.npm_package_version; const date = new Date().toISOString().slice(0, 10); const changelogFileName = 'CHANGELOG.md'; -function updateChangelog() { - fs.readFile(changelogFileName, 'utf8', (err, changelog) => { - if (err) { - throw err; - } +function updateUpperSection(changelog) { + return changelog.replace( + /## \[Unreleased\]/, + `## [Unreleased]\n\n## [${version}] - ${date}` + ); +} + +function updateBottomSectionGithub(changelog) { + /** + * matches[0] = `[Unreleased]: https://github.com/nikolajevp/changelog-updater/compare/v1.0.0...HEAD` + * matches[1] = ` https://github.com/nikolajevp/changelog-updater/compare/` + * matches[2] = `1.0.0` + */ + const regex = /\[Unreleased\]:(.+\/)v(.+)\.\.\.HEAD/i; + const matches = changelog.match(regex); + + if (matches) { + const url = matches[1]; + const previousVersion = matches[2]; + const compareToUnreleased = `[Unreleased]:${url}v${version}...HEAD`; + const compareToLatestVersion = `[${version}]:${url}v${previousVersion}...v${version}`; changelog = changelog.replace( - /## \[Unreleased\]/, - `## [Unreleased]\n\n## [${version}] - ${date}` + regex, + `${compareToUnreleased}\n${compareToLatestVersion}` ); + } + + return changelog; +} - /** - * unreleasedLink[0] = `[Unreleased]: https://github.com/nikolajevp/changelog-updater/compare/v1.0.0...HEAD` - * unreleasedLink[1] = ` https://github.com/nikolajevp/changelog-updater/compare/v1.0.0` - * unreleasedLink[2] = ` https://github.com/nikolajevp/changelog-updater/compare/` - */ - const unreleasedLink = changelog.match( - /\[Unreleased\]:((.+\/).+)\.\.\.HEAD/i +function updateBottomSectionBitbucket(changelog) { + /** + * matches[0] = `[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v1.0.0` + * matches[1] = ` https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/` + * matches[2] = `1.0.0` + */ + const regex = /\[Unreleased\]:(.+)HEAD\.\.v(.+)/i; + const matches = changelog.match(regex); + + if (matches) { + const url = matches[1]; + const previousVersion = matches[2]; + const compareToUnreleased = `[Unreleased]:${url}HEAD..v${version}`; + const compareToLatestVersion = `[${version}]:${url}v${version}..v${previousVersion}`; + + changelog = changelog.replace( + regex, + `${compareToUnreleased}\n${compareToLatestVersion}` ); + } + + return changelog; +} - if (unreleasedLink) { - const dynamicResult = `[Unreleased]:${ - unreleasedLink[2] - }v${version}...HEAD\n[${version}]:${ - unreleasedLink[1] - }...v${version}`; - - changelog = changelog.replace( - /\[Unreleased\]:((.+\/).+)\.\.\.HEAD/i, - dynamicResult - ); +function updateChangelog() { + fs.readFile(changelogFileName, 'utf8', (err, changelog) => { + if (err) { + throw err; } + changelog = updateUpperSection(changelog); + changelog = updateBottomSectionGithub(changelog); + changelog = updateBottomSectionBitbucket(changelog); + fs.writeFile(changelogFileName, changelog, 'utf8', (err) => { if (err) { throw err; diff --git a/lib/index.test.js b/lib/index.test.js index eb4e138..4e57bfc 100644 --- a/lib/index.test.js +++ b/lib/index.test.js @@ -60,7 +60,7 @@ describe('updateChangelog', () => { expect(updateChangelog).toThrow('could not write file'); }); - it('Updates [Unreleased] section', () => { + it('Updates [Unreleased] section for Github', () => { readFile .mockClear() .mockImplementation((changelogFileName, format, callback) => @@ -113,4 +113,58 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [1.0.0]: https://github.com/nikolajevp/changelog-updater/compare/v0.1.0...v1.0.0 `); }); + + it('Updates [Unreleased] section for Bitbucket', () => { + readFile + .mockClear() + .mockImplementation((changelogFileName, format, callback) => + callback( + null, + ` +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## 0.1.0 - 2017-12-09 +### Added +- Initial commit + +[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v0.1.0 + ` + ) + ); + + writeFile + .mockClear() + .mockImplementation( + (changelogFileName, changelog, format, callback) => { + return callback(); + } + ); + + updateChangelog(); + + expect(writeFile.mock.calls[0][1]).toEqual(` +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.0.0] - 2017-12-12 + +## 0.1.0 - 2017-12-09 +### Added +- Initial commit + +[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v1.0.0 +[1.0.0]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/v1.0.0..v0.1.0 + `); + }); }); diff --git a/package-lock.json b/package-lock.json index d8a8eaf..34171b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -379,7 +379,7 @@ "requires": { "babel-core": "6.26.0", "babel-runtime": "6.26.0", - "core-js": "2.5.2", + "core-js": "2.5.3", "home-or-tmp": "2.0.0", "lodash": "4.17.4", "mkdirp": "0.5.1", @@ -392,7 +392,7 @@ "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", "dev": true, "requires": { - "core-js": "2.5.2", + "core-js": "2.5.3", "regenerator-runtime": "0.11.1" } }, @@ -692,9 +692,9 @@ "dev": true }, "core-js": { - "version": "2.5.2", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.2.tgz", - "integrity": "sha1-vEZIZW59ydyA19PHu8Fy2W50TmM=", + "version": "2.5.3", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.3.tgz", + "integrity": "sha1-isw4NFgk8W2DZbfJtCWRaOjtYD4=", "dev": true }, "core-util-is": { @@ -873,9 +873,9 @@ } }, "errno": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.5.tgz", - "integrity": "sha512-tv2H+e3KBnMmNRuoVG24uorOj3XfYo+/nJJd07PUISRr0kaMKQKL5kyD+6ANXk1ZIIsvbORsjvHnCfC4KIc7uQ==", + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.6.tgz", + "integrity": "sha512-IsORQDpaaSwcDP4ZZnHxgE85werpo34VYn1Ud3mq+eUsF593faR8oCZNXrROVkpFu2TsbrNhHin0aUrTsQ9vNw==", "dev": true, "requires": { "prr": "1.0.1" @@ -918,9 +918,9 @@ } }, "eslint": { - "version": "4.13.1", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.13.1.tgz", - "integrity": "sha512-UCJVV50RtLHYzBp1DZ8CMPtRSg4iVZvjgO9IJHIKyWU/AnJVjtdRikoUPLB29n5pzMB7TnsLQWf0V6VUJfoPfw==", + "version": "4.14.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.14.0.tgz", + "integrity": "sha512-Ul6CSGRjKscEyg0X/EeNs7o2XdnbTEOD1OM8cTjmx85RPcBJQrEhZLevhuJZNAE/vS2iVl5Uhgiqf3h5uLMCJQ==", "dev": true, "requires": { "ajv": "5.5.1", @@ -931,9 +931,9 @@ "debug": "3.1.0", "doctrine": "2.0.2", "eslint-scope": "3.7.1", + "eslint-visitor-keys": "1.0.0", "espree": "3.5.2", "esquery": "1.0.0", - "estraverse": "4.2.0", "esutils": "2.0.2", "file-entry-cache": "2.0.0", "functional-red-black-tree": "1.0.1", @@ -942,7 +942,7 @@ "ignore": "3.3.7", "imurmurhash": "0.1.4", "inquirer": "3.3.0", - "is-resolvable": "1.0.0", + "is-resolvable": "1.0.1", "js-yaml": "3.10.0", "json-stable-stringify-without-jsonify": "1.0.1", "levn": "0.3.0", @@ -989,20 +989,26 @@ "estraverse": "4.2.0" } }, + "eslint-visitor-keys": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz", + "integrity": "sha512-qzm/XxIbxm/FHyH341ZrbnMUpe+5Bocte9xkmFMzPMjRaZMcXww+MpBptFvtU+79L362nqiLhekCxCxDPaUMBQ==", + "dev": true + }, "espree": { "version": "3.5.2", "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.2.tgz", "integrity": "sha512-sadKeYwaR/aJ3stC2CdvgXu1T16TdYN+qwCpcWbMnGJ8s0zNWemzrvb2GbD4OhmJ/fwpJjudThAlLobGbWZbCQ==", "dev": true, "requires": { - "acorn": "5.2.1", + "acorn": "5.3.0", "acorn-jsx": "3.0.1" }, "dependencies": { "acorn": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.2.1.tgz", - "integrity": "sha512-jG0u7c4Ly+3QkkW18V+NRDN+4bWHdln30NL1ZL2AvFZZmQe/BfopYCtghCKKVBUSetZ4QKcyA0pY6/4Gw8Pv8w==", + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.3.0.tgz", + "integrity": "sha512-Yej+zOJ1Dm/IMZzzj78OntP/r3zHEaKcyNoU2lAaxPtrseM6rF0xwqoz5Q5ysAiED9hTjI2hgtvLXitlCN1/Ug==", "dev": true } } @@ -2591,13 +2597,10 @@ "dev": true }, "is-resolvable": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.0.0.tgz", - "integrity": "sha1-jfV8YeouPFAUCNEA+wE8+NbgzGI=", - "dev": true, - "requires": { - "tryit": "1.0.3" - } + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-resolvable/-/is-resolvable-1.0.1.tgz", + "integrity": "sha512-y5CXYbzvB3jTnWAZH1Nl7ykUWb6T3BcTs56HUruwBf8MhF56n1HWqhDWnVFo8GHrUPDgvUUNVhrc2U8W7iqz5g==", + "dev": true }, "is-stream": { "version": "1.1.0", @@ -3699,9 +3702,9 @@ "dev": true }, "prettier": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.9.1.tgz", - "integrity": "sha512-oYpQsZk7/0o8+YJUB0LfjkTYQa79gUIF2ESeqvG23IzcgqqvmeOE4+lMG7E/UKX3q3RIj8JHSfhrXWhon1L+zA==", + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.9.2.tgz", + "integrity": "sha512-piXx9N2WT8hWb7PBbX1glAuJVIkEyUV9F5fMXFINpZ0x3otVOFKKeGmeuiclFJlP/UrgTckyV606VjH2rNK4bw==", "dev": true }, "pretty-format": { @@ -4369,12 +4372,6 @@ "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=", "dev": true }, - "tryit": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz", - "integrity": "sha1-OTvnMKlEb9Hq1tpZoBQwjzbCics=", - "dev": true - }, "tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", @@ -4567,7 +4564,7 @@ "integrity": "sha512-XxiQ9kZN5n6mmnW+mFJ+wXjNNI/Nx4DIdaAKLX1Bn6LYBWlN/zaBhu34DQYPZ1AJobQuu67S2OfDdNSVULvXkQ==", "dev": true, "requires": { - "errno": "0.1.5", + "errno": "0.1.6", "xtend": "4.0.1" } }, diff --git a/package.json b/package.json index a900dc5..1299fcf 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "dependencies": {}, "devDependencies": { "coveralls": "^3.0.0", - "eslint": "^4.13.1", + "eslint": "^4.14.0", "jest": "^21.2.1", - "prettier": "^1.9.1" + "prettier": "^1.9.2" } }