From de0b6740d1918f616763cab56142d507ae491e95 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Fri, 9 Dec 2022 07:41:39 +0200 Subject: [PATCH] - Simplify the process of env variables setup. Provide default values. - Include env.js in package.json Organized all env in env.js file. It allows us to overwrite these values. --- env.js | 48 ++++++++++++++++++++++++++++++++++++++++++------ package.json | 9 +++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/env.js b/env.js index 86d9ace..97c464e 100644 --- a/env.js +++ b/env.js @@ -1,18 +1,54 @@ 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`. + * 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. */ -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) : ''; +export const MODULE_PATH = getModulePath(); +export const PROJECT_PATH = getProjectPath(); + + +/** + * Overwrite env variables. + */ + +process.env.NODE_CONFIG_DIR = path.join(PROJECT_PATH, 'config'); + + +/** + * Private functions. + * Don't export these, export constants. + */ + +function getModulePath() { + let modulePath = 'node_modules/block-dev-tool'; + + if (typeof process.env.MODULE_PATH !== 'undefined') { + modulePath = process.env.MODULE_PATH; + } else if (BLOCK_NAME) { + modulePath = 'node_modules/@axe-web/block-dev-tool'; + } + + return modulePath; +} + +function getProjectPath() { + let projectPath = ''; + + if (typeof process.env.PROJECT_PATH !== 'undefined') { + projectPath = path.join(process.env.PROJECT_PATH, BLOCK_NAME) + } else if (BLOCK_NAME) { + projectPath = path.join('blocks', BLOCK_NAME) + } + + return projectPath; +} diff --git a/package.json b/package.json index d7d2c80..6aad4ff 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,9 @@ "url": "https://axe-web.com/" }, "scripts": { - "info": "node debug.js", - "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", + "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" }, @@ -58,7 +58,8 @@ }, "bin": { "component-dev": "./server.js", - "component-build": "./build.js" + "component-build": "./build.js", + "component-info": "./debug.js" }, "files": [ "env.js",