UNPKG

@git.zone/tsdoc

Version:

A comprehensive TypeScript documentation tool that leverages AI to generate and enhance project documentation, including dynamic README creation, API docs via TypeDoc, and smart commit message generation.

119 lines (104 loc) 3.51 kB
import * as plugins from '../plugins.js'; export class ProjectContext { public static async fromDir(dirArg: string) {} // INSTANCE public projectDir: string; private tokenCount: number = 0; private contextString: string = ''; constructor(projectDirArg: string) { this.projectDir = projectDirArg; } public async gatherFiles() { const smartfilePackageJSON = await plugins.smartfileFactory.fromFilePath( plugins.path.join(this.projectDir, 'package.json'), this.projectDir, ); const smartfilesReadme = await plugins.smartfileFactory.fromFilePath( plugins.path.join(this.projectDir, 'readme.md'), this.projectDir, ); const smartfilesReadmeHints = await plugins.smartfileFactory.fromFilePath( plugins.path.join(this.projectDir, 'readme.hints.md'), this.projectDir, ); const smartfilesNpmextraJSON = await plugins.smartfileFactory.fromFilePath( plugins.path.join(this.projectDir, 'npmextra.json'), this.projectDir, ); const smartfilesMod = await plugins.smartfileFactory.virtualDirectoryFromPath( this.projectDir, ).then(vd => vd.filter(f => f.relative.startsWith('ts') && f.relative.endsWith('.ts')).listFiles()); const smartfilesTest = await plugins.smartfileFactory.virtualDirectoryFromPath( this.projectDir, ).then(vd => vd.filter(f => f.relative.startsWith('test/') && f.relative.endsWith('.ts')).listFiles()); return { smartfilePackageJSON, smartfilesReadme, smartfilesReadmeHints, smartfilesNpmextraJSON, smartfilesMod, smartfilesTest, }; } public async convertFilesToContext(filesArg: plugins.smartfile.SmartFile[]) { filesArg.map((fileArg) => { // console.log(` -> ${fileArg.relative}`); }); return filesArg .map((smartfile) => { return ` ====== START OF FILE ${smartfile.relative} ====== ${smartfile.contents.toString()} ====== END OF FILE ${smartfile.relative} ====== `; }) .join('\n'); } /** * Estimate token count for a string * Uses a rough estimate of 4 characters per token * @param text The text to estimate tokens for * @returns Estimated number of tokens */ public countTokens(text: string): number { // Rough estimate: ~4 characters per token for English text return Math.ceil(text.length / 4); } private async buildContext(dirArg: string) { const files = await this.gatherFiles(); let context = await this.convertFilesToContext([ files.smartfilePackageJSON, files.smartfilesReadme, files.smartfilesReadmeHints, files.smartfilesNpmextraJSON, ...files.smartfilesMod, ...files.smartfilesTest, ]); // Count tokens in the context this.contextString = context; this.tokenCount = this.countTokens(context); // console.log(context); return context; } /** * Get the token count for the current context * @returns The number of tokens in the context */ public getTokenCount(): number { return this.tokenCount; } /** * Get both the context string and its token count * @returns An object containing the context string and token count */ public getContextWithTokenCount(): { context: string; tokenCount: number } { return { context: this.contextString, tokenCount: this.tokenCount }; } public async update() { const result = await this.buildContext(this.projectDir); return result; } }