Skip to content

Commit

Permalink
feat: pass space data as an object to the save components to files ac…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
alvarosabu committed Dec 12, 2024
1 parent 775968d commit 5081d23
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
27 changes: 7 additions & 20 deletions src/commands/components/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface SpaceComponentPreset {
description: string
}

export interface SpaceData {
components: SpaceComponent[]
groups: SpaceComponentGroup[]
presets: SpaceComponentPreset[]
}
/**
* Resolves the nested folder structure based on component group hierarchy.
* @param groupUuid - The UUID of the component group.
Expand Down Expand Up @@ -113,11 +118,10 @@ export const fetchComponentPresets = async (space: string, token: string, region

export const saveComponentsToFiles = async (
space: string,
components: SpaceComponent[],
groups: SpaceComponentGroup[],
presets: SpaceComponentPreset[],
spaceData: SpaceData,
options: PullComponentsOptions,
) => {
const { components, groups, presets } = spaceData
const { filename = 'components', suffix = space, path, separateFiles } = options
const resolvedPath = resolvePath(path, 'components')

Expand Down Expand Up @@ -163,20 +167,3 @@ export const saveComponentsToFiles = async (
handleFileSystemError('write', error as Error)
}
}

export const saveComponentPresetsToFiles = async (
space: string,
presets: SpaceComponentPreset[],
options: PullComponentsOptions,
) => {
const { filename = 'presets', suffix = space, path } = options

try {
const resolvedPath = resolvePath(path, 'components')
const filePath = join(resolvedPath, `${filename}.${suffix}.json`)
await saveToFile(filePath, JSON.stringify(presets, null, 2))
}
catch (error) {
handleFileSystemError('write', error as Error)
}
}
18 changes: 5 additions & 13 deletions src/commands/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ componentsCommand
}

// Save everything using the new structure
await saveComponentsToFiles(space, components, groups || [], presets || [], { ...options, path })
await saveComponentsToFiles(
space,
{ components, groups: groups || [], presets: presets || [] },
{ ...options, path },
)

if (separateFiles) {
if (filename !== 'components') {
Expand All @@ -64,18 +68,6 @@ componentsCommand
const msgFilename = `${filename}.${suffix}.json`
konsola.ok(`Components downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(path ? `${path}/${msgFilename}` : `./storyblok/components/${msgFilename}`)}`)
}

// Fetch component groups
/* const componentGroups = await fetchComponentGroups(space, state.password, state.region)
if (!componentGroups || componentGroups.length === 0) {
konsola.warn(`No component groups found in the space ${space}`)
return
}
await saveComponentsToFiles(space, componentGroups, { ...options, path, filename: 'groups' })
konsola.ok(`Component groups downloaded successfully in ${chalk.hex(colorPalette.PRIMARY)(path ? `${path}/groups.${suffix}.json` : `./storyblok/components/groups.${suffix}.json`)}`) */
}
catch (error) {
handleError(error as Error, verbose)
Expand Down

0 comments on commit 5081d23

Please sign in to comment.