Skip to content

Commit

Permalink
add checks for directory cases
Browse files Browse the repository at this point in the history
  • Loading branch information
abarrell committed Aug 6, 2024
1 parent ad59904 commit c18735b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 28 deletions.
15 changes: 12 additions & 3 deletions packages/cli/project-loader/src/loadProject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
APIS_DIRECTORY,
DEFINITION_DIRECTORY,
fernConfigJson,
FERN_DIRECTORY,
generatorsYml,
GENERATORS_CONFIGURATION_FILENAME,
getFernDirectory
} from "@fern-api/configuration";
import { ASYNCAPI_DIRECTORY, OPENAPI_DIRECTORY } from "@fern-api/configuration/src/constants";

Check failure on line 10 in packages/cli/project-loader/src/loadProject.ts

View workflow job for this annotation

GitHub Actions / eslint

Reaching to "@fern-api/configuration/src/constants" is not allowed
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { TaskContext } from "@fern-api/task-context";
import {
Expand Down Expand Up @@ -46,9 +49,15 @@ export async function loadProject({
return context.failAndThrow(`Directory "${nameOverride ?? FERN_DIRECTORY}" not found.`);
}

var apiWorkspaces: APIWorkspace[] | undefined = undefined;
let apiWorkspaces: APIWorkspace[] = [];

if (await doesPathExist(join(fernDirectory, RelativeFilePath.of(APIS_DIRECTORY)))) {
if (
(await doesPathExist(join(fernDirectory, RelativeFilePath.of(APIS_DIRECTORY)))) ||
doesPathExist(join(fernDirectory, RelativeFilePath.of(DEFINITION_DIRECTORY))) ||
doesPathExist(join(fernDirectory, RelativeFilePath.of(GENERATORS_CONFIGURATION_FILENAME))) ||
doesPathExist(join(fernDirectory, RelativeFilePath.of(OPENAPI_DIRECTORY))) ||
doesPathExist(join(fernDirectory, RelativeFilePath.of(ASYNCAPI_DIRECTORY)))
) {
apiWorkspaces = await loadApis({
cliName,
fernDirectory,
Expand All @@ -61,7 +70,7 @@ export async function loadProject({

return {
config: await fernConfigJson.loadProjectConfig({ directory: fernDirectory, context }),
apiWorkspaces: apiWorkspaces ?? [],
apiWorkspaces: apiWorkspaces,

Check failure on line 73 in packages/cli/project-loader/src/loadProject.ts

View workflow job for this annotation

GitHub Actions / eslint

Expected property shorthand
docsWorkspaces: await loadDocsWorkspace({ fernDirectory, context })
};
}
Expand Down
20 changes: 1 addition & 19 deletions packages/cli/workspace-loader/src/loadAPIWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
APIS_DIRECTORY,
ASYNCAPI_DIRECTORY,
DEFINITION_DIRECTORY,
generatorsYml,
OPENAPI_DIRECTORY
} from "@fern-api/configuration";
import { ASYNCAPI_DIRECTORY, DEFINITION_DIRECTORY, generatorsYml, OPENAPI_DIRECTORY } from "@fern-api/configuration";
import { AbsoluteFilePath, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
import { isDirectoryEmpty } from "@fern-api/fs-utils/src/doesPathExist";
import { TaskContext } from "@fern-api/task-context";
import { loadAPIChangelog } from "./loadAPIChangelog";
import { getValidAbsolutePathToAsyncAPIFromFolder } from "./loadAsyncAPIFile";
Expand Down Expand Up @@ -156,17 +149,6 @@ export async function loadAPIWorkspace({
};
}

if (await isDirectoryEmpty(join(absolutePathToWorkspace, RelativeFilePath.of(APIS_DIRECTORY)))) {
return {
didSucceed: false,
failures: {
[RelativeFilePath.of(APIS_DIRECTORY)]: {
type: WorkspaceLoaderFailureType.FILE_MISSING
}
}
};
}

return {
didSucceed: false,
failures: {
Expand Down
7 changes: 1 addition & 6 deletions packages/commons/fs-utils/src/doesPathExist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lstatSync } from "fs";
import { lstat, readdir } from "fs/promises";
import { lstat } from "fs/promises";
import { AbsoluteFilePath } from "./AbsoluteFilePath";

export async function doesPathExist(filepath: AbsoluteFilePath): Promise<boolean> {
Expand All @@ -11,11 +11,6 @@ export async function doesPathExist(filepath: AbsoluteFilePath): Promise<boolean
}
}

export async function isDirectoryEmpty(directoryPath: AbsoluteFilePath): Promise<boolean> {
const files = await readdir(directoryPath);
return files.length === 0;
}

export function doesPathExistSync(filepath: AbsoluteFilePath): boolean {
try {
lstatSync(filepath);
Expand Down

0 comments on commit c18735b

Please sign in to comment.