Browse Source

Merge pull request 'project-path-feat' (#7) from project-path-feat into master

Reviewed-on: https://git.devdevdev.life/AXE-WEB/block-dev-tool/pulls/7
test-gpt-generated
Roman Axelrod 3 years ago
parent
commit
7dcaa80d71
  1. 46
      env.js
  2. 40
      helpers.js
  3. 4
      package-lock.json
  4. 18
      package.json
  5. 9
      platforms/hubspot/hubspot-email-adapter.js
  6. 2
      platforms/hubspot/hubspot-page-adapter.js
  7. 7
      server.js
  8. 10
      test.js

46
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;
}

40
helpers.js

@ -1,4 +1,5 @@
import path from 'path'; import path from 'path';
import {BLOCK_NAME, getModulePath, getProjectPath, IS_DEV} from "./env.js";
import config from 'config'; import config from 'config';
import {fileURLToPath} from 'url'; import {fileURLToPath} from 'url';
import memFs from 'mem-fs'; import memFs from 'mem-fs';
@ -13,14 +14,11 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
export function getConfigs() { 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 { return {
isDev, isDev: IS_DEV,
developmentBlockName, developmentBlockName: BLOCK_NAME,
modulesPath: process.env.MODULE_PATH ?? (isDev ? '' : 'node_modules/block-dev-tool'), modulesPath: getModulePath(),
projectPath: process.env.PROJECT_PATH ?? (isDev ? path.join('blocks', developmentBlockName) : ''), projectPath: getProjectPath(),
}; };
} }
@ -157,19 +155,19 @@ export async function zipProject(srcDir, outputFileName = 'dist.zip') {
} }
export async function buildExportFiles(blockName, platform) { export async function buildExportFiles(blockName, platform) {
if (platform.name.startsWith('wordpress')) { if (platform.name.startsWith('wordpress')) {
if (platform.name === 'wordpress-acf-block') { if (platform.name === 'wordpress-acf-block') {
await buildWordPress(blockName, {block: true}); await buildWordPress(blockName, {block: true});
} else if (platform.name === 'wordpress-elementor') { } else if (platform.name === 'wordpress-elementor') {
await buildWordPress(blockName, {elementor: true}); await buildWordPress(blockName, {elementor: true});
} else if (platform.name === 'wordpress-component-manager') { } else if (platform.name === 'wordpress-component-manager') {
await buildWordPress(blockName, {componentManager: true}); await buildWordPress(blockName, {componentManager: true});
} else { } else {
await buildWordPress(blockName); await buildWordPress(blockName);
}
} else if (platform.name === 'hubspot-email') {
await buildHubspotEmail(blockName)
} else if (platform.name === 'hubspot') {
await buildHubspotPage(blockName)
} }
} else if (platform.name === 'hubspot-email') {
await buildHubspotEmail(blockName)
} else if (platform.name === 'hubspot') {
await buildHubspotPage(blockName)
}
} }

4
package-lock.json

