@reliverse/pathkit 1.2.7 → 1.2.9
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 +14 -23
- package/bin/mod.js +27 -19
- package/package.json +1 -1
package/bin/mod.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PlatformPath } from "node:path";
|
|
2
2
|
import { getFileImportsExports, type ImportExportInfo, type ImportExportSpecifier, type GetFileImportsExportsOptions } from "./impl/getFileImportsExports.js";
|
|
3
3
|
declare const normalizedAliasSymbol: unique symbol;
|
|
4
|
+
export declare const IMPORT_EXPORT_REGEX: RegExp;
|
|
4
5
|
interface NormalizedRecord extends Record<string, string> {
|
|
5
6
|
[normalizedAliasSymbol]?: true;
|
|
6
7
|
}
|
|
@@ -95,13 +96,14 @@ declare function convertStringAliasRelative({ importPath, importerFile, pathPatt
|
|
|
95
96
|
targetDir: string;
|
|
96
97
|
}): Promise<string>;
|
|
97
98
|
/**
|
|
98
|
-
* main function to convert import paths from aliases to relative paths
|
|
99
|
+
* main function to convert import/export paths from aliases to relative paths
|
|
99
100
|
*/
|
|
100
|
-
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, pathExtFilter, displayLogsOnlyFor, }: {
|
|
101
|
+
declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, pathExtFilter, displayLogsOnlyFor, displayLogs, }: {
|
|
101
102
|
targetDir: string;
|
|
102
103
|
aliasToReplace: string;
|
|
103
104
|
pathExtFilter: PathExtFilter;
|
|
104
105
|
displayLogsOnlyFor?: string[];
|
|
106
|
+
displayLogs?: boolean;
|
|
105
107
|
}): Promise<{
|
|
106
108
|
file: string;
|
|
107
109
|
changes: {
|
|
@@ -110,13 +112,14 @@ declare function convertImportsAliasToRelative({ targetDir, aliasToReplace, path
|
|
|
110
112
|
}[];
|
|
111
113
|
}[]>;
|
|
112
114
|
/**
|
|
113
|
-
* converts extensions in import paths from one format to another
|
|
115
|
+
* converts extensions in import/export paths from one format to another
|
|
114
116
|
*/
|
|
115
|
-
declare function convertImportsExt({ targetDir, extFrom, extTo, alias, }: {
|
|
117
|
+
declare function convertImportsExt({ targetDir, extFrom, extTo, alias, displayLogs, }: {
|
|
116
118
|
targetDir: string;
|
|
117
119
|
extFrom: ImportExtType;
|
|
118
120
|
extTo: ImportExtType;
|
|
119
121
|
alias?: string;
|
|
122
|
+
displayLogs?: boolean;
|
|
120
123
|
}): Promise<{
|
|
121
124
|
file: string;
|
|
122
125
|
changes: {
|
|
@@ -140,11 +143,12 @@ declare function stripPathSegments(path: string, count?: number, alias?: string)
|
|
|
140
143
|
* @param extensionsToProcess - Array of file extensions to process (default: EXTENSIONS)
|
|
141
144
|
* @returns Array of processed files and their changes
|
|
142
145
|
*/
|
|
143
|
-
declare function stripPathSegmentsInDirectory({ targetDir, segmentsToStrip, alias, extensionsToProcess, }: {
|
|
146
|
+
declare function stripPathSegmentsInDirectory({ targetDir, segmentsToStrip, alias, extensionsToProcess, displayLogs, }: {
|
|
144
147
|
targetDir: string;
|
|
145
148
|
segmentsToStrip: number;
|
|
146
149
|
alias?: string;
|
|
147
150
|
extensionsToProcess?: string[];
|
|
151
|
+
displayLogs?: boolean;
|
|
148
152
|
}): Promise<{
|
|
149
153
|
file: string;
|
|
150
154
|
changes: {
|
|
@@ -167,18 +171,19 @@ declare function attachPathSegments(path: string, segments: string | string[], o
|
|
|
167
171
|
preserveAlias?: string;
|
|
168
172
|
}): string;
|
|
169
173
|
/**
|
|
170
|
-
* recursively processes files in a directory to attach path segments to import statements
|
|
174
|
+
* recursively processes files in a directory to attach path segments to import/export statements
|
|
171
175
|
* @param targetDir - The directory to process
|
|
172
176
|
* @param segments - The segments to attach
|
|
173
177
|
* @param options - Configuration options for path segment attachment
|
|
174
178
|
* @param extensionsToProcess - Array of file extensions to process (default: EXTENSIONS)
|
|
175
179
|
* @returns Array of processed files and their changes
|
|
176
180
|
*/
|
|
177
|
-
declare function attachPathSegmentsInDirectory({ targetDir, segments, options, extensionsToProcess, }: {
|
|
181
|
+
declare function attachPathSegmentsInDirectory({ targetDir, segments, options, extensionsToProcess, displayLogs, }: {
|
|
178
182
|
targetDir: string;
|
|
179
183
|
segments: string | string[];
|
|
180
184
|
options?: Parameters<typeof attachPathSegments>[2];
|
|
181
185
|
extensionsToProcess?: string[];
|
|
186
|
+
displayLogs?: boolean;
|
|
182
187
|
}): Promise<{
|
|
183
188
|
file: string;
|
|
184
189
|
changes: {
|
|
@@ -201,26 +206,12 @@ declare const _pathBase: {
|
|
|
201
206
|
relative: (from: string, to: string) => string;
|
|
202
207
|
filename: typeof filename;
|
|
203
208
|
};
|
|
204
|
-
declare const path: PlatformPath & {
|
|
205
|
-
sep: string;
|
|
206
|
-
normalize: (path: string) => string;
|
|
207
|
-
join: (...segments: string[]) => string;
|
|
208
|
-
resolve: (...args: string[]) => string;
|
|
209
|
-
isAbsolute: (p: string) => boolean;
|
|
210
|
-
dirname: (p: string) => string;
|
|
211
|
-
basename: (p: string, ext?: string) => string;
|
|
212
|
-
extname: (p: string) => string;
|
|
213
|
-
format: (p: FormatInputPathObject) => string;
|
|
214
|
-
parse: (p: string) => ParsedPath;
|
|
215
|
-
toNamespacedPath: (p: string) => string;
|
|
216
|
-
relative: (from: string, to: string) => string;
|
|
217
|
-
filename: typeof filename;
|
|
218
|
-
} & {
|
|
209
|
+
declare const path: PlatformPath & typeof _pathBase & {
|
|
219
210
|
posix: PlatformPath;
|
|
220
211
|
win32: PlatformPath;
|
|
221
212
|
};
|
|
222
213
|
declare const win32: PlatformPath;
|
|
223
|
-
declare const delimiter:
|
|
214
|
+
declare const delimiter: ";" | ":";
|
|
224
215
|
export type { PlatformPath, PathExtFilter, ImportExtType, ImportExportInfo, ImportExportSpecifier, GetFileImportsExportsOptions, };
|
|
225
216
|
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, getFileImportsExports, };
|
|
226
217
|
export default path;
|
package/bin/mod.js
CHANGED
|
@@ -10,8 +10,8 @@ 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 DEBUG_MODE = false;
|
|
13
14
|
const logInternal = (msg) => {
|
|
14
|
-
const DEBUG_MODE = true;
|
|
15
15
|
if (DEBUG_MODE) {
|
|
16
16
|
const message = typeof msg === "function" ? msg() : msg;
|
|
17
17
|
console.log(`\x1B[36;2m${message}\x1B[0m`);
|
|
@@ -30,7 +30,7 @@ const UNC_REGEX = /^[/\\]{2}/;
|
|
|
30
30
|
const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
31
31
|
const ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/;
|
|
32
32
|
const PATH_ROOT_RE = /^[/\\]|^[a-zA-Z]:[/\\]/;
|
|
33
|
-
const
|
|
33
|
+
export const IMPORT_EXPORT_REGEX = /(?:import\s+(?:type\s+)?[\s\S]*?\s+from\s+|import\s*\(\s*|export\s+(?:type\s+)?(?:\*\s+from\s+|(?:\w+(?:\s*,\s*|\s+))?\{[\s\S]*?\}(?:\s*,\s*\w+)?\s+from\s+|\w+\s+from\s+))\s*(['"])([^'"]+)\1/g;
|
|
34
34
|
function normalizeWindowsPath(input = "") {
|
|
35
35
|
if (!input) return input;
|
|
36
36
|
return input.replace(/\\/g, SLASH).replace(DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
@@ -464,26 +464,28 @@ function replaceAllInString(original, searchValue, replaceValue) {
|
|
|
464
464
|
}
|
|
465
465
|
return result;
|
|
466
466
|
}
|
|
467
|
-
async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, displayLogsOnlyFor) {
|
|
468
|
-
const shouldLog = !displayLogsOnlyFor || displayLogsOnlyFor.includes(filePath);
|
|
467
|
+
async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, displayLogsOnlyFor, displayLogs = false) {
|
|
468
|
+
const shouldLog = displayLogs && (!displayLogsOnlyFor || displayLogsOnlyFor.includes(filePath));
|
|
469
469
|
const log = (msg) => shouldLog && filteredLogger(msg);
|
|
470
470
|
const content = await fs.readFile(filePath, "utf-8");
|
|
471
471
|
let updated = content;
|
|
472
472
|
const changes = [];
|
|
473
|
-
const matches = Array.from(content.matchAll(
|
|
473
|
+
const matches = Array.from(content.matchAll(IMPORT_EXPORT_REGEX));
|
|
474
474
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
475
475
|
const baseAlias = aliasToReplace.replace("/*", "");
|
|
476
476
|
log(`[processFile] Processing file: ${filePath}`);
|
|
477
477
|
log(` - normalizedAlias: ${normalizedAlias}`);
|
|
478
478
|
log(` - baseAlias: ${baseAlias}`);
|
|
479
|
-
log(` - found ${matches.length} import statements`);
|
|
479
|
+
log(` - found ${matches.length} import/export statements`);
|
|
480
480
|
for (const match of matches) {
|
|
481
481
|
const originalQuote = match[1];
|
|
482
482
|
const importPath = match[2];
|
|
483
483
|
if (!importPath) continue;
|
|
484
484
|
log(` Processing import: ${importPath}`);
|
|
485
485
|
if (!importPath.startsWith(baseAlias)) {
|
|
486
|
-
|
|
486
|
+
if (DEBUG_MODE) {
|
|
487
|
+
log(` - skipping: import/export doesn't start with ${baseAlias}`);
|
|
488
|
+
}
|
|
487
489
|
continue;
|
|
488
490
|
}
|
|
489
491
|
const importExt = extname(importPath);
|
|
@@ -519,7 +521,9 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, d
|
|
|
519
521
|
await fs.writeFile(filePath, updated);
|
|
520
522
|
log(`\u2713 processed: ${filePath}`);
|
|
521
523
|
} else {
|
|
522
|
-
|
|
524
|
+
if (DEBUG_MODE) {
|
|
525
|
+
log(` - no changes made to file: ${filePath}`);
|
|
526
|
+
}
|
|
523
527
|
}
|
|
524
528
|
return changes;
|
|
525
529
|
}
|
|
@@ -575,7 +579,8 @@ async function convertImportsAliasToRelative({
|
|
|
575
579
|
targetDir,
|
|
576
580
|
aliasToReplace,
|
|
577
581
|
pathExtFilter,
|
|
578
|
-
displayLogsOnlyFor
|
|
582
|
+
displayLogsOnlyFor,
|
|
583
|
+
displayLogs = false
|
|
579
584
|
}) {
|
|
580
585
|
const normalizedAlias = aliasToReplace.endsWith("/*") ? aliasToReplace : `${aliasToReplace}/*`;
|
|
581
586
|
regularLogger(
|
|
@@ -592,7 +597,7 @@ async function convertImportsAliasToRelative({
|
|
|
592
597
|
pathExtFilter,
|
|
593
598
|
displayLogsOnlyFor
|
|
594
599
|
});
|
|
595
|
-
if (results.length > 0) {
|
|
600
|
+
if (results.length > 0 && displayLogs) {
|
|
596
601
|
regularLogger("\n[convertImportsAliasToRelative] Summary of changes:");
|
|
597
602
|
for (const { file, changes } of results) {
|
|
598
603
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
@@ -602,14 +607,15 @@ async function convertImportsAliasToRelative({
|
|
|
602
607
|
}
|
|
603
608
|
}
|
|
604
609
|
}
|
|
605
|
-
regularLogger("Import path conversion process complete.");
|
|
610
|
+
regularLogger("Import/export path conversion process complete.");
|
|
606
611
|
return results;
|
|
607
612
|
}
|
|
608
613
|
async function convertImportsExt({
|
|
609
614
|
targetDir,
|
|
610
615
|
extFrom,
|
|
611
616
|
extTo,
|
|
612
|
-
alias
|
|
617
|
+
alias,
|
|
618
|
+
displayLogs = false
|
|
613
619
|
}) {
|
|
614
620
|
const fromExtStr = extFrom === "none" ? "" : `.${extFrom}`;
|
|
615
621
|
const toExtStr = extTo === "none" ? "" : `.${extTo}`;
|
|
@@ -691,7 +697,7 @@ async function convertImportsExt({
|
|
|
691
697
|
}
|
|
692
698
|
})
|
|
693
699
|
);
|
|
694
|
-
if (results.length > 0) {
|
|
700
|
+
if (results.length > 0 && displayLogs) {
|
|
695
701
|
regularLogger("\n[convertImportsExt] Summary of changes:");
|
|
696
702
|
for (const { file, changes } of results) {
|
|
697
703
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
@@ -760,7 +766,8 @@ async function stripPathSegmentsInDirectory({
|
|
|
760
766
|
targetDir,
|
|
761
767
|
segmentsToStrip,
|
|
762
768
|
alias = "",
|
|
763
|
-
extensionsToProcess = EXTENSIONS
|
|
769
|
+
extensionsToProcess = EXTENSIONS,
|
|
770
|
+
displayLogs = false
|
|
764
771
|
}) {
|
|
765
772
|
regularLogger(
|
|
766
773
|
() => `[stripPathSegmentsInDirectory] Processing directory: ${targetDir}`
|
|
@@ -787,7 +794,7 @@ async function stripPathSegmentsInDirectory({
|
|
|
787
794
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
788
795
|
let updated = content;
|
|
789
796
|
const changes = [];
|
|
790
|
-
const matches = Array.from(content.matchAll(
|
|
797
|
+
const matches = Array.from(content.matchAll(IMPORT_EXPORT_REGEX));
|
|
791
798
|
for (const match of matches) {
|
|
792
799
|
const originalQuote = match[1];
|
|
793
800
|
const importPath = match[2];
|
|
@@ -822,7 +829,7 @@ async function stripPathSegmentsInDirectory({
|
|
|
822
829
|
}
|
|
823
830
|
})
|
|
824
831
|
);
|
|
825
|
-
if (results.length > 0) {
|
|
832
|
+
if (results.length > 0 && displayLogs) {
|
|
826
833
|
regularLogger(() => "[stripPathSegmentsInDirectory] Summary of changes:");
|
|
827
834
|
for (const { file, changes } of results) {
|
|
828
835
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
@@ -881,7 +888,8 @@ async function attachPathSegmentsInDirectory({
|
|
|
881
888
|
targetDir,
|
|
882
889
|
segments,
|
|
883
890
|
options = {},
|
|
884
|
-
extensionsToProcess = EXTENSIONS
|
|
891
|
+
extensionsToProcess = EXTENSIONS,
|
|
892
|
+
displayLogs = false
|
|
885
893
|
}) {
|
|
886
894
|
try {
|
|
887
895
|
const entries = await fs.readdir(targetDir, { withFileTypes: true });
|
|
@@ -902,7 +910,7 @@ async function attachPathSegmentsInDirectory({
|
|
|
902
910
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
903
911
|
let updated = content;
|
|
904
912
|
const changes = [];
|
|
905
|
-
const matches = Array.from(content.matchAll(
|
|
913
|
+
const matches = Array.from(content.matchAll(IMPORT_EXPORT_REGEX));
|
|
906
914
|
for (const match of matches) {
|
|
907
915
|
const originalQuote = match[1];
|
|
908
916
|
const importPath = match[2];
|
|
@@ -929,7 +937,7 @@ async function attachPathSegmentsInDirectory({
|
|
|
929
937
|
}
|
|
930
938
|
})
|
|
931
939
|
);
|
|
932
|
-
if (results.length > 0) {
|
|
940
|
+
if (results.length > 0 && displayLogs) {
|
|
933
941
|
regularLogger(
|
|
934
942
|
() => "\n[attachPathSegmentsInDirectory] Summary of changes:"
|
|
935
943
|
);
|