Skip to content

Commit

Permalink
feat: simplify file saving logic by removing nested structure and mak…
Browse files Browse the repository at this point in the history
…ing suffix optional
  • Loading branch information
alvarosabu committed Jan 8, 2025
1 parent 5081d23 commit 6b28089
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/commands/components/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,44 +122,37 @@ export const saveComponentsToFiles = async (
options: PullComponentsOptions,
) => {
const { components, groups, presets } = spaceData
const { filename = 'components', suffix = space, path, separateFiles } = options
const resolvedPath = resolvePath(path, 'components')
const { filename = 'components', suffix, path, separateFiles } = options
const resolvedPath = resolvePath(path, space)

try {
if (separateFiles) {
// Save in separate files with nested structure
// Save in separate files without nested structure
for (const component of components) {
const groupPath = component.component_group_uuid
? resolveGroupPath(component.component_group_uuid, groups)
: ''

const componentPath = join(resolvedPath, groupPath)

// Save component definition
const componentFilePath = join(componentPath, `${component.name}.${suffix}.json`)
const componentFilePath = join(resolvedPath, suffix ? `${component.name}.${suffix}.json` : `${component.name}.json`)
await saveToFile(componentFilePath, JSON.stringify(component, null, 2))

// Find and save associated presets
const componentPresets = presets.filter(preset => preset.component_id === component.id)
if (componentPresets.length > 0) {
const presetsFilePath = join(componentPath, `${component.name}.presets.${suffix}.json`)
const presetsFilePath = join(resolvedPath, suffix ? `${component.name}.presets.${suffix}.json` : `${component.name}.presets.json`)
await saveToFile(presetsFilePath, JSON.stringify(componentPresets, null, 2))
}
}
return
}

// Default to saving consolidated files
const componentsFilePath = join(resolvedPath, `${filename}.${suffix}.json`)
const componentsFilePath = join(resolvedPath, suffix ? `${filename}.${suffix}.json` : `${filename}.json`)
await saveToFile(componentsFilePath, JSON.stringify(components, null, 2))

if (groups.length > 0) {
const groupsFilePath = join(resolvedPath, `groups.${suffix}.json`)
const groupsFilePath = join(resolvedPath, suffix ? `groups.${suffix}.json` : `groups.json`)
await saveToFile(groupsFilePath, JSON.stringify(groups, null, 2))
}

if (presets.length > 0) {
const presetsFilePath = join(resolvedPath, `presets.${suffix}.json`)
const presetsFilePath = join(resolvedPath, suffix ? `presets.${suffix}.json` : `presets.json`)
await saveToFile(presetsFilePath, JSON.stringify(presets, null, 2))
}
}
Expand Down

0 comments on commit 6b28089

Please sign in to comment.