From e56852d1bfa9b13a9d00e1ef341fd41ccf71cc26 Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Wed, 17 Mar 2021 14:08:58 -0600 Subject: [PATCH] Moved configs --- .env.template | 3 +++ commerce.config.json | 1 - framework/commerce/config.js | 46 ++++++++++++++++++++++++++++++++++++ next.config.js | 10 +++++--- tsconfig.json | 4 ++-- 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 framework/commerce/config.js diff --git a/.env.template b/.env.template index 9d2f8f1bf..5139a1b26 100644 --- a/.env.template +++ b/.env.template @@ -1,3 +1,6 @@ +# Only include the environment variables for a single provider +# To test a different provider, comment or remove the variables of other providers first + BIGCOMMERCE_STOREFRONT_API_URL= BIGCOMMERCE_STOREFRONT_API_TOKEN= BIGCOMMERCE_STORE_API_URL= diff --git a/commerce.config.json b/commerce.config.json index bef7db222..08ea78814 100644 --- a/commerce.config.json +++ b/commerce.config.json @@ -1,5 +1,4 @@ { - "provider": "bigcommerce", "features": { "wishlist": true, "customCheckout": false diff --git a/framework/commerce/config.js b/framework/commerce/config.js new file mode 100644 index 000000000..fec443604 --- /dev/null +++ b/framework/commerce/config.js @@ -0,0 +1,46 @@ +/** + * This file is expected to be used in next.config.js only + */ + +const merge = require('deepmerge') + +const PROVIDERS = ['bigcommerce', 'shopify'] + +function getProviderName() { + return process.env.BIGCOMMERCE_STOREFRONT_API_URL + ? 'bigcommerce' + : process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN + ? 'shopify' + : null +} + +function withCommerceConfig(nextConfig = {}) { + const commerce = nextConfig.commerce || {} + const name = commerce.provider || getProviderName() + + if (!name) { + throw new Error( + `The commerce provider is missing, please add a valid provider name or its environment variables` + ) + } + if (!PROVIDERS.includes(name)) { + throw new Error( + `The commerce provider "${name}" can't be found, please use one of "${PROVIDERS.join( + ', ' + )}"` + ) + } + + const commerceNextConfig = require(`../${name}/next.config`) + const config = merge(commerceNextConfig, nextConfig) + + config.env = config.env || {} + + Object.entries(config.commerce.features).forEach(([k, v]) => { + if (v) config.env[`COMMERCE_${k.toUpperCase()}_ENABLED`] = true + }) + + return config +} + +module.exports = { withCommerceConfig, getProviderName } diff --git a/next.config.js b/next.config.js index 7e86695a0..b3d9e541b 100644 --- a/next.config.js +++ b/next.config.js @@ -1,8 +1,12 @@ const commerce = require('./commerce.config.json') -const withCommerceConfig = require('./framework/commerce/with-config') +const { + withCommerceConfig, + getProviderName, +} = require('./framework/commerce/config') -const isBC = commerce.provider === 'bigcommerce' -const isShopify = commerce.provider === 'shopify' +const provider = commerce.provider || getProviderName() +const isBC = provider === 'bigcommerce' +const isShopify = provider === 'shopify' module.exports = withCommerceConfig({ commerce, diff --git a/tsconfig.json b/tsconfig.json index 9e712fb18..e20f37099 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,8 +22,8 @@ "@components/*": ["components/*"], "@commerce": ["framework/commerce"], "@commerce/*": ["framework/commerce/*"], - "@framework": ["framework/bigcommerce"], - "@framework/*": ["framework/bigcommerce/*"] + "@framework": ["framework/shopify"], + "@framework/*": ["framework/shopify/*"] } }, "include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],