Skip to content

Commit

Permalink
fix: take into account ignore-new and ignore-existing options in diff
Browse files Browse the repository at this point in the history
  • Loading branch information
robrechtme authored Aug 31, 2022
2 parents e474b84 + c0be531 commit 5c2ab6d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/commands/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const push = async ({ yes, status, tag }: CommandOptions, program: Command) => {
const remote = await apiPull(accessKey, pullOptions);
const { added, deleted, updated, totalCount, deletedCount } = diff(
remote,
local
local,
pushOptions
);

if (!totalCount || (!deleteAbsent && totalCount === deletedCount)) {
Expand Down
14 changes: 10 additions & 4 deletions src/lib/diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { detailedDiff } from "deep-object-diff";
import { Translations } from "../../types";
import { PushOptions, Translations } from "../../types";
import { dotObject } from "./dotObject";

interface DetailedDiff {
Expand All @@ -8,14 +8,20 @@ interface DetailedDiff {
deleted: object;
}

export const diff = (local: Translations, remote: Translations) => {
export const diff = (
local: Translations,
remote: Translations,
options?: PushOptions
) => {
const { added, updated, deleted } = detailedDiff(
local,
remote
) as DetailedDiff;
const ignoreAdded = options?.["ignore-new"];
const ignoreUpdated = options?.["ignore-existing"];

const addedRes = dotObject(added);
const updatedRes = dotObject(updated);
const addedRes = ignoreAdded ? {} : dotObject(added);
const updatedRes = ignoreUpdated ? {} : dotObject(updated);
const deletedRes = dotObject(deleted);
return {
totalCount:
Expand Down
52 changes: 52 additions & 0 deletions test/__snapshots__/diff.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,55 @@ exports[`returns diff 1`] = `
"updatedCount": 1,
}
`;

exports[`returns diff with options 1`] = `
{
"added": {},
"addedCount": 0,
"deleted": {
"en.buttons.skip": undefined,
"en.title": undefined,
},
"deletedCount": 2,
"totalCount": 3,
"updated": {
"en.buttons.confirm": "Different translation local/remote",
},
"updatedCount": 1,
}
`;

exports[`returns diff without new assets 1`] = `
{
"added": {},
"addedCount": 0,
"deleted": {
"en.buttons.skip": undefined,
"en.title": undefined,
},
"deletedCount": 2,
"totalCount": 3,
"updated": {
"en.buttons.confirm": "Different translation local/remote",
},
"updatedCount": 1,
}
`;

exports[`returns diff without updated assets 1`] = `
{
"added": {
"en.body": "Newly added key remote in EN only",
"en.buttons.accept": "Newly added key deep remote in EN only",
},
"addedCount": 2,
"deleted": {
"en.buttons.skip": undefined,
"en.title": undefined,
},
"deletedCount": 2,
"totalCount": 4,
"updated": {},
"updatedCount": 0,
}
`;
8 changes: 8 additions & 0 deletions test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ import { local, remote } from "./mockdata/mockDiff";
test("returns diff", () => {
expect(diff(local, remote)).toMatchSnapshot();
});

test("returns diff without new assets", () => {
expect(diff(local, remote, { "ignore-new": true })).toMatchSnapshot();
});

test("returns diff without updated assets", () => {
expect(diff(local, remote, { "ignore-existing": true })).toMatchSnapshot();
});

0 comments on commit 5c2ab6d

Please sign in to comment.