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.
243 lines
6.1 KiB
243 lines
6.1 KiB
import path from "path";
|
|
import {readFile, writeFile, mkdir, copyFile} from "fs/promises";
|
|
import {capitalize, getConfigs} from "../../helpers.js";
|
|
|
|
const {modulesPath, projectPath} = getConfigs();
|
|
|
|
export async function buildHubspotEmail(blockName) {
|
|
const distPath = await createDistFolder(blockName);
|
|
|
|
await copyFile(`${projectPath}/src/${blockName}.template.hbs`, `${distPath}/module.html`)
|
|
|
|
await buildHubspotJSONFiles(distPath, {
|
|
global: false,
|
|
host_template_types: ["EMAIL"],
|
|
label: capitalize(blockName),
|
|
is_available_for_new_content: true
|
|
});
|
|
}
|
|
|
|
export async function createDistFolder(blockName) {
|
|
const distPath = path.join('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 = [];
|
|
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) {
|
|
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));
|
|
}
|
|
|