Skip to content

Commit

Permalink
chore(cli): Add @fern-api/path-utils (#5285)
Browse files Browse the repository at this point in the history
  • Loading branch information
amckinney authored Nov 26, 2024
1 parent 7dd123f commit ea5e828
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 52 deletions.
3 changes: 3 additions & 0 deletions packages/commons/fs-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 3 additions & 15 deletions packages/commons/fs-utils/src/AbsoluteFilePath.ts
Original file line number Diff line number Diff line change
@@ -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";
18 changes: 3 additions & 15 deletions packages/commons/fs-utils/src/RelativeFilePath.ts
Original file line number Diff line number Diff line change
@@ -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";
27 changes: 11 additions & 16 deletions packages/commons/fs-utils/src/osPathConverter.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
21 changes: 17 additions & 4 deletions packages/commons/fs-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
9 changes: 9 additions & 0 deletions packages/commons/path-utils/.depcheckrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ignores": [
"@types/jest",
"globals"
],
"ignore-patterns": [
"lib"
]
}
1 change: 1 addition & 0 deletions packages/commons/path-utils/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("../../../.prettierrc.json");
38 changes: 38 additions & 0 deletions packages/commons/path-utils/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
17 changes: 17 additions & 0 deletions packages/commons/path-utils/src/AbsoluteFilePath.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
17 changes: 17 additions & 0 deletions packages/commons/path-utils/src/RelativeFilePath.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
15 changes: 15 additions & 0 deletions packages/commons/path-utils/src/convertToOsPath.ts
Original file line number Diff line number Diff line change
@@ -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, "/");
}
}
14 changes: 14 additions & 0 deletions packages/commons/path-utils/src/getPathModule.ts
Original file line number Diff line number Diff line change
@@ -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");
}
}
3 changes: 3 additions & 0 deletions packages/commons/path-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { AbsoluteFilePath } from "./AbsoluteFilePath";
export { RelativeFilePath } from "./RelativeFilePath";
export { convertToOsPath } from "./convertToOsPath";
3 changes: 3 additions & 0 deletions packages/commons/path-utils/src/isBrowser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBrowser(): boolean {
return typeof window !== "undefined";
}
11 changes: 11 additions & 0 deletions packages/commons/path-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../shared/tsconfig.shared.json",
"compilerOptions": {
"composite": true,
"outDir": "lib",
"rootDir": "src"
},
"include": [
"./src/**/*"
],
}
1 change: 1 addition & 0 deletions packages/commons/path-utils/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "../../../shared/vitest.config";
29 changes: 27 additions & 2 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 ea5e828

Please sign in to comment.