@reliverse/pathkit 1.1.4 → 1.1.5
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 +1 -1
- package/bin/mod.d.ts +1 -9
- package/bin/mod.js +0 -77
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
- ⚙️ **node.js api compatible** – familiar methods, no learning curve
|
|
13
13
|
- 🚀 **modern & fast** – typescript, pure esm, bun & node‑ready
|
|
14
14
|
- 🧠 **predictable & testable** – deterministic output across windows / macos / linux
|
|
15
|
-
- 🧼 **no dependencies** – just better path api + couple of cool utilities = [
|
|
15
|
+
- 🧼 **no dependencies** – just better path api + couple of cool utilities = [4kB](https://bundlephobia.com/package/@reliverse/pathkit@latest)
|
|
16
16
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
package/bin/mod.d.ts
CHANGED
|
@@ -19,14 +19,6 @@ export interface FormatInputPathObject {
|
|
|
19
19
|
}
|
|
20
20
|
type PathExtFilter = "js" | "ts" | "none" | "js-ts-none";
|
|
21
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
22
|
/**
|
|
31
23
|
* normalizes windows paths to use forward slashes
|
|
32
24
|
*/
|
|
@@ -228,5 +220,5 @@ declare const path: PlatformPath & {
|
|
|
228
220
|
declare const win32: PlatformPath;
|
|
229
221
|
declare const delimiter: string;
|
|
230
222
|
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,
|
|
223
|
+
export { _pathBase as posix, win32, basename, delimiter, dirname, extname, filename, format, isAbsolute, join, normalize, parse, relative, resolve, sep, toNamespacedPath, normalizeAliases, resolveAlias, reverseResolveAlias, normalizeWindowsPath, convertStringAliasRelative, convertImportsAliasToRelative, convertImportsExt, stripPathSegments, stripPathSegmentsInDirectory, attachPathSegments, attachPathSegmentsInDirectory, };
|
|
232
224
|
export default path;
|
package/bin/mod.js
CHANGED
|
@@ -15,42 +15,6 @@ const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
|
15
15
|
const ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/;
|
|
16
16
|
const PATH_ROOT_RE = /^[/\\]|^[a-zA-Z]:[/\\]/;
|
|
17
17
|
const IMPORT_REGEX = /(?:import\s+(?:[\s\S]*?)\s+from\s+|import\s*\(\s*)\s*(['"])([^'"]+)\1/g;
|
|
18
|
-
async function cleanDirs(dirs) {
|
|
19
|
-
await Promise.all(
|
|
20
|
-
dirs.map(async (d) => {
|
|
21
|
-
try {
|
|
22
|
-
await fs.rm(d, { recursive: true, force: true });
|
|
23
|
-
logInternal(`\u2713 cleaned: ${d}`);
|
|
24
|
-
} catch (error) {
|
|
25
|
-
log(
|
|
26
|
-
`\u2717 error cleaning ${d}: ${error instanceof Error ? error.message : String(error)}`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
async function copyDir(src, dest) {
|
|
33
|
-
logInternal(`\u2713 copying: ${src} \u2192 ${dest}`);
|
|
34
|
-
try {
|
|
35
|
-
await fs.mkdir(dest, { recursive: true });
|
|
36
|
-
const entries = await fs.readdir(src, { withFileTypes: true });
|
|
37
|
-
await Promise.all(
|
|
38
|
-
entries.map(async (entry) => {
|
|
39
|
-
const srcPath = join(src, entry.name);
|
|
40
|
-
const destPath = join(dest, entry.name);
|
|
41
|
-
if (entry.isDirectory()) {
|
|
42
|
-
return copyDir(srcPath, destPath);
|
|
43
|
-
}
|
|
44
|
-
await fs.copyFile(srcPath, destPath);
|
|
45
|
-
logInternal(` copied: ${srcPath} \u2192 ${destPath}`);
|
|
46
|
-
})
|
|
47
|
-
);
|
|
48
|
-
} catch (error) {
|
|
49
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
50
|
-
log(`\u2717 error copying directory ${src} to ${dest}: ${errorMsg}`);
|
|
51
|
-
throw error;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
18
|
function normalizeWindowsPath(input = "") {
|
|
55
19
|
if (!input) return input;
|
|
56
20
|
return input.replace(/\\/g, SLASH).replace(DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
@@ -510,7 +474,6 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter) {
|
|
|
510
474
|
}
|
|
511
475
|
if (content !== updated) {
|
|
512
476
|
await fs.writeFile(filePath, updated);
|
|
513
|
-
logInternal(`\u2713 processed: ${filePath}`);
|
|
514
477
|
}
|
|
515
478
|
return changes;
|
|
516
479
|
}
|
|
@@ -548,7 +511,6 @@ async function processAllFiles({
|
|
|
548
511
|
results.push({ file: fullPath, changes });
|
|
549
512
|
}
|
|
550
513
|
} else {
|
|
551
|
-
logInternal(` - skipping non-matching file: ${entry.name}`);
|
|
552
514
|
}
|
|
553
515
|
})
|
|
554
516
|
);
|
|
@@ -571,7 +533,6 @@ async function convertImportsAliasToRelative({
|
|
|
571
533
|
`Converting aliased imports starting with '${aliasToReplace}' to relative paths in "${targetDir}"...`
|
|
572
534
|
);
|
|
573
535
|
log(` (Assuming "${normalizedAlias}" resolves relative to "${targetDir}")`);
|
|
574
|
-
logInternal(` (Using extension mode: ${pathExtFilter})`);
|
|
575
536
|
const results = await processAllFiles({
|
|
576
537
|
srcDir: targetDir,
|
|
577
538
|
aliasToReplace: normalizedAlias,
|
|
@@ -598,9 +559,6 @@ async function convertImportsExt({
|
|
|
598
559
|
extFrom,
|
|
599
560
|
extTo
|
|
600
561
|
}) {
|
|
601
|
-
logInternal(
|
|
602
|
-
`Converting import extensions from '${extFrom}' to '${extTo}' in "${targetDir}"...`
|
|
603
|
-
);
|
|
604
562
|
const fromExtStr = extFrom === "none" ? "" : `.${extFrom}`;
|
|
605
563
|
const toExtStr = extTo === "none" ? "" : `.${extTo}`;
|
|
606
564
|
const importRegex = new RegExp(
|
|
@@ -645,13 +603,11 @@ async function convertImportsExt({
|
|
|
645
603
|
}
|
|
646
604
|
if (content !== updated) {
|
|
647
605
|
await fs.writeFile(fullPath, updated);
|
|
648
|
-
logInternal(`\u2713 processed: ${fullPath}`);
|
|
649
606
|
if (changes.length > 0) {
|
|
650
607
|
results.push({ file: fullPath, changes });
|
|
651
608
|
}
|
|
652
609
|
}
|
|
653
610
|
} else {
|
|
654
|
-
logInternal(` - skipping non-matching file: ${entry.name}`);
|
|
655
611
|
}
|
|
656
612
|
})
|
|
657
613
|
);
|
|
@@ -666,7 +622,6 @@ async function convertImportsExt({
|
|
|
666
622
|
}
|
|
667
623
|
} else {
|
|
668
624
|
}
|
|
669
|
-
logInternal("Extension conversion complete.");
|
|
670
625
|
return results;
|
|
671
626
|
} catch (error) {
|
|
672
627
|
log(
|
|
@@ -678,12 +633,8 @@ async function convertImportsExt({
|
|
|
678
633
|
function stripPathSegments(path2, count = 1, alias = "") {
|
|
679
634
|
if (typeof path2 !== "string" || path2.length === 0) return path2;
|
|
680
635
|
if (count <= 0) return path2;
|
|
681
|
-
logInternal(`[stripPathSegments] Processing path: ${path2}`);
|
|
682
|
-
logInternal(` - count: ${count}, alias: ${alias}`);
|
|
683
636
|
const normalizedPath = normalizeWindowsPath(path2);
|
|
684
|
-
logInternal(` - normalized: ${normalizedPath}`);
|
|
685
637
|
const parsed = parse(normalizedPath);
|
|
686
|
-
logInternal(` - parsed: ${JSON.stringify(parsed)}`);
|
|
687
638
|
let pathSegments = [];
|
|
688
639
|
if (parsed.dir && parsed.dir !== parsed.root) {
|
|
689
640
|
let dirRelativeToRoot = parsed.dir;
|
|
@@ -696,32 +647,22 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
696
647
|
pathSegments.push(parsed.base);
|
|
697
648
|
}
|
|
698
649
|
pathSegments = pathSegments.filter(Boolean);
|
|
699
|
-
logInternal(` - initial segments: ${JSON.stringify(pathSegments)}`);
|
|
700
650
|
const leadingPreservedSegments = [];
|
|
701
651
|
if (alias && pathSegments.length > 0 && pathSegments[0].startsWith(alias)) {
|
|
702
652
|
const preserved = pathSegments.shift();
|
|
703
653
|
leadingPreservedSegments.push(preserved);
|
|
704
|
-
logInternal(` - preserved alias segment: ${preserved}`);
|
|
705
654
|
}
|
|
706
655
|
while (pathSegments.length > 0 && (pathSegments[0] === DOT || pathSegments[0] === DOUBLE_DOT)) {
|
|
707
656
|
const preserved = pathSegments.shift();
|
|
708
657
|
leadingPreservedSegments.push(preserved);
|
|
709
|
-
logInternal(` - preserved relative segment: ${preserved}`);
|
|
710
658
|
}
|
|
711
659
|
const numToStrip = Math.min(count, pathSegments.length);
|
|
712
660
|
const remainingBodySegments = pathSegments.slice(numToStrip);
|
|
713
|
-
logInternal(
|
|
714
|
-
` - stripping ${numToStrip} segments from: ${JSON.stringify(pathSegments)}`
|
|
715
|
-
);
|
|
716
|
-
logInternal(
|
|
717
|
-
` - remaining body segments: ${JSON.stringify(remainingBodySegments)}`
|
|
718
|
-
);
|
|
719
661
|
const pathRoot = parsed.root;
|
|
720
662
|
const effectiveSegments = [
|
|
721
663
|
...leadingPreservedSegments,
|
|
722
664
|
...remainingBodySegments
|
|
723
665
|
];
|
|
724
|
-
logInternal(` - effective segments: ${JSON.stringify(effectiveSegments)}`);
|
|
725
666
|
let result;
|
|
726
667
|
if (effectiveSegments.length === 0) {
|
|
727
668
|
result = normalize(pathRoot || DOT);
|
|
@@ -730,7 +671,6 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
730
671
|
} else {
|
|
731
672
|
result = join(...effectiveSegments);
|
|
732
673
|
}
|
|
733
|
-
logInternal(` - final result: ${result}`);
|
|
734
674
|
return result;
|
|
735
675
|
}
|
|
736
676
|
async function stripPathSegmentsInDirectory({
|
|
@@ -741,7 +681,6 @@ async function stripPathSegmentsInDirectory({
|
|
|
741
681
|
}) {
|
|
742
682
|
log(`[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`);
|
|
743
683
|
log(` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}`);
|
|
744
|
-
logInternal(` - extensions: ${JSON.stringify(extensionsToProcess)}`);
|
|
745
684
|
try {
|
|
746
685
|
const entries = await fs.readdir(targetDir, { withFileTypes: true });
|
|
747
686
|
const results = [];
|
|
@@ -750,7 +689,6 @@ async function stripPathSegmentsInDirectory({
|
|
|
750
689
|
const fullPath = join(targetDir, entry.name);
|
|
751
690
|
if (entry.isDirectory()) {
|
|
752
691
|
if (entry.name === "node_modules") return;
|
|
753
|
-
logInternal(` - recursing into directory: ${entry.name}`);
|
|
754
692
|
const subdirResults = await stripPathSegmentsInDirectory({
|
|
755
693
|
targetDir: fullPath,
|
|
756
694
|
segmentsToStrip,
|
|
@@ -759,50 +697,40 @@ async function stripPathSegmentsInDirectory({
|
|
|
759
697
|
});
|
|
760
698
|
results.push(...subdirResults);
|
|
761
699
|
} else if (extensionsToProcess.includes(extname(entry.name))) {
|
|
762
|
-
logInternal(` Processing file: ${entry.name}`);
|
|
763
700
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
764
701
|
let updated = content;
|
|
765
702
|
const changes = [];
|
|
766
703
|
const matches = Array.from(content.matchAll(IMPORT_REGEX));
|
|
767
|
-
logInternal(` - found ${matches.length} import statements`);
|
|
768
704
|
for (const match of matches) {
|
|
769
705
|
const originalQuote = match[1];
|
|
770
706
|
const importPath = match[2];
|
|
771
707
|
if (!importPath.includes(SLASH)) {
|
|
772
|
-
logInternal(` - skipping non-path import: ${importPath}`);
|
|
773
708
|
continue;
|
|
774
709
|
}
|
|
775
710
|
if (alias && !importPath.startsWith(alias.replace("/*", ""))) {
|
|
776
|
-
logInternal(` - skipping non-alias import: ${importPath}`);
|
|
777
711
|
continue;
|
|
778
712
|
}
|
|
779
|
-
logInternal(` Processing import: ${importPath}`);
|
|
780
713
|
const strippedPath = stripPathSegments(
|
|
781
714
|
importPath,
|
|
782
715
|
segmentsToStrip,
|
|
783
716
|
alias
|
|
784
717
|
);
|
|
785
718
|
if (importPath === strippedPath) {
|
|
786
|
-
logInternal(" - no changes needed");
|
|
787
719
|
continue;
|
|
788
720
|
}
|
|
789
721
|
changes.push({ from: importPath, to: strippedPath });
|
|
790
|
-
logInternal(` - transformed: ${importPath} \u2192 ${strippedPath}`);
|
|
791
722
|
const searchStr = `${originalQuote}${importPath}${originalQuote}`;
|
|
792
723
|
const replaceStr = `${originalQuote}${strippedPath}${originalQuote}`;
|
|
793
724
|
updated = replaceAllInString(updated, searchStr, replaceStr);
|
|
794
725
|
}
|
|
795
726
|
if (content !== updated) {
|
|
796
727
|
await fs.writeFile(fullPath, updated);
|
|
797
|
-
logInternal(" \u2713 wrote changes to file");
|
|
798
728
|
if (changes.length > 0) {
|
|
799
729
|
results.push({ file: fullPath, changes });
|
|
800
730
|
}
|
|
801
731
|
} else {
|
|
802
|
-
logInternal(" - no changes made to file");
|
|
803
732
|
}
|
|
804
733
|
} else {
|
|
805
|
-
logInternal(` - skipping non-matching file: ${entry.name}`);
|
|
806
734
|
}
|
|
807
735
|
})
|
|
808
736
|
);
|
|
@@ -816,7 +744,6 @@ async function stripPathSegmentsInDirectory({
|
|
|
816
744
|
}
|
|
817
745
|
}
|
|
818
746
|
} else {
|
|
819
|
-
logInternal(" No changes were made in any files");
|
|
820
747
|
}
|
|
821
748
|
return results;
|
|
822
749
|
} catch (error) {
|
|
@@ -905,13 +832,11 @@ async function attachPathSegmentsInDirectory({
|
|
|
905
832
|
}
|
|
906
833
|
if (content !== updated) {
|
|
907
834
|
await fs.writeFile(fullPath, updated);
|
|
908
|
-
logInternal(`\u2713 processed: ${fullPath}`);
|
|
909
835
|
if (changes.length > 0) {
|
|
910
836
|
results.push({ file: fullPath, changes });
|
|
911
837
|
}
|
|
912
838
|
}
|
|
913
839
|
} else {
|
|
914
|
-
logInternal(` - skipping non-matching file: ${entry.name}`);
|
|
915
840
|
}
|
|
916
841
|
})
|
|
917
842
|
);
|
|
@@ -1013,8 +938,6 @@ export {
|
|
|
1013
938
|
resolveAlias,
|
|
1014
939
|
reverseResolveAlias,
|
|
1015
940
|
normalizeWindowsPath,
|
|
1016
|
-
cleanDirs,
|
|
1017
|
-
copyDir,
|
|
1018
941
|
convertStringAliasRelative,
|
|
1019
942
|
convertImportsAliasToRelative,
|
|
1020
943
|
convertImportsExt,
|
package/package.json
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"name": "@reliverse/pathkit",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"version": "1.1.
|
|
8
|
+
"version": "1.1.5",
|
|
9
9
|
"devDependencies": {
|
|
10
10
|
"@biomejs/biome": "1.9.4",
|
|
11
11
|
"@eslint/js": "^9.27.0",
|
|
12
|
-
"@reliverse/dler": "^1.
|
|
12
|
+
"@reliverse/dler": "^1.4.6",
|
|
13
13
|
"@reliverse/relifso": "^1.2.10",
|
|
14
|
-
"@reliverse/relinka": "^1.4.
|
|
14
|
+
"@reliverse/relinka": "^1.4.6",
|
|
15
15
|
"@reliverse/rematch": "^1.1.0",
|
|
16
16
|
"@stylistic/eslint-plugin": "^4.2.0",
|
|
17
17
|
"@types/bun": "^1.2.14",
|
|
@@ -39,4 +39,4 @@
|
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
41
41
|
}
|
|
42
|
-
}
|
|
42
|
+
}
|