/
index.ts
170 lines (153 loc) · 5.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { Changeset } from '@changesets/types';
import { execSync } from 'child_process';
import type { ManyPkgPackage } from '../types';
interface Commit {
commitHash: string;
commitMessage: string;
}
interface ConventionalMessagesToCommits {
changelogMessage: string;
commitHashes: string[];
}
/*
* Copied from conventional commits config:
* https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/writer-opts.js
* "section" is currently unused but is left in, with the intent to update changeset changelog generation once more fleshed out
*/
const defaultCommitTypes = [
{ type: 'feat', section: 'Features' },
{ type: 'feature', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'perf', section: 'Performance Improvements' },
{ type: 'revert', section: 'Reverts' },
{ type: 'docs', section: 'Documentation' },
{ type: 'style', section: 'Styles' },
{ type: 'chore', section: 'Miscellaneous Chores' },
{ type: 'refactor', section: 'Code Refactoring' },
{ type: 'test', section: 'Tests' },
{ type: 'build', section: 'Build System' },
{ type: 'ci', section: 'Continuous Integration' },
];
export const isBreakingChange = (commit: string) => {
return (
commit.includes('BREAKING CHANGE:') ||
// eslint-disable-next-line no-useless-escape
defaultCommitTypes.some((commitType) => commit.match(new RegExp(`^${commitType.type}(?:\(.*\))?!:`)))
);
};
export const isConventionalCommit = (commit: string) => {
// eslint-disable-next-line no-useless-escape
return defaultCommitTypes.some((commitType) => commit.match(new RegExp(`^${commitType.type}(?:\(.*\))?!?:`)));
};
/* Attempts to associate non-conventional commits to the nearest conventional commit */
export const associateCommitsToConventionalCommitMessages = (commits: Commit[]): ConventionalMessagesToCommits[] => {
return commits.reduce((acc, curr) => {
if (!acc.length) {
return [
{
changelogMessage: curr.commitMessage,
commitHashes: [curr.commitHash],
},
];
}
if (isConventionalCommit(curr.commitMessage)) {
if (isConventionalCommit(acc[acc.length - 1].changelogMessage)) {
return [
...acc,
{
changelogMessage: curr.commitMessage,
commitHashes: [curr.commitHash],
},
];
} else {
return [
...acc.slice(0, acc.length - 1),
{
changelogMessage: curr.commitMessage,
commitHashes: [...acc[acc.length - 1].commitHashes, curr.commitHash],
},
];
}
} else {
return [
...acc.slice(0, acc.length - 1),
{
...acc[acc.length - 1],
commitHashes: [...acc[acc.length - 1].commitHashes, curr.commitHash],
},
];
}
}, [] as ConventionalMessagesToCommits[]);
};
export const getFilesChangedSince = (opts: { from: string; to: string }) => {
return execSync(`git diff --name-only ${opts.from}~1...${opts.to}`).toString().trim().split('\n');
};
export const getRepoRoot = () => {
return execSync('git rev-parse --show-toplevel').toString().trim().replace(/\n|\r/g, '');
};
export const conventionalMessagesWithCommitsToChangesets = (
conventionalMessagesToCommits: ConventionalMessagesToCommits[],
options: { ignoredFiles?: (string | RegExp)[]; packages: ManyPkgPackage[] },
) => {
const { ignoredFiles = [], packages } = options;
return conventionalMessagesToCommits
.map((entry) => {
const filesChanged = getFilesChangedSince({
from: entry.commitHashes[0],
to: entry.commitHashes[entry.commitHashes.length - 1],
}).filter((file) => {
return ignoredFiles.every((ignoredPattern) => !file.match(ignoredPattern));
});
const packagesChanged = packages.filter((pkg) => {
return filesChanged.some((file) => file.match(pkg.dir.replace(`${getRepoRoot()}/`, '')));
});
if (packagesChanged.length === 0) return null;
return {
releases: packagesChanged.map((pkg) => {
return {
name: pkg.packageJson.name,
type: isBreakingChange(entry.changelogMessage)
? 'major'
: entry.changelogMessage.startsWith('feat')
? 'minor'
: 'patch',
};
}),
summary: entry.changelogMessage,
packagesChanged,
};
})
.filter(Boolean) as Changeset[];
};
export const gitFetch = (branch: string) => {
execSync(`git fetch origin ${branch}`);
};
export const getCurrentBranch = () => {
return execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
};
// This could be running on the main branch or on a branch that was created from the main branch.
// If this is running on the main branch, we want to get all commits since the last release.
// If this is running on a branch that was created from the main branch, we want to get all commits since the branch was created.
export const getCommitsSinceRef = (branch: string) => {
gitFetch(branch);
const currentBranch = getCurrentBranch();
let sinceRef = `origin/${branch}`;
if (currentBranch === branch) {
try {
sinceRef = execSync('git describe --tags --abbrev=0').toString();
} catch (e) {
console.log(
"No git tags found, using repo's first commit for automated change detection. Note: this may take a while.",
);
sinceRef = execSync('git rev-list --max-parents=0 HEAD').toString();
}
}
sinceRef = sinceRef.trim();
return execSync(`git rev-list --ancestry-path ${sinceRef}...HEAD`).toString().split('\n').filter(Boolean).reverse();
};
const compareChangeSet = (a: Changeset, b: Changeset): boolean => {
return a.summary === b.summary && JSON.stringify(a.releases) == JSON.stringify(b.releases);
};
export const difference = (a: Changeset[], b: Changeset[]): Changeset[] => {
return a.filter((changeA) => !b.some((changeB) => compareChangeSet(changeA, changeB)));
};