import path from 'path'; import {BLOCK_NAME, IS_DEV, MODULE_PATH, PROJECT_PATH} from "./env.js"; import config from 'config'; import {fileURLToPath} from 'url'; import memFs from 'mem-fs'; import editor from 'mem-fs-editor'; import fsExtra from "fs-extra"; import archiver from "archiver"; import {buildWordPress} from "./platforms/wordpress/wordpress-adapter.js"; import {buildHubspotEmail} from "./platforms/hubspot/hubspot-email-adapter.js"; import {buildHubspotPage} from "./platforms/hubspot/hubspot-page-adapter.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export function getConfigs() { return { isDev: IS_DEV, developmentBlockName: BLOCK_NAME, modulesPath: MODULE_PATH, projectPath: PROJECT_PATH, }; } export async function readJSONFile(jsonFile) { let data = {}; try { data = await fsExtra.readJson(jsonFile); } catch (e) { return { error: true, errorMessage: getErrorHtml("JSON Syntax error. Please make sure the dataFile is valid.", e), }; } return data; } function getErrorHtml(message = '', errorMessage = '') { return `

${message}

${errorMessage}
`; } export async function getBlockConfigs(jsonFileName = 'default', {includeConfigs, projectPath, modulesPath, dataFiles} = {}) { let data = await readJSONFile(path.join(projectPath, 'data', `${jsonFileName}.json`)); if (data.error) { return data; } if (includeConfigs) { Object.assign(data, { config: Object.assign(JSON.parse(JSON.stringify(config)), // The entire config object. { projectDir: modulesPath, activeDataFile: jsonFileName, dataFiles: dataFiles.map((name) => { return { name, active: jsonFileName === name, }; }), remToPx: config.has('remToPx') ? config.get('remToPx') : 16, }) }); } return data; } export function getBlockName(name = '') { if (name.startsWith('@')) { name = name.substring(1); } const arr = name.split('/'); return { project: arr[0], name: arr[1], }; } export async function createFiles(data, files = [], {pathDist, generatorsPath}) { generatorsPath = generatorsPath ?? path.join(__dirname, 'generators/block/templates'); const store = memFs.create(); const filesystem = editor.create(store); for (let file of files) { const from = typeof file !== 'string' ? `${generatorsPath}/${file.from}` : `${generatorsPath}/${file}`; const to = typeof file !== 'string' ? `${pathDist}/${file.to}` : `${pathDist}/${file}`; await filesystem.copyTplAsync(from, to, data); } return filesystem.commit(); // Promise } export function capitalize(str) { if (typeof str !== 'string') { return ''; } return str .toLowerCase() .split(/[ -_]/g) .filter((word) => !!word) .map((word) => { return word.charAt(0).toUpperCase() + word.slice(1); }) .join(' '); } export async function zipProject(srcDir, outputFileName = 'dist.zip') { // create a file to stream archive data to. const output = await fsExtra.createWriteStream(outputFileName); const archive = archiver('zip', {}); // listen for all archive data to be written // 'close' event is fired only when a file descriptor is involved output.on('close', function () { console.log(archive.pointer() + ' total bytes'); console.log('archiver has been finalized and the output file descriptor has closed.'); }); // This event is fired when the data source is drained no matter what was the data source. // It is not part of this library but rather from the NodeJS Stream API. // @see: https://nodejs.org/api/stream.html#stream_event_end output.on('end', function () { console.log('Data has been drained'); }); // good practice to catch warnings (ie stat failures and other non-blocking errors) archive.on('warning', function (err) { if (err.code === 'ENOENT') { // log warning } else { // throw error throw err; } }); // good practice to catch this error explicitly archive.on('error', function (err) { throw err; }); // pipe archive data to the file archive.pipe(output); // append files from a subdirectory, putting its contents at the root of archive archive.directory(srcDir, false); // finalize the archive (ie we are done appending files but streams have to finish yet) // 'close', 'end' or 'finish' may be fired right after calling this method so register to them beforehand await archive.finalize(); } export async function buildExportFiles(blockName, platform) { if (platform.name.startsWith('wordpress')) { if (platform.name === 'wordpress-acf-block') { await buildWordPress(blockName, {block: true}); } else if (platform.name === 'wordpress-elementor') { await buildWordPress(blockName, {elementor: true}); } else if (platform.name === 'wordpress-component-manager') { await buildWordPress(blockName, {componentManager: true}); } else { await buildWordPress(blockName); } } else if (platform.name === 'hubspot-email') { await buildHubspotEmail(blockName) } else if (platform.name === 'hubspot') { await buildHubspotPage(blockName) } }