@reliverse/pathkit 1.1.3 → 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 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 = [5kB](https://bundlephobia.com/package/@reliverse/pathkit@latest)
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, cleanDirs, copyDir, convertStringAliasRelative, convertImportsAliasToRelative, convertImportsExt, stripPathSegments, stripPathSegmentsInDirectory, attachPathSegments, attachPathSegmentsInDirectory, };
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());
@@ -438,13 +402,9 @@ async function resolveFileWithExtensions(basePath, extensions = ["", ...EXTENSIO
438
402
  return null;
439
403
  }
440
404
  function getTargetExtension(relPath, originalExt) {
441
- const foundExt = extname(relPath);
442
405
  if (originalExt) {
443
406
  return relPath;
444
407
  }
445
- if (foundExt) {
446
- return relPath.slice(0, -foundExt.length);
447
- }
448
408
  return relPath;
449
409
  }
450
410
  async function convertStringAliasRelative({
@@ -504,16 +464,16 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter) {
504
464
  pathPattern: normalizedAlias,
505
465
  targetDir
506
466
  });
507
- if (importPath !== relPath) {
508
- changes.push({ from: importPath, to: relPath });
467
+ const finalPath = pathExtFilter === "none" ? relPath.replace(/\.(ts|js|tsx|jsx|mjs|cjs)$/, "") : relPath;
468
+ if (importPath !== finalPath) {
469
+ changes.push({ from: importPath, to: finalPath });
509
470
  const searchString = `${originalQuote}${importPath}${originalQuote}`;
510
- const replacementString = `${originalQuote}${relPath}${originalQuote}`;
471
+ const replacementString = `${originalQuote}${finalPath}${originalQuote}`;
511
472
  updated = replaceAllInString(updated, searchString, replacementString);
512
473
  }
513
474
  }
514
475
  if (content !== updated) {
515
476
  await fs.writeFile(filePath, updated);
516
- logInternal(`\u2713 processed: ${filePath}`);
517
477
  }
518
478
  return changes;
519
479
  }
@@ -551,7 +511,6 @@ async function processAllFiles({
551
511
  results.push({ file: fullPath, changes });
552
512
  }
553
513
  } else {
554
- logInternal(` - skipping non-matching file: ${entry.name}`);
555
514
  }
556
515
  })
557
516
  );
