2021-07-04 15:10:25 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
const util = require("util");
|
|
|
|
const exec = util.promisify(require("child_process").exec);
|
|
|
|
|
2023-12-12 07:02:51 +01:00
|
|
|
const excalidrawDir = `${__dirname}/../packages/excalidraw`;
|
2021-07-04 15:10:25 +02:00
|
|
|
const excalidrawPackage = `${excalidrawDir}/package.json`;
|
|
|
|
const pkg = require(excalidrawPackage);
|
|
|
|
const lastVersion = pkg.version;
|
|
|
|
const existingChangeLog = fs.readFileSync(
|
|
|
|
`${excalidrawDir}/CHANGELOG.md`,
|
|
|
|
"utf8",
|
|
|
|
);
|
|
|
|
|
|
|
|
const supportedTypes = ["feat", "fix", "style", "refactor", "perf", "build"];
|
|
|
|
const headerForType = {
|
|
|
|
feat: "Features",
|
|
|
|
fix: "Fixes",
|
|
|
|
style: "Styles",
|
|
|
|
refactor: " Refactor",
|
|
|
|
perf: "Performance",
|
|
|
|
build: "Build",
|
|
|
|
};
|
2022-02-16 16:31:59 +01:00
|
|
|
const badCommits = [];
|
2021-07-04 15:10:25 +02:00
|
|
|
const getCommitHashForLastVersion = async () => {
|
|
|
|
try {
|
|
|
|
const commitMessage = `"release @excalidraw/excalidraw@${lastVersion}"`;
|
|
|
|
const { stdout } = await exec(
|
|
|
|
`git log --format=format:"%H" --grep=${commitMessage}`,
|
|
|
|
);
|
|
|
|
return stdout;
|
2021-11-02 13:24:16 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2021-07-04 15:10:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getLibraryCommitsSinceLastRelease = async () => {
|
|
|
|
const commitHash = await getCommitHashForLastVersion();
|
|
|
|
const { stdout } = await exec(
|
|
|
|
`git log --pretty=format:%s ${commitHash}...master`,
|
|
|
|
);
|
|
|
|
const commitsSinceLastRelease = stdout.split("\n");
|
|
|
|
const commitList = {};
|
|
|
|
supportedTypes.forEach((type) => {
|
|
|
|
commitList[type] = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
commitsSinceLastRelease.forEach((commit) => {
|
|
|
|
const indexOfColon = commit.indexOf(":");
|
|
|
|
const type = commit.slice(0, indexOfColon);
|
|
|
|
if (!supportedTypes.includes(type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const messageWithoutType = commit.slice(indexOfColon + 1).trim();
|
|
|
|
const messageWithCapitalizeFirst =
|
|
|
|
messageWithoutType.charAt(0).toUpperCase() + messageWithoutType.slice(1);
|
2022-02-16 16:31:59 +01:00
|
|
|
const prMatch = commit.match(/\(#([0-9]*)\)/);
|
|
|
|
if (prMatch) {
|
|
|
|
const prNumber = prMatch[1];
|
2021-07-04 15:10:25 +02:00
|
|
|
|
2022-02-16 16:31:59 +01:00
|
|
|
// return if the changelog already contains the pr number which would happen for package updates
|
|
|
|
if (existingChangeLog.includes(prNumber)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const prMarkdown = `[#${prNumber}](https://github.com/excalidraw/excalidraw/pull/${prNumber})`;
|
|
|
|
const messageWithPRLink = messageWithCapitalizeFirst.replace(
|
|
|
|
/\(#[0-9]*\)/,
|
|
|
|
prMarkdown,
|
|
|
|
);
|
|
|
|
commitList[type].push(messageWithPRLink);
|
|
|
|
} else {
|
|
|
|
badCommits.push(commit);
|
|
|
|
commitList[type].push(messageWithCapitalizeFirst);
|
2021-07-04 15:10:25 +02:00
|
|
|
}
|
|
|
|
});
|
2022-02-16 16:31:59 +01:00
|
|
|
console.info("Bad commits:", badCommits);
|
2021-07-04 15:10:25 +02:00
|
|
|
return commitList;
|
|
|
|
};
|
|
|
|
|
2021-07-05 20:43:56 +02:00
|
|
|
const updateChangelog = async (nextVersion) => {
|
2021-07-04 15:10:25 +02:00
|
|
|
const commitList = await getLibraryCommitsSinceLastRelease();
|
|
|
|
let changelogForLibrary =
|
|
|
|
"## Excalidraw Library\n\n**_This section lists the updates made to the excalidraw library and will not affect the integration._**\n\n";
|
|
|
|
supportedTypes.forEach((type) => {
|
|
|
|
if (commitList[type].length) {
|
|
|
|
changelogForLibrary += `### ${headerForType[type]}\n\n`;
|
|
|
|
const commits = commitList[type];
|
|
|
|
commits.forEach((commit) => {
|
|
|
|
changelogForLibrary += `- ${commit}\n\n`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
changelogForLibrary += "---\n";
|
|
|
|
const lastVersionIndex = existingChangeLog.indexOf(`## ${lastVersion}`);
|
2021-07-05 20:43:56 +02:00
|
|
|
let updatedContent =
|
2021-07-04 15:10:25 +02:00
|
|
|
existingChangeLog.slice(0, lastVersionIndex) +
|
|
|
|
changelogForLibrary +
|
2021-07-05 20:43:56 +02:00
|
|
|
existingChangeLog.slice(lastVersionIndex);
|
|
|
|
const currentDate = new Date().toISOString().slice(0, 10);
|
|
|
|
const newVersion = `## ${nextVersion} (${currentDate})`;
|
|
|
|
updatedContent = updatedContent.replace(`## Unreleased`, newVersion);
|
2021-07-04 15:10:25 +02:00
|
|
|
fs.writeFileSync(`${excalidrawDir}/CHANGELOG.md`, updatedContent, "utf8");
|
|
|
|
};
|
|
|
|
|
2021-07-05 18:59:35 +02:00
|
|
|
module.exports = updateChangelog;
|