.release-it.js
/**
 * A release-it "before:release" hook function.
 *
 * After generating the conventional changelog file for the current release,
 * insert release notes between the release header and the commit messages.
 * Additionally, ensure the Changelog title is moved/added to the beginning of
 * the file and remove any duplicate version of the new release.
 */
A release-it "before:release" hook function.
After generating the conventional changelog file for the current release,
insert release notes between the release header and the commit messages.
Additionally, ensure the Changelog title is moved/added to the beginning of
the file and remove any duplicate version of the new release.
A release - it " before : release " hook function . After generating the conventional changelog file for the current release insert release notes between the release header and the commit messages . Additionally ensure the Changelog title is moved / added to the beginning of the file and remove any duplicate version of the new release .
function augmentChangelog() {
    const readLines = file => fs.readFileSync(file, "utf-8").replace(/\r\n/g, "\n").split("\n");
    const findLineIndex = (regexp, allLines, startIndex) => {
        const len = allLines.length;
        for (let i = startIndex || 0; i < len; i++) {
            if (regexp.test(allLines[i])) {
                return i;
            }
        }
        return undefined;
    };
    const escapeRegex = s => s.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
    const releaseRegex = version => {
        const versionRegex = escapeRegex(version);
        const urlRegex = "\\S+";
        return new RegExp(`^##? (${versionRegex})|(\\[${versionRegex}\\]\\(${urlRegex}\\)) \\(\\d\\d\\d\\d-\\d\\d-\\d\\d\\)$`);
    };
    const anyReleaseRegex = /^##? (\S+)|(\[\S+\]\(\S+\)) \(\d\d\d\d-\d\d-\d\d\)$/;
    const [version, latestVersion, isCi] = process.argv.slice(1);
    let relNotes;
    try {
        relNotes = readLines(releaseNotesFile);
        fs.unlinkSync(releaseNotesFile);
    } catch { }
    const lines = readLines(changelogFile);
    const titleLine = "# Changelog";
    const titleRegex = new RegExp(`^${escapeRegex(titleLine)}$`);
    const indexTitle = findLineIndex(titleRegex, lines);
    if (indexTitle !== undefined) {
        lines.unshift(...lines.splice(indexTitle, 2));
    } else {
        lines.unshift(titleLine, "");
    }
    const indexVersion = findLineIndex(releaseRegex(version), lines);
    if (version === latestVersion) {
        const indexLatestVersion = findLineIndex(releaseRegex(latestVersion), lines, indexVersion + 1);
        if (indexLatestVersion !== undefined) {
            let indexNext = findLineIndex(anyReleaseRegex, lines, indexLatestVersion + 1);
            if (indexNext === undefined) {
                indexNext = lines.length;
            }
            lines.splice(indexLatestVersion, indexNext - indexLatestVersion);
        }
    }
    if (relNotes && relNotes.length > 0) {
        lines.splice(indexVersion + 2, 0, ...relNotes);
    }
    fs.writeFileSync(changelogFile, lines.join("\n"), "utf-8");
    execSync(`git add ${changelogFile}`);
}
function augmentChangelog ( ) { const readLines = file => fs . readFileSync ( file , "utf-8" ) . replace ( / \r\n / g , "\n" ) . split ( "\n" ) ; const findLineIndex = ( regexp , allLines , startIndex ) => { const len = allLines . length ; for ( let i = startIndex || 0 ; i < len ; i ++ ) { if ( regexp . test ( allLines [ i ] ) ) { return i ; } } return undefined ; } ; const escapeRegex = s => s . replace ( / [.*+\-?^${}()|[\]\\] / g , "\\$&" ) ; const releaseRegex = version => { const versionRegex = escapeRegex ( version ) ; const urlRegex = "\\S+" ; return new RegExp ( ` ${ versionRegex } \\ ${ versionRegex } \\ \\ ${ urlRegex } \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ ` ) ; } ; const anyReleaseRegex = / ^##? (\S+)|(\[\S+\]\(\S+\)) \(\d\d\d\d-\d\d-\d\d\)$ / ; const [ version , latestVersion , isCi ] = process . argv . slice ( 1 ) ; let relNotes ; try { relNotes = readLines ( releaseNotesFile ) ; fs . unlinkSync ( releaseNotesFile ) ; } catch { } const lines = readLines ( changelogFile ) ; const titleLine = "# Changelog" ; const titleRegex = new RegExp ( ` ${ escapeRegex ( titleLine ) } ` ) ; const indexTitle = findLineIndex ( titleRegex , lines ) ; if ( indexTitle !== undefined ) { lines . unshift ( ... lines . splice ( indexTitle , 2 ) ) ; } else { lines . unshift ( titleLine , "" ) ; } const indexVersion = findLineIndex ( releaseRegex ( version ) , lines ) ; if ( version === latestVersion ) { const indexLatestVersion = findLineIndex ( releaseRegex ( latestVersion ) , lines , indexVersion + 1 ) ; if ( indexLatestVersion !== undefined ) { let indexNext = findLineIndex ( anyReleaseRegex , lines , indexLatestVersion + 1 ) ; if ( indexNext === undefined ) { indexNext = lines . length ; } lines . splice ( indexLatestVersion , indexNext - indexLatestVersion ) ; } } if ( relNotes && relNotes . length > 0 ) { lines . splice ( indexVersion + 2 , 0 , ... relNotes ) ; } fs . writeFileSync ( changelogFile , lines . join ( "\n" ) , "utf-8" ) ; execSync ( ` ${ changelogFile } ` ) ; }
A release-it "before:release" hook function.
A release - it " before : release " hook function .
// Enforce uniform line endings. // Use the prompted release notes in a non-CI environment. In a CI // environment, the release notes file could have been generated by a tool.  // Move/add changelog title to the top. // Remove latest release version if it equals the new release. // Insert release notes if given. // Stage the modified changelog file again.