Skip to content

Commit

Permalink
Merge pull request #12 from vaidik/copy-features
Browse files Browse the repository at this point in the history
Copy features
  • Loading branch information
vaidik authored Jun 7, 2023
2 parents 0765d09 + c792bad commit 178f78a
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 175 deletions.
274 changes: 144 additions & 130 deletions src/copy.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -126,139 +131,148 @@ copy_cmd.action(async () => {
true
);
await targetAccount.init();
await Promise.all([sourceAccount.getData(), targetAccount.getData()]);

if (!isReset) {
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;
}
}

if (isReset) {
await list(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.getData();
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,
},
]);

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');
if (yes || answers.continueReset) {
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);

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 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 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 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');
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CopyResponse<tagmanager_v2.Schema$Variable>> {
Expand Down
Loading

0 comments on commit 178f78a

Please sign in to comment.