Merge pull request 'excluding_yeo' (#2) from excluding_yeo into master
Reviewed-on: AXE-WEB/block-dev-tool#2
This commit is contained in:
@@ -1,74 +1,70 @@
|
||||
#!/usr/bin/env node
|
||||
// For development purposes - run `npm run build-platform`.
|
||||
|
||||
import {exec} from 'child_process';
|
||||
import config from 'config';
|
||||
import Generator from "yeoman-generator";
|
||||
import yeoman from 'yeoman-environment';
|
||||
import prompts from "prompts";
|
||||
import {buildHubspot} from "./platforms/hubspot/hubspot-adapter.js";
|
||||
import {getConfigs} from "./helpers.js";
|
||||
import {buildWordPress} from "./platforms/wordpress/wordpress-adapter.js";
|
||||
import path from "path";
|
||||
|
||||
const {modulesPath, projectPath} = getConfigs();
|
||||
const {isDev, developmentBlockName} = getConfigs();
|
||||
const blockName = !isDev && config.has('blockName') ? config.get('blockName') : developmentBlockName;
|
||||
|
||||
const blockName = config.has('blockName') ? config.get('blockName') : 'development';
|
||||
export const PLATFORM_OPTIONS = [{
|
||||
name: 'wordpress',
|
||||
title: 'WordPress'
|
||||
}, {
|
||||
name: 'wordpress-blocks',
|
||||
title: 'WordPress Block'
|
||||
}, {
|
||||
name: 'hubspot',
|
||||
title: 'Hubspot'
|
||||
}, {
|
||||
name: 'hubspot-email',
|
||||
title: 'Hubspot Email'
|
||||
}, {
|
||||
name: 'javascript',
|
||||
title: 'JavaScript'
|
||||
}, {
|
||||
name: 'php',
|
||||
title: 'PHP'
|
||||
}];
|
||||
|
||||
class buildGenerator extends Generator {
|
||||
async prompting() {
|
||||
this.data = await this.prompt([
|
||||
{
|
||||
type: "list",
|
||||
name: "platform",
|
||||
message: "Choose Platform",
|
||||
choices: ['WordPress', 'WordPress Block', 'Hubspot', 'Hubspot Email', 'JavaScript', 'PHP'],
|
||||
default: 'WordPress'
|
||||
}
|
||||
])
|
||||
}
|
||||
const data = await getExportData();
|
||||
const selectedPlatform = PLATFORM_OPTIONS[data['platform']];
|
||||
await buildExportFiles(selectedPlatform);
|
||||
|
||||
writing() {
|
||||
new Promise((resolve => {
|
||||
if (['WordPress', 'PHP'].includes(this.data.platform)) {
|
||||
const backPath = modulesPath ? modulesPath.split('/').map(() => '..').join('/') : '';
|
||||
console.log('--------------------\nDone!');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const phpGeneratorPath = path.join(modulesPath, 'platforms', 'php');
|
||||
exec(`cd ${phpGeneratorPath} && composer install && php build.php '${blockName}' '${backPath}' '${projectPath}'`, function (error, stdout) {
|
||||
if (error) {
|
||||
console.log('Error:', error)
|
||||
reject(error);
|
||||
}
|
||||
//
|
||||
// Functions
|
||||
//
|
||||
|
||||
console.log(stdout);
|
||||
resolve();
|
||||
});
|
||||
}).then(() => {
|
||||
if (this.data.platform === 'WordPress') {
|
||||
return buildWordPress();
|
||||
}
|
||||
export async function buildExportFiles(platform) {
|
||||
if (['wordpress', 'php'].includes(platform.name)) {
|
||||
|
||||
if (this.data.platform === 'WordPress Block') {
|
||||
return buildWordPress(true);
|
||||
}
|
||||
});
|
||||
|
||||
} else if (this.data.platform === 'Hubspot Email') {
|
||||
buildHubspot(blockName)
|
||||
.then(() => {
|
||||
resolve();
|
||||
});
|
||||
} else if (this.data.platform === 'Hubspot') {
|
||||
console.log('"Hubspot" Coming soon...');
|
||||
resolve();
|
||||
if (platform.name === 'wordpress') {
|
||||
await buildWordPress(blockName);
|
||||
} else {
|
||||
resolve();
|
||||
if (platform.name === 'wordpress-blocks') {
|
||||
await buildWordPress(blockName, true);
|
||||
}
|
||||
}))
|
||||
.then(() => {
|
||||
console.log('--------------------\nDone!');
|
||||
});
|
||||
}
|
||||
} else if (platform.name === 'hubspot-email') {
|
||||
await buildHubspot(blockName)
|
||||
} else if (platform.name === 'hubspot') {
|
||||
console.log('"Hubspot" Coming soon...');
|
||||
}
|
||||
}
|
||||
|
||||
const build = new buildGenerator([], {env: yeoman.createEnv()}, {});
|
||||
build.run().then(() => null);
|
||||
function getExportData() {
|
||||
return prompts([
|
||||
{
|
||||
type: "select",
|
||||
name: "platform",
|
||||
message: "Choose Platform",
|
||||
choices: PLATFORM_OPTIONS.map(item => item.title),
|
||||
default: 'WordPress'
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
+1
-2
@@ -3,6 +3,7 @@ import config from 'config';
|
||||
import {fileURLToPath} from 'url';
|
||||
import memFs from 'mem-fs';
|
||||
import editor from 'mem-fs-editor';
|
||||
import fsExtra from "fs-extra";
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
@@ -19,8 +20,6 @@ export function getConfigs() {
|
||||
};
|
||||
}
|
||||
|
||||
import fsExtra from "fs-extra";
|
||||
|
||||
export async function readJSONFile(jsonFile) {
|
||||
let data = {};
|
||||
|
||||
|
||||
Generated
+538
-5271
File diff suppressed because it is too large
Load Diff
+3
-3
@@ -22,6 +22,7 @@
|
||||
"browser-sync": "^2.27.9",
|
||||
"config": "^3.3.7",
|
||||
"escape-html": "^1.0.3",
|
||||
"exec-php": "^0.0.6",
|
||||
"express": "^4.17.3",
|
||||
"express-handlebars": "^6.0.4",
|
||||
"fs-extra": "^10.0.1",
|
||||
@@ -37,10 +38,9 @@
|
||||
"node-fetch": "^3.2.10",
|
||||
"open": "^8.4.0",
|
||||
"plugin-error": "^2.0.0",
|
||||
"prompts": "^2.4.2",
|
||||
"sanitize-html": "^2.7.1",
|
||||
"sass": "^1.50.1",
|
||||
"yeoman-environment": "^3.10.0",
|
||||
"yeoman-generator": "^5.6.1"
|
||||
"sass": "^1.50.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/preset-react": "^7.18.6",
|
||||
|
||||
+21
-14
@@ -3,8 +3,10 @@
|
||||
// Composer - Autoloader
|
||||
require_once __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
use Brick\VarExporter\ExportException;
|
||||
use LightnCandy\Flags;
|
||||
use LightnCandy\LightnCandy;
|
||||
use Brick\VarExporter\VarExporter;
|
||||
|
||||
trait Custom_Handlebars {
|
||||
public array $custom_handlebars = [];
|
||||
@@ -52,7 +54,8 @@ class Component_Builder {
|
||||
use Custom_Handlebars;
|
||||
|
||||
public string $component_name = '';
|
||||
private string $module_path = '';
|
||||
public string $module_path = '';
|
||||
public string $project_path = '';
|
||||
|
||||
function __construct( $component_name, $module_path, $project_path ) {
|
||||
$this->module_path = $module_path;
|
||||
@@ -63,22 +66,15 @@ class Component_Builder {
|
||||
|
||||
function build(): void {
|
||||
$root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path;
|
||||
|
||||
$this->buildTemplatePhpFile( $root_path );
|
||||
}
|
||||
|
||||
private function buildTemplatePhpFile( $root_path ) {
|
||||
$file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" );
|
||||
|
||||
$output_folder = $root_path . '/exports/wordpress/templates';
|
||||
foreach ( [ 'styles', 'scripts', 'images' ] as $dir ) {
|
||||
$output_dir = "$output_folder/$dir";
|
||||
if ( is_dir( $output_dir ) === false ) {
|
||||
mkdir( $output_dir, 0777, true );
|
||||
}
|
||||
}
|
||||
|
||||
rename( $file_name, "$output_folder/$this->component_name.template.php" );
|
||||
copy( "$root_path/src/styles/$this->component_name.min.css", "$output_folder/styles/$this->component_name.min.css" );
|
||||
copy( "$root_path/src/scripts/$this->component_name.min.js", "$output_folder/scripts/$this->component_name.min.js" );
|
||||
shell_exec( "cp -r $root_path/src/images $output_folder" );
|
||||
|
||||
echo "Generated '$file_name'.";
|
||||
}
|
||||
|
||||
private function get_handlebars_template( $path = '' ): string {
|
||||
@@ -123,4 +119,15 @@ class Component_Builder {
|
||||
}
|
||||
}
|
||||
|
||||
( new Component_Builder( $argv[1], $argv[2], $argv[3] ) )->build();
|
||||
function build( $args = [] ) {
|
||||
( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ExportException
|
||||
*/
|
||||
function jsonToPhp( $args = [] ): ?string {
|
||||
$json = $args['json'] ?? [];
|
||||
|
||||
return VarExporter::export( $json );
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"zordius/lightncandy": "^1.2.6"
|
||||
"zordius/lightncandy": "^1.2.6",
|
||||
"brick/varexporter": "^0.3.7"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+106
-1
@@ -4,8 +4,113 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "1d9b4e7a02be366b48383c185193727f",
|
||||
"content-hash": "6731e0a19d488ee457b6682e62ef9018",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/varexporter",
|
||||
"version": "0.3.7",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/brick/varexporter.git",
|
||||
"reference": "3e263cd718d242594c52963760fee2059fd5833c"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/brick/varexporter/zipball/3e263cd718d242594c52963760fee2059fd5833c",
|
||||
"reference": "3e263cd718d242594c52963760fee2059fd5833c",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"nikic/php-parser": "^4.0",
|
||||
"php": "^7.2 || ^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"php-coveralls/php-coveralls": "^2.2",
|
||||
"phpunit/phpunit": "^8.5 || ^9.0",
|
||||
"vimeo/psalm": "4.23.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Brick\\VarExporter\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"description": "A powerful alternative to var_export(), which can export closures and objects without __set_state()",
|
||||
"keywords": [
|
||||
"var_export"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/brick/varexporter/issues",
|
||||
"source": "https://github.com/brick/varexporter/tree/0.3.7"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/BenMorel",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2022-06-29T23:37:57+00:00"
|
||||
},
|
||||
{
|
||||
"name": "nikic/php-parser",
|
||||
"version": "v4.15.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/nikic/PHP-Parser.git",
|
||||
"reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc",
|
||||
"reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-tokenizer": "*",
|
||||
"php": ">=7.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"ircmaxell/php-yacc": "^0.0.7",
|
||||
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0"
|
||||
},
|
||||
"bin": [
|
||||
"bin/php-parse"
|
||||
],
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "4.9-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhpParser\\": "lib/PhpParser"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Nikita Popov"
|
||||
}
|
||||
],
|
||||
"description": "A PHP parser written in PHP",
|
||||
"keywords": [
|
||||
"parser",
|
||||
"php"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/nikic/PHP-Parser/issues",
|
||||
"source": "https://github.com/nikic/PHP-Parser/tree/v4.15.2"
|
||||
},
|
||||
"time": "2022-11-12T15:38:23+00:00"
|
||||
},
|
||||
{
|
||||
"name": "zordius/lightncandy",
|
||||
"version": "v1.2.6",
|
||||
|
||||
@@ -1,30 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>;
|
||||
|
||||
use Core\Global_Functions;
|
||||
|
||||
class <%= blockClassModel %>_Component {
|
||||
|
||||
public function __construct() {
|
||||
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
|
||||
// add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
|
||||
add_action( 'after_setup_theme', [ $this, 'register_assets' ] );
|
||||
}
|
||||
|
||||
function register_assets(): void {
|
||||
wp_enqueue_style( '<%= blockFilename %>',
|
||||
wp_register_style( '<%= blockFilename %>',
|
||||
get_template_directory_uri() . '/components/partials/<%= blockFilename %>/templates/styles/<%= blockFilename %>.min.css',
|
||||
[ 'style-wp' ],
|
||||
Global_Functions::get_current_version_number()
|
||||
);
|
||||
wp_enqueue_style( '<%= blockFilename %>' )
|
||||
|
||||
wp_enqueue_script( '<%= blockFilename %>',
|
||||
wp_register_script( 'script-<%= blockFilename %>',
|
||||
get_template_directory_uri() . '/components/partials/<%= blockFilename %>/templates/scripts/<%= blockFilename %>.min.js',
|
||||
[ 'jquery', 'swiper' ],
|
||||
Global_Functions::get_current_version_number(),
|
||||
true
|
||||
);
|
||||
wp_enqueue_script( 'script-<%= blockFilename %>' )
|
||||
}
|
||||
|
||||
public function render( $args = [] ): void {
|
||||
$output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( array_merge( [], $args ), self::class );
|
||||
$args = array_merge( [], Helpers\<%= blockClassModel %>_Defaults::default_args( $args ), $args);
|
||||
$output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( $args, self::class );
|
||||
|
||||
echo apply_filters( 'the_content', wpautop( $output ) );
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,10 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>\Helpers;
|
||||
|
||||
class <%= blockClassModel %>_Defaults {
|
||||
public static function default_args(): array {
|
||||
return [
|
||||
'title' => '<%= title %>',
|
||||
];
|
||||
$args = <%- defaultData %>;
|
||||
|
||||
// $args['base_url'] = \Core\Global_Functions::get_file_url( __DIR__ . '/../templates/' );
|
||||
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user