diff --git a/README.md b/README.md index cfb33be..e8cb9aa 100644 --- a/README.md +++ b/README.md @@ -11,20 +11,18 @@ This is a project to automates postman collections using the [Reactor API](https -- [AEP Tag Tool](#aep-tag-tool) - - [Overview](#overview) - - [Installation](#installation) - - [Command Line Tool](#command-line-tool) - - [Create config file for Authentication](#create-config-file-for-authentication) - - [Usage](#usage) - - [Export a Tag](#export-a-tag) - - [Import a Tag](#import-a-tag) - - [CEDRLP params](#cedrlp-params) - - [Import into other Adobe Organizations](#import-into-other-adobe-organizations) - - [newSettings.yml](#newsettingsyml) - - [Delete tag properties that contain a specific string](#delete-tag-properties-that-contain-a-specific-string) - - [Using this tool without NPM](#using-this-tool-without-npm) - - [Postman files](#postman-files) +- [Installation](#installation) +- [Command Line Tool](#command-line-tool) +- [Create config file for Authentication](#create-config-file-for-authentication) +- [Usage](#usage) +- [Export a Tag](#export-a-tag) +- [Import a Tag](#import-a-tag) + - [CEDRLP params](#cedrlp-params) + - [Import into other Adobe Organizations](#import-into-other-adobe-organizations) + - [newSettings.yml](#newsettingsyml) +- [Delete tag properties that contain a specific string](#delete-tag-properties-that-contain-a-specific-string) +- [Using this tool without NPM](#using-this-tool-without-npm) + - [Postman files](#postman-files) diff --git a/importObjectUtil.js b/importObjectUtil.js index 46188bc..9d4f123 100644 --- a/importObjectUtil.js +++ b/importObjectUtil.js @@ -5,13 +5,13 @@ const yaml = require("js-yaml"); //Mac: DEBUG=* aep-tag-tool.... //WIN: set DEBUG=* & aep-tag-tool.... const debug = require("debug"); -const debugParser = debug("parser"); -const debugNewSettings = debug("new setting"); -const debugConfig = require("debug")("config"); +const debugImportObj = debug("import"); +const debugNewSettings = debug("import:setting"); +const debugConfig = debug("import:config"); exports.debugOptions = { - "json": "Messages on JSON obj creation from files", - "property": "messages related to the property file", - "config": "Messages related to config file" + "import": "checks if values have been found/replaces in import object", + "import:setting": "new settings displayed", + "import:config": "debug the web property created from the file" }; //Create an web property import object from a file @@ -39,18 +39,15 @@ async function updateSettings(importObj, newSettingsFile) { const yamlContent = await fs.readFile(newSettingsFile, "utf8"); const parsedYaml = yaml.loadAll(yamlContent); - debugParser("Parsing through replacement values"); + debugImportObj("Parsing through replacement values"); for (const tagResourceName in parsedYaml[0]) { const resourceObjects = parsedYaml[0][tagResourceName]; for (const objName in resourceObjects) { const newSettings = resourceObjects[objName]; - const found = replaceSettings(importObj, tagResourceName, objName, newSettings); - - if (!found) { - debugParser("'" + tagResourceName + ":" + objName + "' not found in import json"); - } + importObj = replaceSettings(importObj, tagResourceName, objName, newSettings); } } + if(debug.enabled("import")) await fs.writeFile("updatedImport.json", JSON.stringify(importObj, null, 2)); return importObj; } catch (error) { console.error("Error:", error); @@ -58,24 +55,28 @@ async function updateSettings(importObj, newSettingsFile) { } // Replaces a settings key/value pair in the objName if present in the importData -function replaceSettings(importData, tagResourceName, objName, newSettings) { - if (importData[tagResourceName]) { - for (const key of importData[tagResourceName]) { +function replaceSettings(importData, tagComponentName, objName, newSettings) { + let componentArr = importData[tagComponentName]; + if (componentArr) { + for (const key of componentArr) { const keyName = key.attributes.name; if (keyName === objName) { - debugParser("Found '" + tagResourceName + ":" + objName + "'. Replacing old settings."); + debugImportObj("Found '" + tagComponentName + ":" + objName + "'. Replacing old settings."); debugNewSettings(newSettings); - const settings = JSON.parse(key.attributes.settings); + let settings = JSON.parse(key.attributes.settings); for (const setting in newSettings) { - parserUtil.replaceValueInJSON(settings, setting, newSettings[setting]); + settings = parserUtil.replaceValueInJSON(settings, setting, newSettings[setting]); } + debugNewSettings("Applying to settings JSON:"); + debugNewSettings(settings); key.attributes.settings = JSON.stringify(settings); - return true; + return importData; } } } - return false; + debugImportObj("'" + tagComponentName + ":" + objName + "' not found in import json"); + return importData; } diff --git a/package-lock.json b/package-lock.json index 3a24d64..f5b4365 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@knennigtri/aep-tag-tool", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@knennigtri/aep-tag-tool", - "version": "1.2.0", + "version": "1.2.1", "license": "ISC", "dependencies": { "debug": "^4.3.4", diff --git a/package.json b/package.json index c3ca8c0..f9bae10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@knennigtri/aep-tag-tool", - "version": "1.2.0", + "version": "1.2.1", "description": "Export and Import Adobe Experience Platform Tags (previously Launch) with optional variable updates based on Organzations", "repository": { "type": "git", diff --git a/parserUtil.js b/parserUtil.js index d100be6..6de3b62 100644 --- a/parserUtil.js +++ b/parserUtil.js @@ -2,13 +2,13 @@ const fs = require("fs"); const path = require("path"); const yaml = require("js-yaml"); const debug = require("debug"); -const debugJSON = debug("json"); -const debugReplace = debug("replace"); -const debugProperty = debug("property"); -const debugConfig = debug("config"); +const debugJSON = debug("parser:json"); +const debugReplace = debug("parser:replace"); +const debugVerbose = debug("parser:verbose"); exports.debugOptions = { - "json": "Messages on JSON obj creation from files", - "replace": "Messages on key/value replacement" + "parser:json": "Messages on JSON obj creation from files", + "parser:replace": "Messages on key/value replacement", + "parser:verbose": "" }; //Input yaml contents or JSON contents to return a valid JSON object @@ -40,20 +40,20 @@ function replaceValueInJSON(jsonObject, keyToFind, newValue) { if (typeof jsonObject !== "object" || jsonObject === null) { return jsonObject; } - + if (Array.isArray(jsonObject)) { + for (let i = 0; i < jsonObject.length; i++) { jsonObject[i] = replaceValueInJSON(jsonObject[i], keyToFind, newValue); } } else { for (const key in jsonObject) { - if (Object.prototype.hasOwnProperty.call(key)) { - if (key === keyToFind) { - debugReplace("Found key '" + keyToFind + "' with value '" + jsonObject[key] + "' and replacing with '" + newValue + "'"); - jsonObject[key] = newValue; - } else { - jsonObject[key] = replaceValueInJSON(jsonObject[key], keyToFind, newValue); - } + debugReplace("replace: " + key); + if (key === keyToFind) { + debugReplace("Found key '" + keyToFind + "' with value '" + jsonObject[key] + "' and replacing with '" + newValue + "'"); + jsonObject[key] = newValue; + } else { + jsonObject[key] = replaceValueInJSON(jsonObject[key], keyToFind, newValue); } } } @@ -66,7 +66,6 @@ function replaceValueInJSON(jsonObject, keyToFind, newValue) { // workingDir: // } function getFileObjAndWorkingDir(file){ - debugProperty(getFileObjAndWorkingDir); let obj = {}; if(typeof file == "string"){ if(fs.lstatSync(file).isFile()){ @@ -99,7 +98,7 @@ function findNestedObj(entireObj, keyToFind) { let foundValue; JSON.stringify(entireObj, (curKey, curVal) => { if(curKey.toUpperCase().replace("-","_").replace(" ","_") == keyToFind){ - debugConfig("Found: " + keyToFind); + debugVerbose("Found: " + keyToFind); foundValue = curVal; } return curVal;