Skip to content

Commit c855dbd

Browse files
kazuponEunjae Lee
authored andcommitted
feat(releaseType): expose at hooks ("major", "minor", ...) (#441)
* feat: release type * fix: tweak parameter names
1 parent d46d83f commit c855dbd

15 files changed

+129
-27
lines changed

GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ const fs = require("fs");
284284
const path = require("path");
285285
286286
module.exports = {
287-
versionUpdated: ({ version, dir, exec }) => {
287+
versionUpdated: ({ version, releaseType, dir, exec }) => {
288288
// update `lerna.json`
289289
const lernaConfigPath = path.resolve(dir, "lerna.json");
290290
const lernaConfig = JSON.parse(fs.readFileSync(lernaConfigPath).toString());

packages/shipjs-lib/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as getNextVersion } from './lib/util/getNextVersion';
44
export { default as updateVersion } from './lib/util/updateVersion';
55
export { default as isValidVersion } from './lib/util/isValidVersion';
66
export { default as getReleaseTag } from './lib/util/getReleaseTag';
7+
export { default as getReleaseType } from './lib/util/getReleaseType';
78

89
/* git */
910
export { default as hasLocalBranch } from './lib/git/hasLocalBranch';

packages/shipjs-lib/src/lib/config/defaultConfig.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ export default {
1111
conventionalChangelogArgs: '-p angular -i CHANGELOG.md -s',
1212
installCommand: ({ isYarn }) =>
1313
isYarn ? 'yarn install --silent' : 'npm install',
14-
versionUpdated: ({ version, dir, exec }) => {},
15-
beforeCommitChanges: ({ nextVersion, exec, dir }) => {},
16-
getStagingBranchName: ({ nextVersion }) => `releases/v${nextVersion}`,
17-
formatCommitMessage: ({ version, mergeStrategy, baseBranch }) =>
14+
versionUpdated: ({ version, releaseType, dir, exec }) => {},
15+
beforeCommitChanges: ({ nextVersion, releaseType, exec, dir }) => {},
16+
getStagingBranchName: ({ nextVersion, releaseType }) => `releases/v${nextVersion}`,
17+
formatCommitMessage: ({ version, releaseType, mergeStrategy, baseBranch }) =>
1818
mergeStrategy.toSameBranch.includes(baseBranch)
1919
? `chore: release v${version}`
2020
: `chore: prepare v${version}`,
21-
formatPullRequestTitle: ({ version }) => `chore: release v${version}`,
21+
formatPullRequestTitle: ({ version, releaseType }) => `chore: release v${version}`,
2222
formatPullRequestMessage: ({
2323
formatPullRequestTitle,
2424
repoURL,
@@ -28,8 +28,12 @@ export default {
2828
mergeStrategy,
2929
currentVersion,
3030
nextVersion,
31+
releaseType,
3132
}) => {
32-
const pullRequestTitle = formatPullRequestTitle({ version: nextVersion });
33+
const pullRequestTitle = formatPullRequestTitle({
34+
version: nextVersion,
35+
releaseType,
36+
});
3337
const lines = [
3438
pullRequestTitle,
3539
'',
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import getReleaseType from '../getReleaseType';
2+
3+
describe('getReleaseType', () => {
4+
it('gets release type from current version and next version', () => {
5+
expect(getReleaseType('1.0.0', '0.10.99')).toBe('major');
6+
});
7+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { diff } from 'semver';
2+
3+
export default function getReleaseType(currentVersion, nextVersion) {
4+
return diff(currentVersion, nextVersion);
5+
}

packages/shipjs/src/flow/prepare.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getAppName, loadConfig } from 'shipjs-lib';
1+
import { getAppName, loadConfig, getReleaseType } from 'shipjs-lib';
22

33
import printHelp from '../step/prepare/printHelp';
44
import printDryRunBanner from '../step/printDryRunBanner';
@@ -54,24 +54,34 @@ async function prepare({
5454
nextVersion,
5555
dryRun,
5656
});
57+
const releaseType = getReleaseType(currentVersion, nextVersion);
5758
const { stagingBranch } = prepareStagingBranch({
5859
config,
5960
nextVersion,
61+
releaseType,
6062
dir,
6163
});
6264
checkoutToStagingBranch({ stagingBranch, dir, dryRun });
6365
const updateVersionFn = config.monorepo
6466
? updateVersionMonorepo
6567
: updateVersion;
66-
await updateVersionFn({ config, nextVersion, dir, dryRun });
68+
await updateVersionFn({ config, nextVersion, releaseType, dir, dryRun });
6769
installDependencies({ config, dir, dryRun });
6870
updateChangelog({ config, firstRelease, releaseCount, dir, dryRun });
69-
await commitChanges({ nextVersion, dir, config, baseBranch, dryRun });
71+
await commitChanges({
72+
nextVersion,
73+
releaseType,
74+
dir,
75+
config,
76+
baseBranch,
77+
dryRun,
78+
});
7079
const { pullRequestUrl } = createPullRequest({
7180
baseBranch,
7281
stagingBranch,
7382
currentVersion,
7483
nextVersion,
84+
releaseType,
7585
noBrowse,
7686
config,
7787
dir,

packages/shipjs/src/step/prepare/__tests__/commitChanges.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,37 @@ describe('commitChanges', () => {
3232
.fn()
3333
.mockImplementation(() => Promise.resolve());
3434
wrapExecWithDir.mockImplementation(() => jest.fn());
35+
const formatCommitMessage = jest
36+
.fn()
37+
.mockImplementation(() => 'test message');
3538

3639
await commitChanges({
3740
config: {
38-
formatCommitMessage: () => 'test message',
41+
formatCommitMessage,
3942
beforeCommitChanges,
4043
},
4144
dryRun: false,
4245
dir: '.',
4346
nextVersion: '1.2.3',
47+
releaseType: 'patch',
4448
});
4549

4650
expect(wrapExecWithDir).toHaveBeenCalledTimes(1);
4751
expect(wrapExecWithDir).toHaveBeenCalledWith('.');
52+
expect(formatCommitMessage).toHaveBeenCalledTimes(1);
53+
expect(formatCommitMessage).toHaveBeenCalledWith(
54+
expect.objectContaining({
55+
version: '1.2.3',
56+
releaseType: 'patch',
57+
})
58+
);
4859
expect(beforeCommitChanges).toHaveBeenCalledTimes(1);
4960
expect(beforeCommitChanges).toHaveBeenCalledWith(
5061
expect.objectContaining({
5162
exec: expect.any(Function),
5263
dir: '.',
5364
nextVersion: '1.2.3',
65+
releaseType: 'patch',
5466
})
5567
);
5668
expect(run.mock.calls[0]).toMatchInlineSnapshot(`

packages/shipjs/src/step/prepare/__tests__/createPullRequest.spec.js

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ import { run } from '../../../util';
55
import { getDestinationBranchName } from '../../../helper';
66
jest.mock('temp-write');
77

8-
const defaultParams = {
8+
const getDefaultParams = ({
9+
currentVersion = '1.2.2',
10+
nextVersion = '1.2.3',
11+
releaseType = 'patch',
12+
formatPullRequestTitle = () => 'chore: releases v0.1.1',
13+
formatPullRequestMessage = () => 'chore: releases v0.1.1',
14+
dryRun = false,
15+
} = {}) => ({
916
baseBranch: 'master',
17+
currentVersion,
18+
nextVersion,
19+
releaseType,
1020
config: {
1121
mergeStrategy: {
1222
toSameBranch: ['master'],
1323
},
14-
formatPullRequestMessage: () => 'chore: releases v0.1.1',
24+
formatPullRequestTitle,
25+
formatPullRequestMessage,
1526
remote: 'origin',
1627
pullRequestReviewer: ['foo', 'bar'],
1728
},
1829
dir: '.',
19-
};
30+
dryRun,
31+
});
2032

2133
describe('createPullRequest', () => {
2234
beforeEach(() => {
@@ -26,10 +38,7 @@ describe('createPullRequest', () => {
2638
});
2739

2840
it('works in dry mode', () => {
29-
const ret = createPullRequest({
30-
...defaultParams,
31-
dryRun: true,
32-
});
41+
const ret = createPullRequest(getDefaultParams({ dryRun: true }));
3342
expect(run.mock.calls[0][0]).toMatchInlineSnapshot(`
3443
Object {
3544
"command": "git remote prune origin",
@@ -51,9 +60,42 @@ describe('createPullRequest', () => {
5160
silentExec.mockImplementationOnce(() => ({
5261
toString: () => `13 chore: releases v0.1.1\n12 docs: update README`,
5362
}));
54-
const { pullRequestUrl } = createPullRequest({
55-
...defaultParams,
56-
});
63+
const { pullRequestUrl } = createPullRequest(getDefaultParams());
5764
expect(pullRequestUrl).toEqual('https://github.com/my/repo/pull/13');
5865
});
66+
67+
it('pass releaseType to hooks', () => {
68+
const mockFormatPullRequestTitle = jest
69+
.fn()
70+
.mockImplementation(({ version, type }) => `# v${version} (${type})`);
71+
const mockFormatPullRequestMessage = jest
72+
.fn()
73+
.mockImplementation(
74+
({ formatPullRequestTitle, nextVersion, releaseType }) => {
75+
return [
76+
formatPullRequestTitle({
77+
version: nextVersion,
78+
releaseType,
79+
}),
80+
].join('\n');
81+
}
82+
);
83+
createPullRequest(
84+
getDefaultParams({
85+
dryRun: true,
86+
formatPullRequestTitle: mockFormatPullRequestTitle,
87+
formatPullRequestMessage: mockFormatPullRequestMessage,
88+
})
89+
);
90+
expect(mockFormatPullRequestTitle).toHaveBeenCalledWith({
91+
version: '1.2.3',
92+
releaseType: 'patch',
93+
});
94+
expect(mockFormatPullRequestMessage).toHaveBeenCalled();
95+
expect(mockFormatPullRequestMessage.mock.calls[0][0]).toMatchObject({
96+
currentVersion: '1.2.2',
97+
nextVersion: '1.2.3',
98+
releaseType: 'patch',
99+
});
100+
});
59101
});

packages/shipjs/src/step/prepare/__tests__/updateVersion.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('updateVersion', () => {
1111
versionUpdated,
1212
},
1313
nextVersion: '1.2.3',
14+
releaseType: 'patch',
1415
dir: '.',
1516
dryRun: false,
1617
});
@@ -23,6 +24,7 @@ describe('updateVersion', () => {
2324
Object {
2425
"dir": ".",
2526
"exec": undefined,
27+
"releaseType": "patch",
2628
"version": "1.2.3",
2729
}
2830
`);

packages/shipjs/src/step/prepare/__tests__/updateVersionMonorepo.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('updateVersionMonorepo', () => {
2020
},
2121
dir: '.',
2222
nextVersion: '1.2.3',
23+
releaseType: 'patch',
2324
});
2425
expect(updateVersion).toHaveBeenCalledTimes(3);
2526
expect(updateVersion.mock.calls[0]).toMatchInlineSnapshot(`
@@ -52,6 +53,7 @@ describe('updateVersionMonorepo', () => {
5253
Object {
5354
"dir": ".",
5455
"exec": undefined,
56+
"releaseType": "patch",
5557
"version": "1.2.3",
5658
}
5759
`);

0 commit comments

Comments
 (0)