From cdbb4d3064b0efddaca0abc02e0e55ec13ba9537 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Fri, 9 Dec 2022 07:11:33 +0200 Subject: [PATCH] Organized all env in env.js file. It allows us to overwrite these values. --- env.js | 18 ++++++++++++++++++ helpers.js | 40 +++++++++++++++++++--------------------- package.json | 10 ++++------ server.js | 3 +-- 4 files changed, 42 insertions(+), 29 deletions(-) create mode 100644 env.js diff --git a/env.js b/env.js new file mode 100644 index 0000000..86d9ace --- /dev/null +++ b/env.js @@ -0,0 +1,18 @@ +import path from 'path'; + +/** + * Since this file overwrites environment variables from `config` lib. + * It's important to "import" this file before all scripts in entry point file, especially before `config`. + */ + +process.env.NODE_CONFIG_DIR = path.join(process.env.PROJECT_PATH ?? '', process.env.BLOCK_NAME ?? '', 'config'); + +/** + * 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; +export const MODULE_PATH = process.env.MODULE_PATH ?? 'node_modules/block-dev-tool'; +export const PROJECT_PATH = process.env.PROJECT_PATH ? path.join(process.env.PROJECT_PATH, BLOCK_NAME) : ''; diff --git a/helpers.js b/helpers.js index 6c7016e..00146ac 100644 --- a/helpers.js +++ b/helpers.js @@ -1,4 +1,5 @@ import path from 'path'; +import {BLOCK_NAME, IS_DEV, MODULE_PATH, PROJECT_PATH} 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: MODULE_PATH, + projectPath: PROJECT_PATH, }; } @@ -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.json b/package.json index b557ff9..e23f2b4 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,11 @@ "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" + "dev": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= PROJECT_PATH=blocks node server.js", + "build-platform": "NODE_ENV=development BLOCK_NAME=header MODULE_PATH= PROJECT_PATH=blocks node ./build.js", + "dev-dev-tool": "NODE_ENV=development rollup --config rollup.config.js --watch", + "build-dev-tool": "rollup --config rollup.config.js" }, "license": "ISC", "type": "module", diff --git a/server.js b/server.js index bd069af..92481b7 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;