-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
68 lines (56 loc) · 2.53 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Denomander from 'https://deno.land/x/denomander/mod.ts';
import ProgressBar from 'https://deno.land/x/[email protected]/mod.ts';
import { appLogger } from './lib/logger.ts';
import { EmailTemplatesBackup } from './features/email-templates-backup.ts';
import { BrazeMediaType } from './lib/types.ts';
import { EmailContentBlocksBackup } from './features/email-content-blocks-backup.ts';
import { BackupMediaType } from './features/backup-media-type.ts';
const program = new Denomander({
app_name: "Email templates backup",
app_description: "Simple tool to backup and update email templates and content block in Braze",
app_version: "1.0.0"
});
const contentBlocksBackup = new EmailContentBlocksBackup();
const templatesBackup = new EmailTemplatesBackup();
const BACKUP_MEDIA_TYPE = new Map<BrazeMediaType, BackupMediaType>([
[templatesBackup.mediaType, templatesBackup],
[contentBlocksBackup.mediaType, contentBlocksBackup]
]);
function isBrazeMediaType(commandParam: unknown): commandParam is BrazeMediaType {
const possibleValues: string[] = Object.values(BrazeMediaType);
return typeof commandParam === 'string' && possibleValues.includes(commandParam);
}
program
.command('backup [type]', 'Backup media type from Braze')
.option('-l, --list-only', 'Backup only list of objects and save as map')
.option('-i, --id', 'Specific id')
.action(async (params: any) => {
const type: unknown = params.type;
const listOnly = program['list-only'];
const itemId = program.id;
const possibleValues = Object.values(BrazeMediaType);
if (!!itemId && typeof itemId !== 'string') {
appLogger.error('Invalid argument "id". Target id expected.');
return;
}
if (isBrazeMediaType(type)) {
const backupMedia = BACKUP_MEDIA_TYPE.get(type);
appLogger.info(`Backup ${type} map`);
await backupMedia?.backupMap();
if (listOnly) { return; }
const total = await backupMedia?.getItemsCount();
const bar = new ProgressBar({ total });
let completed = 0;
if (itemId) {
await backupMedia?.backupId(itemId);
return;
}
appLogger.info(`Backing up all ${type} data`);
await backupMedia?.backupAll(() => {
bar.render(completed++);
});
} else {
appLogger.error(`Unknown media type: ${type}. Possible values: ${possibleValues.join()}`);
}
})
program.parse(Deno.args);