/** * @module core-generate-comment-cmd * * Isomorphic execution engine for `vcs generate-comment` (synonyms: `gen-comment`, `gen-msg`, `gcomm`, `gmsg`). * Scans repository status or target paths and projects `.ib..md` or `.ib..md` * companion metadata files into the workspace projection space using virtual items. */ import { extractErrorMsg } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { dirnamePath, basenamePath, extnamePath, toPosixPath, joinPaths } from '@ibgib/helper-gib/dist/helpers/path-helper.mjs'; import { GLOBAL_LOG_A_LOT } from '../../../core-constants.mjs'; import { createVirtualItemIbGib } from '../item/item-helpers.mjs'; import { executeVcsStatus } from './core-status-cmd.mjs'; import { VcsGenerateCommentOpts, VcsGenerateCommentResult } from './command-types.mjs'; const lcFile = `[generate-comment-cmd]`; const logalot = GLOBAL_LOG_A_LOT; /** * Generates companion comment markdown files in the workspace projection space * for specified paths or auto-detected modified/staged/untracked paths. */ export async function executeVcsGenerateComment({ metaspace, localSpace, workspaceSpace, activeBranchSpace, galaxyIbGib, paths = [], folderTopic = 'notes', b2tfsGalaxyRoot, targetDir, vcsFolderName, }: VcsGenerateCommentOpts): Promise { const lc = `${lcFile}[${executeVcsGenerateComment.name}]`; try { if (logalot) { console.log(`${lc} starting... (I: 2e8b09f7a6344adca26e846174d81226)`); } if (!workspaceSpace) { throw new Error(`workspaceSpace is required to generate companion comments (E: e3f0980a31244e439bb4c1a851d7c326)`); } interface TargetItem { relPath: string; fsType?: 'file' | 'folder'; } const targetItems: TargetItem[] = []; if (paths.length > 0) { for (const p of paths) { if (!p || typeof p !== 'string' || !p.trim()) { continue; } const hasTrailingSlash = /[/\\]+$/.test(p); const normalized = toPosixPath(p).replace(/^\.?\//, '').replace(/\/+$/, ''); if (!normalized) { // Target is root folder targetItems.push({ relPath: '', fsType: 'folder' }); continue; } let fsType: 'file' | 'folder' | undefined; if (hasTrailingSlash) { fsType = 'folder'; } if (!targetItems.some(t => t.relPath === normalized)) { targetItems.push({ relPath: normalized, fsType }); } } } else { // Auto-detect from staged, modified, and untracked status items const statusRes = await executeVcsStatus({ metaspace, localSpace, activeBranchSpace, workspaceSpace, galaxyIbGib, b2tfsGalaxyRoot, targetDir, vcsFolderName, }); const addStatusItem = (item: { path: string; fsType: 'file' | 'folder' }) => { const normalized = toPosixPath(item.path).replace(/^\.?\//, '').replace(/\/+$/, ''); if (!targetItems.some(t => t.relPath === normalized)) { targetItems.push({ relPath: normalized, fsType: item.fsType }); } }; for (const s of statusRes.staged) { addStatusItem(s); } for (const m of statusRes.modified) { addStatusItem(m); } for (const u of statusRes.untracked) { addStatusItem(u); } } const createdFiles: string[] = []; const skippedFiles: string[] = []; for (const target of targetItems) { const relP = target.relPath; let isDirectory: boolean; if (target.fsType === 'folder') { isDirectory = true; } else if (target.fsType === 'file') { isDirectory = false; } else { // Query workspaceSpace to determine if it is an existing file const checkItemRes = await workspaceSpace.witness({ data: { cmd: 'get', relPath: relP, }, }); const hasFileBytes = (checkItemRes?.ibGibs && checkItemRes.ibGibs.length > 0); if (hasFileBytes) { isDirectory = false; } else { // Fallback for not-yet-created items: check extension const ext = extnamePath(relP); isDirectory = !ext; } } let commentFileName: string; let commentDirPath: string; if (isDirectory) { commentDirPath = relP; commentFileName = `.ib.${folderTopic}.md`; } else { commentDirPath = dirnamePath(relP); const baseName = basenamePath(relP); commentFileName = `.ib.${baseName}.md`; } const commentRelPath = (commentDirPath && commentDirPath !== '.') ? toPosixPath(joinPaths(commentDirPath, commentFileName)) : commentFileName; // Check if companion comment file already exists in workspace const checkRes = await workspaceSpace.witness({ data: { cmd: 'get', relPath: commentRelPath, }, }); const alreadyExists = (checkRes?.ibGibs && checkRes.ibGibs.length > 0); if (alreadyExists) { skippedFiles.push(commentRelPath); } else { const headerTarget = relP ? `\`${relP}\`` : 'root'; const boilerplate = `# ${headerTarget}\n\n(Enter contemporaneous notes/comments here)\n`; const virtualItem = await createVirtualItemIbGib({ name: commentRelPath, content: boilerplate, }); await workspaceSpace.witness({ data: { cmd: 'put', }, ibGibs: [virtualItem], }); createdFiles.push(commentRelPath); } } return { createdFiles, skippedFiles, }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: d23e082728cb489e830e9d6d5a1b3c4f)`); throw error; } finally { if (logalot) { console.log(`${lc} complete. (I: a3b71946358e469d80fc4ef2b281c726)`); } } }