-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathchangelog-write.js
More file actions
63 lines (49 loc) · 1.61 KB
/
changelog-write.js
File metadata and controls
63 lines (49 loc) · 1.61 KB
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
const fs = require('fs');
const prettier = require('prettier');
const cwd = process.cwd();
function getCurrentDate() {
const date = new Date();
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const year = date.getFullYear();
return `## ${day}-${month}-${year}`;
}
function getContent(releases) {
const releaseEntries = Object.entries(
releases,
).map(([displayName, changesets]) =>
[displayName, '\n\n', ...changesets].join(''),
);
let content = [getCurrentDate(), ...releaseEntries].join('\n\n');
content = prettier.format(content, {
parser: 'markdown',
printWidth: 80,
singleQuote: true,
trailingComma: 'es5',
});
return content;
}
async function main() {
const releases = JSON.parse(
fs.readFileSync(`${cwd}/.changelogrc`).toString(),
);
if (!Object.entries(releases).length) return;
const content = getContent(releases);
const changelogPath = `${cwd}/CHANGELOG.md`;
const changelogPathWebsite = `${cwd}/packages/website/content/changelog.mdx`;
const changelog = await fs.promises.readFile(changelogPath, 'utf8');
const newChangelog = changelog.replace(
'<!-- CHANGELOG:INSERT -->',
`<!-- CHANGELOG:INSERT -->\n\n${content}`,
);
// write new changelog
await fs.promises.writeFile(changelogPath, newChangelog);
// Write changelog to website package
await fs.promises.writeFile(
changelogPathWebsite,
newChangelog.replace('<!-- CHANGELOG:INSERT -->\n\n', ''),
);
// clean changelogrc file
await fs.promises.writeFile(`${cwd}/.changelogrc`, '{}');
}
main();