From a1929fe33c3b4d08be9b4ca1f9d553a9760c13f2 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Sun, 13 Aug 2023 23:50:52 +0300 Subject: [PATCH] Support missing images folder. --- helpers.js | 8 ++++++++ platforms/hubspot/hubspot-email-adapter.js | 4 ++++ platforms/wordpress/wordpress-adapter.js | 17 ++++++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/helpers.js b/helpers.js index a48d634..1fc24cb 100644 --- a/helpers.js +++ b/helpers.js @@ -213,6 +213,14 @@ export async function uploadFile(filePath, uploadUrl, validator) { export async function getImagesList(rootFolder, subFolder = '') { const imagesPath = path.join(rootFolder, subFolder); + + try { + await fs.access(imagesPath); + } catch (err) { + // Folder doesn't exist. + return []; + } + const images = await fs.readdir(imagesPath); const imagesList = []; diff --git a/platforms/hubspot/hubspot-email-adapter.js b/platforms/hubspot/hubspot-email-adapter.js index b965372..adabd78 100644 --- a/platforms/hubspot/hubspot-email-adapter.js +++ b/platforms/hubspot/hubspot-email-adapter.js @@ -26,6 +26,10 @@ export async function createDistFolder(blockName, projectPath = '') { } export function getBlockFields(block = {}, type = 'content') { + if (!block['field_groups']) { + return []; + } + const fields_group = block['field_groups'].find((group) => group.name === type); const fields = []; diff --git a/platforms/wordpress/wordpress-adapter.js b/platforms/wordpress/wordpress-adapter.js index 9d8bb56..657c7f5 100644 --- a/platforms/wordpress/wordpress-adapter.js +++ b/platforms/wordpress/wordpress-adapter.js @@ -1,5 +1,5 @@ import path from "path"; -import {mkdir, copyFile} from "fs/promises"; +import fs, {mkdir, copyFile} from "fs/promises"; import {capitalize, createFiles, getBlockName, getConfigs, readJSONFile} from "../../helpers.js"; import {fileURLToPath} from 'url'; import {copy} from "fs-extra"; @@ -68,10 +68,17 @@ export async function buildWordPress(blockName, args = {}) { path.join(distPath, 'templates', 'scripts', `${data.blockFilename}.min.js`), ); - await copy( - path.join(projectPath, 'src', 'images'), - path.join(distPath, 'templates', 'images'), - ); + try { + const imagesPath = path.join(projectPath, 'src', 'images'); + await fs.access(imagesPath); + + await copy( + path.join(imagesPath), + path.join(distPath, 'templates', 'images'), + ); + } catch (err) { + // Folder doesn't exist. + } const phpDataObject = await execPHPFile(path.join(phpGeneratorPath, 'build.php'), 'jsonToPhp', { json: await readJSONFile(path.join(projectPath, 'data', 'default.json'), "utf8"),