mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
update new workspace validation to throw errors to make code more reusable (#13856)
This commit is contained in:
@@ -161,30 +161,25 @@ export abstract class DialogBase {
|
||||
}
|
||||
}
|
||||
|
||||
public async validateNewWorkspace(sameFolderAsNewProject: boolean): Promise<boolean> {
|
||||
public async validateNewWorkspace(sameFolderAsNewProject: boolean): Promise<void> {
|
||||
// workspace file should end in .code-workspace
|
||||
const workspaceValid = this.workspaceInputBox!.value!.endsWith(constants.WorkspaceFileExtension);
|
||||
if (!workspaceValid) {
|
||||
this.showErrorMessage(constants.WorkspaceFileInvalidError(this.workspaceInputBox!.value!));
|
||||
return false;
|
||||
throw new Error(constants.WorkspaceFileInvalidError(this.workspaceInputBox!.value!));
|
||||
}
|
||||
|
||||
// if the workspace file is not going to be in the same folder as the newly created project, then check that it's a valid folder
|
||||
if (!sameFolderAsNewProject) {
|
||||
const workspaceParentDirectoryExists = await directoryExist(path.dirname(this.workspaceInputBox!.value!));
|
||||
if (!workspaceParentDirectoryExists) {
|
||||
this.showErrorMessage(constants.WorkspaceParentDirectoryNotExistError(path.dirname(this.workspaceInputBox!.value!)));
|
||||
return false;
|
||||
throw new Error(constants.WorkspaceParentDirectoryNotExistError(path.dirname(this.workspaceInputBox!.value!)));
|
||||
}
|
||||
}
|
||||
|
||||
// workspace file should not be an existing workspace file
|
||||
const workspaceFileExists = await fileExist(this.workspaceInputBox!.value!);
|
||||
if (workspaceFileExists) {
|
||||
this.showErrorMessage(constants.WorkspaceFileAlreadyExistsError(this.workspaceInputBox!.value!));
|
||||
return false;
|
||||
throw new Error(constants.WorkspaceFileAlreadyExistsError(this.workspaceInputBox!.value!));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,9 +43,9 @@ export class NewProjectDialog extends DialogBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
const sameFolderAsNewProject = path.join(this.model.location, this.model.name) === path.dirname(this.workspaceInputBox!.value!);
|
||||
if (this.workspaceInputBox!.enabled && !await this.validateNewWorkspace(sameFolderAsNewProject)) {
|
||||
return false;
|
||||
if (this.workspaceInputBox!.enabled) {
|
||||
const sameFolderAsNewProject = path.join(this.model.location, this.model.name) === path.dirname(this.workspaceInputBox!.value!);
|
||||
await this.validateNewWorkspace(sameFolderAsNewProject);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -37,17 +37,13 @@ export class OpenExistingDialog extends DialogBase {
|
||||
try {
|
||||
// the selected location should be an existing directory
|
||||
if (this._targetTypeRadioCardGroup?.selectedCardId === constants.Project) {
|
||||
if (!await this.validateFile(this._projectFile, constants.Project.toLowerCase())) {
|
||||
return false;
|
||||
}
|
||||
await this.validateFile(this._projectFile, constants.Project.toLowerCase());
|
||||
|
||||
if (this.workspaceInputBox!.enabled && !await this.validateNewWorkspace(false)) {
|
||||
return false;
|
||||
if (this.workspaceInputBox!.enabled) {
|
||||
await this.validateNewWorkspace(false);
|
||||
}
|
||||
} else if (this._targetTypeRadioCardGroup?.selectedCardId === constants.Workspace) {
|
||||
if (!await this.validateFile(this._workspaceFile, constants.Workspace.toLowerCase())) {
|
||||
return false;
|
||||
}
|
||||
await this.validateFile(this._workspaceFile, constants.Workspace.toLowerCase());
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -58,14 +54,11 @@ export class OpenExistingDialog extends DialogBase {
|
||||
}
|
||||
}
|
||||
|
||||
public async validateFile(file: string, fileType: string) {
|
||||
public async validateFile(file: string, fileType: string): Promise<void> {
|
||||
const fileExists = await fileExist(file);
|
||||
if (!fileExists) {
|
||||
this.showErrorMessage(constants.FileNotExistError(fileType, file));
|
||||
return false;
|
||||
throw new Error(constants.FileNotExistError(fileType, file));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async onComplete(): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user