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.
 
 
 
 

171 lines
5.0 KiB

import path from "path";
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";
import {exec} from 'child_process';
import execPhp from "exec-php";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export async function buildWordPress(blockName, args = {}) {
const isBlock = args.platform === 'wordpress-acf-block';
const isElementor = args.platform === 'wordpress-elementor';
const isComponentManager = args.platform === 'wordpress-component-manager'
const {modulesPath, projectPath} = getConfigs();
const distPath = path.join(projectPath, 'exports', args.platform);
// await mkdir(distPath, {recursive: true})
await mkdir(path.join(distPath, 'templates'), {recursive: true})
const blockFilePath = path.join(projectPath, '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,
isComponentManager,
});
await copyFile(blockFilePath, path.join(distPath, data.blockFilename + '.block.json'));
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,
platform: args.platform
});
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`),
);
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"),
});
// 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) {
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) => {
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);
}