From 4c7ff013d0bb77f0d86c79d9968a43990ddfe978 Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 16:39:17 +0530 Subject: [PATCH 1/6] feat(copy): implement diff instead of list while copying --- src/copy.ts | 4 ++-- src/diff.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/copy.ts b/src/copy.ts index f61b40d..56be674 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -1,7 +1,7 @@ import {Command, Option} from 'commander'; import colors from 'colors'; import inquirer from 'inquirer'; -import {list} from './list.js'; +import {diff} from './diff.js'; import {validateSingleAccountOpts} from './core.js'; import {TagManagerData} from './core.js'; import {Config} from './config.js'; @@ -128,7 +128,7 @@ copy_cmd.action(async () => { await targetAccount.init(); if (isReset) { - await list(sourceAccount); + await diff(sourceAccountAlias, targetAccountAlias, sourceAccount, targetAccount); inquirer .prompt([ { diff --git a/src/diff.ts b/src/diff.ts index 2f1f004..ad2c3a5 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -163,6 +163,16 @@ diffCmd.action(async () => { await targetAccount.init(); await Promise.all([sourceAccount.getData(), targetAccount.getData()]); + await diff(sourceAccountAlias, targetAccountAlias, sourceAccount, targetAccount, showUnchangedChanges); +}); + +export function diff( + sourceAccountAlias: string, + targetAccountAlias: string, + sourceAccount: TagManagerData, + targetAccount: TagManagerData, + showUnchangedChanges: boolean = false +) { outputEntityDiff( sourceAccountAlias, targetAccountAlias, @@ -300,7 +310,7 @@ diffCmd.action(async () => { }, showUnchangedChanges ); -}); +} function outputEntityDiff< T extends {name?: string | null | undefined; type?: string | null | undefined} @@ -315,7 +325,7 @@ function outputEntityDiff< additionalColumns: { [key: string]: (sourceEntity?: T, targetEntity?: T) => Change[]; }, - showUnchangedChanges = false + showUnchangedChanges: boolean = false ) { const entitiesByName = new Map< string, From ddbd7958cbc70d97d9303f313ac537ecb54da39a Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 22:17:03 +0530 Subject: [PATCH 2/6] feat(copy): exit if there is no diff between accounts --- src/copy.ts | 16 +++++++++-- src/diff.ts | 79 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/copy.ts b/src/copy.ts index 56be674..94b3975 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -126,9 +126,22 @@ copy_cmd.action(async () => { true ); await targetAccount.init(); + await Promise.all([sourceAccount.getData(), targetAccount.getData()]); if (isReset) { - await diff(sourceAccountAlias, targetAccountAlias, sourceAccount, targetAccount); + const hasChanges = await diff( + sourceAccountAlias, + targetAccountAlias, + sourceAccount, + targetAccount + ); + if (!hasChanges) { + console.log( + 'There are no changes between the source and target accounts to copy. Exiting.' + ); + return; + } + inquirer .prompt([ { @@ -145,7 +158,6 @@ copy_cmd.action(async () => { 'Resetting target GTM account and copying entities from source GTM account...' .gray ); - await targetAccount.getData(); await targetAccount.reset(); const responses = await targetAccount.copyDataFromAccount( sourceAccount diff --git a/src/diff.ts b/src/diff.ts index ad2c3a5..95789c3 100644 --- a/src/diff.ts +++ b/src/diff.ts @@ -163,17 +163,23 @@ diffCmd.action(async () => { await targetAccount.init(); await Promise.all([sourceAccount.getData(), targetAccount.getData()]); - await diff(sourceAccountAlias, targetAccountAlias, sourceAccount, targetAccount, showUnchangedChanges); + await diff( + sourceAccountAlias, + targetAccountAlias, + sourceAccount, + targetAccount, + showUnchangedChanges + ); }); -export function diff( +export async function diff( sourceAccountAlias: string, targetAccountAlias: string, sourceAccount: TagManagerData, targetAccount: TagManagerData, - showUnchangedChanges: boolean = false -) { - outputEntityDiff( + showUnchangedChanges = false +): Promise { + const hasVariableChanges = outputEntityDiff( sourceAccountAlias, targetAccountAlias, 'Variable', @@ -212,7 +218,7 @@ export function diff( showUnchangedChanges ); - outputEntityDiff( + const hasTriggerChanges = outputEntityDiff( sourceAccountAlias, targetAccountAlias, 'Trigger', @@ -251,7 +257,7 @@ export function diff( showUnchangedChanges ); - outputEntityDiff( + const hasTagChanges = outputEntityDiff( sourceAccountAlias, targetAccountAlias, 'Tag', @@ -310,6 +316,10 @@ export function diff( }, showUnchangedChanges ); + + return Promise.resolve( + hasVariableChanges || hasTriggerChanges || hasTagChanges + ); } function outputEntityDiff< @@ -325,8 +335,8 @@ function outputEntityDiff< additionalColumns: { [key: string]: (sourceEntity?: T, targetEntity?: T) => Change[]; }, - showUnchangedChanges: boolean = false -) { + showUnchangedChanges = false +): boolean { const entitiesByName = new Map< string, {source?: T; target?: T; changeType?: RowChangeTypeEnum} @@ -430,32 +440,41 @@ function outputEntityDiff< } }); + const additions = Array.from(entitiesByName.values()).filter( + row => row.changeType === RowChangeTypeEnum.Added + ).length; + const deletions = Array.from(entitiesByName.values()).filter( + row => row.changeType === RowChangeTypeEnum.Removed + ).length; + const modifications = Array.from(entitiesByName.values()).filter( + row => row.changeType === RowChangeTypeEnum.Modified + ).length; + const unchanged = Array.from(entitiesByName.values()).filter( + row => row.changeType === RowChangeTypeEnum.Unchanged + ).length; + const hasChanges = !!(additions || modifications || deletions); + console.log( `==> ${entityName}s`.blue, '[', - `${ - Array.from(entitiesByName.values()).filter( - row => row.changeType === RowChangeTypeEnum.Added - ).length - } additions, `.green, - `${ - Array.from(entitiesByName.values()).filter( - row => row.changeType === RowChangeTypeEnum.Removed - ).length - } deletions, `.red, - `${ - Array.from(entitiesByName.values()).filter( - row => row.changeType === RowChangeTypeEnum.Modified - ).length - } modifications, `.yellow, - `${ - Array.from(entitiesByName.values()).filter( - row => row.changeType === RowChangeTypeEnum.Unchanged - ).length - } unchanged${!showUnchangedChanges ? ' (hidden)' : ''}`.grey, + `${additions} additions, `.green, + `${deletions} deletions, `.red, + `${modifications} modifications, `.yellow, + `${unchanged} unchanged${!showUnchangedChanges ? ' (hidden)' : ''}`.grey, ']' ); - console.log(outputTable.toString()); + if (hasChanges || (showUnchangedChanges && unchanged)) { + console.log(outputTable.toString()); + } + if (!hasChanges) { + console.log( + `No changes in ${entityName.toLowerCase()}s between the source and target accounts.` + .grey + ); + } + console.log('\n'); + + return hasChanges; } export {diffCmd}; From b475b699cfa1319d50beca0f8f46860b4b72dc79 Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 22:31:01 +0530 Subject: [PATCH 3/6] feat(copy): handle cases around copy, diff and reset --- src/copy.ts | 252 ++++++++++++++++++++++++++-------------------------- 1 file changed, 125 insertions(+), 127 deletions(-) diff --git a/src/copy.ts b/src/copy.ts index 94b3975..b509d16 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -128,7 +128,7 @@ copy_cmd.action(async () => { await targetAccount.init(); await Promise.all([sourceAccount.getData(), targetAccount.getData()]); - if (isReset) { + if (!isReset) { const hasChanges = await diff( sourceAccountAlias, targetAccountAlias, @@ -141,137 +141,135 @@ copy_cmd.action(async () => { ); return; } + } - inquirer - .prompt([ - { - type: 'confirm', - name: 'continueReset', - message: - 'Do you want to continue to reset the target GTM account and copy all entities from the source GTM account?', - default: false, - }, - ]) - .then(async answers => { - if (answers.continueReset) { - console.log( - 'Resetting target GTM account and copying entities from source GTM account...' - .gray - ); - await targetAccount.reset(); - const responses = await targetAccount.copyDataFromAccount( - sourceAccount - ); + inquirer + .prompt([ + { + type: 'confirm', + name: 'continueReset', + message: + 'Do you want to continue to reset the target GTM account and copy all entities from the source GTM account?', + default: false, + }, + ]) + .then(async answers => { + if (answers.continueReset) { + console.log( + 'Resetting target GTM account and copying entities from source GTM account...' + .gray + ); + await targetAccount.reset(); + const responses = await targetAccount.copyDataFromAccount( + sourceAccount + ); - const variablesTable = new Table({ - head: [ - 'Variable ID', - 'Name', - 'Type', - 'πŸ‘‰ Copy Status', - '✨ New Variable ID', - ], - }); - responses.variables.forEach(val => { - const sourceVariable = sourceAccount.variables.get( - val.sourceVariableId - ); - variablesTable.push([ - val.sourceVariableId as string, - sourceVariable?.name as string, - sourceVariable?.type as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetVariableId === undefined ? '' : val.targetVariableId, - ]); - }); - console.log( - '==> Variables'.blue, - `(${sourceAccount.variables.size} variables)` + const variablesTable = new Table({ + head: [ + 'Variable ID', + 'Name', + 'Type', + 'πŸ‘‰ Copy Status', + '✨ New Variable ID', + ], + }); + responses.variables.forEach(val => { + const sourceVariable = sourceAccount.variables.get( + val.sourceVariableId ); - console.log(variablesTable.toString()); - console.log('\n'); + variablesTable.push([ + val.sourceVariableId as string, + sourceVariable?.name as string, + sourceVariable?.type as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetVariableId === undefined ? '' : val.targetVariableId, + ]); + }); + console.log( + '==> Variables'.blue, + `(${sourceAccount.variables.size} variables)` + ); + console.log(variablesTable.toString()); + console.log('\n'); - const triggersTable = new Table({ - head: [ - 'Trigger ID', - 'Name', - 'Type', - 'πŸ‘‰ Copy Status', - '✨New Trigger ID', - ], - }); - responses.triggers.forEach(val => { - const sourceTrigger = sourceAccount.triggers.get( - val.sourceTriggerId - ); - triggersTable.push([ - val.sourceTriggerId as string, - sourceTrigger?.name as string, - sourceTrigger?.type as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetTriggerId === undefined ? '' : val.targetTriggerId, - ]); - }); - console.log( - '==> Triggers'.blue, - `(${sourceAccount.triggers.size} triggers)` - ); - console.log(triggersTable.toString()); - console.log('\n'); + const triggersTable = new Table({ + head: [ + 'Trigger ID', + 'Name', + 'Type', + 'πŸ‘‰ Copy Status', + '✨New Trigger ID', + ], + }); + responses.triggers.forEach(val => { + const sourceTrigger = sourceAccount.triggers.get(val.sourceTriggerId); + triggersTable.push([ + val.sourceTriggerId as string, + sourceTrigger?.name as string, + sourceTrigger?.type as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetTriggerId === undefined ? '' : val.targetTriggerId, + ]); + }); + console.log( + '==> Triggers'.blue, + `(${sourceAccount.triggers.size} triggers)` + ); + console.log(triggersTable.toString()); + console.log('\n'); - const tagsTable = new Table({ - head: [ - 'Tag ID', - 'Name', - 'Type', - 'Firing Triggers (Trigger ID)', - 'πŸ‘‰ Copy Status', - '✨ New Tag ID', - '✨ New Firing Triggers (New Trigger ID)', - ], - }); - responses.tags.forEach(val => { - const sourceTag = sourceAccount.tags.get(val.sourceTagId); - tagsTable.push([ - val.sourceTagId as string, - sourceTag?.name as string, - sourceTag?.type as string, - (sourceTag?.firingTriggerId - ?.map( - x => - `${sourceAccount.triggers?.get(x)?.name} (${ - sourceAccount.triggers?.get(x)?.triggerId - })` - ) - .join(', ') ?? '') as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetTagId === undefined ? '' : val.targetTagId, - (targetAccount.tags - .get(val.targetTagId) - ?.firingTriggerId?.map( - x => - `${targetAccount.triggers?.get(x)?.name} (${ - targetAccount.triggers?.get(x)?.triggerId - })` - ) - .join(', ') ?? '') as string, - ]); - }); - console.log('==> Tags'.blue, `(${sourceAccount.tags.size} tags)`); - console.log(tagsTable.toString()); - console.log('\n'); - } - }) - .catch(error => { - console.log(error); - }); - } + const tagsTable = new Table({ + head: [ + 'Tag ID', + 'Name', + 'Type', + 'Firing Triggers (Trigger ID)', + 'πŸ‘‰ Copy Status', + '✨ New Tag ID', + '✨ New Firing Triggers (New Trigger ID)', + ], + }); + responses.tags.forEach(val => { + const sourceTag = sourceAccount.tags.get(val.sourceTagId); + tagsTable.push([ + val.sourceTagId as string, + sourceTag?.name as string, + sourceTag?.type as string, + (sourceTag?.firingTriggerId + ?.map( + x => + `${sourceAccount.triggers?.get(x)?.name} (${ + sourceAccount.triggers?.get(x)?.triggerId + })` + ) + .join(', ') ?? '') as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetTagId === undefined ? '' : val.targetTagId, + (targetAccount.tags + .get(val.targetTagId) + ?.firingTriggerId?.map( + x => + `${targetAccount.triggers?.get(x)?.name} (${ + targetAccount.triggers?.get(x)?.triggerId + })` + ) + .join(', ') ?? '') as string, + ]); + }); + console.log('==> Tags'.blue, `(${sourceAccount.tags.size} tags)`); + console.log(tagsTable.toString()); + console.log('\n'); + } + }) + .catch(error => { + console.log(error); + }); }); export {copy_cmd}; From 5b265e61b6c89676e2e6bdd60e05601c21f3fde2 Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 22:48:14 +0530 Subject: [PATCH 4/6] feat(copy): implement --yes option in copy command for automation --- src/copy.ts | 243 ++++++++++++++++++++++++++-------------------------- 1 file changed, 120 insertions(+), 123 deletions(-) diff --git a/src/copy.ts b/src/copy.ts index b509d16..d3735a5 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -63,6 +63,10 @@ copy_cmd.option( '-r, --reset', 'Reset the target GTM account and copy all entities from the source account' ); +copy_cmd.option( + '-y, --yes', + 'Answer yes (y) for all prompts (useful for automation)' +); copy_cmd.action(async () => { try { @@ -96,6 +100,7 @@ copy_cmd.action(async () => { let targetContainerId: string = copy_cmd.opts().targetContainer; let targetWorkspaceId: string = copy_cmd.opts().targetWorkspace; const isReset: boolean = copy_cmd.opts().reset; + const yes: boolean = copy_cmd.opts().yes; if (sourceAccountAlias !== undefined) { const config = new Config(); @@ -143,133 +148,125 @@ copy_cmd.action(async () => { } } - inquirer - .prompt([ - { - type: 'confirm', - name: 'continueReset', - message: - 'Do you want to continue to reset the target GTM account and copy all entities from the source GTM account?', - default: false, - }, - ]) - .then(async answers => { - if (answers.continueReset) { - console.log( - 'Resetting target GTM account and copying entities from source GTM account...' - .gray - ); - await targetAccount.reset(); - const responses = await targetAccount.copyDataFromAccount( - sourceAccount - ); + const answers = await inquirer.prompt([ + { + type: 'confirm', + name: 'continueReset', + message: + 'Do you want to continue to reset the target GTM account and copy all entities from the source GTM account?', + default: false, + when: !yes, + }, + ]); + + if (yes || answers.continueReset) { + console.log( + 'Resetting target GTM account and copying entities from source GTM account...' + .gray + ); + await targetAccount.reset(); + const responses = await targetAccount.copyDataFromAccount(sourceAccount); - const variablesTable = new Table({ - head: [ - 'Variable ID', - 'Name', - 'Type', - 'πŸ‘‰ Copy Status', - '✨ New Variable ID', - ], - }); - responses.variables.forEach(val => { - const sourceVariable = sourceAccount.variables.get( - val.sourceVariableId - ); - variablesTable.push([ - val.sourceVariableId as string, - sourceVariable?.name as string, - sourceVariable?.type as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetVariableId === undefined ? '' : val.targetVariableId, - ]); - }); - console.log( - '==> Variables'.blue, - `(${sourceAccount.variables.size} variables)` - ); - console.log(variablesTable.toString()); - console.log('\n'); + const variablesTable = new Table({ + head: [ + 'Variable ID', + 'Name', + 'Type', + 'πŸ‘‰ Copy Status', + '✨ New Variable ID', + ], + }); + responses.variables.forEach(val => { + const sourceVariable = sourceAccount.variables.get(val.sourceVariableId); + variablesTable.push([ + val.sourceVariableId as string, + sourceVariable?.name as string, + sourceVariable?.type as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetVariableId === undefined ? '' : val.targetVariableId, + ]); + }); + console.log( + '==> Variables'.blue, + `(${sourceAccount.variables.size} variables)` + ); + console.log(variablesTable.toString()); + console.log('\n'); - const triggersTable = new Table({ - head: [ - 'Trigger ID', - 'Name', - 'Type', - 'πŸ‘‰ Copy Status', - '✨New Trigger ID', - ], - }); - responses.triggers.forEach(val => { - const sourceTrigger = sourceAccount.triggers.get(val.sourceTriggerId); - triggersTable.push([ - val.sourceTriggerId as string, - sourceTrigger?.name as string, - sourceTrigger?.type as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetTriggerId === undefined ? '' : val.targetTriggerId, - ]); - }); - console.log( - '==> Triggers'.blue, - `(${sourceAccount.triggers.size} triggers)` - ); - console.log(triggersTable.toString()); - console.log('\n'); + const triggersTable = new Table({ + head: [ + 'Trigger ID', + 'Name', + 'Type', + 'πŸ‘‰ Copy Status', + '✨New Trigger ID', + ], + }); + responses.triggers.forEach(val => { + const sourceTrigger = sourceAccount.triggers.get(val.sourceTriggerId); + triggersTable.push([ + val.sourceTriggerId as string, + sourceTrigger?.name as string, + sourceTrigger?.type as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetTriggerId === undefined ? '' : val.targetTriggerId, + ]); + }); + console.log( + '==> Triggers'.blue, + `(${sourceAccount.triggers.size} triggers)` + ); + console.log(triggersTable.toString()); + console.log('\n'); - const tagsTable = new Table({ - head: [ - 'Tag ID', - 'Name', - 'Type', - 'Firing Triggers (Trigger ID)', - 'πŸ‘‰ Copy Status', - '✨ New Tag ID', - '✨ New Firing Triggers (New Trigger ID)', - ], - }); - responses.tags.forEach(val => { - const sourceTag = sourceAccount.tags.get(val.sourceTagId); - tagsTable.push([ - val.sourceTagId as string, - sourceTag?.name as string, - sourceTag?.type as string, - (sourceTag?.firingTriggerId - ?.map( - x => - `${sourceAccount.triggers?.get(x)?.name} (${ - sourceAccount.triggers?.get(x)?.triggerId - })` - ) - .join(', ') ?? '') as string, - val.response.error === undefined - ? 'βœ… Copy Successful' - : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, - val.targetTagId === undefined ? '' : val.targetTagId, - (targetAccount.tags - .get(val.targetTagId) - ?.firingTriggerId?.map( - x => - `${targetAccount.triggers?.get(x)?.name} (${ - targetAccount.triggers?.get(x)?.triggerId - })` - ) - .join(', ') ?? '') as string, - ]); - }); - console.log('==> Tags'.blue, `(${sourceAccount.tags.size} tags)`); - console.log(tagsTable.toString()); - console.log('\n'); - } - }) - .catch(error => { - console.log(error); + const tagsTable = new Table({ + head: [ + 'Tag ID', + 'Name', + 'Type', + 'Firing Triggers (Trigger ID)', + 'πŸ‘‰ Copy Status', + '✨ New Tag ID', + '✨ New Firing Triggers (New Trigger ID)', + ], + }); + responses.tags.forEach(val => { + const sourceTag = sourceAccount.tags.get(val.sourceTagId); + tagsTable.push([ + val.sourceTagId as string, + sourceTag?.name as string, + sourceTag?.type as string, + (sourceTag?.firingTriggerId + ?.map( + x => + `${sourceAccount.triggers?.get(x)?.name} (${ + sourceAccount.triggers?.get(x)?.triggerId + })` + ) + .join(', ') ?? '') as string, + val.response.error === undefined + ? 'βœ… Copy Successful' + : `❌ Copy Failed \n\n${val.response.error?.message ?? ''}`, + val.targetTagId === undefined ? '' : val.targetTagId, + (targetAccount.tags + .get(val.targetTagId) + ?.firingTriggerId?.map( + x => + `${targetAccount.triggers?.get(x)?.name} (${ + targetAccount.triggers?.get(x)?.triggerId + })` + ) + .join(', ') ?? '') as string, + ]); }); + console.log('==> Tags'.blue, `(${sourceAccount.tags.size} tags)`); + console.log(tagsTable.toString()); + console.log('\n'); + } }); export {copy_cmd}; From 749c4f263a319a8d4667d9793d47b5aa23b88776 Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 22:56:55 +0530 Subject: [PATCH 5/6] feat(reset): add --yes option for automation --- src/reset.ts | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/reset.ts b/src/reset.ts index f5dd617..bb93f4b 100644 --- a/src/reset.ts +++ b/src/reset.ts @@ -64,6 +64,10 @@ const resetCmdOptions = { ], }; reset_cmd.addOption(resetCmdOptions.primaryOption); +reset_cmd.option( + '-y, --yes', + 'Answer yes (y) for all prompts (useful for automation)' +); resetCmdOptions.conflictingOptions.forEach(op => reset_cmd.addOption(op)); reset_cmd.action(async () => { @@ -77,6 +81,7 @@ reset_cmd.action(async () => { let accountId: string = reset_cmd.opts().account; let containerId: string = reset_cmd.opts().container; let workspaceId: string = reset_cmd.opts().workspace; + const yes: boolean = reset_cmd.opts().yes; let isResettable = true; if (accountAlias !== undefined) { @@ -112,25 +117,21 @@ reset_cmd.action(async () => { return; } - inquirer - .prompt([ - { - type: 'confirm', - name: 'continueReset', - message: 'Do you want to continue to reset the this GTM account?', - default: false, - }, - ]) - .then(async answers => { - if (answers.continueReset) { - console.log('Resetting GTM account...'.gray); - await account.reset(); - console.log('Resetting GTM account complete'.green); - } - }) - .catch(error => { - console.log(error); - }); + const answers = await inquirer.prompt([ + { + type: 'confirm', + name: 'continueReset', + message: 'Do you want to continue to reset the this GTM account?', + default: false, + when: !yes, + }, + ]); + + if (yes || answers.continueReset) { + console.log('Resetting GTM account...'.gray); + await account.reset(); + console.log('Resetting GTM account complete'.green); + } }); export {reset_cmd, reset}; From c792bad0f3e4f6becd862554619f57264a9a4d2f Mon Sep 17 00:00:00 2001 From: Vaidik Kapoor Date: Wed, 7 Jun 2023 23:07:01 +0530 Subject: [PATCH 6/6] feat(copy): add log for resetting when there is no data in the target account --- src/copy.ts | 15 +++++++++++---- src/core.ts | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/copy.ts b/src/copy.ts index d3735a5..6e5daac 100644 --- a/src/copy.ts +++ b/src/copy.ts @@ -160,10 +160,17 @@ copy_cmd.action(async () => { ]); if (yes || answers.continueReset) { - console.log( - 'Resetting target GTM account and copying entities from source GTM account...' - .gray - ); + if (targetAccount.isEmpty()) { + console.log( + 'There is no data in the target GTM account to reset. Continuing to copy entities from source GTM account...' + .yellow + ); + } else { + console.log( + 'Resetting target GTM account and copying entities from source GTM account...' + .gray + ); + } await targetAccount.reset(); const responses = await targetAccount.copyDataFromAccount(sourceAccount); diff --git a/src/core.ts b/src/core.ts index 48c762c..0ef249a 100644 --- a/src/core.ts +++ b/src/core.ts @@ -126,6 +126,10 @@ export class TagManagerData { }); } + isEmpty(): boolean { + return !(this.variables.size || this.triggers.size || this.tags.size); + } + async copyVariable( val: tagmanager_v2.Schema$Variable ): Promise> {