@reliverse/pathkit 1.1.6 → 1.1.8
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/bin/impl/getFileImportsExports.js +14 -12
- package/bin/mod.d.ts +1 -2
- package/bin/mod.js +59 -30
- package/package.json +1 -1
|
@@ -29,7 +29,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
29
29
|
const specifiers = [];
|
|
30
30
|
const isTypeOnly = statement.includes("import type") || statement.includes("export type");
|
|
31
31
|
const namespaceMatch = /(?:import|export)\s+(?:type\s+)?\*\s+as\s+(\w+)/.exec(statement);
|
|
32
|
-
if (namespaceMatch) {
|
|
32
|
+
if (namespaceMatch?.[1]) {
|
|
33
33
|
specifiers.push({
|
|
34
34
|
type: "namespace",
|
|
35
35
|
name: namespaceMatch[1],
|
|
@@ -40,7 +40,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
40
40
|
const defaultMatch = /import\s+(?:type\s+)?(\w+)(?:\s*,|\s+from)/.exec(
|
|
41
41
|
statement
|
|
42
42
|
);
|
|
43
|
-
if (defaultMatch && !statement.includes("{")) {
|
|
43
|
+
if (defaultMatch?.[1] && !statement.includes("{")) {
|
|
44
44
|
specifiers.push({
|
|
45
45
|
type: "default",
|
|
46
46
|
name: defaultMatch[1],
|
|
@@ -48,14 +48,16 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
const namedMatch = /{([^}]*)}/.exec(statement);
|
|
51
|
-
if (namedMatch) {
|
|
51
|
+
if (namedMatch?.[1]) {
|
|
52
52
|
const items = namedMatch[1].split(",").map((item) => item.trim()).filter((item) => item.length > 0);
|
|
53
53
|
for (const item of items) {
|
|
54
54
|
const typeMatch = /^type\s+(.+)/.exec(item);
|
|
55
55
|
const actualItem = typeMatch ? typeMatch[1] : item;
|
|
56
|
+
if (!actualItem) continue;
|
|
56
57
|
const isItemType = !!typeMatch || isTypeOnly;
|
|
57
58
|
if (actualItem.includes(" as ")) {
|
|
58
59
|
const [name, alias] = actualItem.split(" as ").map((p) => p.trim());
|
|
60
|
+
if (!name) continue;
|
|
59
61
|
specifiers.push({
|
|
60
62
|
type: "named",
|
|
61
63
|
name,
|
|
@@ -79,7 +81,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
79
81
|
const source = match[1] || match[3];
|
|
80
82
|
if (!source) continue;
|
|
81
83
|
const pathType = getPathType(source);
|
|
82
|
-
if (!pathTypes.includes(pathType)) continue;
|
|
84
|
+
if (!pathType || !pathTypes.includes(pathType)) continue;
|
|
83
85
|
const info = {
|
|
84
86
|
statement: match[0],
|
|
85
87
|
type: "static",
|
|
@@ -89,8 +91,8 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
89
91
|
pathTypeSymbol: pathType === "alias" ? /^[@~]/.exec(source)?.[0] : void 0,
|
|
90
92
|
isTypeOnly: match[0].includes("import type"),
|
|
91
93
|
specifiers: extractSpecifiers(match[0]),
|
|
92
|
-
start: match.index,
|
|
93
|
-
end: match.index + match[0].length
|
|
94
|
+
start: match.index ?? 0,
|
|
95
|
+
end: (match.index ?? 0) + match[0].length
|
|
94
96
|
};
|
|
95
97
|
results.push(info);
|
|
96
98
|
}
|
|
@@ -99,7 +101,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
99
101
|
const source = match[1];
|
|
100
102
|
if (!source) continue;
|
|
101
103
|
const pathType = getPathType(source);
|
|
102
|
-
if (!pathTypes.includes(pathType)) continue;
|
|
104
|
+
if (!pathType || !pathTypes.includes(pathType)) continue;
|
|
103
105
|
const info = {
|
|
104
106
|
statement: match[0],
|
|
105
107
|
type: "dynamic",
|
|
@@ -109,8 +111,8 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
109
111
|
pathTypeSymbol: pathType === "alias" ? /^[@~]/.exec(source)?.[0] : void 0,
|
|
110
112
|
isTypeOnly: false,
|
|
111
113
|
specifiers: [],
|
|
112
|
-
start: match.index,
|
|
113
|
-
end: match.index + match[0].length
|
|
114
|
+
start: match.index ?? 0,
|
|
115
|
+
end: (match.index ?? 0) + match[0].length
|
|
114
116
|
};
|
|
115
117
|
results.push(info);
|
|
116
118
|
}
|
|
@@ -121,7 +123,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
121
123
|
const source = match[1];
|
|
122
124
|
if (!source) continue;
|
|
123
125
|
const pathType = getPathType(source);
|
|
124
|
-
if (!pathTypes.includes(pathType)) continue;
|
|
126
|
+
if (!pathType || !pathTypes.includes(pathType)) continue;
|
|
125
127
|
const info = {
|
|
126
128
|
statement: match[0],
|
|
127
129
|
type: "static",
|
|
@@ -131,8 +133,8 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
131
133
|
pathTypeSymbol: pathType === "alias" ? /^[@~]/.exec(source)?.[0] : void 0,
|
|
132
134
|
isTypeOnly: match[0].includes("export type"),
|
|
133
135
|
specifiers: extractSpecifiers(match[0]),
|
|
134
|
-
start: match.index,
|
|
135
|
-
end: match.index + match[0].length
|
|
136
|
+
start: match.index ?? 0,
|
|
137
|
+
end: (match.index ?? 0) + match[0].length
|
|
136
138
|
};
|
|
137
139
|
results.push(info);
|
|
138
140
|
}
|
package/bin/mod.d.ts
CHANGED
|
@@ -97,8 +97,7 @@ declare function convertStringAliasRelative({ importPath, importerFile, pathPatt
|
|
|
97
97
|
/**
|
|
98
98
|
* main function to convert import paths from aliases to relative paths
|
|
99
99
|
*/
|
|
100
|
-
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace,
|
|
101
|
-
pathExtFilter, }: {
|
|
100
|
+
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, pathExtFilter, }: {
|
|
102
101
|
targetDir: string;
|
|
103
102
|
aliasToReplace: string;
|
|
104
103
|
pathExtFilter: PathExtFilter;
|
package/bin/mod.js
CHANGED
|
@@ -2,8 +2,19 @@ import fs from "node:fs/promises";
|
|
|
2
2
|
import {
|
|
3
3
|
getFileImportsExports
|
|
4
4
|
} from "./impl/getFileImportsExports.js";
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const logger = (msg, debugOnly = false) => {
|
|
6
|
+
if (!debugOnly || DEBUG_MODE) {
|
|
7
|
+
const message = typeof msg === "function" ? msg() : msg;
|
|
8
|
+
console.log(`\x1B[2m${message}\x1B[0m`);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const logInternal = (msg) => {
|
|
12
|
+
if (DEBUG_MODE) {
|
|
13
|
+
const message = typeof msg === "function" ? msg() : msg;
|
|
14
|
+
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const DEBUG_MODE = false;
|
|
7
18
|
const EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
|
|
8
19
|
const SLASH = "/";
|
|
9
20
|
const BACK_SLASH = "\\";
|
|
@@ -39,7 +50,7 @@ function normalizeString(path2, allowAboveRoot) {
|
|
|
39
50
|
let char = null;
|
|
40
51
|
for (let index = 0; index <= path2.length; ++index) {
|
|
41
52
|
if (index < path2.length) {
|
|
42
|
-
char = path2[index];
|
|
53
|
+
char = path2[index] ?? null;
|
|
43
54
|
} else if (char === SLASH) {
|
|
44
55
|
break;
|
|
45
56
|
} else {
|
|
@@ -190,7 +201,7 @@ const relative = (from, to) => {
|
|
|
190
201
|
if (resolvedFrom === resolvedTo) return EMPTY;
|
|
191
202
|
const fromSegments = resolvedFrom.replace(ROOT_FOLDER_RE, "$1").split(SLASH).filter(Boolean);
|
|
192
203
|
const toSegments = resolvedTo.replace(ROOT_FOLDER_RE, "$1").split(SLASH).filter(Boolean);
|
|
193
|
-
if (fromSegments.length > 0 && toSegments.length > 0 && DRIVE_LETTER_RE.test(fromSegments[0]) && DRIVE_LETTER_RE.test(toSegments[0]) && fromSegments[0].toUpperCase() !== toSegments[0].toUpperCase()) {
|
|
204
|
+
if (fromSegments.length > 0 && toSegments.length > 0 && fromSegments[0] && toSegments[0] && DRIVE_LETTER_RE.test(fromSegments[0]) && DRIVE_LETTER_RE.test(toSegments[0]) && fromSegments[0].toUpperCase() !== toSegments[0].toUpperCase()) {
|
|
194
205
|
return resolvedTo;
|
|
195
206
|
}
|
|
196
207
|
let commonSegments = 0;
|
|
@@ -345,6 +356,7 @@ function reverseResolveAlias(path2, aliases) {
|
|
|
345
356
|
}
|
|
346
357
|
const findAliasMatch = (importPath, paths) => {
|
|
347
358
|
const firstPathKey = Object.keys(paths)[0];
|
|
359
|
+
if (!firstPathKey) return null;
|
|
348
360
|
const baseAlias = firstPathKey.replace("/*", "");
|
|
349
361
|
if (importPath.startsWith("@") && !importPath.startsWith(baseAlias)) {
|
|
350
362
|
return null;
|
|
@@ -455,6 +467,7 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter) {
|
|
|
455
467
|
for (const match of matches) {
|
|
456
468
|
const originalQuote = match[1];
|
|
457
469
|
const importPath = match[2];
|
|
470
|
+
if (!importPath) continue;
|
|
458
471
|
if (!importPath.startsWith(baseAlias)) {
|
|
459
472
|
continue;
|
|
460
473
|
}
|
|
@@ -519,7 +532,7 @@ async function processAllFiles({
|
|
|
519
532
|
);
|
|
520
533
|
return results;
|
|
521
534
|
} catch (error) {
|
|
522
|
-
|
|
535
|
+
logger(
|
|
523
536
|
`error processing directory ${srcDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
524
537
|
);
|
|
525
538
|
return [];
|
|
@@ -528,14 +541,15 @@ async function processAllFiles({
|
|
|
528
541
|
async function convertImportsAliasToRelative({
|
|
529
542
|
targetDir,
|
|
530
543
|
aliasToReplace,
|
|
531
|
-
// e.g. @, ~, @/*, ~/*
|
|
532
544
|
pathExtFilter
|
|
533
545
|
}) {
|
|
534
546
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
535
|
-
|
|
547
|
+
logger(
|
|
536
548
|
`Converting aliased imports starting with '${aliasToReplace}' to relative paths in "${targetDir}"...`
|
|
537
549
|
);
|
|
538
|
-
|
|
550
|
+
logger(
|
|
551
|
+
` (Assuming "${normalizedAlias}" resolves relative to "${targetDir}")`
|
|
552
|
+
);
|
|
539
553
|
const results = await processAllFiles({
|
|
540
554
|
srcDir: targetDir,
|
|
541
555
|
aliasToReplace: normalizedAlias,
|
|
@@ -544,17 +558,17 @@ async function convertImportsAliasToRelative({
|
|
|
544
558
|
pathExtFilter
|
|
545
559
|
});
|
|
546
560
|
if (results.length > 0) {
|
|
547
|
-
|
|
561
|
+
logger("\n[convertImportsAliasToRelative] Summary of changes:", true);
|
|
548
562
|
for (const { file, changes } of results) {
|
|
549
563
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
550
|
-
|
|
564
|
+
logger(() => ` in ${displayPath}:`, true);
|
|
551
565
|
for (const { from, to } of changes) {
|
|
552
|
-
|
|
566
|
+
logger(() => ` - ${from} \u2192 ${to}`, true);
|
|
553
567
|
}
|
|
554
568
|
}
|
|
555
569
|
} else {
|
|
556
570
|
}
|
|
557
|
-
|
|
571
|
+
logger("Import path conversion process complete.");
|
|
558
572
|
return results;
|
|
559
573
|
}
|
|
560
574
|
async function convertImportsExt({
|
|
@@ -591,6 +605,7 @@ async function convertImportsExt({
|
|
|
591
605
|
while (match !== null) {
|
|
592
606
|
const quote = match[1];
|
|
593
607
|
const importPath = match[2];
|
|
608
|
+
if (!importPath) continue;
|
|
594
609
|
let replacementPath;
|
|
595
610
|
if (extFrom === "none") {
|
|
596
611
|
replacementPath = importPath + toExtStr;
|
|
@@ -617,19 +632,18 @@ async function convertImportsExt({
|
|
|
617
632
|
})
|
|
618
633
|
);
|
|
619
634
|
if (results.length > 0) {
|
|
620
|
-
|
|
635
|
+
logger("\n[convertImportsExt] Summary of changes:", true);
|
|
621
636
|
for (const { file, changes } of results) {
|
|
622
637
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
623
|
-
|
|
638
|
+
logger(() => ` in ${displayPath}:`, true);
|
|
624
639
|
for (const { from, to } of changes) {
|
|
625
|
-
|
|
640
|
+
logger(() => ` - ${from} \u2192 ${to}`, true);
|
|
626
641
|
}
|
|
627
642
|
}
|
|
628
|
-
} else {
|
|
629
643
|
}
|
|
630
644
|
return results;
|
|
631
645
|
} catch (error) {
|
|
632
|
-
|
|
646
|
+
logger(
|
|
633
647
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
634
648
|
);
|
|
635
649
|
return [];
|
|
@@ -653,13 +667,17 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
653
667
|
}
|
|
654
668
|
pathSegments = pathSegments.filter(Boolean);
|
|
655
669
|
const leadingPreservedSegments = [];
|
|
656
|
-
if (alias && pathSegments.length > 0 && pathSegments[0]
|
|
670
|
+
if (alias && pathSegments.length > 0 && pathSegments[0]?.startsWith(alias)) {
|
|
657
671
|
const preserved = pathSegments.shift();
|
|
658
|
-
|
|
672
|
+
if (preserved) {
|
|
673
|
+
leadingPreservedSegments.push(preserved);
|
|
674
|
+
}
|
|
659
675
|
}
|
|
660
676
|
while (pathSegments.length > 0 && (pathSegments[0] === DOT || pathSegments[0] === DOUBLE_DOT)) {
|
|
661
677
|
const preserved = pathSegments.shift();
|
|
662
|
-
|
|
678
|
+
if (preserved) {
|
|
679
|
+
leadingPreservedSegments.push(preserved);
|
|
680
|
+
}
|
|
663
681
|
}
|
|
664
682
|
const numToStrip = Math.min(count, pathSegments.length);
|
|
665
683
|
const remainingBodySegments = pathSegments.slice(numToStrip);
|
|
@@ -684,8 +702,14 @@ async function stripPathSegmentsInDirectory({
|
|
|
684
702
|
alias = "",
|
|
685
703
|
extensionsToProcess = EXTENSIONS
|
|
686
704
|
}) {
|
|
687
|
-
|
|
688
|
-
|
|
705
|
+
logger(
|
|
706
|
+
() => `[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`,
|
|
707
|
+
true
|
|
708
|
+
);
|
|
709
|
+
logger(
|
|
710
|
+
() => ` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}`,
|
|
711
|
+
true
|
|
712
|
+
);
|
|
689
713
|
try {
|
|
690
714
|
const entries = await fs.readdir(targetDir, { withFileTypes: true });
|
|
691
715
|
const results = [];
|
|
@@ -709,6 +733,7 @@ async function stripPathSegmentsInDirectory({
|
|
|
709
733
|
for (const match of matches) {
|
|
710
734
|
const originalQuote = match[1];
|
|
711
735
|
const importPath = match[2];
|
|
736
|
+
if (!importPath) continue;
|
|
712
737
|
if (!importPath.includes(SLASH)) {
|
|
713
738
|
continue;
|
|
714
739
|
}
|
|
@@ -740,19 +765,19 @@ async function stripPathSegmentsInDirectory({
|
|
|
740
765
|
})
|
|
741
766
|
);
|
|
742
767
|
if (results.length > 0) {
|
|
743
|
-
|
|
768
|
+
logger(() => "[stripPathSegmentsInDirectory] Summary of changes:", true);
|
|
744
769
|
for (const { file, changes } of results) {
|
|
745
770
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
746
|
-
|
|
771
|
+
logger(() => ` in ${displayPath}:`, true);
|
|
747
772
|
for (const { from, to } of changes) {
|
|
748
|
-
|
|
773
|
+
logger(() => ` - ${from} \u2192 ${to}`, true);
|
|
749
774
|
}
|
|
750
775
|
}
|
|
751
776
|
} else {
|
|
752
777
|
}
|
|
753
778
|
return results;
|
|
754
779
|
} catch (error) {
|
|
755
|
-
|
|
780
|
+
logger(
|
|
756
781
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
757
782
|
);
|
|
758
783
|
return [];
|
|
@@ -823,6 +848,7 @@ async function attachPathSegmentsInDirectory({
|
|
|
823
848
|
for (const match of matches) {
|
|
824
849
|
const originalQuote = match[1];
|
|
825
850
|
const importPath = match[2];
|
|
851
|
+
if (!importPath) continue;
|
|
826
852
|
if (!importPath.includes(SLASH)) continue;
|
|
827
853
|
const modifiedPath = attachPathSegments(
|
|
828
854
|
importPath,
|
|
@@ -846,18 +872,21 @@ async function attachPathSegmentsInDirectory({
|
|
|
846
872
|
})
|
|
847
873
|
);
|
|
848
874
|
if (results.length > 0) {
|
|
849
|
-
|
|
875
|
+
logger(
|
|
876
|
+
() => "\n[attachPathSegmentsInDirectory] Summary of changes:",
|
|
877
|
+
true
|
|
878
|
+
);
|
|
850
879
|
for (const { file, changes } of results) {
|
|
851
880
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
852
|
-
|
|
881
|
+
logger(() => ` in ${displayPath}:`, true);
|
|
853
882
|
for (const { from, to } of changes) {
|
|
854
|
-
|
|
883
|
+
logger(() => ` - ${from} \u2192 ${to}`, true);
|
|
855
884
|
}
|
|
856
885
|
}
|
|
857
886
|
}
|
|
858
887
|
return results;
|
|
859
888
|
} catch (error) {
|
|
860
|
-
|
|
889
|
+
logger(
|
|
861
890
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
862
891
|
);
|
|
863
892
|
return [];
|