From a5782a55b76a85d1c5df8394a51d8e82bb89f2e5 Mon Sep 17 00:00:00 2001 From: Mayank Date: Wed, 27 Aug 2025 22:57:25 +0530 Subject: [PATCH 1/3] add canonicals --- scripts/publish-canonical.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/publish-canonical.js b/scripts/publish-canonical.js index b5cad66d4..a8c8668fb 100644 --- a/scripts/publish-canonical.js +++ b/scripts/publish-canonical.js @@ -1,7 +1,7 @@ const { execSync } = require("child_process"); // Publish canonical packages -[].forEach(pkg => { +["json-merge-resolver", "json-conflict-resolver"].forEach(pkg => { execSync(`sed -i -e "s/name.*/name\\": \\"${pkg.replace(/\//g, "\\\\/")}\\",/" lib/package.json`); execSync("cd lib && npm publish --provenance --access public"); }); From cf7951d708fedb1e55b7b20a3acefd86427ae220 Mon Sep 17 00:00:00 2001 From: Mayank Date: Wed, 27 Aug 2025 23:16:46 +0530 Subject: [PATCH 2/3] RELEASING: Releasing 2 package(s) Releases: git-json-resolver@0.0.1 @repo/shared@0.0.1 --- lib/CHANGELOG.md | 7 +++++++ lib/package.json | 2 +- lib/src/index.ts | 8 +++++--- packages/shared/CHANGELOG.md | 8 ++++++++ packages/shared/package.json | 2 +- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 lib/CHANGELOG.md create mode 100644 packages/shared/CHANGELOG.md diff --git a/lib/CHANGELOG.md b/lib/CHANGELOG.md new file mode 100644 index 000000000..3abe30faa --- /dev/null +++ b/lib/CHANGELOG.md @@ -0,0 +1,7 @@ +# git-json-resolver + +## 0.0.1 + +### Patch Changes + +- Wire generic types through resolveConflicts function for improved type safety with custom strategies diff --git a/lib/package.json b/lib/package.json index bcc6e3d5d..9a47f7718 100644 --- a/lib/package.json +++ b/lib/package.json @@ -2,7 +2,7 @@ "name": "git-json-resolver", "author": "Mayank Kumar Chaudhari (https://mayank-chaudhari.vercel.app)", "private": false, - "version": "0.0.0", + "version": "0.0.1", "description": "A rules-based JSON conflict resolver that parses Git conflict markers, reconstructs ours/theirs, and merges with deterministic strategies — beyond line-based merges.", "license": "MPL-2.0", "main": "./dist/index.js", diff --git a/lib/src/index.ts b/lib/src/index.ts index 45150f3c7..01c637878 100644 --- a/lib/src/index.ts +++ b/lib/src/index.ts @@ -3,7 +3,7 @@ import { parseConflictContent } from "./file-parser"; import { serialize } from "./file-serializer"; import { Conflict, mergeObject } from "./merger"; import { normalizeConfig, NormalizedConfig } from "./normalizer"; -import { Config } from "./types"; +import { Config, InbuiltMergeStrategies } from "./types"; import { backupFile, listMatchingFiles } from "./utils"; import fs from "node:fs/promises"; import { reconstructConflict } from "./conflict-helper"; @@ -12,8 +12,10 @@ export * from "./types"; const _strategyCache = new Map(); -export const resolveConflicts = async (config: Config) => { - const normalizedConfig: NormalizedConfig = await normalizeConfig(config); +export const resolveConflicts = async ( + config: Config, +) => { + const normalizedConfig: NormalizedConfig = await normalizeConfig(config); const filesEntries = await listMatchingFiles(normalizedConfig); await Promise.all( filesEntries.map(async ({ filePath, content }) => { diff --git a/packages/shared/CHANGELOG.md b/packages/shared/CHANGELOG.md new file mode 100644 index 000000000..31e950728 --- /dev/null +++ b/packages/shared/CHANGELOG.md @@ -0,0 +1,8 @@ +# @repo/shared + +## 0.0.1 + +### Patch Changes + +- Updated dependencies + - git-json-resolver@0.0.1 diff --git a/packages/shared/package.json b/packages/shared/package.json index 199b2cd34..4435d02b4 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -1,6 +1,6 @@ { "name": "@repo/shared", - "version": "0.0.0", + "version": "0.0.1", "private": true, "sideEffects": false, "main": "./dist/index.js", From 60fb92ffac41794a5b557430652c67fc96d2b288 Mon Sep 17 00:00:00 2001 From: Mayank Date: Wed, 27 Aug 2025 23:44:56 +0530 Subject: [PATCH 3/3] Add cli unit tests --- lib/src/cli.test.ts | 133 ++++++++++++++++++++++++++++++++++++++++++++ lib/src/cli.ts | 8 +-- 2 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 lib/src/cli.test.ts diff --git a/lib/src/cli.test.ts b/lib/src/cli.test.ts new file mode 100644 index 000000000..f683d4dc0 --- /dev/null +++ b/lib/src/cli.test.ts @@ -0,0 +1,133 @@ +// cli.test.ts +import { describe, it, expect, vi, beforeEach } from "vitest"; +import fs from "node:fs"; +import path from "node:path"; +import * as child_process from "node:child_process"; + +vi.mock("node:fs"); +vi.mock("node:child_process"); +vi.mock("./index", () => ({ + resolveConflicts: vi.fn(), +})); +vi.mock("./normalizer", () => ({ + DEFAULT_CONFIG: { defaultStrategy: "merge" }, +})); + +// Import after mocks +import type { Config } from "./types"; + +// Re-import CLI helpers (not the top-level IIFE) +import * as cli from "./cli"; + +describe("cli helpers", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + describe("findGitRoot", () => { + it("returns git root from execSync", () => { + vi.spyOn(child_process, "execSync").mockReturnValue("/git/root\n" as any); + const root = (cli as any).findGitRoot(); + expect(root).toBe("/git/root"); + }); + + it("falls back to process.cwd() on error", () => { + vi.spyOn(child_process, "execSync").mockImplementation(() => { + throw new Error("no git"); + }); + const root = (cli as any).findGitRoot(); + expect(root).toBe(process.cwd()); + }); + }); + + describe("parseArgs", () => { + it("parses include/exclude/matcher/debug/strict-arrays/sidecar", () => { + const argv = [ + "node", + "cli", + "--include", + "a.json,b.json", + "--exclude", + "c.json", + "--matcher", + "micromatch", + "--debug", + "--strict-arrays", + "--sidecar", + ]; + const result = (cli as any).parseArgs(argv); + expect(result.overrides).toEqual({ + include: ["a.json", "b.json"], + exclude: ["c.json"], + matcher: "micromatch", + debug: true, + strictArrays: true, + writeConflictSidecar: true, + }); + expect(result.init).toBe(false); + }); + + it("sets init flag", () => { + const result = (cli as any).parseArgs(["node", "cli", "--init"]); + expect(result.init).toBe(true); + }); + + it("warns on unknown option", () => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + (cli as any).parseArgs(["node", "cli", "--unknown"]); + expect(warn).toHaveBeenCalledWith("[git-json-resolver] Unknown option: --unknown"); + }); + }); + + describe("initConfig", () => { + const tmpDir = "/tmp/test-cli"; + const configPath = path.join(tmpDir, "git-json-resolver.config.js"); + + it("writes starter config if none exists", () => { + (fs.existsSync as any).mockReturnValue(false); + const writeFileSync = vi.spyOn(fs, "writeFileSync").mockImplementation(() => {}); + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + + (cli as any).initConfig(tmpDir); + + expect(writeFileSync).toHaveBeenCalled(); + expect(log).toHaveBeenCalledWith( + `[git-json-resolver] Created starter config at ${configPath}`, + ); + }); + + it("exits if config already exists", () => { + (fs.existsSync as any).mockReturnValue(true); + const exit = vi.spyOn(process, "exit").mockImplementation(() => { + throw new Error("exit"); + }); + const error = vi.spyOn(console, "error").mockImplementation(() => {}); + + expect(() => (cli as any).initConfig(tmpDir)).toThrow("exit"); + expect(error).toHaveBeenCalledWith( + `[git-json-resolver] Config file already exists: ${configPath}`, + ); + expect(exit).toHaveBeenCalledWith(1); + }); + }); + + describe("loadConfigFile", () => { + it("returns {} if no config found", async () => { + (fs.existsSync as any).mockReturnValue(false); + const result = await (cli as any).loadConfigFile(); + expect(result).toEqual({}); + }); + + it.skip("loads config from js file", async () => { + const fakeConfig: Partial = { debug: true }; + (fs.existsSync as any).mockReturnValue(true); + vi.doMock("/git/root/git-json-resolver.config.js", () => ({ + default: fakeConfig, + })); + vi.spyOn(child_process, "execSync").mockReturnValue("/git/root\n" as any); + + const mod = await (cli as any).loadConfigFile(); + expect(mod).toEqual(fakeConfig); + }); + }); +}); diff --git a/lib/src/cli.ts b/lib/src/cli.ts index a1c00da30..73e703bfa 100644 --- a/lib/src/cli.ts +++ b/lib/src/cli.ts @@ -12,7 +12,7 @@ const CONFIG_FILENAME = "git-json-resolver.config.js"; /** * Find Git root directory */ -const findGitRoot = (): string => { +export const findGitRoot = (): string => { try { return execSync("git rev-parse --show-toplevel", { encoding: "utf8" }).trim(); } catch { @@ -23,7 +23,7 @@ const findGitRoot = (): string => { /** * Load configuration file (js/ts) from current dir or Git root. */ -const loadConfigFile = async (): Promise> => { +export const loadConfigFile = async (): Promise> => { const searchDirs = [process.cwd(), findGitRoot()]; const configNames = [CONFIG_FILENAME, "git-json-resolver.config.ts"]; @@ -42,7 +42,7 @@ const loadConfigFile = async (): Promise> => { /** * Write a starter config file */ -const initConfig = (targetDir: string) => { +export const initConfig = (targetDir: string) => { const targetPath = path.join(targetDir, CONFIG_FILENAME); if (fs.existsSync(targetPath)) { console.error(`[git-json-resolver] Config file already exists: ${targetPath}`); @@ -63,7 +63,7 @@ module.exports = ${JSON.stringify(DEFAULT_CONFIG, null, 2)}; /** * CLI argument parser (minimal, no external deps). */ -const parseArgs = (argv: string[]): { overrides: Partial; init?: boolean } => { +export const parseArgs = (argv: string[]): { overrides: Partial; init?: boolean } => { const overrides: Partial = {}; let init = false;