Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(new), fail fast when the specified path exists #8703

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BitError } from '@teambit/bit-error';

export class WorkspacePathExists extends BitError {
constructor(readonly path: string) {
super(`unable to create a workspace at "${path}", this path already exists`);
}
}
13 changes: 9 additions & 4 deletions scopes/generator/generator/generator.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from 'fs-extra';
import { resolve } from 'path';
import { GraphqlAspect, GraphqlMain } from '@teambit/graphql';
import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
import { WorkspaceAspect, OutsideWorkspaceError, Workspace } from '@teambit/workspace';
import { EnvDefinition, EnvsAspect, EnvsMain } from '@teambit/envs';
import ComponentConfig from '@teambit/legacy/dist/consumer/config';
import { WorkspaceConfigFilesAspect, WorkspaceConfigFilesMain } from '@teambit/workspace-config-files';

import { ComponentAspect, ComponentID } from '@teambit/component';
import type { ComponentMain, Component } from '@teambit/component';

import { isCoreAspect, loadBit, restoreGlobals } from '@teambit/bit';
import { Slot, SlotRegistry } from '@teambit/harmony';
import { GitAspect, GitMain } from '@teambit/git';
Expand Down Expand Up @@ -35,6 +35,7 @@ import {
import { BasicWorkspaceStarter } from './templates/basic';
import { StarterPlugin } from './starter.plugin';
import { GeneratorService } from './generator.service';
import { WorkspacePathExists } from './exceptions/workspace-path-exists';

export type ComponentTemplateSlot = SlotRegistry<ComponentTemplate[]>;
export type WorkspaceTemplateSlot = SlotRegistry<WorkspaceTemplate[]>;
Expand Down Expand Up @@ -357,14 +358,18 @@ export class GeneratorMain {
if (this.workspace) {
throw new BitError('Error: unable to generate a new workspace inside of an existing workspace');
}
const workspacePath = options.currentDir ? process.cwd() : resolve(workspaceName);
if (!options.currentDir && fs.existsSync(workspacePath)) {
throw new WorkspacePathExists(workspacePath);
}
const { aspect: aspectId, loadFrom } = options;
const { workspaceTemplate, aspect } = loadFrom
? await this.findTemplateInOtherWorkspace(loadFrom, templateName, aspectId)
: await this.getWorkspaceTemplate(templateName, aspectId);

if (!workspaceTemplate) throw new BitError(`template "${templateName}" was not found`);
const workspaceGenerator = new WorkspaceGenerator(workspaceName, options, workspaceTemplate, aspect);
const workspacePath = await workspaceGenerator.generate();
const workspaceGenerator = new WorkspaceGenerator(workspaceName, workspacePath, options, workspaceTemplate, aspect);
await workspaceGenerator.generate();
return { workspacePath, appName: workspaceTemplate.appName };
}

Expand Down
13 changes: 3 additions & 10 deletions scopes/generator/generator/workspace-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ImporterAspect, ImporterMain } from '@teambit/importer';
import { CompilerAspect, CompilerMain } from '@teambit/compiler';
import getGitExecutablePath from '@teambit/legacy/dist/utils/git/git-executable';
import GitNotFound from '@teambit/legacy/dist/utils/git/exceptions/git-not-found';
import { resolve, join } from 'path';
import { join } from 'path';
import { ComponentID } from '@teambit/component-id';
import { GitAspect, GitMain } from '@teambit/git';
import { InstallAspect, InstallMain } from '@teambit/install';
Expand All @@ -27,7 +27,6 @@ import { GeneratorMain } from './generator.main.runtime';
export type GenerateResult = { id: ComponentID; dir: string; files: string[]; envId: string };

export class WorkspaceGenerator {
private workspacePath: string;
private harmony: Harmony;
private workspace: Workspace;
private install: InstallMain;
Expand All @@ -38,21 +37,15 @@ export class WorkspaceGenerator {
private wsConfigFiles: WorkspaceConfigFilesMain;
private generator: GeneratorMain;

// private componentGenerator?: ComponentGenerator;

constructor(
private workspaceName: string,
private workspacePath: string,
private options: NewOptions & { currentDir?: boolean },
private template: WorkspaceTemplate,
private aspectComponent?: Component
) {
this.workspacePath = options.currentDir ? process.cwd() : resolve(this.workspaceName);
}
) {}

async generate(): Promise<string> {
if (!this.options.currentDir && fs.existsSync(this.workspacePath)) {
throw new Error(`unable to create a workspace at "${this.workspaceName}", this path already exists`);
}
await fs.ensureDir(this.workspacePath);
try {
process.chdir(this.workspacePath);
Expand Down