Skip to content

Commit

Permalink
feat: Add hCMS plugin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurTriis1 committed Nov 14, 2024
1 parent db80a44 commit 2c1c3dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
14 changes: 8 additions & 6 deletions packages/cli/src/utils/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand All @@ -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,
Expand Down
19 changes: 16 additions & 3 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 { 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,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 {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> => {
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[]) => {
Expand Down

0 comments on commit 2c1c3dd

Please sign in to comment.