Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat Adds Plugins feature #2563

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions packages/cli/src/utils/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,75 @@ 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 packageFromNodeModules = path.join(
'node_modules',
'@faststore',
'core'
)
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 tmpDir = path.join(getRoot(), tmpFolderName)
const userSrcDir = path.join(getRoot(), 'src')
const getPackagePath = (...packagePath: string[]) =>
path.join(getRoot(), 'node_modules', ...packagePath)

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'
),
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'),
tmpStoreConfigFile: path.join(
tmpDir,
'src',
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -496,5 +497,7 @@ export async function generate(options: GenerateOptions) {
createCmsWebhookUrlsJsonFile(basePath),
updateNextConfig(basePath),
enableRedirectsMiddleware(basePath),

installPlugins(basePath),
])
}
31 changes: 23 additions & 8 deletions packages/cli/src/utils/hcms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CliUx } from '@oclif/core'
import { readFileSync, existsSync, writeFileSync } from 'fs-extra'

import { withBasePath } from './directory'
import { getPluginName, getPluginsList } from './plugins'

export interface ContentTypeOrSectionDefinition {
id?: string
Expand Down Expand Up @@ -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]`
Expand All @@ -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)
Expand All @@ -123,32 +126,44 @@ export async function mergeCMSFile(fileName: string, basePath: string) {

let output: ContentTypeOrSectionDefinition[] = coreDefinitions

const plugins = await getPluginsList(basePath)

const pluginCMSFilePaths = plugins.map((plugin) =>
getPackagePath(getPluginName(plugin), 'cms', 'faststore', fileName)
)

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
if (existsSync(customFilePath)) {
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) {
Expand Down
Loading
Loading