diff --git a/packages/shipjs-lib/src/lib/config/defaultConfig.js b/packages/shipjs-lib/src/lib/config/defaultConfig.js index 67dfded6..8101a49f 100644 --- a/packages/shipjs-lib/src/lib/config/defaultConfig.js +++ b/packages/shipjs-lib/src/lib/config/defaultConfig.js @@ -185,5 +185,6 @@ export default { }, releases: { assetsToUpload: [], + extractChangelog: undefined, // ({ version, dir }) => `some specific changelog to that version`, }, }; diff --git a/packages/shipjs/src/helper/__tests__/extractSpecificChangelog.spec.js b/packages/shipjs/src/helper/__tests__/extractSpecificChangelog.spec.js index 984185fa..d9c1f651 100644 --- a/packages/shipjs/src/helper/__tests__/extractSpecificChangelog.spec.js +++ b/packages/shipjs/src/helper/__tests__/extractSpecificChangelog.spec.js @@ -13,8 +13,12 @@ const lernaChangelogExample = fs describe('extractSpecificChangelog', () => { describe('conventional-changelog', () => { it('works with #', () => { - expect(extractSpecificChangelog(conventionalChangelogExample, '0.8.0')) - .toMatchInlineSnapshot(` + expect( + extractSpecificChangelog({ + changelog: conventionalChangelogExample, + version: '0.8.0', + }) + ).toMatchInlineSnapshot(` "# [0.8.0](https://github.com/algolia/shipjs/compare/v0.7.1...v0.8.0) (2019-10-25) ### Features @@ -26,8 +30,12 @@ describe('extractSpecificChangelog', () => { }); it('works with ##', () => { - expect(extractSpecificChangelog(conventionalChangelogExample, '0.5.5')) - .toMatchInlineSnapshot(` + expect( + extractSpecificChangelog({ + changelog: conventionalChangelogExample, + version: '0.5.5', + }) + ).toMatchInlineSnapshot(` "## [0.5.5](https://github.com/algolia/shipjs/compare/v0.5.4...v0.5.5) (2019-10-01) ### Bug Fixes @@ -39,8 +47,12 @@ describe('extractSpecificChangelog', () => { }); it('works with empty result', () => { - expect(extractSpecificChangelog(conventionalChangelogExample, '0.5.3')) - .toMatchInlineSnapshot(` + expect( + extractSpecificChangelog({ + changelog: conventionalChangelogExample, + version: '0.5.3', + }) + ).toMatchInlineSnapshot(` "## [0.5.3](https://github.com/algolia/shipjs/compare/v0.5.2...v0.5.3) (2019-09-24) " @@ -50,8 +62,12 @@ describe('extractSpecificChangelog', () => { describe('lerna-changelog', () => { it('works', () => { - expect(extractSpecificChangelog(lernaChangelogExample, '0.8.2')) - .toMatchInlineSnapshot(` + expect( + extractSpecificChangelog({ + changelog: lernaChangelogExample, + version: '0.8.2', + }) + ).toMatchInlineSnapshot(` "## v0.8.2 (2018-10-14) #### :bug: Bug Fix diff --git a/packages/shipjs/src/helper/extractSpecificChangelog.js b/packages/shipjs/src/helper/extractSpecificChangelog.js index b75aab57..1bae8f50 100644 --- a/packages/shipjs/src/helper/extractSpecificChangelog.js +++ b/packages/shipjs/src/helper/extractSpecificChangelog.js @@ -1,4 +1,4 @@ -export default function extractSpecificChangelog(changelog, version) { +export default function extractSpecificChangelog({ changelog, version }) { if (!changelog) { return null; } diff --git a/packages/shipjs/src/helper/getChangelog.js b/packages/shipjs/src/helper/getChangelog.js index b54fb500..25e49d1f 100644 --- a/packages/shipjs/src/helper/getChangelog.js +++ b/packages/shipjs/src/helper/getChangelog.js @@ -2,11 +2,11 @@ import path from 'path'; import fs from 'fs'; import { extractSpecificChangelog } from './'; -export default function getChangelog(version, rootDir) { +export default function getChangelog({ version, rootDir }) { const changelogPath = path.resolve(rootDir, 'CHANGELOG.md'); try { const changelogFile = fs.readFileSync(changelogPath, 'utf-8').toString(); - return extractSpecificChangelog(changelogFile, version); + return extractSpecificChangelog({ changelogFile, version }); } catch (err) { if (err.code === 'ENOENT') { return null; diff --git a/packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js b/packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js index 70ee4b38..2f2a9a4c 100644 --- a/packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js +++ b/packages/shipjs/src/step/release/__tests__/createGitHubRelease.spec.js @@ -6,11 +6,14 @@ import { hubInstalled, hubConfigured } from '../../../helper'; jest.mock('temp-write'); jest.mock('globby'); -const getDefaultParams = ({ assetsToUpload } = {}) => ({ +const getDefaultParams = ({ + assetsToUpload, + extractChangelog = () => null, +} = {}) => ({ version: '1.2.3', config: { getTagName: () => 'v1.2.3', - releases: { assetsToUpload }, + releases: { assetsToUpload, extractChangelog }, updateChangelog: false, }, dir: '.', @@ -98,4 +101,31 @@ describe('createGitHubRelease', () => { ] `); }); + + it('works with extractChangelog', async () => { + tempWrite.sync.mockImplementation(() => `/temp/path`); + globby.mockImplementation(path => Promise.resolve([path])); + const mockExtractChangelog = jest + .fn() + .mockImplementation(({ version, dir }) => `# v${version} (${dir})`); + await createGitHubRelease( + getDefaultParams({ + extractChangelog: mockExtractChangelog, + }) + ); + expect(mockExtractChangelog).toHaveBeenCalledWith({ + version: '1.2.3', + dir: '.', + }); + expect(run).toHaveBeenCalledTimes(1); + expect(run.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + Object { + "command": "hub release create -F /temp/path v1.2.3", + "dir": ".", + "dryRun": false, + }, + ] + `); + }); }); diff --git a/packages/shipjs/src/step/release/createGitHubRelease.js b/packages/shipjs/src/step/release/createGitHubRelease.js index c5b491fd..6448f752 100644 --- a/packages/shipjs/src/step/release/createGitHubRelease.js +++ b/packages/shipjs/src/step/release/createGitHubRelease.js @@ -15,41 +15,44 @@ export default async ({ version, config, dir, dryRun }) => skipIf: cannotUseHub, }, async () => { - const { getTagName, releases, updateChangelog } = config; + const { + getTagName, + releases: { assetsToUpload, extractChangelog } = {}, + } = config; const tagName = getTagName({ version }); const args = []; // extract matching changelog - const changelog = updateChangelog ? getChangelog(version, dir) : null; + const getChangelogFn = extractChangelog || getChangelog; + const changelog = getChangelogFn({ version, dir }); const content = `${tagName}\n\n${changelog || ''}`; const exportedPath = tempWrite.sync(content); args.push('-F', quote([exportedPath])); // handle assets - if (releases && releases.assetsToUpload) { - const option = releases.assetsToUpload; + if (assetsToUpload) { const assetPaths = []; - if (typeof option === 'function') { + if (typeof assetsToUpload === 'function') { // function // assetsToUpload: ({dir, version, tagName}) => [...] const files = await Promise.resolve( - option({ dir, version, tagName }) + assetsToUpload({ dir, version, tagName }) ); assetPaths.push(...files); - } else if (Array.isArray(option) && option.length > 0) { + } else if (Array.isArray(assetsToUpload) && assetsToUpload.length > 0) { // list // assetsToUpload: ['package.json', 'dist/*.zip'] - for (const asset of option) { + for (const asset of assetsToUpload) { const files = await globby(asset, { cwd: dir }); if (files) { assetPaths.push(...files); } } - } else if (typeof option === 'string') { + } else if (typeof assetsToUpload === 'string') { // string // assetsToUpload: 'archive.zip' - const files = await globby(option, { cwd: dir }); + const files = await globby(assetsToUpload, { cwd: dir }); if (files) { assetPaths.push(...files); }