Skip to content

Commit

Permalink
feat: add extractChangelog to config (#403)
Browse files Browse the repository at this point in the history
* feat: add extractChangelog to config

* chore: update functions to have named parameters

* chore: clean up assetsToUpload part

* feat: move extractChanelog to releases and prioritize it over getChangelog
  • Loading branch information
kazupon authored and Eunjae Lee committed Nov 18, 2019
1 parent 2c1e92b commit 35605ec
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/shipjs-lib/src/lib/config/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,6 @@ export default {
},
releases: {
assetsToUpload: [],
extractChangelog: undefined, // ({ version, dir }) => `some specific changelog to that version`,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/src/helper/extractSpecificChangelog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function extractSpecificChangelog(changelog, version) {
export default function extractSpecificChangelog({ changelog, version }) {
if (!changelog) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/shipjs/src/helper/getChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '.',
Expand Down Expand Up @@ -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,
},
]
`);
});
});
23 changes: 13 additions & 10 deletions packages/shipjs/src/step/release/createGitHubRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 35605ec

Please sign in to comment.