diff --git a/packages/commons/fs-utils/package.json b/packages/commons/fs-utils/package.json index 9fc0ee44f8c..edd64295b7a 100644 --- a/packages/commons/fs-utils/package.json +++ b/packages/commons/fs-utils/package.json @@ -28,12 +28,15 @@ }, "dependencies": { "@fern-api/core-utils": "workspace:*", + "@fern-api/path-utils": "workspace:*", "glob": "^11.0.0", "json-stream-stringify": "^3.1.4", "stream-json": "^1.8.0", "tmp-promise": "^3.0.3" }, "devDependencies": { + "@fern-api/core-utils": "workspace:*", + "@fern-api/path-utils": "workspace:*", "@types/jest": "^29.5.12", "@types/node": "18.7.18", "@types/stream-json": "^1.7.7", diff --git a/packages/commons/fs-utils/src/AbsoluteFilePath.ts b/packages/commons/fs-utils/src/AbsoluteFilePath.ts index 0206ff1ef42..2628f86c198 100644 --- a/packages/commons/fs-utils/src/AbsoluteFilePath.ts +++ b/packages/commons/fs-utils/src/AbsoluteFilePath.ts @@ -1,15 +1,3 @@ -import path from "path"; -import { convertToOsPath } from "./osPathConverter"; - -export type AbsoluteFilePath = string & { - __AbsoluteFilePath: void; -}; - -export const AbsoluteFilePath = { - of: (value: string): AbsoluteFilePath => { - if (!path.isAbsolute(value)) { - throw new Error("Filepath is not absolute: " + value); - } - return convertToOsPath(value) as AbsoluteFilePath; - } -}; +// For convenience, we re-export the AbsoluteFilePath type for any caller +// that requires fs-utils. +export { AbsoluteFilePath } from "@fern-api/path-utils"; diff --git a/packages/commons/fs-utils/src/RelativeFilePath.ts b/packages/commons/fs-utils/src/RelativeFilePath.ts index 8875f564258..39763f43352 100644 --- a/packages/commons/fs-utils/src/RelativeFilePath.ts +++ b/packages/commons/fs-utils/src/RelativeFilePath.ts @@ -1,15 +1,3 @@ -import path from "path"; -import { convertToOsPath } from "./osPathConverter"; - -export type RelativeFilePath = string & { - __RelativeFilePath: void; -}; - -export const RelativeFilePath = { - of: (value: string): RelativeFilePath => { - if (path.isAbsolute(value)) { - throw new Error("Filepath is not relative: " + value); - } - return convertToOsPath(value) as RelativeFilePath; - } -}; +// For convenience, we re-export the RelativeFilePath type for any caller= +// that requires fs-utils. +export { RelativeFilePath } from "@fern-api/path-utils"; diff --git a/packages/commons/fs-utils/src/osPathConverter.ts b/packages/commons/fs-utils/src/osPathConverter.ts index 9c17d1833dd..08094687c07 100644 --- a/packages/commons/fs-utils/src/osPathConverter.ts +++ b/packages/commons/fs-utils/src/osPathConverter.ts @@ -1,13 +1,17 @@ import { AbsoluteFilePath } from "./AbsoluteFilePath"; import { RelativeFilePath } from "./RelativeFilePath"; -// in this function, we ignore drive paths and roots, since many strings are passed as partial relative paths -export function convertToOsPath(path: string): string { - if (process.platform === "win32") { - return path.replace(/\//g, "\\"); - } else { - return path.replace(/\\/g, "/"); - } +// For convenience, we re-export the convertToOsPath type for any caller +// that requires fs-utils. +export { convertToOsPath } from "@fern-api/path-utils"; + +export function convertToFernHostAbsoluteFilePath(path: AbsoluteFilePath): AbsoluteFilePath { + // Don't use 'of' here, as it will use OS path, we want fern path + return convertToFernHostPath(path) as AbsoluteFilePath; +} +export function convertToFernHostRelativeFilePath(path: RelativeFilePath): RelativeFilePath { + // Don't use 'of' here, as it will use OS path, we want fern path + return convertToFernHostPath(path) as RelativeFilePath; } function convertToFernHostPath(path: string): string { @@ -18,12 +22,3 @@ function convertToFernHostPath(path: string): string { return unixPath.replace(/\\/g, "/"); } - -export function convertToFernHostAbsoluteFilePath(path: AbsoluteFilePath): AbsoluteFilePath { - // Don't use 'of' here, as it will use OS path, we want fern path - return convertToFernHostPath(path) as AbsoluteFilePath; -} -export function convertToFernHostRelativeFilePath(path: RelativeFilePath): RelativeFilePath { - // Don't use 'of' here, as it will use OS path, we want fern path - return convertToFernHostPath(path) as RelativeFilePath; -} diff --git a/packages/commons/fs-utils/tsconfig.json b/packages/commons/fs-utils/tsconfig.json index 6b3ca77a3d2..9caf81e3acc 100644 --- a/packages/commons/fs-utils/tsconfig.json +++ b/packages/commons/fs-utils/tsconfig.json @@ -1,6 +1,19 @@ { "extends": "../../../shared/tsconfig.shared.json", - "compilerOptions": { "composite": true, "outDir": "lib", "rootDir": "src" }, - "include": ["./src/**/*"], - "references": [{ "path": "../core-utils" }] -} + "compilerOptions": { + "composite": true, + "outDir": "lib", + "rootDir": "src" + }, + "include": [ + "./src/**/*" + ], + "references": [ + { + "path": "../core-utils" + }, + { + "path": "../path-utils" + } + ] +} \ No newline at end of file diff --git a/packages/commons/path-utils/.depcheckrc.json b/packages/commons/path-utils/.depcheckrc.json new file mode 100644 index 00000000000..dba7a3d3dd1 --- /dev/null +++ b/packages/commons/path-utils/.depcheckrc.json @@ -0,0 +1,9 @@ +{ + "ignores": [ + "@types/jest", + "globals" + ], + "ignore-patterns": [ + "lib" + ] +} \ No newline at end of file diff --git a/packages/commons/path-utils/.prettierrc.cjs b/packages/commons/path-utils/.prettierrc.cjs new file mode 100644 index 00000000000..39cf0d0b8c9 --- /dev/null +++ b/packages/commons/path-utils/.prettierrc.cjs @@ -0,0 +1 @@ +module.exports = require("../../../.prettierrc.json"); diff --git a/packages/commons/path-utils/package.json b/packages/commons/path-utils/package.json new file mode 100644 index 00000000000..b4164ec3557 --- /dev/null +++ b/packages/commons/path-utils/package.json @@ -0,0 +1,38 @@ +{ + "name": "@fern-api/path-utils", + "version": "0.0.0", + "repository": { + "type": "git", + "url": "https://github.com/fern-api/fern.git", + "directory": "packages/commons/path-utils" + }, + "files": [ + "lib" + ], + "type": "module", + "source": "src/index.ts", + "main": "lib/index.js", + "types": "lib/index.d.ts", + "sideEffects": false, + "scripts": { + "clean": "rm -rf ./lib && tsc --build --clean", + "compile": "tsc --build", + "test": "vitest --passWithNoTests --run", + "test:update": "vitest --passWithNoTests --run -u", + "lint:eslint": "eslint --max-warnings 0 . --ignore-path=../../../.eslintignore", + "lint:eslint:fix": "yarn lint:eslint --fix", + "format": "prettier --write --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"", + "format:check": "prettier --check --ignore-unknown --ignore-path ../../../shared/.prettierignore \"**\"", + "organize-imports": "organize-imports-cli tsconfig.json", + "depcheck": "depcheck" + }, + "devDependencies": { + "@types/jest": "^29.5.12", + "depcheck": "^1.4.6", + "eslint": "^8.56.0", + "organize-imports-cli": "^0.10.0", + "prettier": "^2.7.1", + "typescript": "4.6.4", + "vitest": "^2.0.5" + } +} \ No newline at end of file diff --git a/packages/commons/path-utils/src/AbsoluteFilePath.ts b/packages/commons/path-utils/src/AbsoluteFilePath.ts new file mode 100644 index 00000000000..72353da2a40 --- /dev/null +++ b/packages/commons/path-utils/src/AbsoluteFilePath.ts @@ -0,0 +1,17 @@ +import { getPathModule } from "./getPathModule"; +import { convertToOsPath } from "./convertToOsPath"; + +const path = getPathModule(); + +export type AbsoluteFilePath = string & { + __AbsoluteFilePath: void; +}; + +export const AbsoluteFilePath = { + of: (value: string): AbsoluteFilePath => { + if (!path.isAbsolute(value)) { + throw new Error("Filepath is not absolute: " + value); + } + return convertToOsPath(value) as AbsoluteFilePath; + } +}; diff --git a/packages/commons/path-utils/src/RelativeFilePath.ts b/packages/commons/path-utils/src/RelativeFilePath.ts new file mode 100644 index 00000000000..6d97af9c498 --- /dev/null +++ b/packages/commons/path-utils/src/RelativeFilePath.ts @@ -0,0 +1,17 @@ +import { getPathModule } from "./getPathModule"; +import { convertToOsPath } from "./convertToOsPath"; + +const path = getPathModule(); + +export type RelativeFilePath = string & { + __RelativeFilePath: void; +}; + +export const RelativeFilePath = { + of: (value: string): RelativeFilePath => { + if (path.isAbsolute(value)) { + throw new Error("Filepath is not relative: " + value); + } + return convertToOsPath(value) as RelativeFilePath; + } +}; diff --git a/packages/commons/path-utils/src/convertToOsPath.ts b/packages/commons/path-utils/src/convertToOsPath.ts new file mode 100644 index 00000000000..5064d17d576 --- /dev/null +++ b/packages/commons/path-utils/src/convertToOsPath.ts @@ -0,0 +1,15 @@ +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 +export function convertToOsPath(path: string): string { + if (isBrowser()) { + return path.replace(/\\/g, "/"); + } + if (process.platform === "win32") { + return path.replace(/\//g, "\\"); + } else { + return path.replace(/\\/g, "/"); + } +} diff --git a/packages/commons/path-utils/src/getPathModule.ts b/packages/commons/path-utils/src/getPathModule.ts new file mode 100644 index 00000000000..f4002772fce --- /dev/null +++ b/packages/commons/path-utils/src/getPathModule.ts @@ -0,0 +1,14 @@ +import { isBrowser } from "./isBrowser"; + +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +export function getPathModule() { + if (isBrowser()) { + return { + isAbsolute: (value: string): boolean => { + return value.startsWith("/"); + } + }; + } else { + return require("path"); + } +} diff --git a/packages/commons/path-utils/src/index.ts b/packages/commons/path-utils/src/index.ts new file mode 100644 index 00000000000..979318fb6ca --- /dev/null +++ b/packages/commons/path-utils/src/index.ts @@ -0,0 +1,3 @@ +export { AbsoluteFilePath } from "./AbsoluteFilePath"; +export { RelativeFilePath } from "./RelativeFilePath"; +export { convertToOsPath } from "./convertToOsPath"; diff --git a/packages/commons/path-utils/src/isBrowser.ts b/packages/commons/path-utils/src/isBrowser.ts new file mode 100644 index 00000000000..0bd3051f052 --- /dev/null +++ b/packages/commons/path-utils/src/isBrowser.ts @@ -0,0 +1,3 @@ +export function isBrowser(): boolean { + return typeof window !== "undefined"; +} diff --git a/packages/commons/path-utils/tsconfig.json b/packages/commons/path-utils/tsconfig.json new file mode 100644 index 00000000000..e023ef25af3 --- /dev/null +++ b/packages/commons/path-utils/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../shared/tsconfig.shared.json", + "compilerOptions": { + "composite": true, + "outDir": "lib", + "rootDir": "src" + }, + "include": [ + "./src/**/*" + ], +} \ No newline at end of file diff --git a/packages/commons/path-utils/vitest.config.ts b/packages/commons/path-utils/vitest.config.ts new file mode 100644 index 00000000000..fecc099c58a --- /dev/null +++ b/packages/commons/path-utils/vitest.config.ts @@ -0,0 +1 @@ +export { default } from "../../../shared/vitest.config"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee6e6656f8c..333aaa11021 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4044,8 +4044,6 @@ importers: specifier: ^2.0.5 version: 2.0.5(@types/node@18.7.18)(jsdom@20.0.3)(sass@1.72.0)(terser@5.31.5) - packages/cli/cli/dist/local: {} - packages/cli/configuration: dependencies: '@fern-api/core-utils': @@ -6139,6 +6137,9 @@ importers: '@fern-api/core-utils': specifier: workspace:* version: link:../core-utils + '@fern-api/path-utils': + specifier: workspace:* + version: link:../path-utils glob: specifier: ^11.0.0 version: 11.0.0 @@ -6245,6 +6246,30 @@ importers: specifier: ^2.0.5 version: 2.0.5(@types/node@18.7.18)(jsdom@20.0.3)(sass@1.72.0)(terser@5.31.5) + packages/commons/path-utils: + devDependencies: + '@types/jest': + specifier: ^29.5.12 + version: 29.5.12 + depcheck: + specifier: ^1.4.6 + version: 1.4.6 + eslint: + specifier: ^8.56.0 + version: 8.56.0 + organize-imports-cli: + specifier: ^0.10.0 + version: 0.10.0 + prettier: + specifier: ^2.7.1 + version: 2.7.1 + typescript: + specifier: 4.6.4 + version: 4.6.4 + vitest: + specifier: ^2.0.5 + version: 2.1.4(@types/node@18.7.18)(jsdom@20.0.3)(sass@1.72.0)(terser@5.31.5) + packages/core: dependencies: '@fern-api/venus-api-sdk':