Skip to content

Commit

Permalink
feat(create): add --force param for create cmd (teambit#8708)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe authored Mar 25, 2024
1 parent afff1b9 commit 3d2ffe9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions scopes/generator/generator/component-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export class ComponentGenerator {
private envId?: ComponentID
) {}

async generate(): Promise<GenerateResult[]> {
async generate(force = false): Promise<GenerateResult[]> {
const dirsToDeleteIfFailed: string[] = [];
const generateResults = await pMapSeries(this.componentIds, async (componentId) => {
try {
const componentPath = this.newComponentHelper.getNewComponentPath(componentId, {
pathFromUser: this.options.path,
componentsToCreate: this.componentIds.length,
});
if (fs.existsSync(path.join(this.workspace.path, componentPath))) {
if (!force && fs.existsSync(path.join(this.workspace.path, componentPath))) {
throw new BitError(
`unable to create a component at "${componentPath}", this path already exists, please use "--path" to create the component in a different path`
);
Expand Down
3 changes: 3 additions & 0 deletions scopes/generator/generator/create.cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { BaseComponentTemplateOptions } from './component-template';
export type CreateOptions = BaseComponentTemplateOptions & {
env?: string;
aspect?: string;
force?: boolean;
};

export class CreateCmd implements Command {
Expand Down Expand Up @@ -58,6 +59,7 @@ export class CreateCmd implements Command {
['t', 'template <string>', 'env-id of the template. alias for --aspect.'],
['p', 'path <string>', 'relative path in the workspace. by default the path is `<scope>/<namespace>/<name>`'],
['e', 'env <string>', "set the component's environment. (overrides the env from variants and the template)"],
['f', 'force', 'replace existing files at the target location'],
] as CommandOptions;

constructor(private generator: GeneratorMain) {}
Expand All @@ -66,6 +68,7 @@ export class CreateCmd implements Command {
[templateName, componentNames]: [string, string[]],
options: Partial<CreateOptions> & {
template?: string | ComponentID;
force?: boolean;
}
) {
options.aspectId = options.aspectId ?? options.template;
Expand Down
2 changes: 1 addition & 1 deletion scopes/generator/generator/generator.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export class GeneratorMain {
templateWithId.id,
envId
);
return componentGenerator.generate();
return componentGenerator.generate(options.force);
}

private async getEnvIdFromTemplateWithId(templateWithId: ComponentTemplateWithId): Promise<ComponentID | undefined> {
Expand Down

0 comments on commit 3d2ffe9

Please sign in to comment.