Skip to content

Commit 8cffce7

Browse files
authored
fix: remove race condition from the changelog generation (#1186)
Removes the reintroduced changelog generator race condition. Fixes the `options.changelogFromRoot` bug when the default changelog is never overwritten (default changelog is always newer than the source changelog) by tracking which changelogs are default and can be overwritten.
1 parent bf0e52d commit 8cffce7

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

apify-docs-theme/src/theme.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@ async function generateChangelogFromGitHubReleases(paths, repo) {
4040
});
4141

4242
paths.forEach((p) => {
43-
fs.writeFileSync(`${p}/changelog.md`, updateChangelog(markdown));
43+
fs.writeFileSync(path.join(p, 'changelog.md'), updateChangelog(markdown));
4444
});
4545
}
4646

47-
function copyChangelogFromRoot(paths) {
48-
const changelogPath = findPathInParentOrThrow('CHANGELOG.md');
47+
function copyChangelogFromRoot(paths, hasDefaultChangelog) {
48+
const sourceChangelogPath = findPathInParentOrThrow('CHANGELOG.md');
4949

5050
for (const docsPath of paths) {
51-
if (fs.existsSync(path.join(docsPath, 'changelog.md')) && fs.statSync(
52-
path.join(docsPath, 'changelog.md')).mtime >= fs.statSync(changelogPath).mtime) continue;
53-
const changelog = fs.readFileSync(changelogPath, 'utf-8');
54-
fs.writeFileSync(`${docsPath}/changelog.md`, updateChangelog(changelog));
51+
const targetChangelogPath = path.join(docsPath, 'changelog.md');
52+
53+
if (fs.existsSync(targetChangelogPath)
54+
&& fs.statSync(targetChangelogPath).mtime >= fs.statSync(sourceChangelogPath).mtime
55+
&& !hasDefaultChangelog.get(docsPath)) {
56+
continue;
57+
}
58+
59+
const changelog = fs.readFileSync(sourceChangelogPath, 'utf-8');
60+
fs.writeFileSync(targetChangelogPath, updateChangelog(changelog));
5561
}
5662
}
5763

@@ -81,11 +87,7 @@ function theme(
8187
),
8288
];
8389

84-
if (options.changelogFromRoot) {
85-
copyChangelogFromRoot(pathsToCopyChangelog);
86-
} else {
87-
await generateChangelogFromGitHubReleases(pathsToCopyChangelog, `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`);
88-
}
90+
const hasDefaultChangelog = new Map();
8991

9092
for (const p of pathsToCopyChangelog) {
9193
// the changelog page has to exist for the sidebar to work - async loadContent() is (apparently) not awaited for by sidebar
@@ -97,6 +99,13 @@ sidebar_label: Changelog
9799
It seems that the changelog is not available.
98100
This either means that your Docusaurus setup is misconfigured, or that your GitHub repository contains no releases yet.
99101
`);
102+
hasDefaultChangelog.set(p, true);
103+
}
104+
105+
if (options.changelogFromRoot) {
106+
copyChangelogFromRoot(pathsToCopyChangelog, hasDefaultChangelog);
107+
} else {
108+
await generateChangelogFromGitHubReleases(pathsToCopyChangelog, `${context.siteConfig.organizationName}/${context.siteConfig.projectName}`);
100109
}
101110
} catch (e) {
102111
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)