import path from "path"; import {mkdir, copyFile} from "fs/promises"; import {capitalize, createFiles, getBlockName, getConfigs, readJSONFile} from "../../helpers.js"; import {fileURLToPath} from 'url'; import {copy} from "fs-extra"; import {exec} from 'child_process'; import execPhp from "exec-php"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const {modulesPath, projectPath} = getConfigs(); export async function buildWordPress(blockName, isBlock = false, isElementor = false) { const distPath = path.join(projectPath, 'exports', 'wordpress'); // await mkdir(distPath, {recursive: true}) await mkdir(path.join(distPath, 'templates'), {recursive: true}) const blockFilePath = path.join(projectPath, 'block.json'); await copyFile(blockFilePath, path.join(distPath, 'block.json')); let data = await readJSONFile(blockFilePath); Object.assign(data, getBlockName(data.name)); const title = capitalize(data.name); const owner = capitalize(data.project); data = Object.assign(data, { title, blockClassModel: title.replace(/ /ig, '_'), blockFilename: title.toLowerCase().replace(/ /ig, '-'), blockClassName: title.toLowerCase().replace(/ /ig, '_'), owner, ownerClass: owner.replace(/ /ig, '_'), ownerFilename: owner.toLowerCase().replace(/ /ig, '-'), templateFormat: 'php', include_acf_block: isBlock, include_native_gutenberg_block: false, include_script: true, include_elementor_widget: isElementor, isElementor, }); const backPath = modulesPath ? modulesPath.split('/').map(() => '..').join('/') : ''; const phpGeneratorPath = path.join(modulesPath, 'platforms', 'php'); await execCommand(`cd ${phpGeneratorPath} && composer install`); await execPHPFile(path.join(phpGeneratorPath, 'build.php'), 'build', {blockName, backPath, projectPath}); await copyStaticFile( path.join(projectPath, 'src', 'styles', `${data.blockFilename}.min.css`), path.join(distPath, 'templates', 'styles', `${data.blockFilename}.min.css`), ); await copyStaticFile( path.join(projectPath, 'src', 'scripts', `${data.blockFilename}.min.js`), path.join(distPath, 'templates', 'scripts', `${data.blockFilename}.min.js`), ); await copy( path.join(projectPath, 'src', 'images'), path.join(distPath, 'templates', 'images'), ); const phpDataObject = await execPHPFile(path.join(phpGeneratorPath, 'build.php'), 'jsonToPhp', { json: await readJSONFile(path.join(projectPath, 'data', 'default.json'), "utf8"), }); await createFiles(Object.assign({}, data, {defaultData: phpDataObject}), [{ from: `templates/helpers/Template_Defaults.php`, to: `helpers/${data.blockClassModel}_Defaults.php`, }], { pathDist: distPath, generatorsPath: path.join(__dirname), }); if(isElementor){ await createFiles(data, [{ from: `templates/helpers/Template_Elementor_Widget.php`, to: `helpers/${data.blockClassModel}_Elementor_Widget.php`, }], { pathDist: distPath, generatorsPath: path.join(__dirname) }); } if (isBlock) { await createFiles(data, [{ from: `templates/Template_Component.php`, to: `${data.blockClassModel}_Component.php`, }], { pathDist: distPath, generatorsPath: path.join(__dirname) }); await createFiles(data, [{ from: `templates/helpers/Template_API.php`, to: `helpers/${data.blockClassModel}_API.php`, }], { pathDist: distPath, generatorsPath: path.join(__dirname) }); } else { await createFiles(data, [{ from: `templates/Template_Basic_Component.php`, to: `${data.blockClassModel}_Component.php`, }], { pathDist: distPath, generatorsPath: path.join(__dirname) }); } } export function execCommand(cmd = '') { return new Promise((resolve, reject) => { exec(cmd, function (error, stdout) { if (error) { console.log('Error:', error) reject(error); } // console.log(stdout); resolve(); }); }); } function execPHPFile(file = '', functionName = '', args = {}) { return new Promise((resolve, reject) => { execPhp(file, 'php', (err, php, out) => { if (err) { console.error(out); return reject(err); } php[functionName.toLowerCase()](args, (err, res, out, print) => { if (err) { console.error(out); return reject(err); } return resolve(res); }) }) }); } async function copyStaticFile(from = '', to = '') { await mkdir(path.dirname(to), {recursive: true}) await copyFile(from, to); }