Skip to content

Commit

Permalink
chore(cli): @fern-api/api-workspace-commons is browser compatible (#5294
Browse files Browse the repository at this point in the history
)
  • Loading branch information
amckinney authored Nov 27, 2024
1 parent b6bd0d3 commit ad26104
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 38 deletions.
6 changes: 0 additions & 6 deletions generators/browser-compatible-base/src/utils/getBasename.ts

This file was deleted.

1 change: 0 additions & 1 deletion generators/browser-compatible-base/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { getBasename } from "./getBasename";
export { getPackageName } from "./getPackageName";
export { getSdkVersion } from "./getSdkVersion";
10 changes: 3 additions & 7 deletions generators/go-v2/ast/src/ast/core/Writer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
AbstractFormatter,
AbstractWriter,
getBasename,
NopFormatter
} from "@fern-api/browser-compatible-base-generator";
import { AbstractFormatter, AbstractWriter, NopFormatter } from "@fern-api/browser-compatible-base-generator";
import { BaseGoCustomConfigSchema } from "../../custom-config/BaseGoCustomConfigSchema";
import { basename } from "@fern-api/path-utils";

type Alias = string;
type ImportPath = string;
Expand Down Expand Up @@ -62,7 +58,7 @@ export class Writer extends AbstractWriter {
if (maybeAlias != null) {
return maybeAlias;
}
let alias = this.getValidAlias(getBasename(importPath));
let alias = this.getValidAlias(basename(importPath));
while (alias in this.imports) {
alias = "_" + alias;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FernGeneratorExec, getBasename } from "@fern-api/browser-compatible-base-generator";
import { FernGeneratorExec } from "@fern-api/browser-compatible-base-generator";
import { basename } from "@fern-api/path-utils";
import { BaseGoCustomConfigSchema } from "./BaseGoCustomConfigSchema";

const DEFAULT_MODULE_PATH = "sdk";
Expand Down Expand Up @@ -64,7 +65,7 @@ function maybeAppendMajorVersionSuffix({
importPath: string;
majorVersion: string;
}): string {
if (getBasename(importPath) === majorVersion) {
if (basename(importPath) === majorVersion) {
return importPath;
}
return `${importPath}/${majorVersion}`;
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/workspace-commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
"dependencies": {
"@fern-api/configuration": "workspace:*",
"@fern-api/core-utils": "workspace:*",
"@fern-api/fs-utils": "workspace:*",
"@fern-api/path-utils": "workspace:*",
"@fern-api/task-context": "workspace:*",
"@fern-api/fern-definition-schema": "workspace:*",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/lodash-es": "^4.17.12",
"@types/node": "18.7.18",
"depcheck": "^1.4.6",
"eslint": "^8.56.0",
"vitest": "^2.1.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/workspace-commons/src/APIChangelog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { AbsoluteFilePath } from "@fern-api/path-utils";

export interface APIChangelog {
files: ChangelogFile[];
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/workspace-commons/src/AbstractAPIWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TaskContext } from "@fern-api/task-context";
import { generatorsYml } from "@fern-api/configuration";
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/fs-utils";
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/path-utils";
import { ParsedFernFile } from "./FernFile";
import { DefinitionFileSchema, PackageMarkerFileSchema, RootApiFileSchema } from "@fern-api/fern-definition-schema";
import { FernWorkspace } from "./FernWorkspace";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/workspace-commons/src/FernFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/fs-utils";
import { AbsoluteFilePath, RelativeFilePath } from "@fern-api/path-utils";

export interface FernFile {
relativeFilepath: RelativeFilePath;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/workspace-commons/src/FernWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dependenciesYml } from "@fern-api/configuration";
import { AbsoluteFilePath } from "@fern-api/fs-utils";
import { AbsoluteFilePath } from "@fern-api/path-utils";
import { AbstractAPIWorkspace, FernDefinition } from "./AbstractAPIWorkspace";

export declare namespace FernWorkspace {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RelativeFilePath } from "@fern-api/fs-utils";
import { RelativeFilePath } from "@fern-api/path-utils";
import { DefinitionFileSchema } from "@fern-api/fern-definition-schema";
import { ParsedFernFile } from "../FernFile";
import { FernDefinition } from "../AbstractAPIWorkspace";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { entries } from "@fern-api/core-utils";
import { join, RelativeFilePath } from "@fern-api/fs-utils";
import { join, RelativeFilePath } from "@fern-api/path-utils";
import { DefinitionFileSchema } from "@fern-api/fern-definition-schema";
import { mapKeys } from "lodash-es";
import { ParsedFernFile } from "../FernFile";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { entries } from "@fern-api/core-utils";
import { join, RelativeFilePath } from "@fern-api/fs-utils";
import { join, RelativeFilePath } from "@fern-api/path-utils";
import { PackageMarkerFileSchema } from "@fern-api/fern-definition-schema";
import { mapKeys } from "lodash-es";
import { ParsedFernFile } from "../FernFile";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RelativeFilePath } from "@fern-api/fs-utils";
import { RelativeFilePath } from "@fern-api/path-utils";
import { DefinitionFileSchema } from "@fern-api/fern-definition-schema";
import { FernWorkspace } from "../FernWorkspace";
import { getAllDefinitionFiles } from "./getAllDefinitionFiles";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FERN_PACKAGE_MARKER_FILENAME } from "@fern-api/configuration";
import { entries } from "@fern-api/core-utils";
import { RelativeFilePath } from "@fern-api/fs-utils";
import { RelativeFilePath } from "@fern-api/path-utils";
import { DefinitionFileSchema } from "@fern-api/fern-definition-schema";
import path from "path";
import { FernWorkspace } from "../FernWorkspace";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { entries } from "@fern-api/core-utils";
import { RelativeFilePath } from "@fern-api/fs-utils";
import { RelativeFilePath } from "@fern-api/path-utils";
import { PackageMarkerFileSchema } from "@fern-api/fern-definition-schema";
import { FernWorkspace } from "../FernWorkspace";
import { getAllPackageMarkers } from "./getAllPackageMarkers";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/workspace-commons/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" },
"include": ["./src/**/*"],
"references": [
{ "path": "../../commons/fs-utils" },
{ "path": "../../commons/path-utils" },
{ "path": "../configuration" },
{ "path": "../logger" },
{ "path": "../task-context" },
Expand Down
3 changes: 3 additions & 0 deletions packages/commons/path-utils/src/basename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function basename(path: string): string {
return path.split("/").pop() ?? "";
}
2 changes: 0 additions & 2 deletions packages/commons/path-utils/src/convertToOsPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import { RelativeFilePath } from "./RelativeFilePath";
import { isBrowser } from "./isBrowser";

// in this function, we ignore drive paths and roots, since many strings are passed as partial relative paths
Expand Down
16 changes: 16 additions & 0 deletions packages/commons/path-utils/src/dirname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import { RelativeFilePath } from "./RelativeFilePath";

export function dirname(filepath: RelativeFilePath): RelativeFilePath;
export function dirname(filepath: AbsoluteFilePath): AbsoluteFilePath;
export function dirname(filepath: string): string {
const trimmed = filepath.replace(/\/+$/, "");
const lastSlashIndex = trimmed.lastIndexOf("/");
if (lastSlashIndex === -1) {
return ".";
}
if (lastSlashIndex === 0) {
return "/";
}
return filepath.slice(0, lastSlashIndex);
}
3 changes: 3 additions & 0 deletions packages/commons/path-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { AbsoluteFilePath } from "./AbsoluteFilePath";
export { RelativeFilePath } from "./RelativeFilePath";
export { basename } from "./basename";
export { convertToOsPath } from "./convertToOsPath";
export { dirname } from "./dirname";
export { join } from "./join";
24 changes: 24 additions & 0 deletions packages/commons/path-utils/src/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AbsoluteFilePath } from "./AbsoluteFilePath";
import { RelativeFilePath } from "./RelativeFilePath";

export function join(first: RelativeFilePath, ...parts: RelativeFilePath[]): RelativeFilePath;
export function join(first: AbsoluteFilePath, ...parts: RelativeFilePath[]): AbsoluteFilePath;
export function join(...parts: RelativeFilePath[]): RelativeFilePath;
export function join(...parts: string[]): string {
const stack: string[] = [];
for (const part of parts.flatMap((segment) => segment.split("/")).filter(Boolean)) {
if (part === "." || part === "") {
continue;
}
if (part === "..") {
if (stack.length > 0 && stack[stack.length - 1] !== "..") {
stack.pop();
} else {
stack.push(part);
}
} else {
stack.push(part);
}
}
return (parts[0]?.startsWith("/") ? "/" : "") + stack.join("/");
}
9 changes: 2 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad26104

Please sign in to comment.