From 53b64bb3201ec2467a2ff5cde169ab42ce6ab16c Mon Sep 17 00:00:00 2001 From: Yucel Okcu Date: Sun, 24 Jan 2021 00:53:30 +0300 Subject: [PATCH] Add new component IDs into existing component file entries --- src/commands/connect/addComponent.ts | 2 +- src/tasks/add-component-config.ts | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/commands/connect/addComponent.ts b/src/commands/connect/addComponent.ts index 7fb9388..fff7f54 100644 --- a/src/commands/connect/addComponent.ts +++ b/src/commands/connect/addComponent.ts @@ -18,7 +18,7 @@ import { ConnectedComponentsService } from "./service"; import logger from "../../util/logger"; import { notInitialized, userSelectedNotToInitialize, addSummary, initializationPrompt } from "../../messages"; import { getComponentConfigFiles } from "./config"; -import { initialize } from ".."; +import { initialize } from "./initialize"; import { commandRunner } from "../../util/commander"; const askForInitialization = async (): Promise => { diff --git a/src/tasks/add-component-config.ts b/src/tasks/add-component-config.ts index 42f3366..03fbbb1 100644 --- a/src/tasks/add-component-config.ts +++ b/src/tasks/add-component-config.ts @@ -59,18 +59,34 @@ const addComponent: TaskStep = async (ctx, task) => { selectedComponents.find(sc => existingZeplinId?.includes(sc._id)) )); + const componentWithSameFile = existingComponents.find(ec => ec.path === ctx.file.path); + if ((componentNameExists || componentIdExists) && !(await confirmAddExistingComponent(ctx, task))) { throw new TaskError(ui.existingComponent); } - config.components = [ - ...existingComponents, - { + const selectedComponentIds = selectedComponents.map(c => c._id); + + if (componentWithSameFile) { + const existingZeplinIds = componentWithSameFile.zeplinIds || []; + + componentWithSameFile.zeplinIds = Array.from(new Set([ + ...existingZeplinIds, + ...selectedComponentIds + ])); + logger.debug(`Added new component ID into existing component entry ${stringify(componentWithSameFile)}`); + } else { + const newComponent = { path: ctx.file.path, - zeplinIds: selectedComponents.map(c => c._id) - } - ]; + zeplinIds: selectedComponentIds + }; + config.components = [ + ...existingComponents, + newComponent + ]; + logger.debug(`Added new component entry ${stringify(newComponent)}`); + } logger.debug(`Updated config: ${stringify({ config })}`);