Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZETA-7634: Allow for json codemorph to use strings in addition to objects #68

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading