diff --git a/src/core/packager.ts b/src/core/packager.ts index 12b7f6fa..8c798558 100644 --- a/src/core/packager.ts +++ b/src/core/packager.ts @@ -14,6 +14,7 @@ import { writeOutputToDisk } from "./packager/writeOutputToDisk.js"; import { type SuspiciousFileResult } from "./security/securityCheck.js"; import { validateFileSafety } from "./security/validateFileSafety.js"; import { TokenCounter } from "./tokenCount/tokenCount.js"; +import { copyToClipboardIfEnabled } from "./packager/copyToClipboardIfEnabled.js"; export interface PackResult { totalFiles: number; @@ -35,6 +36,7 @@ export const pack = async ( generateOutput, validateFileSafety, writeOutputToDisk, + copyToClipboardIfEnabled, } ): Promise => { // Get all file paths considering the config @@ -61,16 +63,10 @@ export const pack = async ( safeFilePaths ); - // Write output to file. path is relative to the cwd progressCallback("Writing output file..."); await deps.writeOutputToDisk(output, config); - if (config.output.copyToClipboard) { - // Additionally copy to clipboard if flag is raised - progressCallback("Copying to clipboard..."); - logger.trace("Copying output to clipboard"); - await clipboard.write(output); - } + await deps.copyToClipboardIfEnabled(output, progressCallback, config); // Setup token counter const tokenCounter = new TokenCounter(); diff --git a/src/core/packager/copyToClipboardIfEnabled.ts b/src/core/packager/copyToClipboardIfEnabled.ts new file mode 100644 index 00000000..5df26883 --- /dev/null +++ b/src/core/packager/copyToClipboardIfEnabled.ts @@ -0,0 +1,17 @@ +import clipboard from "clipboardy"; +import { RepomixConfigMerged } from "../../config/configSchema.js"; +import { logger } from "../../shared/logger.js"; +import { type RepomixProgressCallback } from "../../shared/types.js"; + +// Additionally copy to clipboard if flag is raised +export const copyToClipboardIfEnabled = async ( + output: string, + progressCallback: RepomixProgressCallback, + config: RepomixConfigMerged +): Promise => { + if (config.output.copyToClipboard) { + progressCallback("Copying to clipboard..."); + logger.trace("Copying output to clipboard"); + await clipboard.write(output); + } +}; diff --git a/src/core/packager/writeOutputToDisk.ts b/src/core/packager/writeOutputToDisk.ts index 9ddd3a5d..c788240c 100644 --- a/src/core/packager/writeOutputToDisk.ts +++ b/src/core/packager/writeOutputToDisk.ts @@ -3,6 +3,7 @@ import fs from "node:fs/promises"; import { RepomixConfigMerged } from "../../config/configSchema.js"; import { logger } from "../../shared/logger.js"; +// Write output to file. path is relative to the cwd export const writeOutputToDisk = async ( output: string, config: RepomixConfigMerged diff --git a/tests/core/packager.test.ts b/tests/core/packager.test.ts index 22bd00f9..f82fe7f4 100644 --- a/tests/core/packager.test.ts +++ b/tests/core/packager.test.ts @@ -1,16 +1,15 @@ -import * as fs from "node:fs/promises"; import path from "node:path"; -import clipboardy from "clipboardy"; import { beforeEach, describe, expect, test, vi } from "vitest"; import type { collectFiles } from "../../src/core/file/fileCollect.js"; import type { processFiles } from "../../src/core/file/fileProcess.js"; import type { searchFiles } from "../../src/core/file/fileSearch.js"; import type { generateOutput } from "../../src/core/output/outputGenerate.js"; import { pack } from "../../src/core/packager.js"; +import { copyToClipboardIfEnabled } from "../../src/core/packager/copyToClipboardIfEnabled.js"; +import { writeOutputToDisk } from "../../src/core/packager/writeOutputToDisk.js"; +import { validateFileSafety } from "../../src/core/security/validateFileSafety.js"; import { TokenCounter } from "../../src/core/tokenCount/tokenCount.js"; import { createMockConfig } from "../testing/testUtils.js"; -import { validateFileSafety } from "../../src/core/security/validateFileSafety.js"; -import { writeOutputToDisk } from "../../src/core/packager/writeOutputToDisk.js"; vi.mock("node:fs/promises"); vi.mock("fs/promises"); @@ -29,6 +28,7 @@ describe("packager", () => { validateFileSafety: typeof validateFileSafety; generateOutput: typeof generateOutput; writeOutputToDisk: typeof writeOutputToDisk; + copyToClipboardIfEnabled: typeof copyToClipboardIfEnabled; }; beforeEach(() => { @@ -57,6 +57,7 @@ describe("packager", () => { }), generateOutput: vi.fn().mockResolvedValue("mock output"), writeOutputToDisk: vi.fn().mockResolvedValue(undefined), + copyToClipboardIfEnabled: vi.fn().mockResolvedValue(undefined), }; vi.mocked(TokenCounter.prototype.countTokens).mockReturnValue(10); @@ -106,10 +107,6 @@ describe("packager", () => { ], mockConfig ); - expect(mockDeps.writeOutputToDisk).toHaveBeenCalledWith( - "mock output", - mockConfig - ); expect(mockDeps.generateOutput).toHaveBeenCalledWith( "root", mockConfig, @@ -125,6 +122,15 @@ describe("packager", () => { ], ["file1.txt", file2Path] ); + expect(mockDeps.writeOutputToDisk).toHaveBeenCalledWith( + "mock output", + mockConfig + ); + expect(mockDeps.copyToClipboardIfEnabled).toHaveBeenCalledWith( + "mock output", + progressCallback, + mockConfig + ); // Check the result of pack function expect(result.totalFiles).toBe(2); @@ -139,15 +145,4 @@ describe("packager", () => { [file2Path]: 10, }); }); - - test("pack should copy to clipboard when enabled", async () => { - const mockConfig = createMockConfig({ - output: { - copyToClipboard: true, - }, - }); - - await pack("root", mockConfig, () => {}, mockDeps); - expect(clipboardy.write).toHaveBeenCalled(); - }); }); diff --git a/tests/core/packager/copyToClipboardIfEnabled.test.ts b/tests/core/packager/copyToClipboardIfEnabled.test.ts new file mode 100644 index 00000000..de9ff2b1 --- /dev/null +++ b/tests/core/packager/copyToClipboardIfEnabled.test.ts @@ -0,0 +1,41 @@ +import clipboard from "clipboardy"; +import { logger } from "handlebars"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { type RepomixConfigMerged } from "../../../src/config/configSchema.js"; +import { copyToClipboardIfEnabled } from "../../../src/core/packager/copyToClipboardIfEnabled.js"; +import { type RepomixProgressCallback } from "../../../src/shared/types.js"; + +vi.mock("clipboardy"); +vi.mock("../../shared/logger"); + +describe("copyToClipboardIfEnabled", () => { + beforeEach(() => { + vi.resetAllMocks(); + }); + + it("should copy output to clipboard if flag enabled in config", async () => { + const output = "test output"; + const config: RepomixConfigMerged = { + output: { copyToClipboard: true }, + } as RepomixConfigMerged; + const progressCallback: RepomixProgressCallback = vi.fn(); + + await copyToClipboardIfEnabled(output, progressCallback, config); + + expect(progressCallback).toHaveBeenCalledWith("Copying to clipboard..."); + expect(clipboard.write).toHaveBeenCalledWith(output); + }); + + it("should not copy output to clipboard if flag disabled in config", async () => { + const output = "test output"; + const config: RepomixConfigMerged = { + output: { copyToClipboard: false }, + } as RepomixConfigMerged; + const progressCallback: RepomixProgressCallback = vi.fn(); + + await copyToClipboardIfEnabled(output, progressCallback, config); + + expect(progressCallback).not.toHaveBeenCalled(); + expect(clipboard.write).not.toHaveBeenCalled(); + }); +});