@reliverse/pathkit 1.2.6 → 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 +65 -104
- 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,14 +2,21 @@ import fs from "node:fs/promises";
|
|
|
2
2
|
import {
|
|
3
3
|
getFileImportsExports
|
|
4
4
|
} from "./impl/getFileImportsExports.js";
|
|
5
|
-
const
|
|
5
|
+
const regularLogger = (msg) => {
|
|
6
6
|
const message = typeof msg === "function" ? msg() : msg;
|
|
7
7
|
console.log(`\x1B[2m${message}\x1B[0m`);
|
|
8
8
|
};
|
|
9
|
-
const
|
|
9
|
+
const filteredLogger = (msg) => {
|
|
10
10
|
const message = typeof msg === "function" ? msg() : msg;
|
|
11
11
|
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
12
12
|
};
|
|
13
|
+
const logInternal = (msg) => {
|
|
14
|
+
const DEBUG_MODE = true;
|
|
15
|
+
if (DEBUG_MODE) {
|
|
16
|
+
const message = typeof msg === "function" ? msg() : msg;
|
|
17
|
+
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
13
20
|
const EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".d.ts"];
|
|
14
21
|
const SLASH = "/";
|
|
15
22
|
const BACK_SLASH = "\\";
|
|
@@ -350,20 +357,15 @@ function reverseResolveAlias(path2, aliases) {
|
|
|
350
357
|
return matches.sort((a, b) => b.length - a.length);
|
|
351
358
|
}
|
|
352
359
|
const findAliasMatch = (importPath, paths) => {
|
|
353
|
-
logger2(`[findAliasMatch] Processing import: ${importPath}`);
|
|
354
|
-
logger2(` - paths: ${JSON.stringify(paths)}`);
|
|
355
360
|
const firstPathKey = Object.keys(paths)[0];
|
|
356
361
|
if (!firstPathKey) {
|
|
357
|
-
logger2(` - no paths defined`);
|
|
358
362
|
return null;
|
|
359
363
|
}
|
|
360
364
|
const baseAlias = firstPathKey.replace("/*", "");
|
|
361
365
|
if (importPath.startsWith("@") && !importPath.startsWith(baseAlias)) {
|
|
362
|
-
logger2(` - skipping: npm package import`);
|
|
363
366
|
return null;
|
|
364
367
|
}
|
|
365
368
|
if (paths[importPath]?.[0]) {
|
|
366
|
-
logger2(` - found exact match`);
|
|
367
369
|
return {
|
|
368
370
|
key: importPath,
|
|
369
371
|
root: importPath,
|
|
@@ -374,15 +376,11 @@ const findAliasMatch = (importPath, paths) => {
|
|
|
374
376
|
for (const aliasKey in paths) {
|
|
375
377
|
if (aliasKey.endsWith("/*")) {
|
|
376
378
|
const aliasRoot = aliasKey.slice(0, -2);
|
|
377
|
-
logger2(` - checking wildcard match for: ${aliasRoot}`);
|
|
378
379
|
if (importPath === aliasRoot || importPath.startsWith(`${aliasRoot}/`)) {
|
|
379
380
|
const suffix = importPath === aliasRoot ? "" : importPath.slice(aliasRoot.length + 1);
|
|
380
381
|
const targetPaths = paths[aliasKey];
|
|
381
382
|
if (targetPaths?.[0]) {
|
|
382
383
|
const resolvedPathPattern = targetPaths[0].slice(0, -2);
|
|
383
|
-
logger2(` - found wildcard match`);
|
|
384
|
-
logger2(` - suffix: ${suffix}`);
|
|
385
|
-
logger2(` - resolvedPathPattern: ${resolvedPathPattern}`);
|
|
386
384
|
return {
|
|
387
385
|
key: aliasKey,
|
|
388
386
|
root: aliasRoot,
|
|
@@ -393,7 +391,6 @@ const findAliasMatch = (importPath, paths) => {
|
|
|
393
391
|
}
|
|
394
392
|
}
|
|
395
393
|
}
|
|
396
|
-
logger2(` - no match found`);
|
|
397
394
|
return null;
|
|
398
395
|
};
|
|
399
396
|
const toRelativeImport = (absPath, fromDir) => {
|
|
@@ -435,32 +432,21 @@ async function convertStringAliasRelative({
|
|
|
435
432
|
pathPattern,
|
|
436
433
|
targetDir
|
|
437
434
|
}) {
|
|
438
|
-
logger2(`[convertStringAliasRelative] Processing import: ${importPath}`);
|
|
439
|
-
logger2(` - importerFile: ${importerFile}`);
|
|
440
|
-
logger2(` - pathPattern: ${pathPattern}`);
|
|
441
|
-
logger2(` - targetDir: ${targetDir}`);
|
|
442
435
|
const baseAlias = pathPattern.replace("/*", "");
|
|
443
436
|
if (importPath.startsWith("@") && !importPath.startsWith(baseAlias)) {
|
|
444
|
-
logger2(` - skipping: npm package import`);
|
|
445
437
|
return importPath;
|
|
446
438
|
}
|
|
447
439
|
const paths = { [pathPattern]: ["./*"] };
|
|
448
440
|
const importerDir = dirname(importerFile);
|
|
449
441
|
const match = findAliasMatch(importPath, paths);
|
|
450
442
|
if (!match) {
|
|
451
|
-
logger2(` - no alias match found`);
|
|
452
443
|
return importPath;
|
|
453
444
|
}
|
|
454
|
-
logger2(` - found alias match: ${JSON.stringify(match)}`);
|
|
455
445
|
const absPath = resolve(targetDir, match.resolvedPath, match.suffix);
|
|
456
|
-
logger2(` - resolved absolute path: ${absPath}`);
|
|
457
446
|
const resolvedFile = await resolveFileWithExtensions(absPath);
|
|
458
|
-
logger2(` - resolved file with extensions: ${resolvedFile || "not found"}`);
|
|
459
447
|
const relPath = toRelativeImport(resolvedFile || absPath, importerDir);
|
|
460
|
-
logger2(` - relative path: ${relPath}`);
|
|
461
448
|
const originalExt = extname(importPath);
|
|
462
449
|
const result = getTargetExtension(relPath, originalExt);
|
|
463
|
-
logger2(` - final result: ${result}`);
|
|
464
450
|
return result;
|
|
465
451
|
}
|
|
466
452
|
function replaceAllInString(original, searchValue, replaceValue) {
|
|
@@ -478,57 +464,62 @@ function replaceAllInString(original, searchValue, replaceValue) {
|
|
|
478
464
|
}
|
|
479
465
|
return result;
|
|
480
466
|
}
|
|
481
|
-
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);
|
|
482
470
|
const content = await fs.readFile(filePath, "utf-8");
|
|
483
471
|
let updated = content;
|
|
484
472
|
const changes = [];
|
|
485
473
|
const matches = Array.from(content.matchAll(IMPORT_REGEX));
|
|
486
474
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
487
475
|
const baseAlias = aliasToReplace.replace("/*", "");
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
476
|
+
log(`[processFile] Processing file: ${filePath}`);
|
|
477
|
+
log(` - normalizedAlias: ${normalizedAlias}`);
|
|
478
|
+
log(` - baseAlias: ${baseAlias}`);
|
|
479
|
+
log(` - found ${matches.length} import statements`);
|
|
492
480
|
for (const match of matches) {
|
|
493
481
|
const originalQuote = match[1];
|
|
494
482
|
const importPath = match[2];
|
|
495
483
|
if (!importPath) continue;
|
|
496
|
-
|
|
484
|
+
log(` Processing import: ${importPath}`);
|
|
497
485
|
if (!importPath.startsWith(baseAlias)) {
|
|
498
|
-
|
|
486
|
+
log(` - skipping: import doesn't start with ${baseAlias}`);
|
|
499
487
|
continue;
|
|
500
488
|
}
|
|
501
489
|
const importExt = extname(importPath);
|
|
502
|
-
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";
|
|
503
494
|
if (!shouldProcess) {
|
|
504
|
-
|
|
495
|
+
log(` - skipping: doesn't match pathExtFilter ${pathExtFilter}`);
|
|
505
496
|
continue;
|
|
506
497
|
}
|
|
507
|
-
|
|
498
|
+
log(` - converting alias to relative path...`);
|
|
508
499
|
const relPath = await convertStringAliasRelative({
|
|
509
500
|
importPath,
|
|
510
501
|
importerFile: filePath,
|
|
511
502
|
pathPattern: normalizedAlias,
|
|
512
503
|
targetDir
|
|
513
504
|
});
|
|
514
|
-
|
|
515
|
-
|
|
505
|
+
log(` - original path: ${importPath}`);
|
|
506
|
+
log(` - converted to: ${relPath}`);
|
|
516
507
|
const finalPath = pathExtFilter === "none" ? relPath.replace(/\.(ts|js|tsx|jsx|mjs|cjs)$/, "") : relPath;
|
|
517
508
|
if (importPath !== finalPath) {
|
|
518
509
|
changes.push({ from: importPath, to: finalPath });
|
|
519
510
|
const searchString = `${originalQuote}${importPath}${originalQuote}`;
|
|
520
511
|
const replacementString = `${originalQuote}${finalPath}${originalQuote}`;
|
|
521
512
|
updated = replaceAllInString(updated, searchString, replacementString);
|
|
522
|
-
|
|
513
|
+
log(` - applied change: ${importPath} \u2192 ${finalPath}`);
|
|
523
514
|
} else {
|
|
524
|
-
|
|
515
|
+
log(` - no change needed`);
|
|
525
516
|
}
|
|
526
517
|
}
|
|
527
518
|
if (content !== updated) {
|
|
528
519
|
await fs.writeFile(filePath, updated);
|
|
529
|
-
|
|
520
|
+
log(`\u2713 processed: ${filePath}`);
|
|
530
521
|
} else {
|
|
531
|
-
|
|
522
|
+
log(` - no changes made to file`);
|
|
532
523
|
}
|
|
533
524
|
return changes;
|
|
534
525
|
}
|
|
@@ -537,7 +528,8 @@ async function processAllFiles({
|
|
|
537
528
|
aliasToReplace,
|
|
538
529
|
extensionsToProcess,
|
|
539
530
|
rootDir,
|
|
540
|
-
pathExtFilter
|
|
531
|
+
pathExtFilter,
|
|
532
|
+
displayLogsOnlyFor
|
|
541
533
|
}) {
|
|
542
534
|
try {
|
|
543
535
|
const entries = await fs.readdir(srcDir, { withFileTypes: true });
|
|
@@ -552,7 +544,8 @@ async function processAllFiles({
|
|
|
552
544
|
aliasToReplace,
|
|
553
545
|
extensionsToProcess,
|
|
554
546
|
rootDir,
|
|
555
|
-
pathExtFilter
|
|
547
|
+
pathExtFilter,
|
|
548
|
+
displayLogsOnlyFor
|
|
556
549
|
});
|
|
557
550
|
results.push(...subdirResults);
|
|
558
551
|
} else if (extensionsToProcess.includes(extname(entry.name))) {
|
|
@@ -560,19 +553,19 @@ async function processAllFiles({
|
|
|
560
553
|
fullPath,
|
|
561
554
|
aliasToReplace,
|
|
562
555
|
rootDir,
|
|
563
|
-
pathExtFilter
|
|
556
|
+
pathExtFilter,
|
|
557
|
+
displayLogsOnlyFor
|
|
564
558
|
);
|
|
565
559
|
if (changes.length > 0) {
|
|
566
560
|
results.push({ file: fullPath, changes });
|
|
567
561
|
}
|
|
568
562
|
} else {
|
|
569
|
-
logger2(` - skipping non-matching file: ${entry.name}`);
|
|
570
563
|
}
|
|
571
564
|
})
|
|
572
565
|
);
|
|
573
566
|
return results;
|
|
574
567
|
} catch (error) {
|
|
575
|
-
|
|
568
|
+
regularLogger(
|
|
576
569
|
`error processing directory ${srcDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
577
570
|
);
|
|
578
571
|
return [];
|
|
@@ -581,35 +574,35 @@ async function processAllFiles({
|
|
|
581
574
|
async function convertImportsAliasToRelative({
|
|
582
575
|
targetDir,
|
|
583
576
|
aliasToReplace,
|
|
584
|
-
pathExtFilter
|
|
577
|
+
pathExtFilter,
|
|
578
|
+
displayLogsOnlyFor
|
|
585
579
|
}) {
|
|
586
580
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
587
|
-
|
|
581
|
+
regularLogger(
|
|
588
582
|
`Converting aliased imports starting with '${aliasToReplace}' to relative paths in "${targetDir}"...`
|
|
589
583
|
);
|
|
590
|
-
|
|
584
|
+
regularLogger(
|
|
591
585
|
` (Assuming "${normalizedAlias}" resolves relative to "${targetDir}")`
|
|
592
586
|
);
|
|
593
|
-
logger2(` (Using extension mode: ${pathExtFilter})`);
|
|
594
587
|
const results = await processAllFiles({
|
|
595
588
|
srcDir: targetDir,
|
|
596
589
|
aliasToReplace: normalizedAlias,
|
|
597
590
|
extensionsToProcess: EXTENSIONS,
|
|
598
591
|
rootDir: targetDir,
|
|
599
|
-
pathExtFilter
|
|
592
|
+
pathExtFilter,
|
|
593
|
+
displayLogsOnlyFor
|
|
600
594
|
});
|
|
601
595
|
if (results.length > 0) {
|
|
602
|
-
|
|
596
|
+
regularLogger("\n[convertImportsAliasToRelative] Summary of changes:");
|
|
603
597
|
for (const { file, changes } of results) {
|
|
604
598
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
605
|
-
|
|
599
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
606
600
|
for (const { from, to } of changes) {
|
|
607
|
-
|
|
601
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
608
602
|
}
|
|
609
603
|
}
|
|
610
|
-
} else {
|
|
611
604
|
}
|
|
612
|
-
|
|
605
|
+
regularLogger("Import path conversion process complete.");
|
|
613
606
|
return results;
|
|
614
607
|
}
|
|
615
608
|
async function convertImportsExt({
|
|
@@ -618,9 +611,6 @@ async function convertImportsExt({
|
|
|
618
611
|
extTo,
|
|
619
612
|
alias
|
|
620
613
|
}) {
|
|
621
|
-
logger2(
|
|
622
|
-
`Converting import extensions from '${extFrom}' to '${extTo}' in "${targetDir}"...`
|
|
623
|
-
);
|
|
624
614
|
const fromExtStr = extFrom === "none" ? "" : `.${extFrom}`;
|
|
625
615
|
const toExtStr = extTo === "none" ? "" : `.${extTo}`;
|
|
626
616
|
const normalizedAlias = alias ? alias.endsWith("/*") ? alias : `${alias}/*` : void 0;
|
|
@@ -693,30 +683,27 @@ async function convertImportsExt({
|
|
|
693
683
|
}
|
|
694
684
|
if (content !== updated) {
|
|
695
685
|
await fs.writeFile(fullPath, updated);
|
|
696
|
-
logger2(`\u2713 processed: ${fullPath}`);
|
|
697
686
|
if (changes.length > 0) {
|
|
698
687
|
results.push({ file: fullPath, changes });
|
|
699
688
|
}
|
|
700
689
|
}
|
|
701
690
|
} else {
|
|
702
|
-
logger2(` - skipping non-matching file: ${entry.name}`);
|
|
703
691
|
}
|
|
704
692
|
})
|
|
705
693
|
);
|
|
706
694
|
if (results.length > 0) {
|
|
707
|
-
|
|
695
|
+
regularLogger("\n[convertImportsExt] Summary of changes:");
|
|
708
696
|
for (const { file, changes } of results) {
|
|
709
697
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
710
|
-
|
|
698
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
711
699
|
for (const { from, to } of changes) {
|
|
712
|
-
|
|
700
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
713
701
|
}
|
|
714
702
|
}
|
|
715
703
|
}
|
|
716
|
-
logger2("Extension conversion complete.");
|
|
717
704
|
return results;
|
|
718
705
|
} catch (error) {
|
|
719
|
-
|
|
706
|
+
regularLogger(
|
|
720
707
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
721
708
|
);
|
|
722
709
|
return [];
|
|
@@ -725,12 +712,8 @@ async function convertImportsExt({
|
|
|
725
712
|
function stripPathSegments(path2, count = 1, alias = "") {
|
|
726
713
|
if (typeof path2 !== "string" || path2.length === 0) return path2;
|
|
727
714
|
if (count <= 0) return path2;
|
|
728
|
-
logger2(`[stripPathSegments] Processing path: ${path2}`);
|
|
729
|
-
logger2(` - count: ${count}, alias: ${alias}`);
|
|
730
715
|
const normalizedPath = normalizeWindowsPath(path2);
|
|
731
|
-
logger2(` - normalized: ${normalizedPath}`);
|
|
732
716
|
const parsed = parse(normalizedPath);
|
|
733
|
-
logger2(` - parsed: ${JSON.stringify(parsed)}`);
|
|
734
717
|
let pathSegments = [];
|
|
735
718
|
if (parsed.dir && parsed.dir !== parsed.root) {
|
|
736
719
|
let dirRelativeToRoot = parsed.dir;
|
|
@@ -743,36 +726,26 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
743
726
|
pathSegments.push(parsed.base);
|
|
744
727
|
}
|
|
745
728
|
pathSegments = pathSegments.filter(Boolean);
|
|
746
|
-
logger2(` - initial segments: ${JSON.stringify(pathSegments)}`);
|
|
747
729
|
const leadingPreservedSegments = [];
|
|
748
730
|
if (alias && pathSegments.length > 0 && pathSegments[0]?.startsWith(alias)) {
|
|
749
731
|
const preserved = pathSegments.shift();
|
|
750
732
|
if (preserved) {
|
|
751
733
|
leadingPreservedSegments.push(preserved);
|
|
752
|
-
logger2(` - preserved alias segment: ${preserved}`);
|
|
753
734
|
}
|
|
754
735
|
}
|
|
755
736
|
while (pathSegments.length > 0 && (pathSegments[0] === DOT || pathSegments[0] === DOUBLE_DOT)) {
|
|
756
737
|
const preserved = pathSegments.shift();
|
|
757
738
|
if (preserved) {
|
|
758
739
|
leadingPreservedSegments.push(preserved);
|
|
759
|
-
logger2(` - preserved relative segment: ${preserved}`);
|
|
760
740
|
}
|
|
761
741
|
}
|
|
762
742
|
const numToStrip = Math.min(count, pathSegments.length);
|
|
763
743
|
const remainingBodySegments = pathSegments.slice(numToStrip);
|
|
764
|
-
logger2(
|
|
765
|
-
` - stripping ${numToStrip} segments from: ${JSON.stringify(pathSegments)}`
|
|
766
|
-
);
|
|
767
|
-
logger2(
|
|
768
|
-
` - remaining body segments: ${JSON.stringify(remainingBodySegments)}`
|
|
769
|
-
);
|
|
770
744
|
const pathRoot = parsed.root;
|
|
771
745
|
const effectiveSegments = [
|
|
772
746
|
...leadingPreservedSegments,
|
|
773
747
|
...remainingBodySegments
|
|
774
748
|
];
|
|
775
|
-
logger2(` - effective segments: ${JSON.stringify(effectiveSegments)}`);
|
|
776
749
|
let result;
|
|
777
750
|
if (effectiveSegments.length === 0) {
|
|
778
751
|
result = normalize(pathRoot || DOT);
|
|
@@ -781,7 +754,6 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
781
754
|
} else {
|
|
782
755
|
result = join(...effectiveSegments);
|
|
783
756
|
}
|
|
784
|
-
logger2(` - final result: ${result}`);
|
|
785
757
|
return result;
|
|
786
758
|
}
|
|
787
759
|
async function stripPathSegmentsInDirectory({
|
|
@@ -790,11 +762,12 @@ async function stripPathSegmentsInDirectory({
|
|
|
790
762
|
alias = "",
|
|
791
763
|
extensionsToProcess = EXTENSIONS
|
|
792
764
|
}) {
|
|
793
|
-
|
|
765
|
+
regularLogger(
|
|
794
766
|
() => `[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`
|
|
795
767
|
);
|
|
796
|
-
|
|
797
|
-
|
|
768
|
+
regularLogger(
|
|
769
|
+
() => ` - segmentsToStrip: ${segmentsToStrip}, alias: ${alias}`
|
|
770
|
+
);
|
|
798
771
|
try {
|
|
799
772
|
const entries = await fs.readdir(targetDir, { withFileTypes: true });
|
|
800
773
|
const results = [];
|
|
@@ -803,7 +776,6 @@ async function stripPathSegmentsInDirectory({
|
|
|
803
776
|
const fullPath = join(targetDir, entry.name);
|
|
804
777
|
if (entry.isDirectory()) {
|
|
805
778
|
if (entry.name === "node_modules") return;
|
|
806
|
-
logger2(` - recursing into directory: ${entry.name}`);
|
|
807
779
|
const subdirResults = await stripPathSegmentsInDirectory({
|
|
808
780
|
targetDir: fullPath,
|
|
809
781
|
segmentsToStrip,
|
|
@@ -812,69 +784,58 @@ async function stripPathSegmentsInDirectory({
|
|
|
812
784
|
});
|
|
813
785
|
results.push(...subdirResults);
|
|
814
786
|
} else if (extensionsToProcess.includes(extname(entry.name))) {
|
|
815
|
-
logger2(` Processing file: ${entry.name}`);
|
|
816
787
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
817
788
|
let updated = content;
|
|
818
789
|
const changes = [];
|
|
819
790
|
const matches = Array.from(content.matchAll(IMPORT_REGEX));
|
|
820
|
-
logger2(` - found ${matches.length} import statements`);
|
|
821
791
|
for (const match of matches) {
|
|
822
792
|
const originalQuote = match[1];
|
|
823
793
|
const importPath = match[2];
|
|
824
794
|
if (!importPath) continue;
|
|
825
795
|
if (!importPath.includes(SLASH)) {
|
|
826
|
-
logger2(` - skipping non-path import: ${importPath}`);
|
|
827
796
|
continue;
|
|
828
797
|
}
|
|
829
798
|
if (alias && !importPath.startsWith(alias.replace("/*", ""))) {
|
|
830
|
-
logger2(` - skipping non-alias import: ${importPath}`);
|
|
831
799
|
continue;
|
|
832
800
|
}
|
|
833
|
-
logger2(` Processing import: ${importPath}`);
|
|
834
801
|
const strippedPath = stripPathSegments(
|
|
835
802
|
importPath,
|
|
836
803
|
segmentsToStrip,
|
|
837
804
|
alias
|
|
838
805
|
);
|
|
839
806
|
if (importPath === strippedPath) {
|
|
840
|
-
logger2(" - no changes needed");
|
|
841
807
|
continue;
|
|
842
808
|
}
|
|
843
809
|
changes.push({ from: importPath, to: strippedPath });
|
|
844
|
-
logger2(` - transformed: ${importPath} \u2192 ${strippedPath}`);
|
|
845
810
|
const searchStr = `${originalQuote}${importPath}${originalQuote}`;
|
|
846
811
|
const replaceStr = `${originalQuote}${strippedPath}${originalQuote}`;
|
|
847
812
|
updated = replaceAllInString(updated, searchStr, replaceStr);
|
|
848
813
|
}
|
|
849
814
|
if (content !== updated) {
|
|
850
815
|
await fs.writeFile(fullPath, updated);
|
|
851
|
-
logger2(" \u2713 wrote changes to file");
|
|
852
816
|
if (changes.length > 0) {
|
|
853
817
|
results.push({ file: fullPath, changes });
|
|
854
818
|
}
|
|
855
819
|
} else {
|
|
856
|
-
logger2(" - no changes made to file");
|
|
857
820
|
}
|
|
858
821
|
} else {
|
|
859
|
-
logger2(` - skipping non-matching file: ${entry.name}`);
|
|
860
822
|
}
|
|
861
823
|
})
|
|
862
824
|
);
|
|
863
825
|
if (results.length > 0) {
|
|
864
|
-
|
|
826
|
+
regularLogger(() => "[stripPathSegmentsInDirectory] Summary of changes:");
|
|
865
827
|
for (const { file, changes } of results) {
|
|
866
828
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
867
|
-
|
|
829
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
868
830
|
for (const { from, to } of changes) {
|
|
869
|
-
|
|
831
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
870
832
|
}
|
|
871
833
|
}
|
|
872
834
|
} else {
|
|
873
|
-
logger2(" No changes were made in any files");
|
|
874
835
|
}
|
|
875
836
|
return results;
|
|
876
837
|
} catch (error) {
|
|
877
|
-
|
|
838
|
+
regularLogger(
|
|
878
839
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
879
840
|
);
|
|
880
841
|
return [];
|
|
@@ -960,29 +921,29 @@ async function attachPathSegmentsInDirectory({
|
|
|
960
921
|
}
|
|
961
922
|
if (content !== updated) {
|
|
962
923
|
await fs.writeFile(fullPath, updated);
|
|
963
|
-
logger2(`\u2713 processed: ${fullPath}`);
|
|
964
924
|
if (changes.length > 0) {
|
|
965
925
|
results.push({ file: fullPath, changes });
|
|
966
926
|
}
|
|
967
927
|
}
|
|
968
928
|
} else {
|
|
969
|
-
logger2(` - skipping non-matching file: ${entry.name}`);
|
|
970
929
|
}
|
|
971
930
|
})
|
|
972
931
|
);
|
|
973
932
|
if (results.length > 0) {
|
|
974
|
-
|
|
933
|
+
regularLogger(
|
|
934
|
+
() => "\n[attachPathSegmentsInDirectory] Summary of changes:"
|
|
935
|
+
);
|
|
975
936
|
for (const { file, changes } of results) {
|
|
976
937
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
977
|
-
|
|
938
|
+
regularLogger(() => ` in ${displayPath}:`);
|
|
978
939
|
for (const { from, to } of changes) {
|
|
979
|
-
|
|
940
|
+
regularLogger(() => ` - ${from} \u2192 ${to}`);
|
|
980
941
|
}
|
|
981
942
|
}
|
|
982
943
|
}
|
|
983
944
|
return results;
|
|
984
945
|
} catch (error) {
|
|
985
|
-
|
|
946
|
+
regularLogger(
|
|
986
947
|
`error processing directory ${targetDir}: ${error instanceof Error ? error.message : String(error)}`
|
|
987
948
|
);
|
|
988
949
|
return [];
|