@@ -574,7 +533,6 @@ async function convertImportsAliasToRelative({
574
533
  `Converting aliased imports starting with '${aliasToReplace}' to relative paths in "${targetDir}"...`
575
534
  );
576
535
  log(` (Assuming "${normalizedAlias}" resolves relative to "${targetDir}")`);
577
- logInternal(` (Using extension mode: ${pathExtFilter})`);
578
536
  const results = await processAllFiles({
579
537
  srcDir: targetDir,
580
538
  aliasToReplace: normalizedAlias,
@@ -601,9 +559,6 @@ async function convertImportsExt({
601
559
  extFrom,
602
560
  extTo
603
561
  }) {
604
- logInternal(
605
- `Converting import extensions from '${extFrom}' to '${extTo}' in "${targetDir}"...`
606
- );
607
562
  const fromExtStr = extFrom === "none" ? "" : `.${extFrom}`;
608
563
  const toExtStr = extTo === "none" ? "" : `.${extTo}`;
609
564
  const importRegex = new RegExp(
@@ -648,13 +603,11 @@ async function convertImportsExt({
648
603
  }
649
604
  if (content !== updated) {
650
605
  await fs.writeFile(fullPath, updated);
651
- logInternal(`\u2713 processed: ${fullPath}`);
652
606
  if (changes.length > 0) {
653
607
  results.push({ file: fullPath, changes });
654
608
  }
655
609
  }
656
610
  } else {
657
- logInternal(` - skipping non-matching file: ${entry.name}`);
658
611
  }
659
612
  })
660
613
  );
@@ -669,7 +622,6 @@ async function convertImportsExt({
669
622
  }
670
623
  } else {
671
624
  }
672
- logInternal("Extension conversion complete.");
673
625
  return results;
674
626
  } catch (error) {
675
627
  log(
@@ -681,12 +633,8 @@ async function convertImportsExt({
681
633
  function stripPathSegments(path2, count = 1, alias = "") {
682
634
  if (typeof path2 !== "string" || path2.length === 0) return path2;
683
635
  if (count <= 0) return path2;
684
- logInternal(`[stripPathSegments] Processing path: ${path2}`);
685
- logInternal(` - count: ${count}, alias: ${alias}`);
686
636
  const normalizedPath = normalizeWindowsPath(path2);
687
- logInternal(` - normalized: ${normalizedPath}`);
688
637
  const parsed = parse(normalizedPath);
689
- logInternal(` - parsed: ${JSON.stringify(parsed)}`);
690
638
  let pathSegments = [];
691
639
  if (parsed.dir && parsed.dir !== parsed.root) {
692
640
  let dirRelativeToRoot = parsed.dir;
@@ -699,32 +647,22 @@ function stripPathSegments(path2, count = 1, alias = "") {
699
647
  pathSegments.push(parsed.base);
700
648
  }
701
649
  pathSegments = pathSegments.filter(Boolean);
702
- logInternal(` - initial segments: ${JSON.stringify(pathSegments)}`);
703
650
  const leadingPreservedSegments = [];
704
651
  if (alias && pathSegments.length > 0 && pathSegments[0].startsWith(alias)) {
705
652
  const preserved = pathSegments.shift();
706
653
  leadingPreservedSegments.push(preserved);
707
- logInternal(` - preserved alias segment: ${preserved}`);
708
654
  }
709
655
  while (pathSegments.length > 0 && (pathSegments[0] === DOT || pathSegments[0] === DOUBLE_DOT)) {
710
656
  const preserved = pathSegments.shift();
711
657
  leadingPreservedSegments.push(preserved);
712
- logInternal(` - preserved relative segment: ${preserved}`);
713
658
  }
714
659
  const numToStrip = Math.min(count, pathSegments.length);
715
660
  const remainingBodySegments = pathSegments.slice(numToStrip);
716
- logInternal(
717
- ` - stripping ${numToStrip} segments from: ${JSON.stringify(pathSegments)}`
718
- );
719
- logInternal(
720
- ` - remaining body segments: ${JSON.stringify(remainingBodySegments)}`
721
- );
722
661
  const pathRoot = parsed.root;
723
662
  const effectiveSegments = [
724
663
  ...leadingPreservedSegments,
725
664
  ...remainingBodySegments
726
665
  ];
727
- logInternal(` - effective segments: ${JSON.stringify(effectiveSegments)}`);
728
666
  let result;
729
667
  if (effectiveSegments.length === 0) {
730
668
  result = normalize(pathRoot || DOT);
@@ -733,7 +671,6 @@ function stripPathSegments(path2, count = 1, alias = "") {
733
671
  } else {
734
672
  result = join(...effectiveSegments);
735
673
  }
736
- logInternal(` - final result: ${result}`);
737
674
  return result;
738
675
  }
739
676
  async function stripPathSegmentsInDirectory({
@@ -744,7 +681,6 @@ async function stripPathSegmentsInDirectory({
744
681
  }) {
745
682
  log(`[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`);
746
683
  log(` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}`);
747
- logInternal(` - extensions: ${JSON.stringify(extensionsToProcess)}`);
748
684
  try {
749
685
  const entries = await fs.readdir(targetDir, { withFileTypes: true });
750
686
  const results = [];
@@ -753,7 +689,6 @@ async function stripPathSegmentsInDirectory({
753
689
  const fullPath = join(targetDir, entry.name);
754
690
  if (entry.isDirectory()) {
755
691
  if (entry.name === "node_modules") return;
756
- logInternal(` - recursing into directory: ${entry.name}`);
757
692
  const subdirResults = await stripPathSegmentsInDirectory({
758
693
  targetDir: fullPath,
759
694
  segmentsToStrip,
@@ -762,50 +697,40 @@ async function stripPathSegmentsInDirectory({
762
697
  });
763
698
  results.push(...subdirResults);
764
699
  } else if (extensionsToProcess.includes(extname(entry.name))) {
765
- logInternal(` Processing file: ${entry.name}`);
766
700
  const content = await fs.readFile(fullPath, "utf-8");
767
701
  let updated = content;
768
702
  const changes = [];
769
703
  const matches = Array.from(content.matchAll(IMPORT_REGEX));
770
- logInternal(` - found ${matches.length} import statements`);
771
704
  for (const match of matches) {
772
705
  const originalQuote = match[1];
773
706
  const importPath = match[2];
774
707
  if (!importPath.includes(SLASH)) {
775
- logInternal(` - skipping non-path import: ${importPath}`);
776
708
  continue;
777
709
  }
778
710
  if (alias && !importPath.startsWith(alias.replace("/*", ""))) {
779
- logInternal(` - skipping non-alias import: ${importPath}`);
780
711
  continue;
781
712
  }
782
- logInternal(` Processing import: ${importPath}`);
783
713
  const strippedPath = stripPathSegments(
784
714
  importPath,
785
715
  segmentsToStrip,
786
716
  alias
787
717
  );
788
718
  if (importPath === strippedPath) {
789
- logInternal(" - no changes needed");
790
719
  continue;
791
720
  }
792
721
  changes.push({ from: importPath, to: strippedPath });
793
- logInternal(` - transformed: ${importPath} \u2192 ${strippedPath}`);
794
722
  const searchStr = `${originalQuote}${importPath}${originalQuote}`;
795
723
  const replaceStr = `${originalQuote}${strippedPath}${originalQuote}`;
796
724
  updated = replaceAllInString(updated, searchStr, replaceStr);
797
725
  }
798
726
  if (content !== updated) {
799
727
  await fs.writeFile(fullPath, updated);
800
- logInternal(" \u2713 wrote changes to file");
801
728
  if (changes.length > 0) {
802
729
  results.push({ file: fullPath, changes });
803
730
  }
804
731
  } else {
805
- logInternal(" - no changes made to file");
806
732
  }
807
733
  } else {
808
- logInternal(` - skipping non-matching file: ${entry.name}`);
809
734
  }
810
735
  })
811
736
  );
@@ -819,7 +744,6 @@ async function stripPathSegmentsInDirectory({
819
744
  }
820
745
  }
821
746
  } else {
822
- logInternal(" No changes were made in any files");
823
747
  }
824
748
  return results;
825
749
  } catch (error) {
@@ -908,13 +832,11 @@ async function attachPathSegmentsInDirectory({
908
832
  }
909
833
  if (content !== updated) {
910
834
  await fs.writeFile(fullPath, updated);
911
- logInternal(`\u2713 processed: ${fullPath}`);
912
835
  if (changes.length > 0) {
913
836
  results.push({ file: fullPath, changes });
914
837
  }
915
838
  }
916
839
  } else {
917
- logInternal(` - skipping non-matching file: ${entry.name}`);
918
840
  }
919
841
  })
920
842
  );
@@ -1016,8 +938,6 @@ export {
1016
938
  resolveAlias,
1017
939
  reverseResolveAlias,
1018
940
  normalizeWindowsPath,
1019
- cleanDirs,
1020
- copyDir,
1021
941
  convertStringAliasRelative,
1022
942
  convertImportsAliasToRelative,
1023
943
  convertImportsExt,
package/package.json CHANGED
@@ -5,21 +5,21 @@
5
5
  "license": "MIT",
6
6
  "name": "@reliverse/pathkit",
7
7
  "type": "module",
8
- "version": "1.1.3",
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.3.6",
12
+ "@reliverse/dler": "^1.4.6",
13
13
  "@reliverse/relifso": "^1.2.10",
14
- "@reliverse/relinka": "^1.4.5",
14
+ "@reliverse/relinka": "^1.4.6",
15
15
  "@reliverse/rematch": "^1.1.0",
16
16
  "@stylistic/eslint-plugin": "^4.2.0",
17
- "@types/bun": "^1.2.13",
17
+ "@types/bun": "^1.2.14",
18
18
  "@types/node": "^22.15.21",
19
19
  "eslint": "^9.27.0",
20
20
  "eslint-plugin-no-relative-import-paths": "^1.6.1",
21
21
  "eslint-plugin-perfectionist": "^4.13.0",
22
- "knip": "^5.57.1",
22
+ "knip": "^5.57.2",
23
23
  "magic-string": "^0.30.17",
24
24
  "p-map": "^7.0.3",
25
25
  "typescript": "^5.8.3",
@@ -39,4 +39,4 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  }
42
- }
42
+ }