diff --git a/README.md b/README.md index ad0921f3..334fb313 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,26 @@ With this example, for each release, a `docs/CHANGELOG.md` will be created or up ### Options -| Options | Description | Default | -| ---------------- | ----------------------------------------------------- | -------------- | -| `changelogFile` | File path of the changelog. | `CHANGELOG.md` | -| `changelogTitle` | Title of the changelog file (first line of the file). | - | +| Options | Description | Default | +| ---------------- | ----------------------------------------------------------------- | -------------- | +| `changelogFile` | File path of the changelog. See [changelogFile](#changelogFile). | `CHANGELOG.md` | +| `changelogTitle` | Title of the changelog file (first line of the file). | - | + +#### `changelogFile` + +The path to the changelog is generated with [Lodash template](https://lodash.com/docs#template). The following variables are available: + +| Parameter | Description | +|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------| +| `branch` | The branch from which the release is done. | +| `branch.name` | The branch name. | +| `branch.type` | The [type of branch](https://github.com/semantic-release/semantic-release/blob/beta/docs/usage/workflow-configuration.md#branch-types). | +| `branch.channel` | The distribution channel on which to publish releases from this branch. | +| `branch.range` | The range of [semantic versions](https://semver.org) to support on this branch. | +| `branch.prerelease` | The pre-release detonation to append to [semantic versions](https://semver.org) released from this branch. | +| `lastRelease` | `Object` with `version`, `gitTag` and `gitHead` of the last release. | +| `nextRelease` | `Object` with `version`, `gitTag`, `gitHead` and `notes` of the release being done. | + ### Examples diff --git a/lib/prepare.js b/lib/prepare.js index a206c6e0..55e2ed1d 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -1,10 +1,13 @@ const path = require('path'); const {readFile, writeFile, ensureFile} = require('fs-extra'); const resolveConfig = require('./resolve-config'); +const {template} = require('lodash'); -module.exports = async (pluginConfig, {cwd, nextRelease: {notes}, logger}) => { +module.exports = async (pluginConfig, {cwd, nextRelease, lastRelease, branch, logger}) => { + const {notes} = nextRelease; const {changelogFile, changelogTitle} = resolveConfig(pluginConfig); - const changelogPath = path.resolve(cwd, changelogFile); + const parsedChangelogFile = template(changelogFile)({branch, lastRelease, nextRelease}); + const changelogPath = path.resolve(cwd, parsedChangelogFile); if (notes) { await ensureFile(changelogPath); diff --git a/package-lock.json b/package-lock.json index e1df4a59..8935a389 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6164,77 +6164,77 @@ "integrity": "sha512-xZAC9GpWNOyiS1TtBqBy0HJpjIVI8zsVXEOEwcmgqYFtqOy7sXUL0ByOrkhfcGmf+akSXz3uOxLYB8aLlYivQQ==", "dev": true, "requires": { - "@isaacs/string-locale-compare": "*", - "@npmcli/arborist": "*", - "@npmcli/ci-detect": "*", - "@npmcli/config": "*", - "@npmcli/map-workspaces": "*", - "@npmcli/package-json": "*", - "@npmcli/run-script": "*", - "abbrev": "*", - "ansicolors": "*", - "ansistyles": "*", - "archy": "*", - "cacache": "*", - "chalk": "*", - "chownr": "*", - "cli-columns": "*", - "cli-table3": "*", - "columnify": "*", - "fastest-levenshtein": "*", - "glob": "*", - "graceful-fs": "*", - "hosted-git-info": "*", - "ini": "*", - "init-package-json": "*", - "is-cidr": "*", - "json-parse-even-better-errors": "*", - "libnpmaccess": "*", - "libnpmdiff": "*", - "libnpmexec": "*", - "libnpmfund": "*", - "libnpmhook": "*", - "libnpmorg": "*", - "libnpmpack": "*", - "libnpmpublish": "*", - "libnpmsearch": "*", - "libnpmteam": "*", - "libnpmversion": "*", - "make-fetch-happen": "*", - "minipass": "*", - "minipass-pipeline": "*", - "mkdirp": "*", - "mkdirp-infer-owner": "*", - "ms": "*", - "node-gyp": "*", - "nopt": "*", - "npm-audit-report": "*", - "npm-install-checks": "*", - "npm-package-arg": "*", - "npm-pick-manifest": "*", - "npm-profile": "*", - "npm-registry-fetch": "*", - "npm-user-validate": "*", - "npmlog": "*", - "opener": "*", - "pacote": "*", - "parse-conflict-json": "*", - "proc-log": "*", - "qrcode-terminal": "*", - "read": "*", - "read-package-json": "*", - "read-package-json-fast": "*", - "readdir-scoped-modules": "*", - "rimraf": "*", - "semver": "*", - "ssri": "*", - "tar": "*", - "text-table": "*", - "tiny-relative-date": "*", - "treeverse": "*", - "validate-npm-package-name": "*", - "which": "*", - "write-file-atomic": "*" + "@isaacs/string-locale-compare": "^1.1.0", + "@npmcli/arborist": "^4.2.1", + "@npmcli/ci-detect": "^1.4.0", + "@npmcli/config": "^2.4.0", + "@npmcli/map-workspaces": "^2.0.0", + "@npmcli/package-json": "^1.0.1", + "@npmcli/run-script": "^2.0.0", + "abbrev": "~1.1.1", + "ansicolors": "~0.3.2", + "ansistyles": "~0.1.3", + "archy": "~1.0.0", + "cacache": "^15.3.0", + "chalk": "^4.1.2", + "chownr": "^2.0.0", + "cli-columns": "^4.0.0", + "cli-table3": "^0.6.0", + "columnify": "~1.5.4", + "fastest-levenshtein": "^1.0.12", + "glob": "^7.2.0", + "graceful-fs": "^4.2.8", + "hosted-git-info": "^4.1.0", + "ini": "^2.0.0", + "init-package-json": "^2.0.5", + "is-cidr": "^4.0.2", + "json-parse-even-better-errors": "^2.3.1", + "libnpmaccess": "^5.0.0", + "libnpmdiff": "^3.0.0", + "libnpmexec": "^3.0.2", + "libnpmfund": "^2.0.2", + "libnpmhook": "^7.0.0", + "libnpmorg": "^3.0.0", + "libnpmpack": "^3.0.1", + "libnpmpublish": "^5.0.0", + "libnpmsearch": "^4.0.0", + "libnpmteam": "^3.0.0", + "libnpmversion": "^2.0.2", + "make-fetch-happen": "^9.1.0", + "minipass": "^3.1.6", + "minipass-pipeline": "^1.2.4", + "mkdirp": "^1.0.4", + "mkdirp-infer-owner": "^2.0.0", + "ms": "^2.1.2", + "node-gyp": "^8.4.1", + "nopt": "^5.0.0", + "npm-audit-report": "^2.1.5", + "npm-install-checks": "^4.0.0", + "npm-package-arg": "^8.1.5", + "npm-pick-manifest": "^6.1.1", + "npm-profile": "^5.0.3", + "npm-registry-fetch": "^11.0.0", + "npm-user-validate": "^1.0.1", + "npmlog": "^6.0.0", + "opener": "^1.5.2", + "pacote": "^12.0.2", + "parse-conflict-json": "^2.0.1", + "proc-log": "^1.0.0", + "qrcode-terminal": "^0.12.0", + "read": "~1.0.7", + "read-package-json": "^4.1.1", + "read-package-json-fast": "^2.0.3", + "readdir-scoped-modules": "^1.1.0", + "rimraf": "^3.0.2", + "semver": "^7.3.5", + "ssri": "^8.0.1", + "tar": "^6.1.11", + "text-table": "~0.2.0", + "tiny-relative-date": "^1.3.0", + "treeverse": "^1.0.4", + "validate-npm-package-name": "~3.0.0", + "which": "^2.0.2", + "write-file-atomic": "^4.0.0" }, "dependencies": { "@gar/promisify": { diff --git a/package.json b/package.json index 30a43ab6..2477c871 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,8 @@ "prettier": true, "space": true, "rules": { - "unicorn/string-content": "off" + "unicorn/string-content": "off", + "no-template-curly-in-string": "off" } }, "renovate": { diff --git a/test/prepare.test.js b/test/prepare.test.js index 1c1aa917..37c8b3a0 100644 --- a/test/prepare.test.js +++ b/test/prepare.test.js @@ -39,6 +39,21 @@ test('Create new changelog with custom path', async t => { t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]); }); +test('Create new changelog with templated path', async t => { + const cwd = tempy.directory(); + const notes = 'Test release note'; + // New file is based on current branch + const changelogFile = '${branch.name}.txt'; + const changelogPath = path.resolve(cwd, 'master.txt'); + + await prepare({changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger, branch: {name: 'master'}}); + + // Verify the content of the CHANGELOG.md + t.is((await readFile(changelogPath)).toString(), `${notes}\n`); + + t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]); +}); + test('Prepend the CHANGELOG.md if there is an existing one', async t => { const cwd = tempy.directory(); const notes = 'Test release note';