Skip to content

Commit

Permalink
Error if project directory inside another project
Browse files Browse the repository at this point in the history
  • Loading branch information
charlielee committed Oct 22, 2024
1 parent 675fa04 commit 7ff2f84
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useContext, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom";
import { PageRoute } from "../../../../common/PageRoute";
import { DEFAULT_PROJECT_NAME } from "../../../../common/utils";
import { DEFAULT_PROJECT_NAME, PROJECT_DIRECTORY_EXTENSION } from "../../../../common/utils";
import useWorkingDirectory from "../../../hooks/useWorkingDirectory";
import {
addProject,
Expand Down Expand Up @@ -37,6 +37,7 @@ import { JSXElementWithTestIds } from "../../../types";
import { PersistedDirectoriesContext } from "../../../context/PersistedDirectoriesContext/PersistedDirectoriesContext";
import { Project } from "../../../../common/project/Project";
import { CreateDirectoryAlreadyExistsError } from "../../../services/fileManager/FileErrors";
import { ProjectDirectoryIsInsideAnotherProjectError } from "../../../context/PersistedDirectoriesContext/PersistedDirectoriesErrors";

const ProjectSettingsModal = (): JSXElementWithTestIds => {
const dispatch: ThunkDispatch<RootState, void, Action> = useDispatch();
Expand Down Expand Up @@ -80,12 +81,17 @@ const ProjectSettingsModal = (): JSXElementWithTestIds => {
navigate(PageRoute.ANIMATOR);
} catch (e) {
if (e instanceof CreateDirectoryAlreadyExistsError) {
setErrorMessage(
return setErrorMessage(
"Unable to create project as a project already exists with this name. Please rename your project and try again."
);
} else {
throw e;
}
if (e instanceof ProjectDirectoryIsInsideAnotherProjectError) {
return setErrorMessage(
`Unable to create project as the selected project directory is another .${PROJECT_DIRECTORY_EXTENSION} folder. Please select a different directory and try again.`
);
}

throw e;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import useWorkingDirectory from "../../hooks/useWorkingDirectory";
import { Project } from "../../../common/project/Project";
import { makeProjectDirectoryName } from "../../services/project/projectBuilder";
import { PROJECT_DIRECTORY_EXTENSION } from "../../../common/utils";
import { ProjectDirectoryIsInsideAnotherProjectError } from "./PersistedDirectoriesErrors";

interface PersistedDirectoriesContextProviderProps {
children: ReactNode;
Expand Down Expand Up @@ -37,6 +39,9 @@ export const PersistedDirectoriesContextProvider = ({
if (workingDirectory === undefined) {
throw "workingDirectory was not found";
}
if (workingDirectory.handle.name.endsWith(`.${PROJECT_DIRECTORY_EXTENSION}`)) {
throw new ProjectDirectoryIsInsideAnotherProjectError(workingDirectory.handle.name);
}

const projectDirectoryName = makeProjectDirectoryName(project);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PROJECT_DIRECTORY_EXTENSION } from "../../../common/utils";

export class ProjectDirectoryIsInsideAnotherProjectError extends Error {
constructor(parentName: string) {
super(
`Unable to create project inside "${parentName}" as it is another .${PROJECT_DIRECTORY_EXTENSION} directory`
);
}
}

0 comments on commit 7ff2f84

Please sign in to comment.