Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pepicrft/typed-file-system-path
📃 Typed primitives for Typescript to work with file paths
https://github.com/pepicrft/typed-file-system-path
Last synced: about 1 month ago
JSON representation
📃 Typed primitives for Typescript to work with file paths
- Host: GitHub
- URL: https://github.com/pepicrft/typed-file-system-path
- Owner: pepicrft
- License: mit
- Created: 2022-09-14T20:35:58.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-16T18:29:22.000Z (2 months ago)
- Last Synced: 2024-09-16T23:22:30.783Z (2 months ago)
- Language: TypeScript
- Size: 477 KB
- Stars: 7
- Watchers: 0
- Forks: 1
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# typed-file-system-path
[![typed-file-system-path](https://github.com/pepicrft/typed-file-system-path/actions/workflows/typed-file-system-path.yml/badge.svg)](https://github.com/pepicrft/typed-file-system-path/actions/workflows/typed-file-system-path.yml)
`typed-file-system-path` takes inspiration from [Path.swift](https://github.com/apple/swift-tools-support-core/blob/main/Sources/TSCBasic/Path.swift) in [swift-tools-support-core](https://github.com/apple/swift-tools-support-core/blob/main/Sources/TSCBasic/Path.swift) and provides typed primitives to work with file-system paths instead of strings. Even though it might seem unnecessary because it wraps a the `string` representing the path, it allows designing APIs that make it explicit if they work with absolute or relative paths and logic that doesn't have to make any assumptions.
We **strongly recommend** turning relative paths into absolute ones as soon as they enter the system. For example, if a path is passed through a flag in a CLI, make it absolute before you pass it down.
## Usage
```ts
import { relativePath, absolutePath } from "typed-file-system-path"// Initialize an absolute path
// @throws InvalidAbsolutePathError if the path is not absolute.
const dirAbsolutePath = absolutePath("/path/to/dir")// Initialize a relative path
// @throws InvalidRelativePathError if the path is not relative.
const fileRelativePath = relativePath("./tsconfig.json")/** Functions common across absolute and relative paths **/
path.parentDirectory // The parent directory path
path.extension // The path extension or undefined otherwise
path.basename // The path's last component
path.basenameWithoutExtension // The path's last component without the extensionpath.appending("src", "index.ts") // Returns a new path appending a relative path
path.appending("src/index.ts")
path.appending(relativePath("src/index.ts"))
```## Setup for development
1. Clone the repo: `git clone https://github.com/pepicrft/typed-file-system-path`.
2. Install dependencies: `pnpm install`.
3. Build the project: `pnpm build`.
4. Test the project: `pnpm test`.