Skip to content

Commit

Permalink
fix duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Makisuo committed Feb 14, 2025
1 parent b1bfd95 commit 9190ff6
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ const exceptions = ["field", "dropdown", "dialog"]
* @param componentName string
* @param processed Set<string>
*/
async function updateIndexFile(
config: Config,
componentName: string,
processed: Set<string> = new Set(),
) {
async function updateIndexFile(config: Config, componentName: string, processed: Set<string>) {
if (processed.has(componentName)) {
return
}
Expand All @@ -42,49 +38,36 @@ async function updateIndexFile(
const primitiveExport = `export * from './primitive';`
const componentExport = `export * from './${componentName}';`

let existingExports: string[] = []
// Use a Set to deduplicate export lines
const exportSet = new Set<string>()

if (fs.existsSync(indexPath)) {
existingExports = fs
.readFileSync(indexPath, "utf-8")
fs.readFileSync(indexPath, "utf-8")
.split("\n")
.map((line) => line.trim())
.filter((line) => line !== "")
.forEach((line) => exportSet.add(line))
}

/**
* Filter out the existing exports that are not related to the component being added.
* This ensures that only the necessary exports are included in the index file.
*/
existingExports = existingExports.filter((line) => {
// Filter out exports related to "primitive" or names in namespaces
Array.from(exportSet).forEach((line) => {
const match = line.match(/export \* from '\.\/(.+)';/)
const matchedComponent = match?.[1]
return (
matchedComponent !== "primitive" &&
!namespaces.includes(matchedComponent ?? "") &&
matchedComponent !== componentName
)
if (matchedComponent === "primitive" || namespaces.includes(matchedComponent ?? "")) {
exportSet.delete(line)
}
})
/**
* If the component is not already exported, add it to the existing exports.
* This ensures that the component is properly exported and included in the index file.
*/
if (!existingExports.includes(componentExport)) {
existingExports.push(componentExport)
}

/**
* Sort the existing exports and add the primitive export at the beginning.
* This ensures that the primitive export is always included first in the index file.
*/
existingExports = [primitiveExport, ...[...new Set(existingExports)].sort()]
exportSet.add(componentExport)

fs.writeFileSync(indexPath, `${existingExports.join("\n")}\n`, { flag: "w" })
const sortedExports = Array.from(exportSet).sort()
const finalExports = [primitiveExport, ...sortedExports]

fs.writeFileSync(indexPath, `${finalExports.join("\n")}\n`, { flag: "w" })

processed.add(componentName)

/**
* If the component has child components, recursively update the index file for each child component.
*/
// Recursively update for child components if any
const component = components.find((c) => c.name === componentName)
if (component?.children) {
for (const child of component.children) {
Expand All @@ -93,6 +76,12 @@ async function updateIndexFile(
}
}

// Wrapper to ensure a single processed Set is used for the update run
export async function updateIndexFileWrapper(config: Config, componentName: string) {
const processed = new Set<string>()
await updateIndexFile(config, componentName, processed)
}

/**
* This function is used to add new components to the project
* @param options any
Expand Down Expand Up @@ -297,7 +286,7 @@ export async function add(options: {
})
}

await updateIndexFile(config, componentName)
await updateIndexFileWrapper(config, componentName)
} catch (error) {
console.error(warningText(`Error processing '${componentName}'.`))
}
Expand Down

0 comments on commit 9190ff6

Please sign in to comment.