From c7ef90a0b8fade3b41332d3ed3b13494e5cd4d35 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 21:37:18 -0500 Subject: [PATCH 01/10] Update package-lock.json --- package-lock.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 28fbd994..edd84d3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6333,7 +6333,6 @@ }, "devDependencies": { "@types/jest": "^29.5.10", - "@types/node": "^20.10.2", "@typescript-eslint/eslint-plugin": "^6.13.1", "@typescript-eslint/parser": "^6.13.1", "eslint": "^8.55.0", From f557780d789fa40492949bd69bae62b4f0eaa476 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 21:38:03 -0500 Subject: [PATCH 02/10] Fix release workflow --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e58bb1a4..3010b19e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,5 +17,6 @@ jobs: registry-url: 'https://registry.npmjs.org' - run: npm ci - run: npm publish + working-directory: ./packages/jsondiffpatch env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} From 6b6c7561e8c7f5e694ed02ea287d96dd3f4930eb Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 21:38:58 -0500 Subject: [PATCH 03/10] Use @types/diff-match-patch --- package-lock.json | 6 + packages/jsondiffpatch/package.json | 1 + .../jsondiffpatch/src/diff-match-patch.d.cts | 128 ------------------ packages/jsondiffpatch/test/index.spec.ts | 1 - 4 files changed, 7 insertions(+), 129 deletions(-) delete mode 100644 packages/jsondiffpatch/src/diff-match-patch.d.cts diff --git a/package-lock.json b/package-lock.json index edd84d3a..ee8f7df7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1904,6 +1904,11 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/diff-match-patch": { + "version": "1.0.36", + "resolved": "https://registry.npmjs.org/@types/diff-match-patch/-/diff-match-patch-1.0.36.tgz", + "integrity": "sha512-xFdR6tkm0MWvBfO8xXCSsinYxHcqkQUlcHeSpMC2ukzOb6lwQAfDmW+Qt0AvlGd8HpsS28qKsB+oPeJn9I39jg==" + }, "node_modules/@types/graceful-fs": { "version": "4.1.9", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.9.tgz", @@ -6325,6 +6330,7 @@ "version": "0.5.0", "license": "MIT", "dependencies": { + "@types/diff-match-patch": "^1.0.36", "chalk": "^5.3.0", "diff-match-patch": "^1.0.5" }, diff --git a/packages/jsondiffpatch/package.json b/packages/jsondiffpatch/package.json index 83b1eea1..eda949d9 100644 --- a/packages/jsondiffpatch/package.json +++ b/packages/jsondiffpatch/package.json @@ -39,6 +39,7 @@ "patch" ], "dependencies": { + "@types/diff-match-patch": "^1.0.36", "chalk": "^5.3.0", "diff-match-patch": "^1.0.5" }, diff --git a/packages/jsondiffpatch/src/diff-match-patch.d.cts b/packages/jsondiffpatch/src/diff-match-patch.d.cts deleted file mode 100644 index c635fc76..00000000 --- a/packages/jsondiffpatch/src/diff-match-patch.d.cts +++ /dev/null @@ -1,128 +0,0 @@ -declare module 'diff-match-patch' { - namespace diff_match_patch { - type Diff = [number, string]; - - interface patch_obj { - diffs: Diff[]; - start1: number | null; - start2: number | null; - length1: number; - length2: number; - } - } - - class diff_match_patch { - Diff_Timeout: number; - Diff_EditCost: number; - Match_Threshold: number; - Match_Distance: number; - Patch_DeleteThreshold: number; - Patch_Margin: number; - Match_MaxBits: number; - - diff_main( - text1: string, - text2: string, - opt_checklines?: boolean, - opt_deadline?: number, - ): diff_match_patch.Diff[]; - - diff_bisect_( - text1: string, - text2: string, - deadline: number, - ): diff_match_patch.Diff[]; - - diff_linesToChars_( - text1: string, - text2: string, - ): { chars1: string; chars2: string; lineArray: string[] }; - - diff_charsToLines_( - diffs: diff_match_patch.Diff[], - lineArray: string[], - ): void; - - diff_commonPrefix(text1: string, text2: string): number; - - diff_commonSuffix(text1: string, text2: string): number; - - diff_commonOverlap_(text1: string, text2: string): number; - - diff_halfMatch_(text1: string, text2: string): string[]; - - diff_cleanupSemantic(diffs: diff_match_patch.Diff[]): void; - - diff_cleanupSemanticLossless(diffs: diff_match_patch.Diff[]): void; - - diff_cleanupEfficiency(diffs: diff_match_patch.Diff[]): void; - - diff_cleanupMerge(diffs: diff_match_patch.Diff[]): void; - - diff_xIndex(diffs: diff_match_patch.Diff[], loc: number): number; - - diff_prettyHtml(diffs: diff_match_patch.Diff[]): string; - - diff_text1(diffs: diff_match_patch.Diff[]): string; - - diff_text2(diffs: diff_match_patch.Diff[]): string; - - diff_levenshtein(diffs: diff_match_patch.Diff[]): number; - - diff_toDelta(diffs: diff_match_patch.Diff[]): string; - - diff_fromDelta(text1: string, delta: string): diff_match_patch.Diff[]; - - match_main(text: string, pattern: string, loc: number): number; - - match_bitap_(text: string, pattern: string, loc: number): number; - - match_alphabet_(pattern: string): { [char: string]: number }; - - patch_addContext_( - patch: typeof diff_match_patch.patch_obj, - text: string, - ): void; - - patch_make( - a: string, - opt_b: string | diff_match_patch.Diff[], - ): Array; - patch_make( - a: diff_match_patch.Diff[], - ): Array; - patch_make( - a: string, - opt_b: string, - opt_c: diff_match_patch.Diff[], - ): Array; - - patch_deepCopy( - patches: Array, - ): Array; - - patch_apply( - patches: Array, - text: string, - ): [string, boolean[]]; - - patch_addPadding(patches: Array): string; - - patch_splitMax(patches: Array): void; - - patch_fromText(text: string): Array; - - patch_toText(patches: Array): string; - - static patch_obj: { - new (): diff_match_patch.patch_obj; - }; - - static diff_match_patch: typeof diff_match_patch; - static DIFF_DELETE: -1; - static DIFF_INSERT: 1; - static DIFF_EQUAL: 0; - } - - export = diff_match_patch; -} diff --git a/packages/jsondiffpatch/test/index.spec.ts b/packages/jsondiffpatch/test/index.spec.ts index bf024ca4..4acfecbd 100644 --- a/packages/jsondiffpatch/test/index.spec.ts +++ b/packages/jsondiffpatch/test/index.spec.ts @@ -1,4 +1,3 @@ -/// import * as jsondiffpatch from '../src/index.js'; import * as jsonpatchFormatter from '../src/formatters/jsonpatch.js'; import * as htmlFormatter from '../src/formatters/html.js'; From 353eec044736b4ae75ebf494329a7887ffb74e35 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 21:54:04 -0500 Subject: [PATCH 04/10] Add diffMatchPatch to options --- demos/console-demo/demo.ts | 2 +- demos/html-demo/demo.ts | 2 +- packages/jsondiffpatch/package.json | 1 + packages/jsondiffpatch/src/filters/texts.ts | 50 ++++++-------- packages/jsondiffpatch/src/types.ts | 2 + packages/jsondiffpatch/src/with-text-diffs.ts | 67 +++++++++++++++++++ .../jsondiffpatch/test/examples/diffpatch.ts | 5 ++ packages/jsondiffpatch/test/index.spec.ts | 5 +- 8 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 packages/jsondiffpatch/src/with-text-diffs.ts diff --git a/demos/console-demo/demo.ts b/demos/console-demo/demo.ts index 82026fb5..059b5e0a 100644 --- a/demos/console-demo/demo.ts +++ b/demos/console-demo/demo.ts @@ -1,4 +1,4 @@ -import * as jsondiffpatch from 'jsondiffpatch'; +import * as jsondiffpatch from 'jsondiffpatch/with-text-diffs'; import * as consoleFormatter from 'jsondiffpatch/formatters/console'; const instance = jsondiffpatch.create({ diff --git a/demos/html-demo/demo.ts b/demos/html-demo/demo.ts index bc6e08d7..f3c640e6 100644 --- a/demos/html-demo/demo.ts +++ b/demos/html-demo/demo.ts @@ -1,4 +1,4 @@ -import * as jsondiffpatch from 'jsondiffpatch'; +import * as jsondiffpatch from 'jsondiffpatch/with-text-diffs'; import * as annotatedFormatter from 'jsondiffpatch/formatters/annotated'; import * as htmlFormatter from 'jsondiffpatch/formatters/html'; diff --git a/packages/jsondiffpatch/package.json b/packages/jsondiffpatch/package.json index eda949d9..534d0abb 100644 --- a/packages/jsondiffpatch/package.json +++ b/packages/jsondiffpatch/package.json @@ -11,6 +11,7 @@ "types": "./lib/index.d.ts", "exports": { ".": "./lib/index.js", + "./with-text-diffs": "./lib/with-text-diffs.js", "./formatters/*": "./lib/formatters/*.js", "./formatters/styles/*.css": "./lib/formatters/styles/*.css" }, diff --git a/packages/jsondiffpatch/src/filters/texts.ts b/packages/jsondiffpatch/src/filters/texts.ts index d7d4737d..f1e66f3e 100644 --- a/packages/jsondiffpatch/src/filters/texts.ts +++ b/packages/jsondiffpatch/src/filters/texts.ts @@ -1,4 +1,4 @@ -import dmp from 'diff-match-patch'; +import type dmp from 'diff-match-patch'; import type DiffContext from '../contexts/diff.js'; import type PatchContext from '../contexts/patch.js'; import type ReverseContext from '../contexts/reverse.js'; @@ -8,13 +8,10 @@ import type { Filter, ModifiedDelta, MovedDelta, + Options, TextDiffDelta, } from '../types.js'; -declare global { - const diff_match_patch: typeof dmp | undefined; -} - interface DiffPatch { diff: (txt1: string, txt2: string) => string; patch: (txt1: string, string: string) => string; @@ -24,30 +21,25 @@ const TEXT_DIFF = 2; const DEFAULT_MIN_LENGTH = 60; let cachedDiffPatch: DiffPatch | null = null; -function getDiffMatchPatch(required: true): DiffPatch; -function getDiffMatchPatch(required?: boolean): DiffPatch; -function getDiffMatchPatch(required?: boolean) { +function getDiffMatchPatch( + options: Options | undefined, + required: true, +): DiffPatch; +function getDiffMatchPatch( + options: Options | undefined, + required?: boolean, +): DiffPatch | null; +function getDiffMatchPatch(options: Options | undefined, required?: boolean) { if (!cachedDiffPatch) { - let instance: dmp | null | undefined; - if (typeof diff_match_patch !== 'undefined') { - // already loaded, probably a browser - instance = - typeof diff_match_patch === 'function' - ? new diff_match_patch() - : new (diff_match_patch as typeof dmp).diff_match_patch(); - } else if (dmp) { - try { - instance = dmp && new dmp(); - } catch (err) { - instance = null; - } - } - if (!instance) { + let instance: dmp; + if (options?.textDiff?.diffMatchPatch) { + instance = new options.textDiff.diffMatchPatch(); + } else { if (!required) { return null; } const error: Error & { diff_match_patch_not_found?: boolean } = new Error( - 'text diff_match_patch library not found', + 'The diff-match-patch library was not provided. Pass the library in through the options or use the `jsondiffpatch/with-text-diffs` entry-point.', ); // eslint-disable-next-line camelcase error.diff_match_patch_not_found = true; @@ -55,11 +47,11 @@ function getDiffMatchPatch(required?: boolean) { } cachedDiffPatch = { diff: function (txt1, txt2) { - return instance!.patch_toText(instance!.patch_make(txt1, txt2)); + return instance.patch_toText(instance.patch_make(txt1, txt2)); }, patch: function (txt1, patch) { - const results = instance!.patch_apply( - instance!.patch_fromText(patch), + const results = instance.patch_apply( + instance.patch_fromText(patch), txt1, ); for (let i = 0; i < results[1].length; i++) { @@ -95,7 +87,7 @@ export const diffFilter: Filter = function textsDiffFilter( return; } // large text, try to use a text-diff algorithm - const diffMatchPatch = getDiffMatchPatch(); + const diffMatchPatch = getDiffMatchPatch(context.options); if (!diffMatchPatch) { // diff-match-patch library not available, // fallback to regular string replace @@ -125,7 +117,7 @@ export const patchFilter: Filter = function textsPatchFilter( const textDiffDelta = nonNestedDelta as TextDiffDelta; // text-diff, use a text-patch algorithm - const patch = getDiffMatchPatch(true).patch; + const patch = getDiffMatchPatch(context.options, true).patch; context.setResult(patch(context.left as string, textDiffDelta[0])).exit(); }; patchFilter.filterName = 'texts'; diff --git a/packages/jsondiffpatch/src/types.ts b/packages/jsondiffpatch/src/types.ts index ade197aa..4cd2905e 100644 --- a/packages/jsondiffpatch/src/types.ts +++ b/packages/jsondiffpatch/src/types.ts @@ -1,3 +1,4 @@ +import type dmp from 'diff-match-patch'; import type Context from './contexts/context.js'; import type DiffContext from './contexts/diff.js'; @@ -9,6 +10,7 @@ export interface Options { includeValueOnMove?: boolean; }; textDiff?: { + diffMatchPatch: typeof dmp; minLength?: number; }; propertyFilter?: (name: string, context: DiffContext) => boolean; diff --git a/packages/jsondiffpatch/src/with-text-diffs.ts b/packages/jsondiffpatch/src/with-text-diffs.ts new file mode 100644 index 00000000..827b366d --- /dev/null +++ b/packages/jsondiffpatch/src/with-text-diffs.ts @@ -0,0 +1,67 @@ +import DiffMatchPatch from 'diff-match-patch'; +import DiffPatcher from './diffpatcher.js'; +import dateReviver from './date-reviver.js'; +import type { Delta, Options } from './types.js'; +import type Context from './contexts/context.js'; +import type DiffContext from './contexts/diff.js'; +import type PatchContext from './contexts/patch.js'; +import type ReverseContext from './contexts/reverse.js'; + +export { DiffPatcher, dateReviver }; + +export type * from './types.js'; +export type { Context, DiffContext, PatchContext, ReverseContext }; + +export function create(options?: Options) { + return new DiffPatcher({ + ...options, + textDiff: { ...options?.textDiff, diffMatchPatch: DiffMatchPatch }, + }); +} + +let defaultInstance: DiffPatcher; + +export function diff(left: unknown, right: unknown) { + if (!defaultInstance) { + defaultInstance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch }, + }); + } + return defaultInstance.diff(left, right); +} + +export function patch(left: unknown, delta: Delta) { + if (!defaultInstance) { + defaultInstance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch }, + }); + } + return defaultInstance.patch(left, delta); +} + +export function unpatch(right: unknown, delta: Delta) { + if (!defaultInstance) { + defaultInstance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch }, + }); + } + return defaultInstance.unpatch(right, delta); +} + +export function reverse(delta: Delta) { + if (!defaultInstance) { + defaultInstance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch }, + }); + } + return defaultInstance.reverse(delta); +} + +export function clone(value: unknown) { + if (!defaultInstance) { + defaultInstance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch }, + }); + } + return defaultInstance.clone(value); +} diff --git a/packages/jsondiffpatch/test/examples/diffpatch.ts b/packages/jsondiffpatch/test/examples/diffpatch.ts index e5974580..8b805ac2 100644 --- a/packages/jsondiffpatch/test/examples/diffpatch.ts +++ b/packages/jsondiffpatch/test/examples/diffpatch.ts @@ -1,3 +1,4 @@ +import DiffMatchPatch from 'diff-match-patch'; import type { Delta, Options } from '../../src/index.js'; interface Example { @@ -579,6 +580,7 @@ const text: ExampleGroup = [ right: largeText, delta: [shortText, largeText], reverse: [largeText, shortText], + options: { textDiff: { diffMatchPatch: DiffMatchPatch } }, }, { left: largeText, @@ -598,11 +600,13 @@ const text: ExampleGroup = [ 2, ], exactReverse: false, + options: { textDiff: { diffMatchPatch: DiffMatchPatch } }, }, { name: 'larger than min length', options: { textDiff: { + diffMatchPatch: DiffMatchPatch, minLength: 10, }, }, @@ -616,6 +620,7 @@ const text: ExampleGroup = [ name: 'shorter than min length', options: { textDiff: { + diffMatchPatch: DiffMatchPatch, minLength: 10, }, }, diff --git a/packages/jsondiffpatch/test/index.spec.ts b/packages/jsondiffpatch/test/index.spec.ts index 4acfecbd..8f10f972 100644 --- a/packages/jsondiffpatch/test/index.spec.ts +++ b/packages/jsondiffpatch/test/index.spec.ts @@ -1,3 +1,4 @@ +import DiffMatchPatch from 'diff-match-patch'; import * as jsondiffpatch from '../src/index.js'; import * as jsonpatchFormatter from '../src/formatters/jsonpatch.js'; import * as htmlFormatter from '../src/formatters/html.js'; @@ -612,7 +613,9 @@ describe('DiffPatcher', () => { let formatter: typeof htmlFormatter; beforeAll(() => { - instance = new DiffPatcher({ textDiff: { minLength: 10 } }); + instance = new DiffPatcher({ + textDiff: { diffMatchPatch: DiffMatchPatch, minLength: 10 }, + }); formatter = htmlFormatter; }); From 570fc0a5eaf0c26587a302c9bc2637c173c61e2c Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 21:57:49 -0500 Subject: [PATCH 05/10] Add sideEffects to package.json --- packages/jsondiffpatch/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jsondiffpatch/package.json b/packages/jsondiffpatch/package.json index 534d0abb..56dd0dca 100644 --- a/packages/jsondiffpatch/package.json +++ b/packages/jsondiffpatch/package.json @@ -7,6 +7,7 @@ "Benjamin Eidelman " ], "type": "module", + "sideEffects": ["*.css"], "main": "./lib/index.js", "types": "./lib/index.d.ts", "exports": { From 33e109fbb04d46c814aebe510f6c6a352df38e48 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 22:03:03 -0500 Subject: [PATCH 06/10] Format --- packages/jsondiffpatch/package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jsondiffpatch/package.json b/packages/jsondiffpatch/package.json index 56dd0dca..94ed50fb 100644 --- a/packages/jsondiffpatch/package.json +++ b/packages/jsondiffpatch/package.json @@ -7,7 +7,9 @@ "Benjamin Eidelman " ], "type": "module", - "sideEffects": ["*.css"], + "sideEffects": [ + "*.css" + ], "main": "./lib/index.js", "types": "./lib/index.d.ts", "exports": { From 0a262e0713f22f798e2d8d30b074d1218e262342 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 22:23:29 -0500 Subject: [PATCH 07/10] Add to README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 124c86a9..e0bf806d 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,10 @@ In a browser, you can load a bundle using a tool like [esm.sh](https://esm.sh) o ```javascript import * as jsondiffpatch from 'jsondiffpatch'; + +// Only import if you want text diffs using diff-match-patch +import DiffMatchPatch from 'diff-match-patch'; + const jsondiffpatchInstance = jsondiffpatch.create({ // used to match objects when diffing arrays, by default only === operator is used objectHash: function (obj) { @@ -200,6 +204,9 @@ const jsondiffpatchInstance = jsondiffpatch.create({ includeValueOnMove: false, }, textDiff: { + // If using text diffs, it's required to pass in the diff-match-patch library in through this proprty. + // Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options. + diffMatchPatch: DiffMatchPatch, // default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch minLength: 60, }, From 169dd7ad34c754a34bfc46dbd3824ccf825fde6d Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 22:30:34 -0500 Subject: [PATCH 08/10] Exclude diffMatchPatch option for with-text-diffs entry-point --- packages/jsondiffpatch/src/with-text-diffs.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/jsondiffpatch/src/with-text-diffs.ts b/packages/jsondiffpatch/src/with-text-diffs.ts index 827b366d..ab67f115 100644 --- a/packages/jsondiffpatch/src/with-text-diffs.ts +++ b/packages/jsondiffpatch/src/with-text-diffs.ts @@ -12,7 +12,11 @@ export { DiffPatcher, dateReviver }; export type * from './types.js'; export type { Context, DiffContext, PatchContext, ReverseContext }; -export function create(options?: Options) { +export function create( + options?: Omit & { + textDiff?: Omit; + }, +) { return new DiffPatcher({ ...options, textDiff: { ...options?.textDiff, diffMatchPatch: DiffMatchPatch }, From fc7cfae1ee4cce9233a8a6fbd1a46ef995250df3 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 22:37:12 -0500 Subject: [PATCH 09/10] Fix bin --- packages/jsondiffpatch/bin/jsondiffpatch | 19 ------------------- packages/jsondiffpatch/bin/jsondiffpatch.js | 18 ++++++++++++++++++ packages/jsondiffpatch/package.json | 2 +- 3 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 packages/jsondiffpatch/bin/jsondiffpatch create mode 100644 packages/jsondiffpatch/bin/jsondiffpatch.js diff --git a/packages/jsondiffpatch/bin/jsondiffpatch b/packages/jsondiffpatch/bin/jsondiffpatch deleted file mode 100644 index 1c9fb205..00000000 --- a/packages/jsondiffpatch/bin/jsondiffpatch +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env node - -const jsondiffpatch = require('..'); - -const fs = require('fs'); - -const fileLeft = process.argv[2]; -const fileRight = process.argv[3]; - -if (!fileLeft || !fileRight) { - console.log('\n USAGE: jsondiffpatch left.json right.json'); - return; -} - -const left = JSON.parse(fs.readFileSync(fileLeft)); -const right = JSON.parse(fs.readFileSync(fileRight)); - -const delta = jsondiffpatch.diff(left, right); -jsondiffpatch.console.log(delta); diff --git a/packages/jsondiffpatch/bin/jsondiffpatch.js b/packages/jsondiffpatch/bin/jsondiffpatch.js new file mode 100644 index 00000000..786b1a5d --- /dev/null +++ b/packages/jsondiffpatch/bin/jsondiffpatch.js @@ -0,0 +1,18 @@ +#!/usr/bin/env node + +import { readFileSync } from 'node:fs'; +import * as jsondiffpatch from '../lib/with-text-diffs.js'; +import * as consoleFormatter from '../lib/formatters/console.js'; + +const fileLeft = process.argv[2]; +const fileRight = process.argv[3]; + +if (!fileLeft || !fileRight) { + console.log('\n USAGE: jsondiffpatch left.json right.json'); +} else { + const left = JSON.parse(readFileSync(fileLeft)); + const right = JSON.parse(readFileSync(fileRight)); + + const delta = jsondiffpatch.diff(left, right); + consoleFormatter.log(delta); +} diff --git a/packages/jsondiffpatch/package.json b/packages/jsondiffpatch/package.json index 94ed50fb..f4e10dba 100644 --- a/packages/jsondiffpatch/package.json +++ b/packages/jsondiffpatch/package.json @@ -23,7 +23,7 @@ "lib" ], "bin": { - "jsondiffpatch": "./bin/jsondiffpatch" + "jsondiffpatch": "./bin/jsondiffpatch.js" }, "scripts": { "build": "tsc && ncp ./src/formatters/styles/ ./lib/formatters/styles/", From ffd52bb30d3d7b206ed74e3926da924e1bf5c709 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Wed, 6 Dec 2023 22:37:58 -0500 Subject: [PATCH 10/10] Format --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e0bf806d..8c3168bb 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,7 @@ const jsondiffpatchInstance = jsondiffpatch.create({ }, textDiff: { // If using text diffs, it's required to pass in the diff-match-patch library in through this proprty. - // Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options. + // Alternatively, you can import jsondiffpatch using `jsondiffpatch/with-text-diffs` to avoid having to pass in diff-match-patch through the options. diffMatchPatch: DiffMatchPatch, // default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch minLength: 60,