From 241cc198b7273b9e0af1e5c14002c4b4b981cee0 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Mon, 11 Nov 2024 17:29:26 -0300 Subject: [PATCH 01/16] feat: Add plugins function --- packages/cli/src/utils/directory.ts | 38 +++++++-- packages/cli/src/utils/generate.ts | 3 + packages/cli/src/utils/plugins.ts | 119 ++++++++++++++++++++++++++++ packages/core/src/plugins/.gitkeep | 0 4 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 packages/cli/src/utils/plugins.ts create mode 100644 packages/core/src/plugins/.gitkeep diff --git a/packages/cli/src/utils/directory.ts b/packages/cli/src/utils/directory.ts index 521354c540..778056c84c 100644 --- a/packages/cli/src/utils/directory.ts +++ b/packages/cli/src/utils/directory.ts @@ -18,49 +18,71 @@ export const withBasePath = (basepath: string) => { return path.resolve(process.cwd(), basepath) } - /* + /* * This will loop from the basepath until the process.cwd() looking for node_modules/@faststore/core - * + * * If it reaches process.cwd() (or /, as a safeguard), without finding it, it will throw an exception */ - const getCorePackagePath = () => { - const coreFromNodeModules = path.join('node_modules', '@faststore', 'core') + const getPackagePath = (...packagePath: string[]) => { + const packageFromNodeModules = path.join('node_modules', ...packagePath) const resolvedCwd = path.resolve(process.cwd()) const parents: string[] = [] let attemptedPath do { - attemptedPath = path.join(resolvedCwd, basepath, ...parents, coreFromNodeModules) + attemptedPath = path.join( + resolvedCwd, + basepath, + ...parents, + packageFromNodeModules + ) if (fs.existsSync(attemptedPath)) { return attemptedPath } parents.push('..') - } while (path.resolve(attemptedPath) !== resolvedCwd || path.resolve(attemptedPath) !== '/') + } while ( + path.resolve(attemptedPath) !== resolvedCwd || + path.resolve(attemptedPath) !== '/' + ) throw `Could not find @node_modules on ${basepath} or any of its parents until ${attemptedPath}` } + const getCorePackagePath = () => { + return getPackagePath('@faststore', 'core') + } + const tmpDir = path.join(getRoot(), tmpFolderName) const userSrcDir = path.join(getRoot(), 'src') return { getRoot, + getPackagePath, userDir: getRoot(), userSrcDir, userThemesFileDir: path.join(userSrcDir, 'themes'), userCMSDir: path.join(getRoot(), 'cms', 'faststore'), userLegacyStoreConfigFile: path.join(getRoot(), 'faststore.config.js'), userStoreConfigFile: path.join(getRoot(), 'discovery.config.js'), - + tmpSeoConfig: path.join(tmpDir, 'next-seo.config.ts'), tmpFolderName, tmpDir, tmpCustomizationsSrcDir: path.join(tmpDir, 'src', 'customizations', 'src'), - tmpThemesCustomizationsFile: path.join(tmpDir, 'src', 'customizations', 'src', 'themes', 'index.scss'), + tmpThemesCustomizationsFile: path.join( + tmpDir, + 'src', + 'customizations', + 'src', + 'themes', + 'index.scss' + ), tmpCMSDir: path.join(tmpDir, 'cms', 'faststore'), + tmpPagesDir: path.join(tmpDir, 'src', 'pages'), + tmpPluginsDir: path.join(tmpDir, 'src', 'plugins'), tmpCMSWebhookUrlsFile: path.join(tmpDir, 'cms-webhook-urls.json'), tmpStoreConfigFile: path.join( tmpDir, diff --git a/packages/cli/src/utils/generate.ts b/packages/cli/src/utils/generate.ts index b1104949d4..16ca0bf065 100644 --- a/packages/cli/src/utils/generate.ts +++ b/packages/cli/src/utils/generate.ts @@ -17,6 +17,7 @@ import ora from 'ora' import { withBasePath } from './directory' import { installDependencies } from './dependencies' import { logger } from './logger' +import { installPlugins } from './plugins' interface GenerateOptions { setup?: boolean @@ -513,5 +514,7 @@ export async function generate(options: GenerateOptions) { createCmsWebhookUrlsJsonFile(basePath), updateNextConfig(basePath), enableRedirectsMiddleware(basePath), + + installPlugins(basePath), ]) } diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts new file mode 100644 index 0000000000..c99f24bbf8 --- /dev/null +++ b/packages/cli/src/utils/plugins.ts @@ -0,0 +1,119 @@ +import { copySync, mkdirSync, writeFileSync } from 'fs-extra' +import { withBasePath } from './directory' +import path from 'path' +import { logger } from './logger' + +type PageConfig = { + path: string + appLayout: boolean + name: string +} + +const pluginConfigFileName = 'plugin.config.js' + +const sanitizePluginName = (pluginName: string) => { + return pluginName.split('/')[1] +} + +const getPluginSrcPath = async (basePath: string, pluginName: string) => { + const { getPackagePath } = withBasePath(basePath) + return getPackagePath(pluginName, 'src') +} + +const getPluginsList = async (basePath: string) => { + const { tmpStoreConfigFile } = withBasePath(basePath) + + const { plugins } = await import(tmpStoreConfigFile) + + return (plugins ?? []) as string[] +} + +const copyPluginsSrc = async (basePath: string, plugins: string[]) => { + const { tmpPluginsDir } = withBasePath(basePath) + + logger.log('Copying plugins files') + + plugins.forEach(async (pluginName) => { + const pluginSrcPath = await getPluginSrcPath(basePath, pluginName) + const pluginDestPath = path.join( + tmpPluginsDir, + sanitizePluginName(pluginName) + ) + + copySync(pluginSrcPath, pluginDestPath) + logger.log(`Copied ${pluginName} files`) + }) +} + +const getPluginPageFileContent = ( + pluginName: string, + pageName: string, + appLayout: boolean +) => ` +// GENERATED FILE +import * as page from 'src/plugins/${pluginName}/pages/${pageName}'; +${appLayout ? `import GlobalSections, { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} + +export async function getStaticProps({previewData}) { + const noop = async function() {} + const loaderData = await (page.loader || noop)() +${appLayout ? `const globalSections = await getGlobalSectionsData(previewData)` : ``} + + return { + props: { + data: loaderData, + ${appLayout ? 'globalSections: globalSections' : ``} + } + } +} +export default function Page(props) { + ${ + appLayout + ? `return + {page.default(props.data)} + ` + : `return page.default(props.data)` + } +} + ` + +const generatePluginPages = async (basePath: string, plugins: string[]) => { + const { tmpPagesDir, getPackagePath } = withBasePath(basePath) + + logger.log('Generating plugin pages') + + plugins.forEach(async (pluginName) => { + const pluginConfigPath = getPackagePath(pluginName, pluginConfigFileName) + + const pluginConfig = await import(pluginConfigPath) + + const pagesConfig: Record = pluginConfig.pages ?? {} + + const pages = Object.keys(pagesConfig) + + pages.forEach(async (pageName) => { + const paths = pagesConfig[pageName].path.split('/') + + const pageFile = paths.pop() + const pagePaths = paths + + const pagePath = path.join(tmpPagesDir, ...pagePaths, pageFile + '.tsx') + + const fileContent = getPluginPageFileContent( + sanitizePluginName(pluginName), + pageName, + pagesConfig[pageName].appLayout + ) + + mkdirSync(path.dirname(pagePath), { recursive: true }) + writeFileSync(pagePath, fileContent) + }) + }) +} + +export const installPlugins = async (basePath: string) => { + const plugins = await getPluginsList(basePath) + + await copyPluginsSrc(basePath, plugins) + await generatePluginPages(basePath, plugins) +} diff --git a/packages/core/src/plugins/.gitkeep b/packages/core/src/plugins/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 From f56ed5e4f7f53ef717e170c22fa5e15b999d6122 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Tue, 12 Nov 2024 10:24:41 -0300 Subject: [PATCH 02/16] feat: add plugins sections --- packages/cli/src/utils/plugins.ts | 54 +++++++++++++++++-- .../src/components/cms/global/Components.ts | 2 + .../src/components/cms/home/Components.ts | 2 + .../core/src/components/cms/plp/Components.ts | 2 + .../src/components/cms/search/Components.ts | 2 + .../templates/LandingPage/LandingPage.tsx | 2 + packages/core/src/pages/404.tsx | 2 + packages/core/src/pages/500.tsx | 2 + packages/core/src/pages/[slug]/p.tsx | 2 + packages/core/src/pages/login.tsx | 2 + packages/core/src/plugins/index.ts | 2 + 11 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 packages/core/src/plugins/index.ts diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index c99f24bbf8..4ac9dfa27b 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -1,4 +1,4 @@ -import { copySync, mkdirSync, writeFileSync } from 'fs-extra' +import { copySync, existsSync, mkdirSync, writeFileSync } from 'fs-extra' import { withBasePath } from './directory' import path from 'path' import { logger } from './logger' @@ -11,8 +11,18 @@ type PageConfig = { const pluginConfigFileName = 'plugin.config.js' -const sanitizePluginName = (pluginName: string) => { - return pluginName.split('/')[1] +const sanitizePluginName = (pluginName: string, pascalCase = false) => { + const sanitized = pluginName.split('/')[1] + + if (pascalCase) { + return sanitized + .toLowerCase() + .split('-') + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) + .join('') + } + + return sanitized } const getPluginSrcPath = async (basePath: string, pluginName: string) => { @@ -111,9 +121,43 @@ const generatePluginPages = async (basePath: string, plugins: string[]) => { }) } +export async function addPluginsSections(basePath: string, plugins: string[]) { + const { tmpPluginsDir, getPackagePath } = withBasePath(basePath) + + logger.log('Adding plugin sections') + + const indexPluginsOverrides = plugins + .filter((plugin) => + existsSync(getPackagePath(plugin, 'src', 'components', 'index.ts')) + ) + .map((plugin) => { + const pluginReference = sanitizePluginName(plugin, true) + 'Components' + + return { + import: `import { default as ${pluginReference} } from 'src/plugins/${sanitizePluginName(plugin)}/components'`, + pluginReference, + } + }) + + const pluginsImportFileContent = ` + ${indexPluginsOverrides.map((plugin) => plugin.import).join('\n')} + + export default { + ${indexPluginsOverrides.map((plugin) => `...${plugin.pluginReference}`).join(',\n')} + } + ` + + const sectionPath = path.join(tmpPluginsDir, 'index.ts') + writeFileSync(sectionPath, pluginsImportFileContent) + logger.log('Writing plugins overrides') + logger.log(sectionPath) + logger.log(pluginsImportFileContent) +} + export const installPlugins = async (basePath: string) => { const plugins = await getPluginsList(basePath) - await copyPluginsSrc(basePath, plugins) - await generatePluginPages(basePath, plugins) + copyPluginsSrc(basePath, plugins) + generatePluginPages(basePath, plugins) + addPluginsSections(basePath, plugins) } diff --git a/packages/core/src/components/cms/global/Components.ts b/packages/core/src/components/cms/global/Components.ts index e3f07cc731..b9a579edd6 100644 --- a/packages/core/src/components/cms/global/Components.ts +++ b/packages/core/src/components/cms/global/Components.ts @@ -6,6 +6,7 @@ import { OverriddenDefaultNavbar as Navbar } from 'src/components/sections/Navba import { OverriddenDefaultRegionBar as RegionBar } from 'src/components/sections/RegionBar/OverriddenDefaultRegionBar' import CUSTOM_COMPONENTS from 'src/customizations/src/components' +import PLUGINS_COMPONENTS from 'src/plugins' const CartSidebar = dynamic( () => @@ -34,6 +35,7 @@ const COMPONENTS: Record> = { CartSidebar, // out of viewport RegionModal, // out of viewport Footer, // out of viewport + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/components/cms/home/Components.ts b/packages/core/src/components/cms/home/Components.ts index b559b3bb4a..c73f04b585 100644 --- a/packages/core/src/components/cms/home/Components.ts +++ b/packages/core/src/components/cms/home/Components.ts @@ -6,6 +6,7 @@ import Incentives from 'src/components/sections/Incentives' import { default as GLOBAL_COMPONENTS } from '../global/Components' import CUSTOM_COMPONENTS from 'src/customizations/src/components' +import PLUGINS_COMPONENTS from 'src/plugins' const BannerText = dynamic( () => @@ -48,6 +49,7 @@ const COMPONENTS: Record> = { Newsletter, ProductShelf, ProductTiles, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/components/cms/plp/Components.ts b/packages/core/src/components/cms/plp/Components.ts index 0ebf2bdc70..1d8347a44d 100644 --- a/packages/core/src/components/cms/plp/Components.ts +++ b/packages/core/src/components/cms/plp/Components.ts @@ -5,6 +5,7 @@ import { OverriddenDefaultBreadcrumb as Breadcrumb } from 'src/components/sectio import { OverriddenDefaultHero as Hero } from 'src/components/sections/Hero/OverriddenDefaultHero' import { OverriddenDefaultProductGallery as ProductGallery } from 'src/components/sections/ProductGallery/OverriddenDefaultProductGallery' import CUSTOM_COMPONENTS from 'src/customizations/src/components' +import PLUGINS_COMPONENTS from 'src/plugins' import { default as GLOBAL_COMPONENTS } from '../global/Components' const BannerText = dynamic( @@ -53,6 +54,7 @@ const COMPONENTS: Record> = { Newsletter, ProductShelf, ProductTiles, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/components/cms/search/Components.ts b/packages/core/src/components/cms/search/Components.ts index 1a6e0deb28..c3b52ea809 100644 --- a/packages/core/src/components/cms/search/Components.ts +++ b/packages/core/src/components/cms/search/Components.ts @@ -5,6 +5,7 @@ import { OverriddenDefaultBreadcrumb as Breadcrumb } from 'src/components/sectio import { OverriddenDefaultHero as Hero } from 'src/components/sections/Hero/OverriddenDefaultHero' import { OverriddenDefaultProductGallery as ProductGallery } from 'src/components/sections/ProductGallery/OverriddenDefaultProductGallery' import CUSTOM_COMPONENTS from 'src/customizations/src/components' +import PLUGINS_COMPONENTS from 'src/plugins' import { default as GLOBAL_COMPONENTS } from '../global/Components' const BannerText = dynamic( @@ -62,6 +63,7 @@ const COMPONENTS: Record> = { Newsletter, ProductShelf, ProductTiles, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/components/templates/LandingPage/LandingPage.tsx b/packages/core/src/components/templates/LandingPage/LandingPage.tsx index dbf9841a1f..ee89e72cf3 100644 --- a/packages/core/src/components/templates/LandingPage/LandingPage.tsx +++ b/packages/core/src/components/templates/LandingPage/LandingPage.tsx @@ -11,6 +11,7 @@ import Incentives from 'src/components/sections/Incentives' import { OverriddenDefaultNewsletter as Newsletter } from 'src/components/sections/Newsletter/OverriddenDefaultNewsletter' import { OverriddenDefaultProductShelf as ProductShelf } from 'src/components/sections/ProductShelf/OverriddenDefaultProductShelf' import ProductTiles from 'src/components/sections/ProductTiles' +import PLUGINS_COMPONENTS from 'src/plugins' import CUSTOM_COMPONENTS from 'src/customizations/src/components' import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components' import MissingContentError from 'src/sdk/error/MissingContentError/MissingContentError' @@ -31,6 +32,7 @@ const COMPONENTS: Record> = { Newsletter, ProductShelf, ProductTiles, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/pages/404.tsx b/packages/core/src/pages/404.tsx index 49d477b5c3..baaebfd175 100644 --- a/packages/core/src/pages/404.tsx +++ b/packages/core/src/pages/404.tsx @@ -10,6 +10,7 @@ import { import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components' import RenderSections from 'src/components/cms/RenderSections' import { OverriddenDefaultEmptyState as EmptyState } from 'src/components/sections/EmptyState/OverriddenDefaultEmptyState' +import PLUGINS_COMPONENTS from 'src/plugins' import CUSTOM_COMPONENTS from 'src/customizations/src/components' import { PageContentType, getPage } from 'src/server/cms' @@ -17,6 +18,7 @@ import { PageContentType, getPage } from 'src/server/cms' const COMPONENTS: Record> = { ...GLOBAL_COMPONENTS, EmptyState, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/pages/500.tsx b/packages/core/src/pages/500.tsx index afa0771dbb..32bb7332db 100644 --- a/packages/core/src/pages/500.tsx +++ b/packages/core/src/pages/500.tsx @@ -10,6 +10,7 @@ import { import { default as GLOBAL_COMPONENTS } from 'src/components/cms/global/Components' import RenderSections from 'src/components/cms/RenderSections' import { OverriddenDefaultEmptyState as EmptyState } from 'src/components/sections/EmptyState/OverriddenDefaultEmptyState' +import PLUGINS_COMPONENTS from 'src/plugins' import CUSTOM_COMPONENTS from 'src/customizations/src/components' import { PageContentType, getPage } from 'src/server/cms' @@ -17,6 +18,7 @@ import { PageContentType, getPage } from 'src/server/cms' const COMPONENTS: Record> = { ...GLOBAL_COMPONENTS, EmptyState, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/pages/[slug]/p.tsx b/packages/core/src/pages/[slug]/p.tsx index e7791cb9e3..ba0c16c135 100644 --- a/packages/core/src/pages/[slug]/p.tsx +++ b/packages/core/src/pages/[slug]/p.tsx @@ -21,6 +21,7 @@ import { OverriddenDefaultNewsletter as Newsletter } from 'src/components/sectio import { OverriddenDefaultProductDetails as ProductDetails } from 'src/components/sections/ProductDetails/OverriddenDefaultProductDetails' import { OverriddenDefaultProductShelf as ProductShelf } from 'src/components/sections/ProductShelf/OverriddenDefaultProductShelf' import ProductTiles from 'src/components/sections/ProductTiles' +import PLUGINS_COMPONENTS from 'src/plugins' import CUSTOM_COMPONENTS from 'src/customizations/src/components' import { useSession } from 'src/sdk/session' import { execute } from 'src/server' @@ -49,6 +50,7 @@ const COMPONENTS: Record> = { ProductShelf, ProductTiles, CrossSellingShelf, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/pages/login.tsx b/packages/core/src/pages/login.tsx index 4a7451a981..99dcda76f4 100644 --- a/packages/core/src/pages/login.tsx +++ b/packages/core/src/pages/login.tsx @@ -11,6 +11,7 @@ import { } from 'src/components/cms/GlobalSections' import RenderSections from 'src/components/cms/RenderSections' import { OverriddenDefaultEmptyState as EmptyState } from 'src/components/sections/EmptyState/OverriddenDefaultEmptyState' +import PLUGINS_COMPONENTS from 'src/plugins' import CUSTOM_COMPONENTS from 'src/customizations/src/components' import { PageContentType, getPage } from 'src/server/cms' import storeConfig from '../../discovery.config' @@ -19,6 +20,7 @@ import storeConfig from '../../discovery.config' const COMPONENTS: Record> = { ...GLOBAL_COMPONENTS, EmptyState, + ...PLUGINS_COMPONENTS, ...CUSTOM_COMPONENTS, } diff --git a/packages/core/src/plugins/index.ts b/packages/core/src/plugins/index.ts new file mode 100644 index 0000000000..f51d770c70 --- /dev/null +++ b/packages/core/src/plugins/index.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line import/no-anonymous-default-export +export default {} From 0218bce5514c05dceb11547d9870c7007e029547 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Wed, 13 Nov 2024 15:12:26 -0300 Subject: [PATCH 03/16] feat: Add hCMS plugin configuration --- packages/cli/src/utils/directory.ts | 14 ++++++++------ packages/cli/src/utils/hcms.ts | 19 ++++++++++++++++--- packages/cli/src/utils/plugins.ts | 6 +++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/cli/src/utils/directory.ts b/packages/cli/src/utils/directory.ts index 778056c84c..ca9101cb93 100644 --- a/packages/cli/src/utils/directory.ts +++ b/packages/cli/src/utils/directory.ts @@ -23,8 +23,12 @@ export const withBasePath = (basepath: string) => { * * If it reaches process.cwd() (or /, as a safeguard), without finding it, it will throw an exception */ - const getPackagePath = (...packagePath: string[]) => { - const packageFromNodeModules = path.join('node_modules', ...packagePath) + const getCorePackagePath = () => { + const packageFromNodeModules = path.join( + 'node_modules', + '@faststore', + 'core' + ) const resolvedCwd = path.resolve(process.cwd()) const parents: string[] = [] @@ -51,12 +55,10 @@ export const withBasePath = (basepath: string) => { throw `Could not find @node_modules on ${basepath} or any of its parents until ${attemptedPath}` } - const getCorePackagePath = () => { - return getPackagePath('@faststore', 'core') - } - const tmpDir = path.join(getRoot(), tmpFolderName) const userSrcDir = path.join(getRoot(), 'src') + const getPackagePath = (...packagePath: string[]) => + path.join(getRoot(), 'node_modules', ...packagePath) return { getRoot, diff --git a/packages/cli/src/utils/hcms.ts b/packages/cli/src/utils/hcms.ts index 469c8aef5d..fe6d032731 100644 --- a/packages/cli/src/utils/hcms.ts +++ b/packages/cli/src/utils/hcms.ts @@ -4,6 +4,7 @@ import { CliUx } from '@oclif/core' import { readFileSync, existsSync, writeFileSync } from 'fs-extra' import { withBasePath } from './directory' +import { getPluginsList } from './plugins' export interface ContentTypeOrSectionDefinition { id?: string @@ -96,7 +97,8 @@ async function confirmUserChoice( fileName: string ) { const goAhead = await CliUx.ux.confirm( - `You are about to override default ${fileName.split('.')[0] + `You are about to override default ${ + fileName.split('.')[0] }:\n\n${duplicates .map((definition) => definition.id || definition.name) .join('\n')}\n\nAre you sure? [yes/no]` @@ -110,7 +112,8 @@ async function confirmUserChoice( } export async function mergeCMSFile(fileName: string, basePath: string) { - const { coreCMSDir, userCMSDir, tmpCMSDir } = withBasePath(basePath) + const { coreCMSDir, userCMSDir, tmpCMSDir, getPackagePath } = + withBasePath(basePath) const coreFilePath = path.join(coreCMSDir, fileName) const customFilePath = path.join(userCMSDir, fileName) @@ -123,8 +126,18 @@ export async function mergeCMSFile(fileName: string, basePath: string) { let output: ContentTypeOrSectionDefinition[] = coreDefinitions + const plugins = await getPluginsList(basePath) + + const pluginPaths = plugins.map((plugin) => + getPackagePath(plugin, 'src', 'cms', fileName) + ) + + const customizations = [...pluginPaths, customFilePath].filter((pluginPath) => + existsSync(pluginPath) + ) + // TODO: create a validation when the CMS files exist but don't have a component for them - if (existsSync(customFilePath)) { + for (const customFilePath of customizations) { const customFile = readFileSync(customFilePath, 'utf8') try { diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 4ac9dfa27b..b0f02a94ad 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -30,12 +30,12 @@ const getPluginSrcPath = async (basePath: string, pluginName: string) => { return getPackagePath(pluginName, 'src') } -const getPluginsList = async (basePath: string) => { +export const getPluginsList = async (basePath: string): Promise => { const { tmpStoreConfigFile } = withBasePath(basePath) - const { plugins } = await import(tmpStoreConfigFile) + const { plugins = [] } = await import(tmpStoreConfigFile) - return (plugins ?? []) as string[] + return plugins } const copyPluginsSrc = async (basePath: string, plugins: string[]) => { From c001d43a56f35c3ca92f648ff3e7b953bd239e6c Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 14 Nov 2024 11:13:47 -0300 Subject: [PATCH 04/16] feat: Add plugin custom page configuration --- packages/cli/src/utils/hcms.ts | 4 +-- packages/cli/src/utils/plugins.ts | 59 +++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/utils/hcms.ts b/packages/cli/src/utils/hcms.ts index fe6d032731..6f5e1821cd 100644 --- a/packages/cli/src/utils/hcms.ts +++ b/packages/cli/src/utils/hcms.ts @@ -4,7 +4,7 @@ import { CliUx } from '@oclif/core' import { readFileSync, existsSync, writeFileSync } from 'fs-extra' import { withBasePath } from './directory' -import { getPluginsList } from './plugins' +import { getPluginName, getPluginsList } from './plugins' export interface ContentTypeOrSectionDefinition { id?: string @@ -129,7 +129,7 @@ export async function mergeCMSFile(fileName: string, basePath: string) { const plugins = await getPluginsList(basePath) const pluginPaths = plugins.map((plugin) => - getPackagePath(plugin, 'src', 'cms', fileName) + getPackagePath(getPluginName(plugin), 'src', 'cms', fileName) ) const customizations = [...pluginPaths, customFilePath].filter((pluginPath) => diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index b0f02a94ad..9cafd4abb2 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -9,6 +9,14 @@ type PageConfig = { name: string } +type Plugin = + | string + | { + [pluginName: string]: { + pages?: { [pageName: string]: Partial } + } + } + const pluginConfigFileName = 'plugin.config.js' const sanitizePluginName = (pluginName: string, pascalCase = false) => { @@ -25,12 +33,28 @@ const sanitizePluginName = (pluginName: string, pascalCase = false) => { return sanitized } +export const getPluginName = (plugin: Plugin) => { + if (typeof plugin === 'string') { + return plugin + } + + return Object.keys(plugin)[0] +} + +const getPluginCustomConfig = (plugin: Plugin) => { + if (typeof plugin === 'string') { + return {} + } + + return plugin[Object.keys(plugin)[0]] +} + const getPluginSrcPath = async (basePath: string, pluginName: string) => { const { getPackagePath } = withBasePath(basePath) return getPackagePath(pluginName, 'src') } -export const getPluginsList = async (basePath: string): Promise => { +export const getPluginsList = async (basePath: string): Promise => { const { tmpStoreConfigFile } = withBasePath(basePath) const { plugins = [] } = await import(tmpStoreConfigFile) @@ -38,13 +62,17 @@ export const getPluginsList = async (basePath: string): Promise => { return plugins } -const copyPluginsSrc = async (basePath: string, plugins: string[]) => { +const copyPluginsSrc = async (basePath: string, plugins: Plugin[]) => { const { tmpPluginsDir } = withBasePath(basePath) logger.log('Copying plugins files') - plugins.forEach(async (pluginName) => { - const pluginSrcPath = await getPluginSrcPath(basePath, pluginName) + plugins.forEach(async (plugin) => { + const pluginName = getPluginName(plugin) + const pluginSrcPath = await getPluginSrcPath( + basePath, + getPluginName(pluginName) + ) const pluginDestPath = path.join( tmpPluginsDir, sanitizePluginName(pluginName) @@ -87,17 +115,23 @@ export default function Page(props) { } ` -const generatePluginPages = async (basePath: string, plugins: string[]) => { +const generatePluginPages = async (basePath: string, plugins: Plugin[]) => { const { tmpPagesDir, getPackagePath } = withBasePath(basePath) logger.log('Generating plugin pages') - plugins.forEach(async (pluginName) => { + plugins.forEach(async (plugin) => { + const pluginName = getPluginName(plugin) const pluginConfigPath = getPackagePath(pluginName, pluginConfigFileName) const pluginConfig = await import(pluginConfigPath) - const pagesConfig: Record = pluginConfig.pages ?? {} + const { pages: pagesCustom } = getPluginCustomConfig(plugin) + + const pagesConfig: Record = { + ...(pluginConfig.pages ?? {}), + ...pagesCustom, + } const pages = Object.keys(pagesConfig) @@ -121,20 +155,23 @@ const generatePluginPages = async (basePath: string, plugins: string[]) => { }) } -export async function addPluginsSections(basePath: string, plugins: string[]) { +export async function addPluginsSections(basePath: string, plugins: Plugin[]) { const { tmpPluginsDir, getPackagePath } = withBasePath(basePath) logger.log('Adding plugin sections') const indexPluginsOverrides = plugins .filter((plugin) => - existsSync(getPackagePath(plugin, 'src', 'components', 'index.ts')) + existsSync( + getPackagePath(getPluginName(plugin), 'src', 'components', 'index.ts') + ) ) .map((plugin) => { - const pluginReference = sanitizePluginName(plugin, true) + 'Components' + const pluginReference = + sanitizePluginName(getPluginName(plugin), true) + 'Components' return { - import: `import { default as ${pluginReference} } from 'src/plugins/${sanitizePluginName(plugin)}/components'`, + import: `import { default as ${pluginReference} } from 'src/plugins/${sanitizePluginName(getPluginName(plugin))}/components'`, pluginReference, } }) From 43d4c9978346c21ebae76cc939f8dde64e2d588b Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 14 Nov 2024 13:51:25 -0300 Subject: [PATCH 05/16] feat: add plugins styles integrations --- packages/cli/src/utils/generate.ts | 14 +++++++++++--- packages/cli/src/utils/plugins.ts | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/utils/generate.ts b/packages/cli/src/utils/generate.ts index 16ca0bf065..13c350c765 100644 --- a/packages/cli/src/utils/generate.ts +++ b/packages/cli/src/utils/generate.ts @@ -1,6 +1,5 @@ import chalk from 'chalk' import { - copyFileSync, copySync, existsSync, mkdirsSync, @@ -17,7 +16,7 @@ import ora from 'ora' import { withBasePath } from './directory' import { installDependencies } from './dependencies' import { logger } from './logger' -import { installPlugins } from './plugins' +import { generateThemeIndexPluginsContent, installPlugins } from './plugins' interface GenerateOptions { setup?: boolean @@ -297,7 +296,12 @@ async function copyTheme(basePath: string) { ) if (existsSync(customTheme)) { try { - copyFileSync(customTheme, tmpThemesCustomizationsFile) + const themeIndexPluginsContent = await generateThemeIndexPluginsContent( + basePath, + `@import "./${storeConfig.theme}.scss";` + ) + + writeFileSync(tmpThemesCustomizationsFile, themeIndexPluginsContent) logger.log( `${chalk.green('success')} - ${ storeConfig.theme @@ -307,6 +311,10 @@ async function copyTheme(basePath: string) { logger.error(`${chalk.red('error')} - ${err}`) } } else { + const themeIndexPluginsContent = + await generateThemeIndexPluginsContent(basePath) + writeFileSync(tmpThemesCustomizationsFile, themeIndexPluginsContent) + logger.info( `${chalk.blue('info')} - The ${ storeConfig.theme diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 9cafd4abb2..16ab58b92f 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -191,6 +191,27 @@ export async function addPluginsSections(basePath: string, plugins: Plugin[]) { logger.log(pluginsImportFileContent) } +export const generateThemeIndexPluginsContent = async ( + basePath: string, + ...customImports: string[] +) => { + const { getPackagePath } = withBasePath(basePath) + + const plugins = await getPluginsList(basePath) + + const pluginImports = plugins + .filter((plugin) => + existsSync( + getPackagePath(getPluginName(plugin), 'src', 'themes', 'index.scss') + ) + ) + .map( + (plugin) => `@import "${getPluginName(plugin)}/src/themes/index.scss";` + ) + + return [...pluginImports, ...customImports].join('\n') +} + export const installPlugins = async (basePath: string) => { const plugins = await getPluginsList(basePath) From 5ec4d8682217000fbfe3f1a60cc9fbfaadb8f5c7 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 14 Nov 2024 15:28:18 -0300 Subject: [PATCH 06/16] feat: add plugins configs --- packages/cli/src/utils/plugins.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 16ab58b92f..a30163497c 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -3,21 +3,23 @@ import { withBasePath } from './directory' import path from 'path' import { logger } from './logger' -type PageConfig = { +export type PageConfig = { path: string appLayout: boolean name: string } -type Plugin = +export type PluginConfig = { + pages?: { [pageName: string]: Partial } +} + +export type Plugin = | string | { - [pluginName: string]: { - pages?: { [pageName: string]: Partial } - } + [pluginName: string]: PluginConfig } -const pluginConfigFileName = 'plugin.config.js' +const PLUGIN_CONFIG_FILE = 'plugin.config.js' const sanitizePluginName = (pluginName: string, pascalCase = false) => { const sanitized = pluginName.split('/')[1] @@ -46,7 +48,7 @@ const getPluginCustomConfig = (plugin: Plugin) => { return {} } - return plugin[Object.keys(plugin)[0]] + return plugin[getPluginName(plugin)] } const getPluginSrcPath = async (basePath: string, pluginName: string) => { @@ -122,7 +124,7 @@ const generatePluginPages = async (basePath: string, plugins: Plugin[]) => { plugins.forEach(async (plugin) => { const pluginName = getPluginName(plugin) - const pluginConfigPath = getPackagePath(pluginName, pluginConfigFileName) + const pluginConfigPath = getPackagePath(pluginName, PLUGIN_CONFIG_FILE) const pluginConfig = await import(pluginConfigPath) From f0586e1f39322aaf0e20c62ed9f00d15ab32ec9a Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 14 Nov 2024 16:33:57 -0300 Subject: [PATCH 07/16] feat: fix import plugin themes --- packages/cli/src/utils/directory.ts | 3 ++- packages/cli/src/utils/generate.ts | 14 +++----------- packages/cli/src/utils/plugins.ts | 18 ++++++++++++++++++ packages/core/src/pages/_app.tsx | 1 + packages/core/src/plugins/index.scss | 3 +++ 5 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 packages/core/src/plugins/index.scss diff --git a/packages/cli/src/utils/directory.ts b/packages/cli/src/utils/directory.ts index ca9101cb93..1c3241c142 100644 --- a/packages/cli/src/utils/directory.ts +++ b/packages/cli/src/utils/directory.ts @@ -82,10 +82,11 @@ export const withBasePath = (basepath: string) => { 'themes', 'index.scss' ), + tmpThemesPluginsFile: path.join(tmpDir, 'src', 'plugins', 'index.scss'), tmpCMSDir: path.join(tmpDir, 'cms', 'faststore'), + tmpCMSWebhookUrlsFile: path.join(tmpDir, 'cms-webhook-urls.json'), tmpPagesDir: path.join(tmpDir, 'src', 'pages'), tmpPluginsDir: path.join(tmpDir, 'src', 'plugins'), - tmpCMSWebhookUrlsFile: path.join(tmpDir, 'cms-webhook-urls.json'), tmpStoreConfigFile: path.join( tmpDir, 'src', diff --git a/packages/cli/src/utils/generate.ts b/packages/cli/src/utils/generate.ts index 13c350c765..16ca0bf065 100644 --- a/packages/cli/src/utils/generate.ts +++ b/packages/cli/src/utils/generate.ts @@ -1,5 +1,6 @@ import chalk from 'chalk' import { + copyFileSync, copySync, existsSync, mkdirsSync, @@ -16,7 +17,7 @@ import ora from 'ora' import { withBasePath } from './directory' import { installDependencies } from './dependencies' import { logger } from './logger' -import { generateThemeIndexPluginsContent, installPlugins } from './plugins' +import { installPlugins } from './plugins' interface GenerateOptions { setup?: boolean @@ -296,12 +297,7 @@ async function copyTheme(basePath: string) { ) if (existsSync(customTheme)) { try { - const themeIndexPluginsContent = await generateThemeIndexPluginsContent( - basePath, - `@import "./${storeConfig.theme}.scss";` - ) - - writeFileSync(tmpThemesCustomizationsFile, themeIndexPluginsContent) + copyFileSync(customTheme, tmpThemesCustomizationsFile) logger.log( `${chalk.green('success')} - ${ storeConfig.theme @@ -311,10 +307,6 @@ async function copyTheme(basePath: string) { logger.error(`${chalk.red('error')} - ${err}`) } } else { - const themeIndexPluginsContent = - await generateThemeIndexPluginsContent(basePath) - writeFileSync(tmpThemesCustomizationsFile, themeIndexPluginsContent) - logger.info( `${chalk.blue('info')} - The ${ storeConfig.theme diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index a30163497c..9941e4456f 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -214,10 +214,28 @@ export const generateThemeIndexPluginsContent = async ( return [...pluginImports, ...customImports].join('\n') } +const addPluginsTheme = async (basePath: string, plugins: Plugin[]) => { + const { getPackagePath, tmpThemesPluginsFile } = withBasePath(basePath) + + const pluginImportsContent = plugins + .filter((plugin) => + existsSync( + getPackagePath(getPluginName(plugin), 'src', 'themes', 'index.scss') + ) + ) + .map( + (plugin) => `@import "${getPluginName(plugin)}/src/themes/index.scss";` + ) + .join('\n') + + writeFileSync(tmpThemesPluginsFile, pluginImportsContent) +} + export const installPlugins = async (basePath: string) => { const plugins = await getPluginsList(basePath) copyPluginsSrc(basePath, plugins) generatePluginPages(basePath, plugins) addPluginsSections(basePath, plugins) + addPluginsTheme(basePath, plugins) } diff --git a/packages/core/src/pages/_app.tsx b/packages/core/src/pages/_app.tsx index 44aca4b429..d8a3bb9bc0 100644 --- a/packages/core/src/pages/_app.tsx +++ b/packages/core/src/pages/_app.tsx @@ -7,6 +7,7 @@ import SEO from '../../next-seo.config' // FastStore UI's base styles import '../styles/global/index.scss' +import '../plugins/index.scss' import '../customizations/src/themes/index.scss' import { DefaultSeo } from 'next-seo' diff --git a/packages/core/src/plugins/index.scss b/packages/core/src/plugins/index.scss new file mode 100644 index 0000000000..59811cc1a2 --- /dev/null +++ b/packages/core/src/plugins/index.scss @@ -0,0 +1,3 @@ +// ---------------------------------------------------------- +// Plugins' themes will override this file +// ---------------------------------------------------------- From 6da35b876afb4dc720673a1c5b667d4b867aa095 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 14 Nov 2024 17:09:12 -0300 Subject: [PATCH 08/16] feat: apply adjusts in hcms --- packages/cli/src/utils/hcms.ts | 8 ++++---- packages/cli/src/utils/plugins.ts | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/utils/hcms.ts b/packages/cli/src/utils/hcms.ts index 6f5e1821cd..fd30f80674 100644 --- a/packages/cli/src/utils/hcms.ts +++ b/packages/cli/src/utils/hcms.ts @@ -128,12 +128,12 @@ export async function mergeCMSFile(fileName: string, basePath: string) { const plugins = await getPluginsList(basePath) - const pluginPaths = plugins.map((plugin) => - getPackagePath(getPluginName(plugin), 'src', 'cms', fileName) + const pluginCMSFilePaths = plugins.map((plugin) => + getPackagePath(getPluginName(plugin), 'cms', 'faststore', fileName) ) - const customizations = [...pluginPaths, customFilePath].filter((pluginPath) => - existsSync(pluginPath) + const customizations = [...pluginCMSFilePaths, customFilePath].filter( + (pluginCMSFilePath) => existsSync(pluginCMSFilePath) ) // TODO: create a validation when the CMS files exist but don't have a component for them diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 9941e4456f..5642891c39 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -59,9 +59,14 @@ const getPluginSrcPath = async (basePath: string, pluginName: string) => { export const getPluginsList = async (basePath: string): Promise => { const { tmpStoreConfigFile } = withBasePath(basePath) - const { plugins = [] } = await import(tmpStoreConfigFile) + try { + const { plugins } = await import(tmpStoreConfigFile) + return plugins + } catch (error) { + logger.error(`Could not load plugins from store config`) + } - return plugins + return [] } const copyPluginsSrc = async (basePath: string, plugins: Plugin[]) => { @@ -94,7 +99,7 @@ const getPluginPageFileContent = ( import * as page from 'src/plugins/${pluginName}/pages/${pageName}'; ${appLayout ? `import GlobalSections, { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} -export async function getStaticProps({previewData}) { +export async function getStaticProps(${appLayout ? '{previewData}' : ''}) { const noop = async function() {} const loaderData = await (page.loader || noop)() ${appLayout ? `const globalSections = await getGlobalSectionsData(previewData)` : ``} From cf066c06693b04cd096d9a4ccd4226c438431e59 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Tue, 19 Nov 2024 12:42:02 -0300 Subject: [PATCH 09/16] feat: adjust hcms file --- packages/cli/src/utils/hcms.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/utils/hcms.ts b/packages/cli/src/utils/hcms.ts index fd30f80674..4dac87bb31 100644 --- a/packages/cli/src/utils/hcms.ts +++ b/packages/cli/src/utils/hcms.ts @@ -137,31 +137,33 @@ export async function mergeCMSFile(fileName: string, basePath: string) { ) // TODO: create a validation when the CMS files exist but don't have a component for them - for (const customFilePath of customizations) { - const customFile = readFileSync(customFilePath, 'utf8') + for (const newFilePath of customizations) { + const customFile = readFileSync(newFilePath, 'utf8') try { const customDefinitions = JSON.parse(customFile) const { duplicates, newDefinitions } = splitCustomDefinitions( - coreDefinitions, + output, customDefinitions, primaryIdentifierForDefinitions ) if (duplicates.length) { - await confirmUserChoice(duplicates, fileName) + if (newFilePath === customFilePath) { + await confirmUserChoice(duplicates, fileName) + } output = [ ...dedupeAndMergeDefinitions( - coreDefinitions, + output, duplicates, primaryIdentifierForDefinitions ), ...newDefinitions, ] } else { - output = [...coreDefinitions, ...newDefinitions] + output = [...output, ...newDefinitions] } } catch (err) { if (err instanceof SyntaxError) { From 2697cdb15e4fbb5f3b85b3ce0d396d85c8ececb1 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 21 Nov 2024 13:54:46 -0300 Subject: [PATCH 10/16] feat: add SectionOverrideV1 support --- packages/cli/src/utils/plugins.ts | 47 ++++++++++++++++++- .../sections/Alert/OverriddenDefaultAlert.ts | 2 + .../BannerText/OverriddenDefaultBannerText.ts | 2 + .../Breadcrumb/OverriddenDefaultBreadcrumb.ts | 2 + .../OverriddenDefaultCrossSellingShelf.ts | 2 + .../EmptyState/OverriddenDefaultEmptyState.ts | 2 + .../sections/Hero/OverriddenDefaultHero.ts | 2 + .../Navbar/OverriddenDefaultNavbar.ts | 2 + .../Newsletter/OverriddenDefaultNewsletter.ts | 2 + .../OverriddenDefaultProductDetails.ts | 2 + .../OverriddenDefaultProductGallery.ts | 2 + .../OverriddenDefaultProductShelf.ts | 2 + .../RegionBar/OverriddenDefaultRegionBar.ts | 2 + .../customizations/src/GlobalOverrides.tsx | 6 +++ packages/core/src/plugins/overrides/Alert.tsx | 3 ++ .../core/src/plugins/overrides/BannerText.tsx | 3 ++ .../core/src/plugins/overrides/Breadcrumb.tsx | 3 ++ .../plugins/overrides/CrossSellingShelf.tsx | 3 ++ .../core/src/plugins/overrides/EmptyState.tsx | 3 ++ packages/core/src/plugins/overrides/Hero.tsx | 3 ++ .../core/src/plugins/overrides/Navbar.tsx | 3 ++ .../core/src/plugins/overrides/Newsletter.tsx | 3 ++ .../src/plugins/overrides/ProductDetails.tsx | 3 ++ .../src/plugins/overrides/ProductGallery.tsx | 3 ++ .../src/plugins/overrides/ProductShelf.tsx | 3 ++ .../core/src/plugins/overrides/RegionBar.tsx | 3 ++ .../plugins/overrides/ThirdPartyScripts.tsx | 3 ++ .../core/src/plugins/overrides/WebFonts.tsx | 3 ++ 28 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 packages/core/src/plugins/overrides/Alert.tsx create mode 100644 packages/core/src/plugins/overrides/BannerText.tsx create mode 100644 packages/core/src/plugins/overrides/Breadcrumb.tsx create mode 100644 packages/core/src/plugins/overrides/CrossSellingShelf.tsx create mode 100644 packages/core/src/plugins/overrides/EmptyState.tsx create mode 100644 packages/core/src/plugins/overrides/Hero.tsx create mode 100644 packages/core/src/plugins/overrides/Navbar.tsx create mode 100644 packages/core/src/plugins/overrides/Newsletter.tsx create mode 100644 packages/core/src/plugins/overrides/ProductDetails.tsx create mode 100644 packages/core/src/plugins/overrides/ProductGallery.tsx create mode 100644 packages/core/src/plugins/overrides/ProductShelf.tsx create mode 100644 packages/core/src/plugins/overrides/RegionBar.tsx create mode 100644 packages/core/src/plugins/overrides/ThirdPartyScripts.tsx create mode 100644 packages/core/src/plugins/overrides/WebFonts.tsx diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 5642891c39..058ff92979 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -1,4 +1,10 @@ -import { copySync, existsSync, mkdirSync, writeFileSync } from 'fs-extra' +import { + copySync, + existsSync, + mkdirSync, + readdirSync, + writeFileSync, +} from 'fs-extra' import { withBasePath } from './directory' import path from 'path' import { logger } from './logger' @@ -198,6 +204,44 @@ export async function addPluginsSections(basePath: string, plugins: Plugin[]) { logger.log(pluginsImportFileContent) } +export async function addPluginsOverrides(basePath: string, plugins: Plugin[]) { + const { tmpPluginsDir, getPackagePath } = withBasePath(basePath) + + logger.log('Adding plugin overrides') + + plugins + .map((plugin) => ({ + pluginName: getPluginName(plugin), + pluginOverridesPath: getPackagePath( + getPluginName(plugin), + 'src', + 'components', + 'overrides' + ), + })) + .filter(({ pluginOverridesPath }) => existsSync(pluginOverridesPath)) + .reverse() + .forEach(({ pluginName, pluginOverridesPath }) => { + const overrideFilesAlreadyCopied: string[] = [] + + const sanitizedPluginName = sanitizePluginName(pluginName) + + const overrideFiles = readdirSync(pluginOverridesPath) + + overrideFiles + .filter((file) => !overrideFilesAlreadyCopied.includes(file)) + .forEach((overrideFileName) => { + const overrideFileContent = `export { override } from 'src/plugins/${sanitizedPluginName}/components/overrides/${overrideFileName.split('.')[0]}'` + + writeFileSync( + path.join(tmpPluginsDir, 'overrides', overrideFileName), + overrideFileContent + ) + overrideFilesAlreadyCopied.push(overrideFileName) + }) + }) +} + export const generateThemeIndexPluginsContent = async ( basePath: string, ...customImports: string[] @@ -242,5 +286,6 @@ export const installPlugins = async (basePath: string) => { copyPluginsSrc(basePath, plugins) generatePluginPages(basePath, plugins) addPluginsSections(basePath, plugins) + addPluginsOverrides(basePath, plugins) addPluginsTheme(basePath, plugins) } diff --git a/packages/core/src/components/sections/Alert/OverriddenDefaultAlert.ts b/packages/core/src/components/sections/Alert/OverriddenDefaultAlert.ts index 801671febb..f130993bb7 100644 --- a/packages/core/src/components/sections/Alert/OverriddenDefaultAlert.ts +++ b/packages/core/src/components/sections/Alert/OverriddenDefaultAlert.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/Alert' +import { override as overridePlugin } from 'src/plugins/overrides/Alert' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import Alert from '.' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default Alert section present in the Headless CMS */ export const OverriddenDefaultAlert = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'Alert'>), ...(override as SectionOverrideDefinitionV1<'Alert'>), Section: Alert, }) diff --git a/packages/core/src/components/sections/BannerText/OverriddenDefaultBannerText.ts b/packages/core/src/components/sections/BannerText/OverriddenDefaultBannerText.ts index 35ba287938..dc7c6f0bb7 100644 --- a/packages/core/src/components/sections/BannerText/OverriddenDefaultBannerText.ts +++ b/packages/core/src/components/sections/BannerText/OverriddenDefaultBannerText.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/BannerText' +import { override as overridePlugin } from 'src/plugins/overrides/BannerText' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import BannerText from '.' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default BannerText section present in the Headless CMS */ export const OverriddenDefaultBannerText = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'BannerText'>), ...(override as SectionOverrideDefinitionV1<'BannerText'>), Section: BannerText, }) diff --git a/packages/core/src/components/sections/Breadcrumb/OverriddenDefaultBreadcrumb.ts b/packages/core/src/components/sections/Breadcrumb/OverriddenDefaultBreadcrumb.ts index 21af53e079..f4f07d1327 100644 --- a/packages/core/src/components/sections/Breadcrumb/OverriddenDefaultBreadcrumb.ts +++ b/packages/core/src/components/sections/Breadcrumb/OverriddenDefaultBreadcrumb.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/Breadcrumb' +import { override as overridePlugin } from 'src/plugins/overrides/Breadcrumb' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import Breadcrumb from '.' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default Breadcrumb section present in the Headless CMS */ export const OverriddenDefaultBreadcrumb = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'Breadcrumb'>), ...(override as SectionOverrideDefinitionV1<'Breadcrumb'>), Section: Breadcrumb, }) diff --git a/packages/core/src/components/sections/CrossSellingShelf/OverriddenDefaultCrossSellingShelf.ts b/packages/core/src/components/sections/CrossSellingShelf/OverriddenDefaultCrossSellingShelf.ts index 89509e655a..8cc3270102 100644 --- a/packages/core/src/components/sections/CrossSellingShelf/OverriddenDefaultCrossSellingShelf.ts +++ b/packages/core/src/components/sections/CrossSellingShelf/OverriddenDefaultCrossSellingShelf.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/CrossSellingShelf' +import { override as overridePlugin } from 'src/plugins/overrides/CrossSellingShelf' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' import CrossSellingShelf from '.' @@ -9,6 +10,7 @@ import CrossSellingShelf from '.' * This allows users to override the default CrossSellingShelf section present in the Headless CMS */ export const OverriddenDefaultCrossSellingShelf = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'CrossSellingShelf'>), ...(override as SectionOverrideDefinitionV1<'CrossSellingShelf'>), Section: CrossSellingShelf, }) diff --git a/packages/core/src/components/sections/EmptyState/OverriddenDefaultEmptyState.ts b/packages/core/src/components/sections/EmptyState/OverriddenDefaultEmptyState.ts index cc43f67ff9..255a107b14 100644 --- a/packages/core/src/components/sections/EmptyState/OverriddenDefaultEmptyState.ts +++ b/packages/core/src/components/sections/EmptyState/OverriddenDefaultEmptyState.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/EmptyState' +import { override as overridePlugin } from 'src/plugins/overrides/EmptyState' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' @@ -10,6 +11,7 @@ import EmptyState from './EmptyState' * This allows users to override the default EmptyState section present in the Headless CMS */ export const OverriddenDefaultEmptyState = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'EmptyState'>), ...(override as SectionOverrideDefinitionV1<'EmptyState'>), Section: EmptyState, }) diff --git a/packages/core/src/components/sections/Hero/OverriddenDefaultHero.ts b/packages/core/src/components/sections/Hero/OverriddenDefaultHero.ts index 48754cfd28..378504ad25 100644 --- a/packages/core/src/components/sections/Hero/OverriddenDefaultHero.ts +++ b/packages/core/src/components/sections/Hero/OverriddenDefaultHero.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/Hero' +import { override as overridePlugin } from 'src/plugins/overrides/Hero' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import Hero from '.' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default Hero section present in the Headless CMS */ export const OverriddenDefaultHero = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'Hero'>), ...(override as SectionOverrideDefinitionV1<'Hero'>), Section: Hero, }) diff --git a/packages/core/src/components/sections/Navbar/OverriddenDefaultNavbar.ts b/packages/core/src/components/sections/Navbar/OverriddenDefaultNavbar.ts index c5c4075fd6..35e4b3ae10 100644 --- a/packages/core/src/components/sections/Navbar/OverriddenDefaultNavbar.ts +++ b/packages/core/src/components/sections/Navbar/OverriddenDefaultNavbar.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/Navbar' +import { override as overridePlugin } from 'src/plugins/overrides/Navbar' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' import Navbar from './Navbar' @@ -9,6 +10,7 @@ import Navbar from './Navbar' * This allows users to override the default Navbar section present in the Headless CMS */ export const OverriddenDefaultNavbar = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'Navbar'>), ...(override as SectionOverrideDefinitionV1<'Navbar'>), Section: Navbar, }) diff --git a/packages/core/src/components/sections/Newsletter/OverriddenDefaultNewsletter.ts b/packages/core/src/components/sections/Newsletter/OverriddenDefaultNewsletter.ts index 79ee9dc12d..ad9ac497c5 100644 --- a/packages/core/src/components/sections/Newsletter/OverriddenDefaultNewsletter.ts +++ b/packages/core/src/components/sections/Newsletter/OverriddenDefaultNewsletter.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/Newsletter' +import { override as overridePlugin } from 'src/plugins/overrides/Newsletter' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' import Newsletter from './Newsletter' @@ -9,6 +10,7 @@ import Newsletter from './Newsletter' * This allows users to override the default Newsletter section present in the Headless CMS */ export const OverriddenDefaultNewsletter = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'Newsletter'>), ...(override as SectionOverrideDefinitionV1<'Newsletter'>), Section: Newsletter, }) diff --git a/packages/core/src/components/sections/ProductDetails/OverriddenDefaultProductDetails.ts b/packages/core/src/components/sections/ProductDetails/OverriddenDefaultProductDetails.ts index fee8029b1f..03e4ec0cdd 100644 --- a/packages/core/src/components/sections/ProductDetails/OverriddenDefaultProductDetails.ts +++ b/packages/core/src/components/sections/ProductDetails/OverriddenDefaultProductDetails.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/ProductDetails' +import { override as overridePlugin } from 'src/plugins/overrides/ProductDetails' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import ProductDetails from './ProductDetails' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default ProductDetails section present in the Headless CMS */ export const OverriddenDefaultProductDetails = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'ProductDetails'>), ...(override as SectionOverrideDefinitionV1<'ProductDetails'>), Section: ProductDetails, }) diff --git a/packages/core/src/components/sections/ProductGallery/OverriddenDefaultProductGallery.ts b/packages/core/src/components/sections/ProductGallery/OverriddenDefaultProductGallery.ts index 2358cc96a1..17ee0a3a37 100644 --- a/packages/core/src/components/sections/ProductGallery/OverriddenDefaultProductGallery.ts +++ b/packages/core/src/components/sections/ProductGallery/OverriddenDefaultProductGallery.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/ProductGallery' +import { override as overridePlugin } from 'src/plugins/overrides/ProductGallery' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' import ProductGallery from '.' @@ -9,6 +10,7 @@ import ProductGallery from '.' * This allows users to override the default ProductGallery section present in the Headless CMS */ export const OverriddenDefaultProductGallery = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'ProductGallery'>), ...(override as SectionOverrideDefinitionV1<'ProductGallery'>), Section: ProductGallery, }) diff --git a/packages/core/src/components/sections/ProductShelf/OverriddenDefaultProductShelf.ts b/packages/core/src/components/sections/ProductShelf/OverriddenDefaultProductShelf.ts index 0303b79a2e..e5e435969c 100644 --- a/packages/core/src/components/sections/ProductShelf/OverriddenDefaultProductShelf.ts +++ b/packages/core/src/components/sections/ProductShelf/OverriddenDefaultProductShelf.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/ProductShelf' +import { override as overridePlugin } from 'src/plugins/overrides/ProductShelf' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import ProductShelf from '.' @@ -10,6 +11,7 @@ import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinitio * This allows users to override the default ProductShelf section present in the Headless CMS */ export const OverriddenDefaultProductShelf = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'ProductShelf'>), ...(override as SectionOverrideDefinitionV1<'ProductShelf'>), Section: ProductShelf, }) diff --git a/packages/core/src/components/sections/RegionBar/OverriddenDefaultRegionBar.ts b/packages/core/src/components/sections/RegionBar/OverriddenDefaultRegionBar.ts index b937467d5d..102c3ba405 100644 --- a/packages/core/src/components/sections/RegionBar/OverriddenDefaultRegionBar.ts +++ b/packages/core/src/components/sections/RegionBar/OverriddenDefaultRegionBar.ts @@ -1,4 +1,5 @@ import { override } from 'src/customizations/src/components/overrides/RegionBar' +import { override as overridePlugin } from 'src/plugins/overrides/RegionBar' import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' import RegionBar from '.' @@ -9,6 +10,7 @@ import RegionBar from '.' * This allows users to override the default RegionBar section present in the Headless CMS */ export const OverriddenDefaultRegionBar = getOverriddenSection({ + ...(overridePlugin as SectionOverrideDefinitionV1<'RegionBar'>), ...(override as SectionOverrideDefinitionV1<'RegionBar'>), Section: RegionBar, }) diff --git a/packages/core/src/customizations/src/GlobalOverrides.tsx b/packages/core/src/customizations/src/GlobalOverrides.tsx index 77ed094088..4872265ab8 100644 --- a/packages/core/src/customizations/src/GlobalOverrides.tsx +++ b/packages/core/src/customizations/src/GlobalOverrides.tsx @@ -1,9 +1,15 @@ import WebFontsOverrides from 'src/customizations/src/components/overrides/WebFonts' import { default as CoreWebFonts } from 'src/fonts/WebFonts' import ThirdPartyScriptsOverrides from 'src/customizations/src/components/overrides/ThirdPartyScripts' +import ThirdPartyScriptsPluginsOverrides from 'src/plugins/overrides/ThirdPartyScripts' +import WebFontsOverridesPlugins from 'src/plugins/overrides/WebFonts' const Components = { WebFonts: CoreWebFonts, + + ...ThirdPartyScriptsPluginsOverrides.components, + ...WebFontsOverridesPlugins.components, + ...WebFontsOverrides.components, ...ThirdPartyScriptsOverrides.components, } diff --git a/packages/core/src/plugins/overrides/Alert.tsx b/packages/core/src/plugins/overrides/Alert.tsx new file mode 100644 index 0000000000..b4ea57ef7e --- /dev/null +++ b/packages/core/src/plugins/overrides/Alert.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/Alert' diff --git a/packages/core/src/plugins/overrides/BannerText.tsx b/packages/core/src/plugins/overrides/BannerText.tsx new file mode 100644 index 0000000000..2994ef1a3b --- /dev/null +++ b/packages/core/src/plugins/overrides/BannerText.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/BannerText' diff --git a/packages/core/src/plugins/overrides/Breadcrumb.tsx b/packages/core/src/plugins/overrides/Breadcrumb.tsx new file mode 100644 index 0000000000..63c97eb129 --- /dev/null +++ b/packages/core/src/plugins/overrides/Breadcrumb.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/Breadcrumb' diff --git a/packages/core/src/plugins/overrides/CrossSellingShelf.tsx b/packages/core/src/plugins/overrides/CrossSellingShelf.tsx new file mode 100644 index 0000000000..d400dc09e8 --- /dev/null +++ b/packages/core/src/plugins/overrides/CrossSellingShelf.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/CrossSellingShelf' diff --git a/packages/core/src/plugins/overrides/EmptyState.tsx b/packages/core/src/plugins/overrides/EmptyState.tsx new file mode 100644 index 0000000000..0f2a072f80 --- /dev/null +++ b/packages/core/src/plugins/overrides/EmptyState.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/EmptyState' diff --git a/packages/core/src/plugins/overrides/Hero.tsx b/packages/core/src/plugins/overrides/Hero.tsx new file mode 100644 index 0000000000..225bac3493 --- /dev/null +++ b/packages/core/src/plugins/overrides/Hero.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/Hero' diff --git a/packages/core/src/plugins/overrides/Navbar.tsx b/packages/core/src/plugins/overrides/Navbar.tsx new file mode 100644 index 0000000000..3d7dcbc801 --- /dev/null +++ b/packages/core/src/plugins/overrides/Navbar.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/Navbar' diff --git a/packages/core/src/plugins/overrides/Newsletter.tsx b/packages/core/src/plugins/overrides/Newsletter.tsx new file mode 100644 index 0000000000..0da945ad41 --- /dev/null +++ b/packages/core/src/plugins/overrides/Newsletter.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/Newsletter' diff --git a/packages/core/src/plugins/overrides/ProductDetails.tsx b/packages/core/src/plugins/overrides/ProductDetails.tsx new file mode 100644 index 0000000000..277e1fde77 --- /dev/null +++ b/packages/core/src/plugins/overrides/ProductDetails.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/ProductDetails' diff --git a/packages/core/src/plugins/overrides/ProductGallery.tsx b/packages/core/src/plugins/overrides/ProductGallery.tsx new file mode 100644 index 0000000000..bc9feca0e3 --- /dev/null +++ b/packages/core/src/plugins/overrides/ProductGallery.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/ProductGallery' diff --git a/packages/core/src/plugins/overrides/ProductShelf.tsx b/packages/core/src/plugins/overrides/ProductShelf.tsx new file mode 100644 index 0000000000..1ebae07955 --- /dev/null +++ b/packages/core/src/plugins/overrides/ProductShelf.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/ProductShelf' diff --git a/packages/core/src/plugins/overrides/RegionBar.tsx b/packages/core/src/plugins/overrides/RegionBar.tsx new file mode 100644 index 0000000000..f4c6bc43de --- /dev/null +++ b/packages/core/src/plugins/overrides/RegionBar.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { override } from 'src/customizations/src/components/overrides/RegionBar' diff --git a/packages/core/src/plugins/overrides/ThirdPartyScripts.tsx b/packages/core/src/plugins/overrides/ThirdPartyScripts.tsx new file mode 100644 index 0000000000..e32f63c144 --- /dev/null +++ b/packages/core/src/plugins/overrides/ThirdPartyScripts.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { default } from 'src/customizations/src/components/overrides/ThirdPartyScripts' diff --git a/packages/core/src/plugins/overrides/WebFonts.tsx b/packages/core/src/plugins/overrides/WebFonts.tsx new file mode 100644 index 0000000000..c192792437 --- /dev/null +++ b/packages/core/src/plugins/overrides/WebFonts.tsx @@ -0,0 +1,3 @@ +// This is an example of how it can be used on the plugins. + +export { default } from 'src/customizations/src/components/overrides/WebFonts' From 6c9c3f7bbd94a5a89ea75bbfa4c839e9065f3d9e Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Mon, 16 Dec 2024 12:04:30 -0300 Subject: [PATCH 11/16] feat: change StaticProps to ServerSide --- packages/cli/src/utils/plugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 058ff92979..a533acd8c5 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -105,7 +105,7 @@ const getPluginPageFileContent = ( import * as page from 'src/plugins/${pluginName}/pages/${pageName}'; ${appLayout ? `import GlobalSections, { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} -export async function getStaticProps(${appLayout ? '{previewData}' : ''}) { +export async function getServerSideProps(${appLayout ? '{previewData}' : ''}) { const noop = async function() {} const loaderData = await (page.loader || noop)() ${appLayout ? `const globalSections = await getGlobalSectionsData(previewData)` : ``} From 4f342eff299b9005e8e3bedb011f9cc81d6f247b Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Thu, 19 Dec 2024 10:57:53 -0300 Subject: [PATCH 12/16] feat: adjust plugin loader function props --- packages/cli/src/utils/plugins.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index a533acd8c5..6f8a21d0a4 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -102,12 +102,13 @@ const getPluginPageFileContent = ( appLayout: boolean ) => ` // GENERATED FILE +// @ts-nocheck import * as page from 'src/plugins/${pluginName}/pages/${pageName}'; ${appLayout ? `import GlobalSections, { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} -export async function getServerSideProps(${appLayout ? '{previewData}' : ''}) { +export async function getServerSideProps(${appLayout ? '{ previewData, ...otherProps }' : 'otherProps'}) { const noop = async function() {} - const loaderData = await (page.loader || noop)() + const loaderData = await (page.loader || noop)(otherProps) ${appLayout ? `const globalSections = await getGlobalSectionsData(previewData)` : ``} return { From 4b98b4d022ddf3178f4f886be306a1b0772b291c Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Fri, 20 Dec 2024 18:42:22 -0300 Subject: [PATCH 13/16] feat: Add copy plugin public files --- packages/cli/src/utils/plugins.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 6f8a21d0a4..cf188825cb 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -96,6 +96,29 @@ const copyPluginsSrc = async (basePath: string, plugins: Plugin[]) => { }) } +const copyPluginPublicFiles = async (basePath: string, plugins: Plugin[]) => { + const { tmpDir, getPackagePath } = withBasePath(basePath) + + logger.log('Copying plugin public files') + + plugins.forEach(async (plugin) => { + const pluginName = getPluginName(plugin) + const pluginPath = getPackagePath(getPluginName(pluginName)) + + try { + if (existsSync(`${pluginPath}/public`)) { + copySync(`${pluginPath}/public`, `${tmpDir}/public`, { + dereference: true, + overwrite: true, + }) + logger.log(`Plugin public files copied`) + } + } catch (e) { + logger.error(e) + } + }) +} + const getPluginPageFileContent = ( pluginName: string, pageName: string, @@ -285,6 +308,7 @@ export const installPlugins = async (basePath: string) => { const plugins = await getPluginsList(basePath) copyPluginsSrc(basePath, plugins) + copyPluginPublicFiles(basePath, plugins) generatePluginPages(basePath, plugins) addPluginsSections(basePath, plugins) addPluginsOverrides(basePath, plugins) From 4aaedb3abf9cb71aff299bfcb9d84998fca60ea0 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Mon, 13 Jan 2025 09:59:57 -0300 Subject: [PATCH 14/16] fix: adjust empty plugins list --- packages/cli/src/utils/plugins.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index cf188825cb..e0e9dcb87f 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -66,7 +66,7 @@ export const getPluginsList = async (basePath: string): Promise => { const { tmpStoreConfigFile } = withBasePath(basePath) try { - const { plugins } = await import(tmpStoreConfigFile) + const { plugins = [] } = await import(tmpStoreConfigFile) return plugins } catch (error) { logger.error(`Could not load plugins from store config`) From a0fbd79984a3b86e7ef4d6777c7ac4cb4497eaf5 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Mon, 13 Jan 2025 10:51:07 -0300 Subject: [PATCH 15/16] fix: adjust page generator after refactor --- packages/cli/src/utils/plugins.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index e0e9dcb87f..746e01bc98 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -127,26 +127,27 @@ const getPluginPageFileContent = ( // GENERATED FILE // @ts-nocheck import * as page from 'src/plugins/${pluginName}/pages/${pageName}'; -${appLayout ? `import GlobalSections, { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} +${appLayout ? `import { getGlobalSectionsData } from 'src/components/cms/GlobalSections'` : ``} +${appLayout ? `import RenderSections from 'src/components/cms/RenderSections'` : ``} export async function getServerSideProps(${appLayout ? '{ previewData, ...otherProps }' : 'otherProps'}) { const noop = async function() {} const loaderData = await (page.loader || noop)(otherProps) -${appLayout ? `const globalSections = await getGlobalSectionsData(previewData)` : ``} +${appLayout ? `const { sections = [] } = await getGlobalSectionsData(previewData)` : ``} return { props: { data: loaderData, - ${appLayout ? 'globalSections: globalSections' : ``} + ${appLayout ? 'globalSections: sections' : ``} } } } export default function Page(props) { ${ appLayout - ? `return + ? `return {page.default(props.data)} - ` + ` : `return page.default(props.data)` } } From 721cebc69ffa5629216ea3fc4d688f3905dc4e66 Mon Sep 17 00:00:00 2001 From: Arthur Andrade Date: Wed, 22 Jan 2025 16:19:58 -0300 Subject: [PATCH 16/16] fix: remove generateThemeIndexPluginsContent --- packages/cli/src/utils/plugins.ts | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/packages/cli/src/utils/plugins.ts b/packages/cli/src/utils/plugins.ts index 746e01bc98..f2e3254260 100644 --- a/packages/cli/src/utils/plugins.ts +++ b/packages/cli/src/utils/plugins.ts @@ -267,27 +267,6 @@ export async function addPluginsOverrides(basePath: string, plugins: Plugin[]) { }) } -export const generateThemeIndexPluginsContent = async ( - basePath: string, - ...customImports: string[] -) => { - const { getPackagePath } = withBasePath(basePath) - - const plugins = await getPluginsList(basePath) - - const pluginImports = plugins - .filter((plugin) => - existsSync( - getPackagePath(getPluginName(plugin), 'src', 'themes', 'index.scss') - ) - ) - .map( - (plugin) => `@import "${getPluginName(plugin)}/src/themes/index.scss";` - ) - - return [...pluginImports, ...customImports].join('\n') -} - const addPluginsTheme = async (basePath: string, plugins: Plugin[]) => { const { getPackagePath, tmpThemesPluginsFile } = withBasePath(basePath)