diff --git a/env.js b/env.js new file mode 100644 index 0000000..6921469 --- /dev/null +++ b/env.js @@ -0,0 +1,46 @@ +import path from 'path'; + +/** + * Since this file overwrites environment variables for `config` lib, + * it's important to "import" this file before all scripts in entry point file, + * especially before `config` module import. + */ + + +/** + * Export constant variables + */ +export const PRODUCTION_REGISTRY_URL = 'https://blocks-registery.axe-web.com'; +export const IS_DEV = process.env.NODE_ENV === 'development'; +export const BLOCK_NAME = process.env.BLOCK_NAME; + +/** + * Overwrite env variables. + */ + +process.env.NODE_CONFIG_DIR = path.join(getProjectPath(), 'config'); + + +export function getModulePath() { + let modulePath = 'node_modules/block-dev-tool'; + + if (typeof process.env.MODULE_PATH !== 'undefined') { + modulePath = process.env.MODULE_PATH; + } else if (process.env.BLOCK_NAME) { + modulePath = 'node_modules/@axe-web/block-dev-tool'; + } + + return modulePath; +} + +export function getProjectPath() { + let projectPath = ''; + + if (typeof process.env.PROJECT_PATH !== 'undefined') { + projectPath = path.join(process.env.PROJECT_PATH ?? '', process.env.BLOCK_NAME ?? '') + } else if (process.env.BLOCK_NAME) { + projectPath = path.join('blocks', process.env.BLOCK_NAME ?? '') + } + + return projectPath; +} diff --git a/helpers.js b/helpers.js index 6c7016e..9f35b04 100644 --- a/helpers.js +++ b/helpers.js @@ -1,4 +1,5 @@ import path from 'path'; +import {BLOCK_NAME, getModulePath, getProjectPath, IS_DEV} from "./env.js"; import config from 'config'; import {fileURLToPath} from 'url'; import memFs from 'mem-fs'; @@ -13,14 +14,11 @@ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export function getConfigs() { - const isDev = process.env.NODE_ENV === 'development'; // Check README file in case you get "missing files" error. - const developmentBlockName = process.env.BLOCK_NAME; - return { - isDev, - developmentBlockName, - modulesPath: process.env.MODULE_PATH ?? (isDev ? '' : 'node_modules/block-dev-tool'), - projectPath: process.env.PROJECT_PATH ?? (isDev ? path.join('blocks', developmentBlockName) : ''), + isDev: IS_DEV, + developmentBlockName: BLOCK_NAME, + modulesPath: getModulePath(), + projectPath: getProjectPath(), }; } @@ -157,19 +155,19 @@ export async function zipProject(srcDir, outputFileName = 'dist.zip') { } 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) + 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) + } } diff --git a/package-lock.json b/package-lock.json index 62995fc..1807f65 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@axe-web/block-dev-tool", - "version": "1.0.25", + "version": "1.0.26", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@axe-web/block-dev-tool", - "version": "1.0.25", + "version": "1.0.26", "license": "ISC", "dependencies": { "@braintree/sanitize-url": "^6.0.0", diff --git a/package.json b/package.json index c1b1d3b..a82b7bc 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,17 @@ { "name": "@axe-web/block-dev-tool", - "version": "1.0.25", + "version": "1.0.26", "author": { "name": "AXE-WEB", "email": "office@axe-web.com", "url": "https://axe-web.com/" }, "scripts": { - "start": "component-dev", - "info": "node debug.js", - "dev": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node server.js", - "build": "rollup --config rollup.config.js", - "build-platform": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node ./build.js", - "build-platform-cli": "component-build", - "dev-js": "NODE_ENV=development rollup --config rollup.config.js --watch" + "info": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node debug.js", + "dev": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node server.js", + "build-platform": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node ./build.js", + "dev-dev-tool": "NODE_ENV=development rollup --config rollup.config.js --watch", + "build-dev-tool": "rollup --config rollup.config.js" }, "engines": { "node": ">=14.17.3" @@ -63,9 +61,11 @@ }, "bin": { "component-dev": "./server.js", - "component-build": "./build.js" + "component-build": "./build.js", + "component-info": "./debug.js" }, "files": [ + "env.js", "helpers.js", "debug.js", "layouts/**/*.hbs", diff --git a/platforms/hubspot/hubspot-email-adapter.js b/platforms/hubspot/hubspot-email-adapter.js index ba1618d..b965372 100644 --- a/platforms/hubspot/hubspot-email-adapter.js +++ b/platforms/hubspot/hubspot-email-adapter.js @@ -18,8 +18,8 @@ export async function buildHubspotEmail(blockName) { }); } -export async function createDistFolder(blockName) { - const distPath = path.join('exports', 'hubspot', `${blockName}.module`); +export async function createDistFolder(blockName, projectPath = '') { + const distPath = path.join(projectPath, 'exports', 'hubspot', `${blockName}.module`); await mkdir(distPath, {recursive: true}) return distPath; @@ -248,9 +248,11 @@ export async function buildHubspotJSONFiles(distPath, metaData) { export function handlebarsToHubl(handlebars) { + handlebars = handlebars.replace(/{{ else }}/g, '{% else %}'); + handlebars = handlebars.replace(/{{else}}/g, '{% else %}'); handlebars = handlebars.replace(/{{#if /g, '{% if module.'); handlebars = handlebars.replace(/{{\/if}}/g, '{% endif %}'); - handlebars = handlebars.replace(/{{#each /g, '{% for module.'); + handlebars = handlebars.replace(/{{#each /g, '{% for item in module.'); handlebars = handlebars.replace(/{{\/each}}/g, '{% endfor %}'); handlebars = handlebars.replace(/{{base_url}}/g, ''); handlebars = handlebars.replace(/{esc_attr /g, '{'); @@ -262,6 +264,7 @@ export function handlebarsToHubl(handlebars) { handlebars = handlebars.replace(/}}}/g, '}}'); handlebars = handlebars.replace(/{{/g, '{{module.'); handlebars = handlebars.replace(/{{module. /g, '{{ module.'); + handlebars = handlebars.replace(/.url/g, '.src'); return handlebars; } diff --git a/platforms/hubspot/hubspot-page-adapter.js b/platforms/hubspot/hubspot-page-adapter.js index f5096c0..a9b1cf7 100644 --- a/platforms/hubspot/hubspot-page-adapter.js +++ b/platforms/hubspot/hubspot-page-adapter.js @@ -5,7 +5,7 @@ import {buildHubspotJSONFiles, createDistFolder, handlebarsToHubl,} from "./hubs export async function buildHubspotPage(blockName) { const {modulesPath, projectPath} = getConfigs(); - const distPath = await createDistFolder(blockName); + const distPath = await createDistFolder(blockName, projectPath); const srcPath = path.join(projectPath, 'src'); diff --git a/server.js b/server.js index 165b430..fa3a959 100755 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ #!/usr/bin/env node +import {PRODUCTION_REGISTRY_URL} from "./env.js"; import path from 'path'; import fetch from "node-fetch"; import express from 'express'; @@ -25,8 +26,6 @@ import PluginError from 'plugin-error'; * Constants */ -const PRODUCTION_REGISTRY_URL = 'https://blocks-registery.axe-web.com'; - const {isDev, modulesPath, projectPath, developmentBlockName} = getConfigs(); const blocksRegistry = isDev ? 'http://localhost:3020' : PRODUCTION_REGISTRY_URL; @@ -132,7 +131,7 @@ app.get('/publish', async (req, res) => { } if (responseData.uploadUrl) { - await zipProject(path.join(projectPath, 'src')); + await zipProject(path.join(projectPath, 'src'), path.join(projectPath, 'dist.zip')); const body = await fs.readFile(path.join(projectPath, 'dist.zip')); const response = await fetch(`${responseData.uploadUrl}`, { method: 'PUT', @@ -228,7 +227,7 @@ function startBrowserSync() { return cb(); }])); - bs.watch("src/**/*.hbs", function (event, file) { + bs.watch(path.join(projectPath, "src/**/*.hbs"), function (event, file) { browserSyncReload(bs, '', 'Template File Change: ' + file) }); diff --git a/test.js b/test.js new file mode 100644 index 0000000..8a8afcc --- /dev/null +++ b/test.js @@ -0,0 +1,10 @@ +/** + * FUTURE TESTS. + * + * # ENV + * In `blocks-builder` service, we update MODULE_PATH and PROJECT_PATH environment variables before we run platform + * bundle build process. Actually before we call buildExportFiles(). + * + * We have to make sure that this logic is working properly and stable. + * + */