@reliverse/pathkit 1.2.5 → 1.2.7
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/mod.d.ts +2 -1
- package/bin/mod.js +63 -41
- package/package.json +1 -1
package/bin/mod.d.ts
CHANGED
|
@@ -97,10 +97,11 @@ 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, pathExtFilter, }: {
|
|
100
|
+
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, pathExtFilter, displayLogsOnlyFor, }: {
|
|
101
101
|
targetDir: string;
|
|
102
102
|
aliasToReplace: string;
|
|
103
103
|
pathExtFilter: PathExtFilter;
|
|
104
|
+
displayLogsOnlyFor?: string[];
|
|
104
105
|
}): Promise<{
|
|
105
106
|
file: string;
|
|
106
107
|
changes: {
|
package/bin/mod.js
CHANGED
|
@@ -2,19 +2,21 @@ import fs from "node:fs/promises";
|
|
|
2
2
|
import {
|
|
3
3
|
getFileImportsExports
|
|
4
4
|
} from "./impl/getFileImportsExports.js";
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
const regularLogger = (msg) => {
|
|
6
|
+
const message = typeof msg === "function" ? msg() : msg;
|
|
7
|
+
console.log(`\x1B[2m${message}\x1B[0m`);
|
|
8
|
+
};
|
|
9
|
+
const filteredLogger = (msg) => {
|
|
10
|
+
const message = typeof msg === "function" ? msg() : msg;
|
|
11
|
+
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
10
12
|
};
|
|
11
13
|
const logInternal = (msg) => {
|
|
14
|
+
const DEBUG_MODE = true;
|
|
12
15
|
if (DEBUG_MODE) {
|
|
13
16
|
const message = typeof msg === "function" ? msg() : msg;
|
|
14
17
|
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
15
18
|
}
|
|
16
19
|
};
|
|
17
|
-
const DEBUG_MODE = true;
|
|
18
20
|
const EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".d.ts"];
|
|
19
21
|
const SLASH = "/";
|
|
20
22
|
const BACK_SLASH = "\\";
|
|
@@ -462,43 +464,62 @@ function replaceAllInString(original, searchValue, replaceValue) {
|
|
|
462
464
|
}
|
|
463
465
|
return result;
|
|
464
466
|
}
|
|
465
|
-
async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter) {
|
|
467
|
+
async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, displayLogsOnlyFor) {
|
|
468
|
+
const shouldLog = !displayLogsOnlyFor || displayLogsOnlyFor.includes(filePath);
|
|
469
|
+
const log = (msg) => shouldLog && filteredLogger(msg);
|
|
466
470
|
const content = await fs.readFile(filePath, "utf-8");
|
|
467
471
|
let updated = content;
|
|
468
472
|
const changes = [];
|
|
469
473
|
const matches = Array.from(content.matchAll(IMPORT_REGEX));
|
|
470
474
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
471
475
|
const baseAlias = aliasToReplace.replace("/*", "");
|
|
476
|
+
log(`[processFile] Processing file: ${filePath}`);
|
|
477
|
+
log(` - normalizedAlias: ${normalizedAlias}`);
|
|
478
|
+
log(` - baseAlias: ${baseAlias}`);
|
|
479
|
+
log(` - found ${matches.length} import statements`);
|
|
472
480
|
for (const match of matches) {
|
|
473
481
|
const originalQuote = match[1];
|
|
474
482
|
const importPath = match[2];
|
|
475
483
|
if (!importPath) continue;
|
|
484
|
+
log(` Processing import: ${importPath}`);
|
|
476
485
|
if (!importPath.startsWith(baseAlias)) {
|
|
486
|
+
log(` - skipping: import doesn't start with ${baseAlias}`);
|
|
477
487
|
continue;
|
|
478
488
|
}
|
|
479
489
|
const importExt = extname(importPath);
|
|
480
|
-
const shouldProcess = pathExtFilter === "js" && importExt === ".js" ||
|
|
490
|
+
const shouldProcess = pathExtFilter === "js" && importExt === ".js" || // process paths with .js extension
|
|
491
|
+
pathExtFilter === "ts" && importExt === ".ts" || // process paths with .ts extension
|
|
492
|
+
pathExtFilter === "none" && importExt === "" || // process paths without any extension
|
|
493
|
+
pathExtFilter === "js-ts-none";
|
|
481
494
|
if (!shouldProcess) {
|
|
495
|
+
log(` - skipping: doesn't match pathExtFilter ${pathExtFilter}`);
|
|
482
496
|
continue;
|
|
483
497
|
}
|
|
498
|
+
log(` - converting alias to relative path...`);
|
|
484
499
|
const relPath = await convertStringAliasRelative({
|
|
485
500
|
importPath,
|
|
486
501
|
importerFile: filePath,
|
|
487
502
|
pathPattern: normalizedAlias,
|
|
488
503
|
targetDir
|
|
489
504
|
});
|
|
505
|
+
log(` - original path: ${importPath}`);
|
|
506
|
+
log(` - converted to: ${relPath}`);
|
|
490
507
|
const finalPath = pathExtFilter === "none" ? relPath.replace(/\.(ts|js|tsx|jsx|mjs|cjs)$/, "") : relPath;
|
|
491
508
|
if (importPath !== finalPath) {
|
|
492
509
|
changes.push({ from: importPath, to: finalPath });
|
|
493
510
|
const searchString = `${originalQuote}${importPath}${originalQuote}`;
|
|
494
511
|
const replacementString = `${originalQuote}${finalPath}${originalQuote}`;
|
|
495
512
|
updated = replaceAllInString(updated, searchString, replacementString);
|
|
513
|
+
log(` - applied change: ${importPath} \u2192 ${finalPath}`);
|
|
496
514
|
} else {
|
|
515
|
+
log(` - no change needed`);
|
|
497
516
|
}
|
|
498
517
|
}
|
|
499
518
|
if (content !== updated) {
|
|
500
519
|
await fs.writeFile(filePath, updated);
|
|
520
|
+
log(`\u2713 processed: ${filePath}`);
|
|
501
521
|
} else {
|
|
522
|
+
log(` - no changes made to file`);
|
|
502
523
|
}
|
|
503
524
|
return changes;
|
|
504
525
|
}
|
|
@@ -507,7 +528,8 @@ async function processAllFiles({
|
|
|
507
528
|
aliasToReplace,
|
|
508
529
|
extensionsToProcess,
|
|
509
530
|
rootDir,
|
|
510
|
-
pathExtFilter
|
|
531
|
+
pathExtFilter,
|
|
532
|
+
displayLogsOnlyFor
|
|
511
533
|
}) {
|
|
512
534
|
try {
|
|
513
535
|
const entries = await fs.readdir(srcDir, { withFileTypes: true });
|
|
@@ -522,7 +544,8 @@ async function processAllFiles({
|
|
|
522
544
|
aliasToReplace,
|
|
523
545
|
extensionsToProcess,
|
|
524
546
|
rootDir,
|
|
525
|
-
pathExtFilter
|
|
547
|
+
pathExtFilter,
|
|
548
|
+
displayLogsOnlyFor
|
|
526
549
|
});
|
|
527
550
|
results.push(...subdirResults);
|
|
528
551
|
} else if (extensionsToProcess.includes(extname(entry.name))) {
|
|
@@ -530,7 +553,8 @@ async function processAllFiles({
|
|
|
530
553
|
fullPath,
|
|
531
554
|
aliasToReplace,
|
|
532
555
|
rootDir,
|
|
533
|
-
pathExtFilter
|
|
556
|
+
pathExtFilter,
|
|
557
|
+
displayLogsOnlyFor
|
|
534
558
|
);
|
|
535
559
|
if (changes.length > 0) {
|
|
536
560
|
results.push({ file: fullPath, changes });
|
|
@@ -541,7 +565,7 @@ async function processAllFiles({
|
|
|
541
565
|
);
|
|
542
566
|
return results;
|
|
543
567
|
} catch (error) {
|
|
544
|
-
|
|
568
|
+
regularLogger(
|
|
545
569
|
`error processing directory ${srcDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
546
570
|
);
|
|
547
571
|
return [];
|
|
@@ -550,13 +574,14 @@ async function processAllFiles({
|
|
|
550
574
|
async function convertImportsAliasToRelative({
|
|
551
575
|
targetDir,
|
|
552
576
|
aliasToReplace,
|
|
553
|
-
pathExtFilter
|
|
577
|
+
pathExtFilter,
|
|
578
|
+
displayLogsOnlyFor
|
|
554
579
|
}) {
|
|
555
580
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
556
|
-
|
|
581
|
+
regularLogger(
|
|
557
582
|
`Converting aliased imports starting with '${aliasToReplace}' to relative paths in "${targetDir}"...`
|
|
558
583
|
);
|
|
559
|
-
|
|
584
|
+
regularLogger(
|
|
560
585
|
` (Assuming "${normalizedAlias}" resolves relative to "${targetDir}")`
|
|
561
586
|
);
|
|
562
587
|
const results = await processAllFiles({
|
|
@@ -564,20 +589,20 @@ async function convertImportsAliasToRelative({
|
|
|
564
589
|
aliasToReplace: normalizedAlias,
|
|
565
590
|
extensionsToProcess: EXTENSIONS,
|
|
566
591
|
rootDir: targetDir,
|
|
567
|
-
pathExtFilter
|
|
592
|
+
pathExtFilter,
|
|
593
|
+
displayLogsOnlyFor
|
|
568
594
|
});
|
|
569
595
|
if (results.length > 0) {
|
|
570
|
-
|
|
596
|
+
regularLogger("\n[convertImportsAliasToRelative] Summary of changes:");
|
|
571
597
|
for (const { file, changes } of results) {
|
|
572
598
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
573
|
-
|
|
599
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
574
600
|
for (const { from, to } of changes) {
|
|
575
|
-
|
|
601
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
576
602
|
}
|
|
577
603
|
}
|
|
578
|
-
} else {
|
|
579
604
|
}
|
|
580
|
-
|
|
605
|
+
regularLogger("Import path conversion process complete.");
|
|
581
606
|
return results;
|
|
582
607
|
}
|
|
583
608
|
async function convertImportsExt({
|
|
@@ -667,18 +692,18 @@ async function convertImportsExt({
|
|
|
667
692
|
})
|
|
668
693
|
);
|
|
669
694
|
if (results.length > 0) {
|
|
670
|
-
|
|
695
|
+
regularLogger("\n[convertImportsExt] Summary of changes:");
|
|
671
696
|
for (const { file, changes } of results) {
|
|
672
697
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
673
|
-
|
|
698
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
674
699
|
for (const { from, to } of changes) {
|
|
675
|
-
|
|
700
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
676
701
|
}
|
|
677
702
|
}
|
|
678
703
|
}
|
|
679
704
|
return results;
|
|
680
705
|
} catch (error) {
|
|
681
|
-
|
|
706
|
+
regularLogger(
|
|
682
707
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
683
708
|
);
|
|
684
709
|
return [];
|
|
@@ -737,13 +762,11 @@ async function stripPathSegmentsInDirectory({
|
|
|
737
762
|
alias = "",
|
|
738
763
|
extensionsToProcess = EXTENSIONS
|
|
739
764
|
}) {
|
|
740
|
-
|
|
741
|
-
() => `[stripPathSegmentsInDirectory] Processing directory: ${targetDir}
|
|
742
|
-
true
|
|
765
|
+
regularLogger(
|
|
766
|
+
() => `[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`
|
|
743
767
|
);
|
|
744
|
-
|
|
745
|
-
() => ` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}
|
|
746
|
-
true
|
|
768
|
+
regularLogger(
|
|
769
|
+
() => ` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}`
|
|
747
770
|
);
|
|
748
771
|
try {
|
|
749
772
|
const entries = await fs.readdir(targetDir, { withFileTypes: true });
|
|
@@ -800,19 +823,19 @@ async function stripPathSegmentsInDirectory({
|
|
|
800
823
|
})
|
|
801
824
|
);
|
|
802
825
|
if (results.length > 0) {
|
|
803
|
-
|
|
826
|
+
regularLogger(() => "[stripPathSegmentsInDirectory] Summary of changes:");
|
|
804
827
|
for (const { file, changes } of results) {
|
|
805
828
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
806
|
-
|
|
829
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
807
830
|
for (const { from, to } of changes) {
|
|
808
|
-
|
|
831
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
809
832
|
}
|
|
810
833
|
}
|
|
811
834
|
} else {
|
|
812
835
|
}
|
|
813
836
|
return results;
|
|
814
837
|
} catch (error) {
|
|
815
|
-
|
|
838
|
+
regularLogger(
|
|
816
839
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
817
840
|
);
|
|
818
841
|
return [];
|
|
@@ -907,21 +930,20 @@ async function attachPathSegmentsInDirectory({
|
|
|
907
930
|
})
|
|
908
931
|
);
|
|
909
932
|
if (results.length > 0) {
|
|
910
|
-
|
|
911
|
-
() => "\n[attachPathSegmentsInDirectory] Summary of changes:"
|
|
912
|
-
true
|
|
933
|
+
regularLogger(
|
|
934
|
+
() => "\n[attachPathSegmentsInDirectory] Summary of changes:"
|
|
913
935
|
);
|
|
914
936
|
for (const { file, changes } of results) {
|
|
915
937
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
916
|
-
|
|
938
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
917
939
|
for (const { from, to } of changes) {
|
|
918
|
-
|
|
940
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
919
941
|
}
|
|
920
942
|
}
|
|
921
943
|
}
|
|
922
944
|
return results;
|
|
923
945
|
} catch (error) {
|
|
924
|
-
|
|
946
|
+
regularLogger(
|
|
925
947
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
926
948
|
);
|
|
927
949
|
return [];
|