Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/cli/actions/defaultAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { generateDefaultSkillName } from '../../core/skill/skillUtils.js';
import { RepomixError, rethrowValidationErrorIfZodError } from '../../shared/errorHandle.js';
import { logger } from '../../shared/logger.js';
import { splitPatterns } from '../../shared/patternUtils.js';
import type { RepomixProgressCallback } from '../../shared/types.js';
import { reportResults } from '../cliReport.js';
import { Spinner } from '../cliSpinner.js';
import { promptSkillLocation, resolveAndPrepareSkillDir } from '../prompts/skillPrompts.js';
Expand All @@ -28,6 +29,7 @@ export const runDefaultAction = async (
directories: string[],
cwd: string,
cliOptions: CliOptions,
progressCallback?: RepomixProgressCallback,
): Promise<DefaultActionRunnerResult> => {
logger.trace('Loaded CLI options:', cliOptions);

Expand Down Expand Up @@ -113,16 +115,20 @@ export const runDefaultAction = async (

const targetPaths = stdinFilePaths ? [cwd] : directories.map((directory) => path.resolve(cwd, directory));

packResult = await pack(
targetPaths,
config,
(message) => {
spinner.update(message);
},
{},
stdinFilePaths,
packOptions,
);
const handleProgress: RepomixProgressCallback = (message) => {
spinner.update(message);
if (progressCallback) {
try {
Promise.resolve(progressCallback(message)).catch((error) => {
logger.trace('progressCallback error:', error);
});
} catch (error) {
logger.trace('progressCallback error:', error);
}
}
};
Comment thread
yamadashy marked this conversation as resolved.

packResult = await pack(targetPaths, config, handleProgress, {}, stdinFilePaths, packOptions);

spinner.succeed('Packing completed successfully!');
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type RepomixProgressCallback = (message: string) => void;
export type RepomixProgressCallback = (message: string) => void | Promise<void>;
90 changes: 90 additions & 0 deletions tests/cli/actions/defaultAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as configLoader from '../../../src/config/configLoad.js';
import * as fileStdin from '../../../src/core/file/fileStdin.js';
import * as packageJsonParser from '../../../src/core/file/packageJsonParse.js';
import * as packager from '../../../src/core/packager.js';
import * as loggerModule from '../../../src/shared/logger.js';
import { createMockConfig } from '../../testing/testUtils.js';

vi.mock('../../../src/core/packager');
Expand Down Expand Up @@ -156,6 +157,95 @@ describe('defaultAction', () => {
expect(mockSpinner.fail).toHaveBeenCalledWith('Error during packing');
});

describe('progressCallback', () => {
it('should forward progress messages to the provided callback', async () => {
// Configure pack mock to invoke its 3rd argument (progressCallback)
vi.mocked(packager.pack).mockImplementation(async (_paths, _config, progressCallback = () => {}) => {
progressCallback('Searching for files...');
progressCallback('Processing files...');
return {
totalFiles: 10,
totalCharacters: 1000,
totalTokens: 200,
fileCharCounts: {},
fileTokenCounts: {},
suspiciousFilesResults: [],
suspiciousGitDiffResults: [],
suspiciousGitLogResults: [],
processedFiles: [],
safeFilePaths: [],
gitDiffTokenCount: 0,
gitLogTokenCount: 0,
skippedFiles: [],
};
});

const callback = vi.fn();
await runDefaultAction(['.'], process.cwd(), {}, callback);

expect(callback).toHaveBeenCalledWith('Searching for files...');
expect(callback).toHaveBeenCalledWith('Processing files...');
});

it('should isolate async callback errors without affecting pack flow', async () => {
vi.mocked(packager.pack).mockImplementation(async (_paths, _config, progressCallback = () => {}) => {
progressCallback('test message');
// Allow microtask to process the rejected promise
await new Promise((resolve) => setTimeout(resolve, 10));
return {
totalFiles: 10,
totalCharacters: 1000,
totalTokens: 200,
fileCharCounts: {},
fileTokenCounts: {},
suspiciousFilesResults: [],
suspiciousGitDiffResults: [],
suspiciousGitLogResults: [],
processedFiles: [],
safeFilePaths: [],
gitDiffTokenCount: 0,
gitLogTokenCount: 0,
skippedFiles: [],
};
});

const rejectingCallback = vi.fn().mockRejectedValue(new Error('callback error'));
const result = await runDefaultAction(['.'], process.cwd(), {}, rejectingCallback);

expect(result.packResult.totalFiles).toBe(10);
expect(loggerModule.logger.trace).toHaveBeenCalledWith('progressCallback error:', expect.any(Error));
});

it('should still update spinner even when callback throws synchronously', async () => {
vi.mocked(packager.pack).mockImplementation(async (_paths, _config, progressCallback = () => {}) => {
progressCallback('test message');
return {
totalFiles: 10,
totalCharacters: 1000,
totalTokens: 200,
fileCharCounts: {},
fileTokenCounts: {},
suspiciousFilesResults: [],
suspiciousGitDiffResults: [],
suspiciousGitLogResults: [],
processedFiles: [],
safeFilePaths: [],
gitDiffTokenCount: 0,
gitLogTokenCount: 0,
skippedFiles: [],
};
});

const throwingCallback = vi.fn().mockImplementation(() => {
throw new Error('sync error');
});
await runDefaultAction(['.'], process.cwd(), {}, throwingCallback);

// Spinner should still be updated despite callback failure
expect(mockSpinner.update).toHaveBeenCalledWith('test message');
});
});

describe('buildCliConfig', () => {
it('should handle custom include patterns', () => {
const options = {
Expand Down
Loading