|
|
|
@ -2,6 +2,13 @@ import * as plugins from '../plugins.js';
|
|
|
|
|
import { AiDoc } from '../classes.aidoc.js';
|
|
|
|
|
import { ProjectContext } from './projectcontext.js';
|
|
|
|
|
|
|
|
|
|
export interface INextCommitObject {
|
|
|
|
|
recommendedNextVersionLevel: 'patch' | 'minor' | 'major'; // the recommended next version level of the project
|
|
|
|
|
recommendedNextVersion: string; // the recommended next version of the project
|
|
|
|
|
message: string; // the commit message. use conventional commits format
|
|
|
|
|
changelog?: string; // the changelog
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Commit {
|
|
|
|
|
private aiDocsRef: AiDoc;
|
|
|
|
|
private projectDir: string;
|
|
|
|
@ -11,9 +18,20 @@ export class Commit {
|
|
|
|
|
this.projectDir = projectDirArg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async build() {
|
|
|
|
|
public async buildNextCommitObject(): Promise<INextCommitObject> {
|
|
|
|
|
const smartgitInstance = new plugins.smartgit.Smartgit();
|
|
|
|
|
await smartgitInstance.init();
|
|
|
|
|
const gitRepo = await plugins.smartgit.GitRepo.fromOpeningRepoDir(smartgitInstance, this.projectDir);
|
|
|
|
|
const diffString = await gitRepo.getUncommittedDiff();
|
|
|
|
|
console.log(diffString);
|
|
|
|
|
const projectContext = new ProjectContext(this.projectDir);
|
|
|
|
|
const contextString = await projectContext.update();
|
|
|
|
|
let contextString = await projectContext.update();
|
|
|
|
|
contextString = `
|
|
|
|
|
${contextString}
|
|
|
|
|
|
|
|
|
|
Here is the diff:
|
|
|
|
|
${diffString}
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
let result = await this.aiDocsRef.openaiInstance.chat({
|
|
|
|
|
systemMessage: `
|
|
|
|
@ -41,15 +59,40 @@ Also you are given a diff
|
|
|
|
|
userMessage: contextString,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(result.message);
|
|
|
|
|
const resultObject = JSON.parse(result.message.replace('```json', '').replace('```', ''));
|
|
|
|
|
// console.log(result.message);
|
|
|
|
|
const resultObject: INextCommitObject = JSON.parse(result.message.replace('```json', '').replace('```', ''));
|
|
|
|
|
|
|
|
|
|
const npmextraJson = (await projectContext.gatherFiles()).smartfilesNpmextraJSON;
|
|
|
|
|
const npmextraJsonContent = JSON.parse(npmextraJson.contents.toString());
|
|
|
|
|
// lets build the changelog based on that
|
|
|
|
|
const commitMessages = await gitRepo.getAllCommitMessages();
|
|
|
|
|
const previousChangelogPath = plugins.path.join(this.projectDir, 'changelog.md');
|
|
|
|
|
let previousChangelog: plugins.smartfile.SmartFile;
|
|
|
|
|
if (await plugins.smartfile.fs.fileExists(previousChangelogPath)) {
|
|
|
|
|
previousChangelog = await plugins.smartfile.SmartFile.fromFilePath(previousChangelogPath);
|
|
|
|
|
}
|
|
|
|
|
let result2 = await this.aiDocsRef.openaiInstance.chat({
|
|
|
|
|
messageHistory: [],
|
|
|
|
|
systemMessage: `
|
|
|
|
|
You are building a changelog file for the projext.
|
|
|
|
|
Omit commits and versions that lack relevant changes.
|
|
|
|
|
|
|
|
|
|
npmextraJsonContent.gitzone.module.commit = resultObject.message;
|
|
|
|
|
You are given
|
|
|
|
|
* the previous changelog file (if available)
|
|
|
|
|
* the commit messages of the project
|
|
|
|
|
|
|
|
|
|
npmextraJson.contents = Buffer.from(JSON.stringify(npmextraJsonContent, null, 2));
|
|
|
|
|
await npmextraJson.write();
|
|
|
|
|
Only return the changelog file, so it can be written directly to changelog.md
|
|
|
|
|
|
|
|
|
|
For the latest version, that is not yet part of the commit messages, use {{nextVersion}} and {{nextDescription}} placeholders.
|
|
|
|
|
`,
|
|
|
|
|
userMessage: `
|
|
|
|
|
The previous changelog file is:
|
|
|
|
|
${(!previousChangelog) ? 'No previous changelog file found' : previousChangelog.contents.toString()}
|
|
|
|
|
|
|
|
|
|
Here are the commit messages so far:
|
|
|
|
|
|
|
|
|
|
${commitMessages.join('\n\n')}
|
|
|
|
|
`
|
|
|
|
|
})
|
|
|
|
|
resultObject.changelog = result2.message;
|
|
|
|
|
return resultObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|