Skip to content

Commit

Permalink
Merge pull request #8 from elecdeer/feat/add_path_utils
Browse files Browse the repository at this point in the history
feat: Add path utility functions
  • Loading branch information
elecdeer authored Jul 20, 2024
2 parents 6cdc71c + 4f6367f commit 1f527e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/hip-chairs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"storybook-addon-source-link": minor
---

Add path utility functions
24 changes: 23 additions & 1 deletion packages/addon-source-link/src/linkUtil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { join } from "path-browserify";
import { join, normalize, parse } from "path-browserify";

export const getFileUrl = (rootPath: string, importPath: string) => {
return new URL(`file:///${join(rootPath, importPath)}`);
};

/**
* Joins path segments into a path.
*/
export const joinPath = (...paths: string[]): string => {
return join(...paths);
};

/**
* Parses a path into an object.
*
* @example "./src/stories/Button.stories.tsx"
* -> { root: "", dir: "./src/stories", base: "Button.stories.tsx", ext: ".tsx", name: "Button.stories" }
*/
export const parsePath = (path: string) => {
return parse(path);
};

/**
* Normalizes a path, resolving `.` and `..` segments.
*
* @example "./src/stories/Button.stories.tsx" -> "src/stories/Button.stories.tsx"
*/
export const normalizePath = (path: string) => {
return normalize(path);
};

0 comments on commit 1f527e5

Please sign in to comment.