1const { getInfo } = require('@changesets/get-github-info');
2
3const REPO = '0no-co/wonka';
4const SEE_LINE = /^See:\s*(.*)/i;
5const TRAILING_CHAR = /[.;:]$/g;
6const listFormatter = new Intl.ListFormat('en-US');
7
8const getSummaryLines = cs => {
9 const lines = cs.summary
10 .trim()
11 .split(/[\r\n]+/)
12 .map(l => l.trim())
13 .filter(Boolean);
14 const size = lines.length;
15 if (size > 0) {
16 lines[size - 1] = lines[size - 1].replace(TRAILING_CHAR, '');
17 }
18
19 return lines;
20};
21
22/** Creates a "(See X)" string from a template */
23const templateSeeRef = links => {
24 const humanReadableLinks = links.filter(Boolean).map(link => {
25 if (typeof link === 'string') return link;
26 return link.pull || link.commit;
27 });
28
29 const size = humanReadableLinks.length;
30 if (size === 0) return '';
31
32 const str = listFormatter.format(humanReadableLinks);
33 return `(See ${str})`;
34};
35
36const changelogFunctions = {
37 getDependencyReleaseLine: async (changesets, dependenciesUpdated) => {
38 if (dependenciesUpdated.length === 0) return '';
39
40 const dependenciesLinks = await Promise.all(
41 changesets.map(async cs => {
42 if (!cs.commit) return undefined;
43
44 const lines = getSummaryLines(cs);
45 const prLine = lines.find(line => SEE_LINE.test(line));
46 if (prLine) {
47 const match = prLine.match(SEE_LINE);
48 return (match && match[1].trim()) || undefined;
49 }
50
51 const { links } = await getInfo({
52 repo: REPO,
53 commit: cs.commit,
54 });
55
56 return links;
57 })
58 );
59
60 let changesetLink = '- Updated dependencies';
61
62 const seeRef = templateSeeRef(dependenciesLinks);
63 if (seeRef) changesetLink += ` ${seeRef}`;
64
65 const detailsLinks = dependenciesUpdated.map(dep => {
66 return ` - ${dep.name}@${dep.newVersion}`;
67 });
68
69 return [changesetLink, ...detailsLinks].join('\n');
70 },
71 getReleaseLine: async (changeset, type) => {
72 let pull, commit, user;
73
74 const lines = getSummaryLines(changeset);
75 const prLineIndex = lines.findIndex(line => SEE_LINE.test(line));
76 if (prLineIndex > -1) {
77 const match = lines[prLineIndex].match(SEE_LINE);
78 pull = (match && match[1].trim()) || undefined;
79 lines.splice(prLineIndex, 1);
80 }
81
82 const [firstLine, ...futureLines] = lines;
83
84 if (changeset.commit && !pull) {
85 const { links } = await getInfo({
86 repo: REPO,
87 commit: changeset.commit,
88 });
89
90 pull = links.pull || undefined;
91 commit = links.commit || undefined;
92 user = links.user || undefined;
93 }
94
95 let annotation = '';
96 if (type === 'patch' && /^\s*fix/i.test(firstLine)) {
97 annotation = '鈿狅笍 ';
98 }
99
100 let str = `- ${annotation}${firstLine}`;
101 if (futureLines.length > 0) {
102 str += `\n${futureLines.map(l => ` ${l}`).join('\n')}`;
103 }
104
105 if (user) {
106 str += `, by ${user}`;
107 }
108
109 if (pull || commit) {
110 const seeRef = templateSeeRef([pull || commit]);
111 if (seeRef) str += ` ${seeRef}`;
112 }
113
114 return str;
115 },
116};
117
118module.exports = {
119 ...changelogFunctions,
120 default: changelogFunctions,
121};