Skip to content

Commit

Permalink
ZETA-7634: Allow for json codemorph to use strings in addition to obj…
Browse files Browse the repository at this point in the history
…ects
  • Loading branch information
CharlieGreenman committed Jan 12, 2024
1 parent 5622fc4 commit 2b6480f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 201 deletions.
23 changes: 17 additions & 6 deletions src/rz/json/edit-json/edit-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ import { readFileSync } from 'fs';
import { EditJson } from './../interfaces/json-morph.interface';
import { editJson, addJsonKeyValue } from './edit-json';
describe('editJson', () => {
it('should edit json and return value', () => {
const mockJson = readFileSync('src/rz/json/snapshots/project.json.snap').toString();
it('should modify nested json and return a string value', () => {
const mockJson = {
"scripts": {
"build": "ng build",
"test": "hello"
}
};
const stringifiedMockJson = JSON.stringify(mockJson);

const mockEditJson: EditJson = {
nodeType: 'editJson',
valueToModify: '/targets/build/configurations/production/fileReplacements',
codeBlock: '[{replace: "libs/common/environments/src/lib/environment.ts", with: "libs/common/environments/src/lib/environment.prod.ts"}]'
valueToModify: '/scripts/build',
codeBlock: "nx build:two"
}
const modifiedJson = editJson(mockEditJson, mockJson);
const expected = JSON.parse(readFileSync('src/rz/json/snapshots/project-output.json.snap').toString());
const modifiedJson = editJson(mockEditJson, stringifiedMockJson);
const expected = {
scripts: {
build: "nx build:two",
test: "hello"
}
};

expect(modifiedJson).toEqual(expected);
});
Expand Down
15 changes: 14 additions & 1 deletion src/rz/json/edit-json/edit-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSONPath } from 'jsonpath-plus';
import * as pointer from 'json-pointer';

export function editJson(editJson: EditJson, json: string): any {
const codeBlock = eval(editJson.codeBlock);
const codeBlock = parseJsonOrString(editJson.codeBlock);
json = JSON.parse(json);
//2. Set value
pointer.set(json as any, editJson.valueToModify, codeBlock);
Expand All @@ -12,6 +12,19 @@ export function editJson(editJson: EditJson, json: string): any {
return json;
}

function parseJsonOrString(input: string): string | object {
try {
const parsedJson = JSON.parse(input);
if (typeof parsedJson === 'object' && parsedJson !== null) {
return parsedJson;
}
} catch (error) {
return input;
}

return input;
}

export function addJsonKeyValue(editJson: EditJson, json: string): any {
const codeBlock = typeof editJson.codeBlock === 'string' ? JSON.parse((editJson.codeBlock)) : editJson.codeBlock;
json = JSON.parse(json);
Expand Down
100 changes: 0 additions & 100 deletions src/rz/json/snapshots/project-output.json.snap

This file was deleted.

94 changes: 0 additions & 94 deletions src/rz/json/snapshots/project.json.snap

This file was deleted.

0 comments on commit 2b6480f

Please sign in to comment.