From 1ae643b306e56233acf9336fd2785cbb28709414 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Wed, 16 Nov 2022 22:28:17 +0200 Subject: [PATCH] Remove duplicated code. --- platforms/hubspot/hubspot-email-adapter.js | 72 ++++++++++++---------- platforms/hubspot/hubspot-page-adapter.js | 53 ++++------------ 2 files changed, 52 insertions(+), 73 deletions(-) diff --git a/platforms/hubspot/hubspot-email-adapter.js b/platforms/hubspot/hubspot-email-adapter.js index 9646851..4136b7e 100644 --- a/platforms/hubspot/hubspot-email-adapter.js +++ b/platforms/hubspot/hubspot-email-adapter.js @@ -1,48 +1,27 @@ +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 = `./exports/hubspot/${blockName}.module`; - await mkdir(distPath, {recursive: true}) + const distPath = await createDistFolder(blockName); + await copyFile(`${projectPath}/src/${blockName}.template.hbs`, `${distPath}/module.html`) - const metaData = { + await buildHubspotJSONFiles(distPath, { global: false, host_template_types: ["EMAIL"], label: capitalize(blockName), is_available_for_new_content: true - } - - await writeFile(`${distPath}/meta.json`, JSON.stringify(metaData, null, 4)); - - const blockJSON = await readFile(`${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 async function createDistFolder(blockName) { + const distPath = path.join('exports', 'hubspot', `${blockName}.module`); + await mkdir(distPath, {recursive: true}) - // Export JSON file. - await writeFile(`${distPath}/fields.json`, JSON.stringify(fields, null, 4)); + return distPath; } export function getBlockFields(block = {}, type = 'content') { @@ -231,3 +210,34 @@ export function convertToHubspotField(field = {}) { }); } } + +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)); +} diff --git a/platforms/hubspot/hubspot-page-adapter.js b/platforms/hubspot/hubspot-page-adapter.js index 60b371c..3667abf 100644 --- a/platforms/hubspot/hubspot-page-adapter.js +++ b/platforms/hubspot/hubspot-page-adapter.js @@ -1,56 +1,25 @@ import path from "path"; -import {readFile, writeFile, mkdir, copyFile} from "fs/promises"; +import {copyFile} from "fs/promises"; import {copy} from "fs-extra"; import {capitalize, getConfigs} from "../../helpers.js"; -import {convertToHubspotField, getBlockFields} from "./hubspot-email-adapter.js"; +import {buildHubspotJSONFiles, createDistFolder,} from "./hubspot-email-adapter.js"; const {modulesPath, projectPath} = getConfigs(); export async function buildHubspotPage(blockName) { - const distPath = `./exports/hubspot/${blockName}.module`; - await mkdir(distPath, {recursive: true}) - await copyFile(`${projectPath}/src/${blockName}.template.hbs`, `${distPath}/module.html`) - await copyFile(`${projectPath}/src/styles/${blockName}.min.css`, `${distPath}/module.css`) - await copyFile(`${projectPath}/src/scripts/${blockName}.min.js`, `${distPath}/module.js`) + const distPath = await createDistFolder(blockName); - await copy( - path.join(projectPath, 'src', 'images'), - path.join(distPath, 'images'), - ); + const srcPath = path.join(projectPath, 'src'); + await copyFile(path.join(srcPath, `${blockName}.template.hbs`), path.join(distPath, 'module.html')); + await copyFile(path.join(srcPath, 'styles', `${blockName}.min.css`), path.join(distPath, 'module.css')); + await copyFile(path.join(srcPath, 'scripts', `${blockName}.min.js`), path.join(distPath, 'module.js')); + await copy(path.join(projectPath, 'src', 'images'), path.join(distPath, 'images')); - const metaData = { + await buildHubspotJSONFiles(distPath, { global: false, host_template_types: ["PAGE"], label: capitalize(blockName), is_available_for_new_content: true - } - - await writeFile(`${distPath}/meta.json`, JSON.stringify(metaData, null, 4)); - - const blockJSON = await readFile(`${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(`${distPath}/fields.json`, JSON.stringify(fields, null, 4)); + }); } +