Skip to content

Commit

Permalink
Merge pull request #69 from razroo/ZETA-7642-update-codemorph-add-jso…
Browse files Browse the repository at this point in the history
…n-logic

ZETA-7642: Add logic for adding json object even as a string for adding json logic
  • Loading branch information
CharlieGreenman authored Jan 13, 2024
2 parents 8860182 + b01ea7c commit 5743ace
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 271 deletions.
62 changes: 62 additions & 0 deletions src/rz/json/add-json/add-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { EditJson } from './../interfaces/json-morph.interface';
import { addJsonKeyValue } from './add-json';

describe('addJson', () => {
it('should add json key value if nested json being added', () => {
const mockJson = `{
"test": "123",
"targets": {}
}`;

const codeBlock = `{
"server": {
"executor": "@angular-devkit/build-angular:server"
}
}`;

const mockEditJson: EditJson = {
nodeType: 'addJsonKeyValue',
valueToModify: 'targets',
codeBlock: codeBlock
}

const modifiedJson = addJsonKeyValue(mockEditJson, mockJson);
const expected = {
"test": "123",
"targets": {
"server": {
"executor": "@angular-devkit/build-angular:server"
}
}
};

expect(modifiedJson).toEqual(expected);
});

it('should add a key value', () => {
const mockJson = `{
"scripts": {
"test": "npm run test"
}
}`;

const mockEditJson: EditJson = {
nodeType: 'editJson',
valueToModify: 'scripts',
codeBlock: '{"test:ci": "npm run nx -- run-many --target=test --all --parallel --coverage --coverageReporters=lcov && node ./tools/coverageMerger.js"}'
}

const modifiedJson = addJsonKeyValue(mockEditJson, mockJson);
const expected = {
scripts: {
"test": "npm run test",
"test:ci": "npm run nx -- run-many --target=test --all --parallel --coverage --coverageReporters=lcov && node ./tools/coverageMerger.js"
}
};

expect(modifiedJson).toEqual(expected);
});

});


21 changes: 21 additions & 0 deletions src/rz/json/add-json/add-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JSONPath } from 'jsonpath-plus';
import { EditJson } from "../interfaces/json-morph.interface";
import * as pointer from 'json-pointer';

export function addJsonKeyValue(editJson: EditJson, json: string): any {
// double json parse hack to make sure /n are removed from string
const codeBlock = typeof editJson.codeBlock === 'string' ? JSON.parse(JSON.parse(JSON.stringify(editJson.codeBlock))) : editJson.codeBlock;
json = JSON.parse(json);
//Get Pointer
const JsonPointer = JSONPath({path: `$..${editJson.valueToModify}`, json, resultType: 'pointer'});
const firstJsonMatchedPointer = JsonPointer[0];

//Get value of json
const jsonToEdit = pointer.get(json as any, firstJsonMatchedPointer);

//Set value
pointer.set(json as any, firstJsonMatchedPointer, {...jsonToEdit, ...codeBlock});

//Return modified value
return json;
}
36 changes: 17 additions & 19 deletions src/rz/json/edit-json/edit-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { EditJson } from './../interfaces/json-morph.interface';
import { editJson, addJsonKeyValue } from './edit-json';
import { editJson } from './edit-json';

describe('editJson', () => {
it('should modify nested json and return a string value', () => {
const mockJson = {
Expand Down Expand Up @@ -28,29 +28,27 @@ describe('editJson', () => {
});

it('should edit nested json and return value', () => {
const mockJson = readFileSync('src/rz/json/snapshots/nested-package-json/nested-package.json.snap').toString();
const mockJson = {
contributes: {
menus: {}
}
};

const stringifiedMockJson = JSON.stringify(mockJson);

const mockEditJson: EditJson = {
nodeType: 'editJson',
valueToModify: '/contributes/menus',
codeBlock: {data: "test"}
}
const modifiedJson = editJson(mockEditJson, mockJson);
const expected = JSON.parse(readFileSync('src/rz/json/snapshots/nested-package-json/nested-package-output.json.snap').toString());

expect(modifiedJson).toEqual(expected);
});

it('should add a key value', () => {
const mockJson = readFileSync('src/rz/json/snapshots/package-json/package.json.snap').toString();
const mockEditJson: EditJson = {
nodeType: 'editJson',
valueToModify: 'scripts',
codeBlock: '{"test:ci": "npm run nx -- run-many --target=test --all --parallel --coverage --coverageReporters=lcov && node ./tools/coverageMerger.js"}'
}

const modifiedJson = addJsonKeyValue(mockEditJson, mockJson);
const expected = JSON.parse(readFileSync('src/rz/json/snapshots/package-json/package-output.json.snap').toString());
const modifiedJson = editJson(mockEditJson, stringifiedMockJson);
const expected = {
contributes: {
menus: {
data: 'test'
}
}
};

expect(modifiedJson).toEqual(expected);
});
Expand Down
18 changes: 0 additions & 18 deletions src/rz/json/edit-json/edit-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EditJson } from "../interfaces/json-morph.interface";
import { JSONPath } from 'jsonpath-plus';
import * as pointer from 'json-pointer';

export function editJson(editJson: EditJson, json: string): any {
Expand All @@ -24,21 +23,4 @@ function parseJsonOrString(input: string): string | object {

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);
//Get Pointer
const JsonPointer = JSONPath({path: `$..${editJson.valueToModify}`, json, resultType: 'pointer'});
const firstJsonMatchedPointer = JsonPointer[0];

//Get value of json
const jsonToEdit = pointer.get(json as any, firstJsonMatchedPointer);

//Set value
pointer.set(json as any, firstJsonMatchedPointer, {...jsonToEdit, ...codeBlock});

//Return modified value
return json;
}

3 changes: 2 additions & 1 deletion src/rz/json/json-morph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EditJson, EditJsonInput } from './interfaces/json-morph.interface';
import { addJsonKeyValue, editJson } from './edit-json/edit-json';
import { editJson } from './edit-json/edit-json';
import { addJsonKeyValue } from './add-json/add-json';

export function morphJson(editJsonInput: EditJsonInput): string {
let json = editJsonInput.fileToBeAddedTo;
Expand Down

This file was deleted.

59 changes: 0 additions & 59 deletions src/rz/json/snapshots/nested-package-json/nested-package.json.snap

This file was deleted.

57 changes: 0 additions & 57 deletions src/rz/json/snapshots/package-json/package-output.json.snap

This file was deleted.

Loading

0 comments on commit 5743ace

Please sign in to comment.