@reliverse/pathkit 1.0.6 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -26
- package/bin/mod.d.ts +219 -31
- package/bin/mod.js +993 -43
- package/package.json +23 -4
- package/bin/delete-me.d.ts +0 -1
- package/bin/delete-me.js +0 -1
- package/bin/pathkit-impl/impl.d.ts +0 -9
- package/bin/pathkit-impl/impl.js +0 -16
- package/bin/pathkit-impl/internal/path.d.ts +0 -26
- package/bin/pathkit-impl/internal/path.js +0 -208
- package/bin/pathkit-impl/internal/win.d.ts +0 -1
- package/bin/pathkit-impl/internal/win.js +0 -7
- package/bin/pathkit-impl/utils.d.ts +0 -30
- package/bin/pathkit-impl/utils.js +0 -75
package/README.md
CHANGED
|
@@ -1,45 +1,41 @@
|
|
|
1
1
|
# pathkit • cross‑platform path manipulation
|
|
2
2
|
|
|
3
|
-
> @reliverse/pathkit is a slash‑consistent, cross‑platform path manipulation
|
|
3
|
+
> @reliverse/pathkit is a slash‑consistent, cross‑platform path manipulation, with POSIX forward slash, drop‑in for node:path and unjs/pathe. This library extends the node:path module with a set of functions for manipulating file paths.
|
|
4
4
|
|
|
5
5
|
[sponsor](https://github.com/sponsors/blefnk) • [discord](https://discord.gg/Pb8uKbwpsJ) • [npm](https://npmjs.com/package/@reliverse/pathkit) • [repo](https://github.com/reliverse/pathkit)
|
|
6
6
|
|
|
7
7
|
## Key Features
|
|
8
8
|
|
|
9
|
-
- 🔹
|
|
10
|
-
- ➕ **`unjs/pathe` on
|
|
11
|
-
- 🌀 **
|
|
12
|
-
- ⚙️ **
|
|
13
|
-
- 🚀 **
|
|
14
|
-
- 🧠 **
|
|
9
|
+
- 🔹 **drop in** and replace `node:path` and `unjs/pathe` instantly
|
|
10
|
+
- ➕ **`unjs/pathe` on steroids** – alias resolution, import parsing, and more
|
|
11
|
+
- 🌀 **always `/`** – posix separators 100% of the time (buh‑bye `\\`)
|
|
12
|
+
- ⚙️ **node.js api compatible** – familiar methods, no learning curve
|
|
13
|
+
- 🚀 **modern & fast** – typescript, pure esm, bun & node‑ready
|
|
14
|
+
- 🧠 **predictable & testable** – deterministic output across windows / macos / linux
|
|
15
|
+
- 🧼 **no dependencies** – just better path api + couple of cool utilities = [4.2 kB](https://bundlephobia.com/package/@reliverse/pathkit@latest)
|
|
15
16
|
|
|
16
17
|
## Installation
|
|
17
18
|
|
|
18
19
|
```bash
|
|
19
20
|
# bun • pnpm • yarn • npm
|
|
20
21
|
bun add @reliverse/pathkit
|
|
21
|
-
bun add -D @reliverse/pathkit-plus
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
_In most cases_ you may need to install `@reliverse/pathkit` as **regular dependency**.
|
|
25
|
-
_In most cases_ you may need to install `@reliverse/pathkit-plus` as **dev dependency**.
|
|
26
|
-
|
|
27
24
|
**Migrate**:
|
|
28
25
|
|
|
29
26
|
```bash
|
|
30
|
-
bun rm pathe
|
|
31
27
|
# soon:
|
|
32
28
|
# bun add -D @reliverse/dler
|
|
33
|
-
# bun dler
|
|
34
|
-
# bun dler
|
|
29
|
+
# bun dler migrate --lib path-to-pathkit
|
|
30
|
+
# bun dler migrate --lib pathe-to-pathkit
|
|
35
31
|
```
|
|
36
32
|
|
|
37
|
-
### `
|
|
33
|
+
### `unjs/pathe` vs `@reliverse/pathkit`
|
|
38
34
|
|
|
39
35
|
| Package | What you get | When to use |
|
|
40
36
|
|---------|--------------|-------------|
|
|
41
|
-
|
|
|
42
|
-
|
|
|
37
|
+
| **`pathe`** | Path API only (with POSIX everywhere) | You only need a drop‑in for `node:path` |
|
|
38
|
+
| **`pathkit`** | Everything in `pathe` **+** advanced utilities | You need alias resolution, import transforms, etc. |
|
|
43
39
|
|
|
44
40
|
## Why Pathkit? — The Problem with Native Paths
|
|
45
41
|
|
|
@@ -92,16 +88,16 @@ console.log(join("users", "blefnk")); // → "users/blefnk"
|
|
|
92
88
|
|
|
93
89
|
Say goodbye to `process.platform` conditionals 👋.
|
|
94
90
|
|
|
95
|
-
## pathkit
|
|
91
|
+
## pathkit advanced features
|
|
96
92
|
|
|
97
|
-
`@reliverse/pathkit
|
|
93
|
+
`@reliverse/pathkit` extends the core functionality of `node:path` with powerful utilities for working with imports, aliases, and more.
|
|
98
94
|
|
|
99
95
|
### Import/Export Analysis
|
|
100
96
|
|
|
101
97
|
The `getFileImportsExports` function provides detailed analysis of ES module imports and exports:
|
|
102
98
|
|
|
103
99
|
```ts
|
|
104
|
-
import { getFileImportsExports } from "@reliverse/pathkit
|
|
100
|
+
import { getFileImportsExports } from "@reliverse/pathkit";
|
|
105
101
|
|
|
106
102
|
const code = `
|
|
107
103
|
import { ref } from "vue";
|
|
@@ -152,7 +148,7 @@ Features:
|
|
|
152
148
|
Convert between different path formats:
|
|
153
149
|
|
|
154
150
|
```ts
|
|
155
|
-
import { convertImportPaths } from "@reliverse/pathkit
|
|
151
|
+
import { convertImportPaths } from "@reliverse/pathkit";
|
|
156
152
|
|
|
157
153
|
await convertImportPaths({
|
|
158
154
|
baseDir: "./src",
|
|
@@ -166,7 +162,7 @@ await convertImportPaths({
|
|
|
166
162
|
### Extension Conversion
|
|
167
163
|
|
|
168
164
|
```ts
|
|
169
|
-
import { convertImportExtensionsJsToTs } from "@reliverse/pathkit
|
|
165
|
+
import { convertImportExtensionsJsToTs } from "@reliverse/pathkit";
|
|
170
166
|
|
|
171
167
|
await convertImportExtensionsJsToTs({
|
|
172
168
|
dirPath: "./src",
|
|
@@ -181,7 +177,7 @@ import {
|
|
|
181
177
|
normalizeAliases,
|
|
182
178
|
resolveAlias,
|
|
183
179
|
reverseResolveAlias
|
|
184
|
-
} from "@reliverse/pathkit
|
|
180
|
+
} from "@reliverse/pathkit";
|
|
185
181
|
|
|
186
182
|
const aliases = { "@/": "/src/", "~/": "/home/user/" };
|
|
187
183
|
|
|
@@ -200,13 +196,11 @@ console.log(reverseResolveAlias("/src/utils", aliases)); // "@/utils"
|
|
|
200
196
|
```ts
|
|
201
197
|
import {
|
|
202
198
|
filename, // Strip extension
|
|
203
|
-
extractPackageName, // Get package name from import
|
|
204
199
|
normalizeQuotes, // Standardize quote style
|
|
205
200
|
matchesGlob // Test glob patterns
|
|
206
|
-
} from "@reliverse/pathkit
|
|
201
|
+
} from "@reliverse/pathkit";
|
|
207
202
|
|
|
208
203
|
console.log(filename("/path/component.vue")); // "component"
|
|
209
|
-
console.log(extractPackageName("@scope/pkg")); // "@scope/pkg"
|
|
210
204
|
console.log(normalizeQuotes("import 'pkg'")); // 'import "pkg"'
|
|
211
205
|
console.log(matchesGlob("file.ts", "**/*.ts")); // true
|
|
212
206
|
```
|
package/bin/mod.d.ts
CHANGED
|
@@ -1,44 +1,232 @@
|
|
|
1
1
|
import type { PlatformPath } from "node:path";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
declare const normalizedAliasSymbol: unique symbol;
|
|
3
|
+
interface NormalizedRecord extends Record<string, string> {
|
|
4
|
+
[normalizedAliasSymbol]?: true;
|
|
5
|
+
}
|
|
6
|
+
export interface ParsedPath {
|
|
7
7
|
root: string;
|
|
8
8
|
dir: string;
|
|
9
9
|
base: string;
|
|
10
10
|
ext: string;
|
|
11
11
|
name: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
12
|
+
}
|
|
13
|
+
export interface FormatInputPathObject {
|
|
14
14
|
root?: string;
|
|
15
15
|
dir?: string;
|
|
16
16
|
base?: string;
|
|
17
17
|
ext?: string;
|
|
18
18
|
name?: string;
|
|
19
|
+
}
|
|
20
|
+
type PathExtFilter = "js" | "ts" | "none" | "js-ts-none";
|
|
21
|
+
type ImportExtType = "js" | "ts" | "none";
|
|
22
|
+
/**
|
|
23
|
+
* removes directories with recursive force option
|
|
24
|
+
*/
|
|
25
|
+
declare function cleanDirs(dirs: string[]): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* recursively copies a directory and its contents
|
|
28
|
+
*/
|
|
29
|
+
declare function copyDir(src: string, dest: string): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* normalizes windows paths to use forward slashes
|
|
32
|
+
*/
|
|
33
|
+
declare function normalizeWindowsPath(input?: string): string;
|
|
34
|
+
declare const sep = "/";
|
|
35
|
+
/**
|
|
36
|
+
* normalizes a path, resolving . and .. segments
|
|
37
|
+
*/
|
|
38
|
+
declare const normalize: (path: string) => string;
|
|
39
|
+
/**
|
|
40
|
+
* joins path segments with proper normalization
|
|
41
|
+
*/
|
|
42
|
+
declare const join: (...segments: string[]) => string;
|
|
43
|
+
/**
|
|
44
|
+
* resolves path to an absolute path
|
|
45
|
+
*/
|
|
46
|
+
declare const resolve: (...args: string[]) => string;
|
|
47
|
+
/**
|
|
48
|
+
* checks if path is absolute
|
|
49
|
+
*/
|
|
50
|
+
declare const isAbsolute: (p: string) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* converts path to namespaced path for windows
|
|
53
|
+
*/
|
|
54
|
+
declare const toNamespacedPath: (p: string) => string;
|
|
55
|
+
/**
|
|
56
|
+
* gets file extension including the dot
|
|
57
|
+
*/
|
|
58
|
+
declare const extname: (p: string) => string;
|
|
59
|
+
/**
|
|
60
|
+
* gets relative path from one path to another
|
|
61
|
+
*/
|
|
62
|
+
declare const relative: (from: string, to: string) => string;
|
|
63
|
+
/**
|
|
64
|
+
* gets directory name from path
|
|
65
|
+
*/
|
|
66
|
+
declare const dirname: (p: string) => string;
|
|
67
|
+
/**
|
|
68
|
+
* formats object parts into a path string
|
|
69
|
+
*/
|
|
70
|
+
declare const format: (p: FormatInputPathObject) => string;
|
|
71
|
+
/**
|
|
72
|
+
* gets base filename from path
|
|
73
|
+
*/
|
|
74
|
+
declare const basename: (p: string, ext?: string) => string;
|
|
75
|
+
/**
|
|
76
|
+
* parses a path into its components
|
|
77
|
+
*/
|
|
78
|
+
declare const parse: (p: string) => ParsedPath;
|
|
79
|
+
/**
|
|
80
|
+
* gets filename without extension
|
|
81
|
+
*/
|
|
82
|
+
declare function filename(pathString: string): string | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* normalizes alias records and optimizes nested aliases
|
|
85
|
+
*/
|
|
86
|
+
declare function normalizeAliases(aliases: Record<string, string>): NormalizedRecord;
|
|
87
|
+
/**
|
|
88
|
+
* resolves a path using aliases
|
|
89
|
+
*/
|
|
90
|
+
declare function resolveAlias(path: string, aliases: Record<string, string>): string;
|
|
91
|
+
/**
|
|
92
|
+
* finds all alias paths that could resolve to the given path
|
|
93
|
+
*/
|
|
94
|
+
declare function reverseResolveAlias(path: string, aliases: Record<string, string>): string[];
|
|
95
|
+
/**
|
|
96
|
+
* converts aliased path to relative path
|
|
97
|
+
*/
|
|
98
|
+
declare function convertStringAliasRelative({ importPath, importerFile, pathPattern, targetDir, }: {
|
|
99
|
+
importPath: string;
|
|
100
|
+
importerFile: string;
|
|
101
|
+
pathPattern: string;
|
|
102
|
+
targetDir: string;
|
|
103
|
+
}): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* main function to convert import paths from aliases to relative paths
|
|
106
|
+
*/
|
|
107
|
+
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, // e.g. @, ~, @/*, ~/*
|
|
108
|
+
pathExtFilter, }: {
|
|
109
|
+
targetDir: string;
|
|
110
|
+
aliasToReplace: string;
|
|
111
|
+
pathExtFilter: PathExtFilter;
|
|
112
|
+
}): Promise<{
|
|
113
|
+
file: string;
|
|
114
|
+
changes: {
|
|
115
|
+
from: string;
|
|
116
|
+
to: string;
|
|
117
|
+
}[];
|
|
118
|
+
}[]>;
|
|
119
|
+
/**
|
|
120
|
+
* converts extensions in import paths from one format to another
|
|
121
|
+
*/
|
|
122
|
+
declare function convertImportsExt({ targetDir, extFrom, extTo, }: {
|
|
123
|
+
targetDir: string;
|
|
124
|
+
extFrom: ImportExtType;
|
|
125
|
+
extTo: ImportExtType;
|
|
126
|
+
}): Promise<{
|
|
127
|
+
file: string;
|
|
128
|
+
changes: {
|
|
129
|
+
from: string;
|
|
130
|
+
to: string;
|
|
131
|
+
}[];
|
|
132
|
+
}[]>;
|
|
133
|
+
/**
|
|
134
|
+
* strips a specified number of segments from the beginning of a path
|
|
135
|
+
* @param path - The path to strip segments from
|
|
136
|
+
* @param count - Number of segments to strip (default: 1)
|
|
137
|
+
* @param alias - Optional alias to preserve (e.g. "@", "~")
|
|
138
|
+
* @returns The path with the specified number of segments removed
|
|
139
|
+
*/
|
|
140
|
+
declare function stripPathSegments(path: string, count?: number, alias?: string): string;
|
|
141
|
+
/**
|
|
142
|
+
* recursively processes files in a directory to strip path segments from their contents
|
|
143
|
+
* @param targetDir - The directory to process
|
|
144
|
+
* @param segmentsToStrip - Number of segments to strip from paths
|
|
145
|
+
* @param alias - Optional alias to preserve (e.g. "@", "~")
|
|
146
|
+
* @param extensionsToProcess - Array of file extensions to process (default: EXTENSIONS)
|
|
147
|
+
* @returns Array of processed files and their changes
|
|
148
|
+
*/
|
|
149
|
+
declare function stripPathSegmentsInDirectory({ targetDir, segmentsToStrip, alias, extensionsToProcess, }: {
|
|
150
|
+
targetDir: string;
|
|
151
|
+
segmentsToStrip: number;
|
|
152
|
+
alias?: string;
|
|
153
|
+
extensionsToProcess?: string[];
|
|
154
|
+
}): Promise<{
|
|
155
|
+
file: string;
|
|
156
|
+
changes: {
|
|
157
|
+
from: string;
|
|
158
|
+
to: string;
|
|
159
|
+
}[];
|
|
160
|
+
}[]>;
|
|
161
|
+
/**
|
|
162
|
+
* attaches path segments to an existing path
|
|
163
|
+
* @param path - The base path to attach segments to
|
|
164
|
+
* @param segments - The segments to attach
|
|
165
|
+
* @param options - Configuration options
|
|
166
|
+
* @returns The path with segments attached
|
|
167
|
+
*/
|
|
168
|
+
declare function attachPathSegments(path: string, segments: string | string[], options?: {
|
|
169
|
+
position?: "before" | "after";
|
|
170
|
+
normalize?: boolean;
|
|
171
|
+
ensureSlash?: boolean;
|
|
172
|
+
preserveRoot?: boolean;
|
|
173
|
+
preserveAlias?: string;
|
|
174
|
+
}): string;
|
|
175
|
+
/**
|
|
176
|
+
* recursively processes files in a directory to attach path segments to import statements
|
|
177
|
+
* @param targetDir - The directory to process
|
|
178
|
+
* @param segments - The segments to attach
|
|
179
|
+
* @param options - Configuration options for path segment attachment
|
|
180
|
+
* @param extensionsToProcess - Array of file extensions to process (default: EXTENSIONS)
|
|
181
|
+
* @returns Array of processed files and their changes
|
|
182
|
+
*/
|
|
183
|
+
declare function attachPathSegmentsInDirectory({ targetDir, segments, options, extensionsToProcess, }: {
|
|
184
|
+
targetDir: string;
|
|
185
|
+
segments: string | string[];
|
|
186
|
+
options?: Parameters<typeof attachPathSegments>[2];
|
|
187
|
+
extensionsToProcess?: string[];
|
|
188
|
+
}): Promise<{
|
|
189
|
+
file: string;
|
|
190
|
+
changes: {
|
|
191
|
+
from: string;
|
|
192
|
+
to: string;
|
|
193
|
+
}[];
|
|
194
|
+
}[]>;
|
|
195
|
+
declare const _pathBase: {
|
|
196
|
+
sep: string;
|
|
197
|
+
normalize: (path: string) => string;
|
|
198
|
+
join: (...segments: string[]) => string;
|
|
199
|
+
resolve: (...args: string[]) => string;
|
|
200
|
+
isAbsolute: (p: string) => boolean;
|
|
201
|
+
dirname: (p: string) => string;
|
|
202
|
+
basename: (p: string, ext?: string) => string;
|
|
203
|
+
extname: (p: string) => string;
|
|
204
|
+
format: (p: FormatInputPathObject) => string;
|
|
205
|
+
parse: (p: string) => ParsedPath;
|
|
206
|
+
toNamespacedPath: (p: string) => string;
|
|
207
|
+
relative: (from: string, to: string) => string;
|
|
208
|
+
filename: typeof filename;
|
|
19
209
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
readonly resolve: (...paths: string[]) => string;
|
|
38
|
-
readonly resolveAlias: typeof resolveAlias;
|
|
39
|
-
readonly reverseResolveAlias: typeof reverseResolveAlias;
|
|
40
|
-
readonly sep: "/";
|
|
41
|
-
readonly toNamespacedPath: (path: string) => string;
|
|
42
|
-
readonly win32: PlatformPath;
|
|
210
|
+
declare const path: PlatformPath & {
|
|
211
|
+
sep: string;
|
|
212
|
+
normalize: (path: string) => string;
|
|
213
|
+
join: (...segments: string[]) => string;
|
|
214
|
+
resolve: (...args: string[]) => string;
|
|
215
|
+
isAbsolute: (p: string) => boolean;
|
|
216
|
+
dirname: (p: string) => string;
|
|
217
|
+
basename: (p: string, ext?: string) => string;
|
|
218
|
+
extname: (p: string) => string;
|
|
219
|
+
format: (p: FormatInputPathObject) => string;
|
|
220
|
+
parse: (p: string) => ParsedPath;
|
|
221
|
+
toNamespacedPath: (p: string) => string;
|
|
222
|
+
relative: (from: string, to: string) => string;
|
|
223
|
+
filename: typeof filename;
|
|
224
|
+
} & {
|
|
225
|
+
posix: PlatformPath;
|
|
226
|
+
win32: PlatformPath;
|
|
43
227
|
};
|
|
44
|
-
|
|
228
|
+
declare const win32: PlatformPath;
|
|
229
|
+
declare const delimiter: string;
|
|
230
|
+
export type { PlatformPath, PathExtFilter, ImportExtType };
|
|
231
|
+
export { _pathBase as posix, win32, basename, delimiter, dirname, extname, filename, format, isAbsolute, join, normalize, parse, relative, resolve, sep, toNamespacedPath, normalizeAliases, resolveAlias, reverseResolveAlias, normalizeWindowsPath, cleanDirs, copyDir, convertStringAliasRelative, convertImportsAliasToRelative, convertImportsExt, stripPathSegments, stripPathSegmentsInDirectory, attachPathSegments, attachPathSegmentsInDirectory, };
|
|
232
|
+
export default path;
|