You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

270 lines
7.3 KiB

import path from "path";
import {readFile, writeFile, mkdir, copyFile} from "fs/promises";
import {capitalize, getConfigs} from "../../helpers.js";
export async function buildHubspotEmail(blockName) {
const distPath = await createDistFolder(blockName);
// Template
let handlebars = await readFile(path.join(srcPath, `${blockName}.template.hbs`), "utf8");
await writeFile(path.join(distPath, 'module.html'), handlebarsToHubl(handlebars));
// JSON
await buildHubspotJSONFiles(distPath, {
global: false,
host_template_types: ["EMAIL"],
label: capitalize(blockName),
is_available_for_new_content: true
});
}
export async function createDistFolder(blockName, projectPath = '') {
const distPath = path.join(projectPath, 'exports', 'hubspot', `${blockName}.module`);
await mkdir(distPath, {recursive: true})
return distPath;
}
export function getBlockFields(block = {}, type = 'content') {
const fields_group = block['field_groups'].find((group) => group.name === type);
const fields = [];
if (!fields_group) {
return fields;
}
Object.keys(fields_group['fields']).forEach((key) => {
const field = fields_group['fields'][key];
field['name'] = key;
fields.push(field);
});
return fields.map((field) => {
return convertToHubspotField(field);
});
}
export function convertToHubspotField(field = {}) {
const data = {
id: field.name,
name: field.name,
label: field.label,
display_width: null,
validation_regex: "",
required: false,
locked: false,
};
if (field.default) {
data.default = field.default;
}
let sub_fields = [];
switch (field.type) {
case 'text':
return Object.assign({}, data, {
type: "text",
allow_new_line: true,
show_emoji_picker: false,
});
case 'wysiwyg':
return Object.assign({}, data, {
type: "richtext"
});
case 'number':
return Object.assign({}, data, {
type: "number",
display: "text",
step: 1,
});
case 'range':
return Object.assign({}, data, {
type: "number",
display: "slider",
min: 0,
max: 100,
step: 3,
});
case 'boolean':
return Object.assign({}, data, {
type: "boolean",
display: "toggle",
});
case 'checkbox':
return Object.assign({}, data, {
type: "boolean",
display: "checkbox",
});
case 'select':
const options = [];
if (field.options) {
Object.keys(field.options).forEach(value => options.push([value, field.options[value]]));
}
return Object.assign({}, data, {
type: "choice",
choices: options
});
case 'link':
return Object.assign({}, data, {
type: "url",
supported_types: [
"EXTERNAL"
],
default: {
content_id: null,
href: "https://www.twitter.com/...",
type: "EXTERNAL"
}
});
case 'image':
return Object.assign({}, data, {
type: "image",
responsive: true,
resizable: false,
show_loading: false,
default: {
src: "",
alt: null,
loading: "lazy"
}
});
case 'file':
return Object.assign({}, data, {
type: "file",
picker: "file",
});
case 'stringList':
return Object.assign({}, data, {
type: "text",
occurrence: {
min: null,
max: null,
sorting_label_field: null,
default: null,
},
allow_new_line: false,
show_emoji_picker: false,
});
case 'gallery':
return Object.assign({}, data, {
type: "image",
occurrence: {
min: null,
max: null,
sorting_label_field: null,
default: null
},
responsive: true,
resizable: false,
show_loading: false,
default: {
src: "",
alt: null,
loading: "lazy"
}
});
case 'group':
field.sub_fields = field.sub_fields || {};
sub_fields = Object.keys(field.sub_fields).map(name => {
// const sub_field = Object.assign({}, field.sub_fields[name], {name: `${field.name}_${name}`});
const sub_field = Object.assign({}, field.sub_fields[name], {name});
return convertToHubspotField(sub_field);
})
return Object.assign({}, data, {
type: "group",
children: sub_fields,
tab: "CONTENT",
expanded: false,
default: {}
});
case 'repeater':
field.sub_fields = field.sub_fields || {};
sub_fields = Object.keys(field.sub_fields).map(name => {
const sub_field = Object.assign({}, field.sub_fields[name], {name});
return convertToHubspotField(sub_field);
})
return Object.assign({}, data, {
type: "group",
children: sub_fields,
occurrence: {
min: null,
max: null,
sorting_label_field: null,
default: null,
},
tab: "CONTENT",
expanded: false,
default: {}
});
// case 'YOUR_FIELD':
// return Object.assign({}, data, {});
default:
// type === 'string'
return Object.assign({}, data, {
type: "text",
allow_new_line: false,
show_emoji_picker: true,
});
}
}
export async function buildHubspotJSONFiles(distPath, metaData) {
const {modulesPath, projectPath} = getConfigs();
await writeFile(path.join(distPath, 'meta.json'), JSON.stringify(metaData, null, 4));
const blockJSON = await readFile(path.join(projectPath, 'block.json'), "utf8");
const block = JSON.parse(blockJSON);
const fields = getBlockFields(block, 'content');
// Styling TAB.
const stylingFields = getBlockFields(block, 'styling');
if (stylingFields.length) {
const stylingFieldsByName = {};
stylingFields.forEach(field => stylingFieldsByName[field.name] = field);
const stylingGroup = convertToHubspotField({
type: 'group',
name: 'style',
label: "Style",
});
stylingGroup.children = Object.values(stylingFieldsByName);
stylingGroup.tab = "STYLE";
fields.push(stylingGroup);
}
// Export JSON file.
await writeFile(path.join(distPath, 'fields.json'), JSON.stringify(fields, null, 4));
}
export function handlebarsToHubl(handlebars) {
handlebars = handlebars.replace(/{{ else }}/g, '{% else %}');
handlebars = handlebars.replace(/{{else}}/g, '{% else %}');
handlebars = handlebars.replace(/{{#if /g, '{% if module.');
handlebars = handlebars.replace(/{{\/if}}/g, '{% endif %}');
handlebars = handlebars.replace(/{{#each /g, '{% for item in module.');
handlebars = handlebars.replace(/{{\/each}}/g, '{% endfor %}');
handlebars = handlebars.replace(/{{base_url}}/g, '');
handlebars = handlebars.replace(/{esc_attr /g, '{');
handlebars = handlebars.replace(/{esc_url /g, '{');
handlebars = handlebars.replace(/{esc_html /g, '{');
handlebars = handlebars.replace(/{{{ /g, '{{');
handlebars = handlebars.replace(/ }}}/g, '}}');
handlebars = handlebars.replace(/{{{/g, '{{');
handlebars = handlebars.replace(/}}}/g, '}}');
handlebars = handlebars.replace(/{{/g, '{{module.');
handlebars = handlebars.replace(/{{module. /g, '{{ module.');
handlebars = handlebars.replace(/.url/g, '.src');
return handlebars;
}