-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to update changelogs on releases
- Loading branch information
1 parent
079c1f5
commit 4049054
Showing
4 changed files
with
70 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require('fs'); | ||
const path = require("path"); | ||
|
||
const EMPTY_UNRELEASED_SECTION = `## Unreleased | ||
### :boom: Breaking Change | ||
### :rocket: (Enhancement) | ||
### :bug: (Bug Fix) | ||
### :books: (Refine Doc) | ||
### :house: (Internal) | ||
` | ||
|
||
function findFirstPackageVersion(basePath){ | ||
const packageDirs = fs.readdirSync(basePath); | ||
for(const packageDir of packageDirs){ | ||
const packageJsonPath = path.join(basePath, packageDir, 'package.json'); | ||
try { | ||
const packageJson = fs.readFileSync(packageJsonPath, 'utf-8'); | ||
const version = JSON.parse(packageJson).version; | ||
|
||
if(version != null){ | ||
return version; | ||
} | ||
|
||
console.log('Version in', packageJsonPath, 'was null or undefined, skipping'); | ||
} catch (err) { | ||
console.log('Could not get package JSON', packageJsonPath, err); | ||
} | ||
} | ||
throw new Error('Unable to extract version from packages in ' + basePath); | ||
} | ||
|
||
function replaceEmptySection(changelog){ | ||
// Only match ## at the end in case the last header does not have any entries and the next one is '## version', | ||
// this makes it safe to replace with only '##' | ||
return changelog.replace(RegExp('###.*\n*##', 'gm'), '##'); | ||
} | ||
|
||
// no special handling for bad args as this is only intended for use via predefined npm scripts. | ||
const changelogPath = path.resolve(process.argv[2]); | ||
const version = findFirstPackageVersion(path.resolve(process.argv[3])); | ||
|
||
let changelog = fs.readFileSync(changelogPath, 'utf8').toString(); | ||
let previousChangelog = replaceEmptySection(changelog); | ||
|
||
// keep replacing until there's nothing to replace anymore | ||
while(changelog !== previousChangelog){ | ||
previousChangelog = changelog; | ||
changelog = replaceEmptySection(changelog); | ||
} | ||
|
||
// replace unreleased header with new unreleased section and a version header for the former unreleased section | ||
changelog = changelog.replace(RegExp('## Unreleased'), EMPTY_UNRELEASED_SECTION + '## ' + version); | ||
|
||
fs.writeFileSync(changelogPath, changelog); |