Skip to content

Commit

Permalink
Add new component IDs into existing component file entries
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqu committed Jan 24, 2021
1 parent 1948632 commit aed47b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands/connect/addComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> => {
Expand Down
28 changes: 22 additions & 6 deletions src/tasks/add-component-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,34 @@ const addComponent: TaskStep<AddComponentContext> = 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<string>([
...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 })}`);

Expand Down

0 comments on commit aed47b8

Please sign in to comment.