mirror of
https://github.com/yamadashy/repomix.git
synced 2026-05-30 11:18:53 +02:00
Extract another function from the packager
This commit is contained in:
@@ -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<PackResult> => {
|
||||
// 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();
|
||||
|
||||
@@ -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<undefined> => {
|
||||
if (config.output.copyToClipboard) {
|
||||
progressCallback("Copying to clipboard...");
|
||||
logger.trace("Copying output to clipboard");
|
||||
await clipboard.write(output);
|
||||
}
|
||||
};
|
||||
@@ -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
|
||||
|
||||
+14
-19
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user