|
|
|
@ -1,24 +1,28 @@ |
|
|
|
import path from "path"; |
|
|
|
import {mkdir, copyFile} from "fs/promises"; |
|
|
|
import {mkdir, copyFile, readFile} from "fs/promises"; |
|
|
|
import {capitalize, createFiles, getBlockName, getConfigs, readJSONFile} from "../../helpers.js"; |
|
|
|
import {fileURLToPath} from 'url'; |
|
|
|
import {copy} from "fs-extra"; |
|
|
|
import fsExtra 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 {projectPath} = getConfigs(); |
|
|
|
const {modulesPath, projectPath} = getConfigs(); |
|
|
|
|
|
|
|
export async function buildWordPress(isBlock = false) { |
|
|
|
export async function buildWordPress(blockName, isBlock = false) { |
|
|
|
const distPath = path.join(projectPath, 'exports', 'wordpress'); |
|
|
|
await mkdir(distPath, {recursive: true}) |
|
|
|
// await mkdir(distPath, {recursive: true})
|
|
|
|
await mkdir(path.join(distPath, 'templates'), {recursive: true}) |
|
|
|
|
|
|
|
const jsonFilePath = path.join(projectPath, 'block.json'); |
|
|
|
await copyFile(jsonFilePath, `${distPath}/block.json`) |
|
|
|
const blockFilePath = path.join(projectPath, 'block.json'); |
|
|
|
await copyFile(blockFilePath, path.join(distPath, 'block.json')); |
|
|
|
|
|
|
|
let data = await readJSONFile(jsonFilePath); |
|
|
|
let data = await readJSONFile(blockFilePath); |
|
|
|
Object.assign(data, getBlockName(data.name)); |
|
|
|
|
|
|
|
// let data = await readJSONFile(path.join(projectPath, `block.json`));
|
|
|
|
const title = capitalize(data.name); |
|
|
|
const owner = capitalize(data.project); |
|
|
|
|
|
|
|
@ -37,6 +41,39 @@ export async function buildWordPress(isBlock = false) { |
|
|
|
include_elementor_widget: false, |
|
|
|
}); |
|
|
|
|
|
|
|
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 (isBlock) { |
|
|
|
await createFiles(data, [{ |
|
|
|
from: `templates/Template_Component.php`, |
|
|
|
@ -53,14 +90,6 @@ export async function buildWordPress(isBlock = false) { |
|
|
|
pathDist: distPath, |
|
|
|
generatorsPath: path.join(__dirname) |
|
|
|
}); |
|
|
|
|
|
|
|
await createFiles(data, [{ |
|
|
|
from: `templates/helpers/Template_Defaults.php`, |
|
|
|
to: `helpers/${data.blockClassModel}_Defaults.php`, |
|
|
|
}], { |
|
|
|
pathDist: distPath, |
|
|
|
generatorsPath: path.join(__dirname) |
|
|
|
}); |
|
|
|
} else { |
|
|
|
await createFiles(data, [{ |
|
|
|
from: `templates/Template_Basic_Component.php`, |
|
|
|
@ -71,3 +100,44 @@ export async function buildWordPress(isBlock = false) { |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
|