From 2b6480fdf8da864d402419dccc029cf10b675299 Mon Sep 17 00:00:00 2001 From: Charlie Greenman Date: Fri, 12 Jan 2024 15:06:19 -0500 Subject: [PATCH] ZETA-7634: Allow for json codemorph to use strings in addition to objects --- src/rz/json/edit-json/edit-json.spec.ts | 23 ++-- src/rz/json/edit-json/edit-json.ts | 15 ++- .../json/snapshots/project-output.json.snap | 100 ------------------ src/rz/json/snapshots/project.json.snap | 94 ---------------- 4 files changed, 31 insertions(+), 201 deletions(-) delete mode 100644 src/rz/json/snapshots/project-output.json.snap delete mode 100644 src/rz/json/snapshots/project.json.snap diff --git a/src/rz/json/edit-json/edit-json.spec.ts b/src/rz/json/edit-json/edit-json.spec.ts index 1f7e230..1123c7a 100644 --- a/src/rz/json/edit-json/edit-json.spec.ts +++ b/src/rz/json/edit-json/edit-json.spec.ts @@ -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); }); diff --git a/src/rz/json/edit-json/edit-json.ts b/src/rz/json/edit-json/edit-json.ts index 5571252..36b57f6 100644 --- a/src/rz/json/edit-json/edit-json.ts +++ b/src/rz/json/edit-json/edit-json.ts @@ -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); @@ -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); diff --git a/src/rz/json/snapshots/project-output.json.snap b/src/rz/json/snapshots/project-output.json.snap deleted file mode 100644 index 8dc9d80..0000000 --- a/src/rz/json/snapshots/project-output.json.snap +++ /dev/null @@ -1,100 +0,0 @@ -{ - "projectType": "application", - "root": "apps/razroo-nrwl-angular-shell", - "sourceRoot": "apps/razroo-nrwl-angular-shell/src", - "prefix": "razroo-nrwl-angular-shell", - "targets": { - "build": { - "executor": "@angular-devkit/build-angular:browser", - "outputs": [ - "{options.outputPath}" - ], - "options": { - "outputPath": "dist/apps/razroo-nrwl-angular-shell", - "index": "apps/razroo-nrwl-angular-shell/src/index.html", - "main": "apps/razroo-nrwl-angular-shell/src/main.ts", - "polyfills": "apps/razroo-nrwl-angular-shell/src/polyfills.ts", - "tsConfig": "apps/razroo-nrwl-angular-shell/tsconfig.app.json", - "inlineStyleLanguage": "scss", - "assets": [ - "apps/razroo-nrwl-angular-shell/src/favicon.ico", - "apps/razroo-nrwl-angular-shell/src/assets" - ], - "styles": [ - "apps/razroo-nrwl-angular-shell/src/styles.scss" - ], - "scripts": [] - }, - "configurations": { - "production": { - "budgets": [ - { - "type": "initial", - "maximumWarning": "500kb", - "maximumError": "1mb" - }, - { - "type": "anyComponentStyle", - "maximumWarning": "2kb", - "maximumError": "4kb" - } - ], - "fileReplacements": [ - { - "replace": "libs/common/environments/src/lib/environment.ts", - "with": "libs/common/environments/src/lib/environment.prod.ts" - } - ], - "outputHashing": "all" - }, - "development": { - "buildOptimizer": false, - "optimization": false, - "vendorChunk": true, - "extractLicenses": false, - "sourceMap": true, - "namedChunks": true - } - }, - "defaultConfiguration": "production" - }, - "serve": { - "executor": "@angular-devkit/build-angular:dev-server", - "configurations": { - "production": { - "browserTarget": "razroo-nrwl-angular-shell:build:production" - }, - "development": { - "browserTarget": "razroo-nrwl-angular-shell:build:development" - } - }, - "defaultConfiguration": "development" - }, - "extract-i18n": { - "executor": "@angular-devkit/build-angular:extract-i18n", - "options": { - "browserTarget": "razroo-nrwl-angular-shell:build" - } - }, - "lint": { - "executor": "@nrwl/linter:eslint", - "options": { - "lintFilePatterns": [ - "apps/razroo-nrwl-angular-shell/src/**/*.ts", - "apps/razroo-nrwl-angular-shell/src/**/*.html" - ] - } - }, - "test": { - "executor": "@nrwl/jest:jest", - "outputs": [ - "coverage/apps/razroo-nrwl-angular-shell" - ], - "options": { - "jestConfig": "apps/razroo-nrwl-angular-shell/jest.config.js", - "passWithNoTests": true - } - } - }, - "tags": [] -} \ No newline at end of file diff --git a/src/rz/json/snapshots/project.json.snap b/src/rz/json/snapshots/project.json.snap deleted file mode 100644 index 56268f8..0000000 --- a/src/rz/json/snapshots/project.json.snap +++ /dev/null @@ -1,94 +0,0 @@ -{ - "projectType": "application", - "root": "apps/razroo-nrwl-angular-shell", - "sourceRoot": "apps/razroo-nrwl-angular-shell/src", - "prefix": "razroo-nrwl-angular-shell", - "targets": { - "build": { - "executor": "@angular-devkit/build-angular:browser", - "outputs": ["{options.outputPath}"], - "options": { - "outputPath": "dist/apps/razroo-nrwl-angular-shell", - "index": "apps/razroo-nrwl-angular-shell/src/index.html", - "main": "apps/razroo-nrwl-angular-shell/src/main.ts", - "polyfills": "apps/razroo-nrwl-angular-shell/src/polyfills.ts", - "tsConfig": "apps/razroo-nrwl-angular-shell/tsconfig.app.json", - "inlineStyleLanguage": "scss", - "assets": [ - "apps/razroo-nrwl-angular-shell/src/favicon.ico", - "apps/razroo-nrwl-angular-shell/src/assets" - ], - "styles": ["apps/razroo-nrwl-angular-shell/src/styles.scss"], - "scripts": [] - }, - "configurations": { - "production": { - "budgets": [ - { - "type": "initial", - "maximumWarning": "500kb", - "maximumError": "1mb" - }, - { - "type": "anyComponentStyle", - "maximumWarning": "2kb", - "maximumError": "4kb" - } - ], - "fileReplacements": [ - { - "replace": "apps/razroo-nrwl-angular-shell/src/environments/environment.ts", - "with": "apps/razroo-nrwl-angular-shell/src/environments/environment.prod.ts" - } - ], - "outputHashing": "all" - }, - "development": { - "buildOptimizer": false, - "optimization": false, - "vendorChunk": true, - "extractLicenses": false, - "sourceMap": true, - "namedChunks": true - } - }, - "defaultConfiguration": "production" - }, - "serve": { - "executor": "@angular-devkit/build-angular:dev-server", - "configurations": { - "production": { - "browserTarget": "razroo-nrwl-angular-shell:build:production" - }, - "development": { - "browserTarget": "razroo-nrwl-angular-shell:build:development" - } - }, - "defaultConfiguration": "development" - }, - "extract-i18n": { - "executor": "@angular-devkit/build-angular:extract-i18n", - "options": { - "browserTarget": "razroo-nrwl-angular-shell:build" - } - }, - "lint": { - "executor": "@nrwl/linter:eslint", - "options": { - "lintFilePatterns": [ - "apps/razroo-nrwl-angular-shell/src/**/*.ts", - "apps/razroo-nrwl-angular-shell/src/**/*.html" - ] - } - }, - "test": { - "executor": "@nrwl/jest:jest", - "outputs": ["coverage/apps/razroo-nrwl-angular-shell"], - "options": { - "jestConfig": "apps/razroo-nrwl-angular-shell/jest.config.js", - "passWithNoTests": true - } - } - }, - "tags": [] -}