-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
232 lines (169 loc) · 5.93 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const path = require('path');
const fs = require('fs-extra');
const prettier = require('prettier');
const chalk = require('chalk');
const { program } = require('commander');
const HubSpotClient = require('./lib/hubspot-client');
const MissingTemplates = require('./lib/missing-templates');
const Mappings = require('./lib/mappings');
const { getIdFromPath, filterKeys, getCwd } = require('./lib/utils');
const SOURCE_PORTAL_HAPIKEY = '';
const DEST_PORTAL_HAPIKEY = '';
const log = console.log;
// SOURCE - enter HAPIKEY here
const hs = new HubSpotClient(SOURCE_PORTAL_HAPIKEY);
// DESTINATION - enter HAPIKEY here
const hsDestinationAPI = new HubSpotClient(DEST_PORTAL_HAPIKEY);
const missingTemplatesInstance = new MissingTemplates(hsDestinationAPI);
const mappingsInstance = new Mappings();
/**
* Only allow certain keys
* @param {*} obj
* @param {*} allowList
*/
const allowedKeys = [
'id', 'campaign', 'campaign_name', 'footer_html', 'head_html', 'isDraft', 'meta_description',
'keywords', 'name', 'password', 'publishDate', 'publish_immediately', 'slug', 'subcategory', 'widgetContainers',
'widgets', 'templatePath', 'meta'
];
// i am lazy
async function getPages(ids) {
const pages = await hs.getPages();
if (typeof ids !== 'undefined' && ids.length > 0) {
const objects = pages.objects;
const filtered = objects.filter((page) => {
return ids.indexOf(page.id.toString()) !== -1;
})
return filtered;
}
return pages.objects;
}
async function downloadContentPayload(pageId, pageData, dest) {
dest = path.resolve(getCwd(), 'content/src/', `${pageId}.content.json`);
const contentToWrite = JSON.stringify(pageData);
const contentJson = prettier.format(contentToWrite, {
parser: 'json',
});
await fs.outputFile(dest, contentJson);
return { filePath: dest };
}
async function downloadAllPages(ids) {
const pages = await getPages(ids);
for (let i=0;i<pages.length;i++) {
let page = pages[i];
const filtered = filterKeys(page, allowedKeys);
await downloadContentPayload(page.id, filtered)
}
return pages;
}
async function uploadSinglePage(sourcePageId) {
const src = path.resolve(getCwd(), 'content/src/', `${sourcePageId}.content.json`);
const data = await fs.readFile(src);
const json = JSON.parse(data);
const srcId = json.id;
console.group('Uploading: ' + json.name + '/' + json.id);
//console.log('Content processed for page: ' + json.name + ' / ' + json.id);
let pageMapping = mappingsInstance.getPageMapping(srcId);
// check dest id exists.
if (pageMapping) {
// console.log('Overriding... existing');
log(chalk.blue('Page already exists on dest according to mappings.json, so updating.'));
delete json.slug;
json.id = pageMapping;
} else {
delete json.id;
log(chalk.green('This is a new page. Creating on dest.'));
}
const generatedTemplatePath = await getTemplateInfo(json.templatePath);
if (generatedTemplatePath) {
json.templatePath = generatedTemplatePath;
}
const isMissingTemplateOnDest = missingTemplatesInstance.checkIsMissing(json);
// We can't check for generated layouts.
if (isMissingTemplateOnDest && !generatedTemplatePath) {
let last = missingTemplatesInstance.getLast();
log(chalk.red(`Template ${last.template} missing on dest, skipping.`))
console.groupEnd();
return false;
}
try {
let res = await hsDestinationAPI.createOrUpdatePage(json);
mappingsInstance.addPageMapping(srcId, res.id);
console.log('Content uploaded for page: ' + json.name + ' / ');
log(chalk.green('Successful upload. Page ID on dest: ' + res.id));
} catch (err) {
console.log(JSON.stringify(err))
console.log('error', err);
}
console.groupEnd();
}
async function uploadAllPages(filter) {
const src = path.resolve(getCwd(), 'content/src/');
const files = await fs.readdir(src);
const filtered = files.filter(file => {
return path.extname(file) == '.json' && file !== 'mappings.content.json';
});
for (let i=0;i<filtered.length;i++) {
await uploadSinglePage(getIdFromPath(filtered[i]));
}
}
const CACHE_TEMPLATES = null;
async function getNewTemplateIdByPath(path) {
let templates = CACHE_TEMPLATES;
if (!CACHE_TEMPLATES) {
const destTemplates = await hsDestinationAPI.getLayouts();
templates = destTemplates.objects;
}
for (let i=0;i<templates.length;i++) {
if (templates[i].path == path) {
return templates[i].generatedTemplatePath;
}
}
// :/
return false;
}
async function getTemplateInfo(strPath) {
const id = getIdFromPath(strPath);
if (!id) {
return false;
}
let template;
try {
template = await hs.getLayout(id);
} catch(err) {
return false;
}
if (!template) {
return false;
}
const generatedPath = await getNewTemplateIdByPath(template.path);
return generatedPath;
}
async function handler(args) {
await mappingsInstance.init();
await missingTemplatesInstance.init()
if (args.ids) {
const ids = args.ids;
let split = ids.split(',');
if (!split.length) {
console.log('no ids provided');
return;
}
split = split.map((id) => id.trim());
let pagesProcessed = await downloadAllPages(split);
for (let i=0;i<pagesProcessed.length;i++) {
await uploadSinglePage(pagesProcessed[i].id);
}
}
if (args.all) {
await downloadAllPages();
await uploadAllPages();
}
await mappingsInstance.flush();
}
program
.command('sync')
.option('-i, --ids [value]', 'List of page ids from source portal')
.option('--all', 'you crazy, sync all')
.action(handler);
program.parse(process.argv);