cli/ts/mod_commit/index.ts

100 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-06-21 17:48:43 +00:00
// this file contains code to create commits in a consistent way
import * as plugins from './mod.plugins.js';
import * as paths from '../paths.js';
import { logger } from '../gitzone.logging.js';
export const run = async (argvArg: any) => {
2024-06-23 11:26:51 +00:00
logger.log('info', `gathering facts...`);
const aidoc = new plugins.tsdoc.AiDoc();
await aidoc.start();
const nextCommitObject = await aidoc.buildNextCommitObject(paths.cwd);
logger.log('info', `---------
Next recommended commit would be:
===========
-> ${nextCommitObject.recommendedNextVersion}:
-> ${nextCommitObject.recommendedNextVersionLevel}(${nextCommitObject.recommendedNextVersionScope}): ${nextCommitObject.recommendedNextVersionMessage}
===========
`);
2024-06-21 17:48:43 +00:00
const commitInteract = new plugins.smartinteract.SmartInteract();
commitInteract.addQuestions([
{
type: 'list',
name: `commitType`,
message: `Choose TYPE of the commit:`,
choices: [`fix`, `feat`, `BREAKING CHANGE`],
2024-06-23 11:26:51 +00:00
default: nextCommitObject.recommendedNextVersionLevel,
2024-06-21 17:48:43 +00:00
},
{
type: 'input',
name: `commitScope`,
message: `What is the SCOPE of the commit:`,
2024-06-23 11:26:51 +00:00
default: nextCommitObject.recommendedNextVersionScope,
2024-06-21 17:48:43 +00:00
},
{
type: `input`,
name: `commitDescription`,
message: `What is the DESCRIPTION of the commit?`,
2024-06-23 11:26:51 +00:00
default: nextCommitObject.recommendedNextVersionMessage,
2024-06-21 17:48:43 +00:00
},
{
type: 'confirm',
name: `pushToOrigin`,
message: `Do you want to push this version now?`,
default: true,
},
]);
const answerBucket = await commitInteract.runQueue();
const commitString = createCommitStringFromAnswerBucket(answerBucket);
const commitVersionType = (() => {
switch (answerBucket.getAnswerFor('commitType')) {
case 'fix':
return 'patch';
case 'feat':
return 'minor';
case 'BREAKING CHANGE':
return 'major';
}
})();
logger.log('info', `OK! Creating commit with message '${commitString}'`);
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
sourceFilePaths: [],
});
2024-06-23 11:26:51 +00:00
logger.log('info', `Baking commitinfo into code ...`);
2024-06-21 17:48:43 +00:00
const commitInfo = new plugins.commitinfo.CommitInfo(paths.cwd, commitVersionType);
await commitInfo.writeIntoPotentialDirs();
2024-06-23 11:26:51 +00:00
logger.log('info', `Writing changelog.md ...`);
let changelog = nextCommitObject.changelog;
changelog = changelog.replaceAll('{{nextVersion}}', (await commitInfo.getNextPlannedVersion()).versionString);
changelog = changelog.replaceAll('{{nextVersionScope}}', `${await answerBucket.getAnswerFor('commitType')}(${await answerBucket.getAnswerFor('commitScope')})`);
changelog = changelog.replaceAll('{{nextVersionMessage}}', nextCommitObject.recommendedNextVersionMessage);
if (nextCommitObject.recommendedNextVersionDetails?.length > 0) {
changelog = changelog.replaceAll('{{nextVersionDetails}}', '- ' + nextCommitObject.recommendedNextVersionDetails.join('\n- '));
} else {
changelog = changelog.replaceAll('\n{{nextVersionDetails}}', '');
}
2024-06-23 11:26:51 +00:00
await plugins.smartfile.memory.toFs(changelog, plugins.path.join(paths.cwd, `changelog.md`));
2024-06-21 17:48:43 +00:00
logger.log('info', `Staging files for commit:`);
await smartshellInstance.exec(`git add -A`);
await smartshellInstance.exec(`git commit -m "${commitString}"`);
await smartshellInstance.exec(`npm version ${commitVersionType}`);
if (answerBucket.getAnswerFor('pushToOrigin') && !(process.env.CI === 'true')) {
await smartshellInstance.exec(`git push origin master --follow-tags`);
}
};
const createCommitStringFromAnswerBucket = (answerBucket: plugins.smartinteract.AnswerBucket) => {
const commitType = answerBucket.getAnswerFor('commitType');
const commitScope = answerBucket.getAnswerFor('commitScope');
const commitDescription = answerBucket.getAnswerFor('commitDescription');
return `${commitType}(${commitScope}): ${commitDescription}`;
};