1 | const path = require('path');
|
2 | const {readFile, writeFile, ensureFile} = require('fs-extra');
|
3 | const resolveConfig = require('./resolve-config');
|
4 |
|
5 | module.exports = async (pluginConfig, {cwd, nextRelease: {notes}, logger}) => {
|
6 | const {changelogFile, changelogTitle} = resolveConfig(pluginConfig);
|
7 | const changelogPath = path.resolve(cwd, changelogFile);
|
8 |
|
9 | if (notes) {
|
10 | await ensureFile(changelogPath);
|
11 | const currentFile = (await readFile(changelogPath)).toString().trim();
|
12 |
|
13 | if (currentFile) {
|
14 | logger.log('Update %s', changelogPath);
|
15 | } else {
|
16 | logger.log('Create %s', changelogPath);
|
17 | }
|
18 |
|
19 | const currentContent =
|
20 | changelogTitle && currentFile.startsWith(changelogTitle)
|
21 | ? currentFile.slice(changelogTitle.length).trim()
|
22 | : currentFile;
|
23 | const content = `${notes.trim()}\n${currentContent ? `\n${currentContent}\n` : ''}`;
|
24 |
|
25 | await writeFile(changelogPath, changelogTitle ? `${changelogTitle}\n\n${content}` : content);
|
26 | }
|
27 | };
|