From 89a53d76452aafdf9114c6c6311bd6e13afbdd9c Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sun, 1 Feb 2026 21:08:38 -0500 Subject: [PATCH] Use structured logging instead of console in archive service --- backend/src/services/archive.ts | 40 ++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/backend/src/services/archive.ts b/backend/src/services/archive.ts index 301ff94c..86573c39 100644 --- a/backend/src/services/archive.ts +++ b/backend/src/services/archive.ts @@ -4,6 +4,11 @@ import { readdir, stat, unlink } from 'fs/promises' import path from 'path' import os from 'os' import { logger } from '../utils/logger' +import { getReposPath } from '@opencode-manager/shared/config/env' + +function resolvePath(userPath: string): string { + return path.isAbsolute(userPath) ? userPath : path.join(getReposPath(), userPath) +} export interface ArchiveOptions { includeGit?: boolean @@ -57,14 +62,14 @@ async function getIgnoredPaths(gitRoot: string, targetPath: string, paths: strin const { spawn } = await import('child_process') const targetRelativeToRoot = path.relative(gitRoot, targetPath) - console.log('[getIgnoredPaths] gitRoot:', gitRoot, 'targetPath:', targetPath, 'targetRelativeToRoot:', targetRelativeToRoot) + logger.debug('[getIgnoredPaths] gitRoot:', gitRoot, 'targetPath:', targetPath, 'targetRelativeToRoot:', targetRelativeToRoot) const relativePaths = paths.map(p => { const relativeToTarget = p return targetRelativeToRoot ? path.join(targetRelativeToRoot, relativeToTarget) : relativeToTarget }) - console.log('[getIgnoredPaths] First 5 relativePaths:', relativePaths.slice(0, 5)) + logger.debug('[getIgnoredPaths] First 5 relativePaths:', relativePaths.slice(0, 5)) return new Promise((resolve) => { const ignored = new Set() @@ -83,9 +88,9 @@ async function getIgnoredPaths(gitRoot: string, targetPath: string, paths: strin proc.stdin?.end() proc.on('close', (code) => { - console.log('[getIgnoredPaths] git check-ignore exited with code:', code) + logger.debug('[getIgnoredPaths] git check-ignore exited with code:', code) const ignoredFullPaths = stdout.split('\n').filter(p => p.trim()) - console.log('[getIgnoredPaths] Raw ignored count:', ignoredFullPaths.length, 'first 5:', ignoredFullPaths.slice(0, 5)) + logger.debug('[getIgnoredPaths] Raw ignored count:', ignoredFullPaths.length, 'first 5:', ignoredFullPaths.slice(0, 5)) const targetRelativeToRoot = path.relative(gitRoot, targetPath) for (const fullPath of ignoredFullPaths) { @@ -99,7 +104,7 @@ async function getIgnoredPaths(gitRoot: string, targetPath: string, paths: strin ignored.add(relativePath) } } - console.log('[getIgnoredPaths] Final ignored set size:', ignored.size) + logger.debug('[getIgnoredPaths] Final ignored set size:', ignored.size) resolve(ignored) }) @@ -200,6 +205,7 @@ async function filterIgnoredPaths(targetPath: string, allPaths: string[], option } export async function createRepoArchive(repoPath: string, options?: ArchiveOptions): Promise { + repoPath = resolvePath(repoPath) const repoName = path.basename(repoPath) const tempFile = path.join(os.tmpdir(), `${repoName}-${Date.now()}.zip`) @@ -237,6 +243,7 @@ export async function createRepoArchive(repoPath: string, options?: ArchiveOptio } export async function createDirectoryArchive(directoryPath: string, archiveName?: string, options?: ArchiveOptions): Promise { + directoryPath = resolvePath(directoryPath) const dirName = archiveName || path.basename(directoryPath) const tempFile = path.join(os.tmpdir(), `${dirName}-${Date.now()}.zip`) @@ -293,34 +300,35 @@ export async function getArchiveSize(filePath: string): Promise { } export async function getIgnoredPathsList(directoryPath: string): Promise { - console.log('[getIgnoredPathsList] Starting for:', directoryPath) + directoryPath = resolvePath(directoryPath) + logger.debug('[getIgnoredPathsList] Starting for:', directoryPath) const gitRoot = await findGitRoot(directoryPath) - console.log('[getIgnoredPathsList] Git root:', gitRoot) + logger.debug('[getIgnoredPathsList] Git root:', gitRoot) if (!gitRoot) { - console.log('[getIgnoredPathsList] No git root found') + logger.debug('[getIgnoredPathsList] No git root found') const hasGitDir = await collectFiles(directoryPath).then( paths => paths.some(p => p.startsWith('.git/') || p === '.git') ).catch(() => false) if (hasGitDir) { - console.log('[getIgnoredPathsList] Has .git dir, returning [.git/]') + logger.debug('[getIgnoredPathsList] Has .git dir, returning [.git/]') return ['.git/'] } - console.log('[getIgnoredPathsList] No .git dir, returning []') + logger.debug('[getIgnoredPathsList] No .git dir, returning []') return [] } const allPaths = await collectFiles(directoryPath) - console.log('[getIgnoredPathsList] Collected', allPaths.length, 'paths') + logger.debug('[getIgnoredPathsList] Collected', allPaths.length, 'paths') const ignoredSet = new Set() const batchSize = 1000 for (let i = 0; i < allPaths.length; i += batchSize) { const batch = allPaths.slice(i, i + batchSize) - console.log('[getIgnoredPathsList] Checking batch', i, 'to', i + batch.length) + logger.debug('[getIgnoredPathsList] Checking batch', i, 'to', i + batch.length) const ignored = await getIgnoredPaths(gitRoot, directoryPath, batch) - console.log('[getIgnoredPathsList] Batch ignored count:', ignored.size) + logger.debug('[getIgnoredPathsList] Batch ignored count:', ignored.size) for (const p of ignored) { ignoredSet.add(p) if (p.endsWith('/')) { @@ -331,7 +339,7 @@ export async function getIgnoredPathsList(directoryPath: string): Promise() @@ -358,14 +366,14 @@ export async function getIgnoredPathsList(directoryPath: string): Promise true).catch(() => false) - console.log('[getIgnoredPathsList] .git exists:', gitDirExists) + logger.debug('[getIgnoredPathsList] .git exists:', gitDirExists) if (gitDirExists && !ignoredDirs.some(p => p.startsWith('.git'))) { ignoredDirs.push('.git/') } ignoredDirs.sort() - console.log('[getIgnoredPathsList] Final result:', ignoredDirs) + logger.debug('[getIgnoredPathsList] Final result:', ignoredDirs) return ignoredDirs }