@ -1,12 +1,12 @@
{ {
"name": "@axe-web/block-dev-tool", "name": "@axe-web/block-dev-tool",
"version": "1.0.25", "version": "1.0.26",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@axe-web/block-dev-tool", "name": "@axe-web/block-dev-tool",
"version": "1.0.25", "version": "1.0.26",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@braintree/sanitize-url": "^6.0.0", "@braintree/sanitize-url": "^6.0.0",

18
package.json

@ -1,19 +1,17 @@
{ {
"name": "@axe-web/block-dev-tool", "name": "@axe-web/block-dev-tool",
"version": "1.0.25", "version": "1.0.26",
"author": { "author": {
"name": "AXE-WEB", "name": "AXE-WEB",
"email": "office@axe-web.com", "email": "office@axe-web.com",
"url": "https://axe-web.com/" "url": "https://axe-web.com/"
}, },
"scripts": { "scripts": {
"start": "component-dev", "info": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node debug.js",
"info": "node debug.js", "dev": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node server.js",
"dev": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node server.js", "build-platform": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= node ./build.js",
"build": "rollup --config rollup.config.js", "dev-dev-tool": "NODE_ENV=development rollup --config rollup.config.js --watch",
"build-platform": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node ./build.js", "build-dev-tool": "rollup --config rollup.config.js"
"build-platform-cli": "component-build",
"dev-js": "NODE_ENV=development rollup --config rollup.config.js --watch"
}, },
"engines": { "engines": {
"node": ">=14.17.3" "node": ">=14.17.3"
@ -63,9 +61,11 @@
}, },
"bin": { "bin": {
"component-dev": "./server.js", "component-dev": "./server.js",
"component-build": "./build.js" "component-build": "./build.js",
"component-info": "./debug.js"
}, },
"files": [ "files": [
"env.js",
"helpers.js", "helpers.js",
"debug.js", "debug.js",
"layouts/**/*.hbs", "layouts/**/*.hbs",

9
platforms/hubspot/hubspot-email-adapter.js

@ -18,8 +18,8 @@ export async function buildHubspotEmail(blockName) {
}); });
} }
export async function createDistFolder(blockName) { export async function createDistFolder(blockName, projectPath = '') {
const distPath = path.join('exports', 'hubspot', `${blockName}.module`); const distPath = path.join(projectPath, 'exports', 'hubspot', `${blockName}.module`);
await mkdir(distPath, {recursive: true}) await mkdir(distPath, {recursive: true})
return distPath; return distPath;
@ -248,9 +248,11 @@ export async function buildHubspotJSONFiles(distPath, metaData) {
export function handlebarsToHubl(handlebars) { 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, '{% if module.');
handlebars = handlebars.replace(/{{\/if}}/g, '{% endif %}'); 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(/{{\/each}}/g, '{% endfor %}');
handlebars = handlebars.replace(/{{base_url}}/g, ''); handlebars = handlebars.replace(/{{base_url}}/g, '');
handlebars = handlebars.replace(/{esc_attr /g, '{'); handlebars = handlebars.replace(/{esc_attr /g, '{');
@ -262,6 +264,7 @@ export function handlebarsToHubl(handlebars) {
handlebars = handlebars.replace(/}}}/g, '}}'); handlebars = handlebars.replace(/}}}/g, '}}');
handlebars = handlebars.replace(/{{/g, '{{module.'); handlebars = handlebars.replace(/{{/g, '{{module.');
handlebars = handlebars.replace(/{{module. /g, '{{ module.'); handlebars = handlebars.replace(/{{module. /g, '{{ module.');
handlebars = handlebars.replace(/.url/g, '.src');
return handlebars; return handlebars;
} }

2
platforms/hubspot/hubspot-page-adapter.js

@ -5,7 +5,7 @@ import {buildHubspotJSONFiles, createDistFolder, handlebarsToHubl,} from "./hubs
export async function buildHubspotPage(blockName) { export async function buildHubspotPage(blockName) {
const {modulesPath, projectPath} = getConfigs(); const {modulesPath, projectPath} = getConfigs();
const distPath = await createDistFolder(blockName); const distPath = await createDistFolder(blockName, projectPath);
const srcPath = path.join(projectPath, 'src'); const srcPath = path.join(projectPath, 'src');

7
server.js

@ -1,5 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
import {PRODUCTION_REGISTRY_URL} from "./env.js";
import path from 'path'; import path from 'path';
import fetch from "node-fetch"; import fetch from "node-fetch";
import express from 'express'; import express from 'express';
@ -25,8 +26,6 @@ import PluginError from 'plugin-error';
* Constants * Constants
*/ */
const PRODUCTION_REGISTRY_URL = 'https://blocks-registery.axe-web.com';
const {isDev, modulesPath, projectPath, developmentBlockName} = getConfigs(); const {isDev, modulesPath, projectPath, developmentBlockName} = getConfigs();
const blocksRegistry = isDev ? 'http://localhost:3020' : PRODUCTION_REGISTRY_URL; const blocksRegistry = isDev ? 'http://localhost:3020' : PRODUCTION_REGISTRY_URL;
@ -132,7 +131,7 @@ app.get('/publish', async (req, res) => {
} }
if (responseData.uploadUrl) { 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 body = await fs.readFile(path.join(projectPath, 'dist.zip'));
const response = await fetch(`${responseData.uploadUrl}`, { const response = await fetch(`${responseData.uploadUrl}`, {
method: 'PUT', method: 'PUT',
@ -228,7 +227,7 @@ function startBrowserSync() {
return cb(); 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) browserSyncReload(bs, '', 'Template File Change: ' + file)
}); });

10
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.
*
*/
Loading…
Cancel
Save