@reliverse/pathkit 2.2.13 → 2.3.1
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/dist/mod.d.ts +1 -3
- package/dist/mod.js +158 -87
- package/dist/utils/get-imports-exports.js +54 -30
- package/package.json +8 -8
package/dist/mod.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { posix } from "node:path";
|
|
2
2
|
type PlatformPath = typeof posix;
|
|
3
3
|
import { type GetFileImportsExportsOptions, getFileImportsExports, type ImportExportInfo, type ImportExportSpecifier } from "./utils/get-imports-exports.js";
|
|
4
|
-
declare const normalizedAliasSymbol: unique symbol;
|
|
5
4
|
export declare const IMPORT_EXPORT_REGEX: RegExp;
|
|
6
5
|
interface NormalizedRecord extends Record<string, string> {
|
|
7
|
-
[normalizedAliasSymbol]?: true;
|
|
8
6
|
}
|
|
9
7
|
export interface ParsedPath {
|
|
10
8
|
root: string;
|
|
@@ -211,7 +209,7 @@ declare const path: PlatformPath & typeof _pathBase & {
|
|
|
211
209
|
posix: PlatformPath;
|
|
212
210
|
win32: PlatformPath;
|
|
213
211
|
};
|
|
214
|
-
declare const win32:
|
|
212
|
+
declare const win32: any;
|
|
215
213
|
declare const delimiter: ";" | ":";
|
|
216
214
|
export type { PlatformPath, PathExtFilter, ImportExtType, ImportExportInfo, ImportExportSpecifier, GetFileImportsExportsOptions, };
|
|
217
215
|
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, };
|
package/dist/mod.js
CHANGED
|
@@ -32,7 +32,9 @@ const ROOT_FOLDER_RE = /^\/([A-Za-z]:)?$/;
|
|
|
32
32
|
const PATH_ROOT_RE = /^[/\\]|^[a-zA-Z]:[/\\]/;
|
|
33
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
|
-
if (!input)
|
|
35
|
+
if (!input) {
|
|
36
|
+
return input;
|
|
37
|
+
}
|
|
36
38
|
return input.replace(/\\/g, SLASH).replace(DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
37
39
|
}
|
|
38
40
|
function compareAliases(a, b) {
|
|
@@ -61,7 +63,7 @@ function normalizeString(path2, allowAboveRoot) {
|
|
|
61
63
|
if (char === SLASH) {
|
|
62
64
|
if (lastSlash === index - 1 || dots === 1) {
|
|
63
65
|
} else if (dots === 2) {
|
|
64
|
-
if (res.length < 2 || lastSegmentLength !== 2 || !res.endsWith(DOT) || res
|
|
66
|
+
if (res.length < 2 || lastSegmentLength !== 2 || !res.endsWith(DOT) || res.at(-2) !== DOT) {
|
|
65
67
|
if (res.length > 2) {
|
|
66
68
|
const lastSlashIndex = res.lastIndexOf(SLASH);
|
|
67
69
|
if (lastSlashIndex === -1) {
|
|
@@ -108,14 +110,18 @@ function normalizeString(path2, allowAboveRoot) {
|
|
|
108
110
|
}
|
|
109
111
|
const sep = SLASH;
|
|
110
112
|
const normalize = (path2) => {
|
|
111
|
-
if (path2.length === 0)
|
|
113
|
+
if (path2.length === 0) {
|
|
114
|
+
return DOT;
|
|
115
|
+
}
|
|
112
116
|
const originalPath = path2;
|
|
113
117
|
path2 = normalizeWindowsPath(path2);
|
|
114
118
|
const isPathAbsolute = IS_ABSOLUTE_RE.test(path2);
|
|
115
119
|
const trailingSeparator = path2.endsWith(SLASH);
|
|
116
120
|
path2 = normalizeString(path2, !isPathAbsolute);
|
|
117
121
|
if (path2.length === 0) {
|
|
118
|
-
if (isPathAbsolute)
|
|
122
|
+
if (isPathAbsolute) {
|
|
123
|
+
return SLASH;
|
|
124
|
+
}
|
|
119
125
|
return trailingSeparator && originalPath.length > 0 && originalPath !== DOT ? "./" : DOT;
|
|
120
126
|
}
|
|
121
127
|
if (trailingSeparator && !path2.endsWith(SLASH)) {
|
|
@@ -139,7 +145,9 @@ const normalize = (path2) => {
|
|
|
139
145
|
return path2;
|
|
140
146
|
};
|
|
141
147
|
const join = (...segments) => {
|
|
142
|
-
if (segments.length === 0)
|
|
148
|
+
if (segments.length === 0) {
|
|
149
|
+
return DOT;
|
|
150
|
+
}
|
|
143
151
|
let joined = EMPTY;
|
|
144
152
|
for (const segment of segments) {
|
|
145
153
|
if (typeof segment !== "string") {
|
|
@@ -153,7 +161,9 @@ const join = (...segments) => {
|
|
|
153
161
|
}
|
|
154
162
|
}
|
|
155
163
|
}
|
|
156
|
-
if (joined.length === 0)
|
|
164
|
+
if (joined.length === 0) {
|
|
165
|
+
return DOT;
|
|
166
|
+
}
|
|
157
167
|
return normalize(joined);
|
|
158
168
|
};
|
|
159
169
|
const resolve = (...args) => {
|
|
@@ -164,7 +174,9 @@ const resolve = (...args) => {
|
|
|
164
174
|
if (typeof path2 !== "string") {
|
|
165
175
|
throw new TypeError("Arguments to path.resolve must be strings");
|
|
166
176
|
}
|
|
167
|
-
if (path2.length === 0)
|
|
177
|
+
if (path2.length === 0) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
168
180
|
const normalizedSegment = normalizeWindowsPath(path2);
|
|
169
181
|
resolvedPath = `${normalizedSegment}${SLASH}${resolvedPath}`;
|
|
170
182
|
resolvedAbsolute = IS_ABSOLUTE_RE.test(normalizedSegment);
|
|
@@ -179,28 +191,42 @@ const resolve = (...args) => {
|
|
|
179
191
|
return resolvedPath.length > 0 ? resolvedPath : DOT;
|
|
180
192
|
};
|
|
181
193
|
const isAbsolute = (p) => {
|
|
182
|
-
if (typeof p !== "string")
|
|
194
|
+
if (typeof p !== "string") {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
183
197
|
return IS_ABSOLUTE_RE.test(normalizeWindowsPath(p));
|
|
184
198
|
};
|
|
185
199
|
const toNamespacedPath = (p) => {
|
|
186
|
-
if (typeof p !== "string" || p.length === 0)
|
|
200
|
+
if (typeof p !== "string" || p.length === 0) {
|
|
201
|
+
return p;
|
|
202
|
+
}
|
|
187
203
|
return normalize(p);
|
|
188
204
|
};
|
|
189
205
|
const extname = (p) => {
|
|
190
|
-
if (typeof p !== "string")
|
|
206
|
+
if (typeof p !== "string") {
|
|
207
|
+
return EMPTY;
|
|
208
|
+
}
|
|
191
209
|
const path2 = normalizeWindowsPath(p);
|
|
192
210
|
let lastSlashIdx = path2.lastIndexOf(SLASH);
|
|
193
|
-
if (lastSlashIdx === -1)
|
|
211
|
+
if (lastSlashIdx === -1) {
|
|
212
|
+
lastSlashIdx = 0;
|
|
213
|
+
}
|
|
194
214
|
const basePart = lastSlashIdx === 0 ? path2 : path2.substring(lastSlashIdx + 1);
|
|
195
|
-
if (basePart === DOT || basePart === DOUBLE_DOT)
|
|
215
|
+
if (basePart === DOT || basePart === DOUBLE_DOT) {
|
|
216
|
+
return EMPTY;
|
|
217
|
+
}
|
|
196
218
|
const lastDotIdx = basePart.lastIndexOf(DOT);
|
|
197
|
-
if (lastDotIdx <= 0)
|
|
219
|
+
if (lastDotIdx <= 0) {
|
|
220
|
+
return EMPTY;
|
|
221
|
+
}
|
|
198
222
|
return basePart.substring(lastDotIdx);
|
|
199
223
|
};
|
|
200
224
|
const relative = (from, to) => {
|
|
201
225
|
const resolvedFrom = resolve(from);
|
|
202
226
|
const resolvedTo = resolve(to);
|
|
203
|
-
if (resolvedFrom === resolvedTo)
|
|
227
|
+
if (resolvedFrom === resolvedTo) {
|
|
228
|
+
return EMPTY;
|
|
229
|
+
}
|
|
204
230
|
const fromSegments = resolvedFrom.replace(ROOT_FOLDER_RE, "$1").split(SLASH).filter(Boolean);
|
|
205
231
|
const toSegments = resolvedTo.replace(ROOT_FOLDER_RE, "$1").split(SLASH).filter(Boolean);
|
|
206
232
|
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()) {
|
|
@@ -211,21 +237,23 @@ const relative = (from, to) => {
|
|
|
211
237
|
while (commonSegments < maxCommon && fromSegments[commonSegments] === toSegments[commonSegments]) {
|
|
212
238
|
commonSegments++;
|
|
213
239
|
}
|
|
214
|
-
const upSegments = Array(fromSegments.length - commonSegments).fill(
|
|
215
|
-
DOUBLE_DOT
|
|
216
|
-
);
|
|
240
|
+
const upSegments = new Array(fromSegments.length - commonSegments).fill(DOUBLE_DOT);
|
|
217
241
|
const downSegments = toSegments.slice(commonSegments);
|
|
218
242
|
const result = [...upSegments, ...downSegments].join(SLASH);
|
|
219
243
|
return result.length > 0 ? result : DOT;
|
|
220
244
|
};
|
|
221
245
|
const dirname = (p) => {
|
|
222
|
-
if (typeof p !== "string" || p.length === 0)
|
|
246
|
+
if (typeof p !== "string" || p.length === 0) {
|
|
247
|
+
return DOT;
|
|
248
|
+
}
|
|
223
249
|
const normalizedPath = normalizeWindowsPath(p);
|
|
224
250
|
const lastSlash = normalizedPath.lastIndexOf(SLASH);
|
|
225
251
|
if (lastSlash === -1) {
|
|
226
252
|
return IS_ABSOLUTE_RE.test(normalizedPath) ? SLASH : DOT;
|
|
227
253
|
}
|
|
228
|
-
if (lastSlash === 0)
|
|
254
|
+
if (lastSlash === 0) {
|
|
255
|
+
return SLASH;
|
|
256
|
+
}
|
|
229
257
|
const dir = normalizedPath.slice(0, lastSlash);
|
|
230
258
|
if (DRIVE_LETTER_RE.test(dir) && dir.length === 2) {
|
|
231
259
|
return dir + SLASH;
|
|
@@ -234,26 +262,33 @@ const dirname = (p) => {
|
|
|
234
262
|
};
|
|
235
263
|
const format = (p) => {
|
|
236
264
|
if (typeof p !== "object" || p === null) {
|
|
237
|
-
throw new TypeError(
|
|
238
|
-
'Parameter "pathObject" must be an object, not null or other type.'
|
|
239
|
-
);
|
|
265
|
+
throw new TypeError('Parameter "pathObject" must be an object, not null or other type.');
|
|
240
266
|
}
|
|
241
267
|
const dir = p.dir || p.root || "";
|
|
242
268
|
const base = p.base || `${p.name || ""}${p.ext || ""}`;
|
|
243
|
-
if (!dir)
|
|
244
|
-
|
|
269
|
+
if (!dir) {
|
|
270
|
+
return base;
|
|
271
|
+
}
|
|
272
|
+
if (dir === p.root) {
|
|
273
|
+
return `${dir}${base}`;
|
|
274
|
+
}
|
|
245
275
|
return normalize(`${dir}${SLASH}${base}`);
|
|
246
276
|
};
|
|
247
277
|
const basename = (p, ext) => {
|
|
248
|
-
if (typeof p !== "string")
|
|
249
|
-
|
|
278
|
+
if (typeof p !== "string") {
|
|
279
|
+
throw new TypeError("Path must be a string.");
|
|
280
|
+
}
|
|
281
|
+
if (ext !== void 0 && typeof ext !== "string") {
|
|
250
282
|
throw new TypeError("[basename] `ext` must be a string.");
|
|
283
|
+
}
|
|
251
284
|
const normalizedPath = normalizeWindowsPath(p);
|
|
252
285
|
let end = normalizedPath.length;
|
|
253
286
|
while (end > 0 && normalizedPath[end - 1] === SLASH) {
|
|
254
287
|
end--;
|
|
255
288
|
}
|
|
256
|
-
if (end === 0)
|
|
289
|
+
if (end === 0) {
|
|
290
|
+
return EMPTY;
|
|
291
|
+
}
|
|
257
292
|
let start = normalizedPath.lastIndexOf(SLASH, end - 1);
|
|
258
293
|
start = start === -1 ? 0 : start + 1;
|
|
259
294
|
let filename2 = normalizedPath.slice(start, end);
|
|
@@ -263,7 +298,9 @@ const basename = (p, ext) => {
|
|
|
263
298
|
return filename2;
|
|
264
299
|
};
|
|
265
300
|
const parse = (p) => {
|
|
266
|
-
if (typeof p !== "string")
|
|
301
|
+
if (typeof p !== "string") {
|
|
302
|
+
throw new TypeError("Path must be a string.");
|
|
303
|
+
}
|
|
267
304
|
const normalizedPath = normalizeWindowsPath(p);
|
|
268
305
|
const B = basename(normalizedPath);
|
|
269
306
|
const E = extname(B);
|
|
@@ -275,11 +312,9 @@ const parse = (p) => {
|
|
|
275
312
|
R = rootMatch[0];
|
|
276
313
|
if (DRIVE_LETTER_RE.test(R) && R.length === 2 && normalizedPath.length > 2 && normalizedPath[2] === SLASH) {
|
|
277
314
|
R += SLASH;
|
|
278
|
-
} else if (R === SLASH && D.startsWith("//")) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
R = uncParts.join(SLASH) || R;
|
|
282
|
-
}
|
|
315
|
+
} else if (R === SLASH && D.startsWith("//") && UNC_REGEX.exec(D)) {
|
|
316
|
+
const uncParts = D.split(SLASH).slice(0, 3);
|
|
317
|
+
R = uncParts.join(SLASH) || R;
|
|
283
318
|
}
|
|
284
319
|
}
|
|
285
320
|
const resolvedDir = D === DOT && R !== EMPTY && R !== DOT ? R : D;
|
|
@@ -293,7 +328,9 @@ const parse = (p) => {
|
|
|
293
328
|
};
|
|
294
329
|
function filename(pathString) {
|
|
295
330
|
const base = basename(pathString);
|
|
296
|
-
if (!base)
|
|
331
|
+
if (!base) {
|
|
332
|
+
return void 0;
|
|
333
|
+
}
|
|
297
334
|
const separatorIndex = base.lastIndexOf(DOT);
|
|
298
335
|
return separatorIndex <= 0 ? base : base.slice(0, separatorIndex);
|
|
299
336
|
}
|
|
@@ -307,8 +344,9 @@ function normalizeAliases(aliases) {
|
|
|
307
344
|
const sortedAliases = Object.fromEntries(sortedAliasesEntries);
|
|
308
345
|
for (const key in sortedAliases) {
|
|
309
346
|
for (const aliasPrefix in sortedAliases) {
|
|
310
|
-
if (aliasPrefix === key || key.startsWith(aliasPrefix + SLASH) || key === aliasPrefix)
|
|
347
|
+
if (aliasPrefix === key || key.startsWith(aliasPrefix + SLASH) || key === aliasPrefix) {
|
|
311
348
|
continue;
|
|
349
|
+
}
|
|
312
350
|
const value = sortedAliases[key];
|
|
313
351
|
if (value?.startsWith(aliasPrefix)) {
|
|
314
352
|
const nextChar = value[aliasPrefix.length];
|
|
@@ -480,7 +518,9 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, d
|
|
|
480
518
|
for (const match of matches) {
|
|
481
519
|
const originalQuote = match[1];
|
|
482
520
|
const importPath = match[2];
|
|
483
|
-
if (!importPath)
|
|
521
|
+
if (!importPath) {
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
484
524
|
log(` Processing import: ${importPath}`);
|
|
485
525
|
if (!importPath.startsWith(baseAlias)) {
|
|
486
526
|
if (DEBUG_MODE) {
|
|
@@ -497,7 +537,7 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, d
|
|
|
497
537
|
log(` - skipping: doesn't match pathExtFilter ${pathExtFilter}`);
|
|
498
538
|
continue;
|
|
499
539
|
}
|
|
500
|
-
log(
|
|
540
|
+
log(" - converting alias to relative path...");
|
|
501
541
|
const relPath = await convertStringAliasRelative({
|
|
502
542
|
importPath,
|
|
503
543
|
importerFile: filePath,
|
|
@@ -514,16 +554,14 @@ async function processFile(filePath, aliasToReplace, targetDir, pathExtFilter, d
|
|
|
514
554
|
updated = replaceAllInString(updated, searchString, replacementString);
|
|
515
555
|
log(` - applied change: ${importPath} \u2192 ${finalPath}`);
|
|
516
556
|
} else {
|
|
517
|
-
log(
|
|
557
|
+
log(" - no change needed");
|
|
518
558
|
}
|
|
519
559
|
}
|
|
520
560
|
if (content !== updated) {
|
|
521
561
|
await fs.writeFile(filePath, updated);
|
|
522
562
|
log(`\u2713 processed: ${filePath}`);
|
|
523
|
-
} else {
|
|
524
|
-
|
|
525
|
-
log(` - no changes made to file: ${filePath}`);
|
|
526
|
-
}
|
|
563
|
+
} else if (DEBUG_MODE) {
|
|
564
|
+
log(` - no changes made to file: ${filePath}`);
|
|
527
565
|
}
|
|
528
566
|
return changes;
|
|
529
567
|
}
|
|
@@ -542,7 +580,9 @@ async function processAllFiles({
|
|
|
542
580
|
entries.map(async (entry) => {
|
|
543
581
|
const fullPath = join(srcDir, entry.name);
|
|
544
582
|
if (entry.isDirectory()) {
|
|
545
|
-
if (entry.name === "node_modules")
|
|
583
|
+
if (entry.name === "node_modules") {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
546
586
|
const subdirResults = await processAllFiles({
|
|
547
587
|
srcDir: fullPath,
|
|
548
588
|
aliasToReplace,
|
|
@@ -619,21 +659,31 @@ async function convertImportsExt({
|
|
|
619
659
|
"gm"
|
|
620
660
|
);
|
|
621
661
|
function shouldSkipPath(path2) {
|
|
622
|
-
if (path2.startsWith("node:") || path2.startsWith("bun:") || path2.startsWith("npm:") || path2.startsWith("jsr:") || path2.startsWith("node_modules"))
|
|
662
|
+
if (path2.startsWith("node:") || path2.startsWith("bun:") || path2.startsWith("npm:") || path2.startsWith("jsr:") || path2.startsWith("node_modules")) {
|
|
623
663
|
return true;
|
|
664
|
+
}
|
|
624
665
|
if (aliasPrefix && path2.startsWith(aliasPrefix)) {
|
|
625
666
|
return false;
|
|
626
667
|
}
|
|
627
668
|
const segments = path2.split("/");
|
|
628
669
|
const firstSegment = segments[0];
|
|
629
|
-
if (!firstSegment)
|
|
670
|
+
if (!firstSegment) {
|
|
671
|
+
return false;
|
|
672
|
+
}
|
|
630
673
|
if (path2.startsWith("@") || // scoped packages
|
|
631
674
|
!firstSegment.includes(".") || // simple package names
|
|
632
|
-
firstSegment.includes("-") && !firstSegment.startsWith("."))
|
|
675
|
+
firstSegment.includes("-") && !firstSegment.startsWith(".")) {
|
|
633
676
|
return true;
|
|
634
|
-
|
|
635
|
-
if (
|
|
636
|
-
|
|
677
|
+
}
|
|
678
|
+
if (path2.startsWith("http://") || path2.startsWith("https://")) {
|
|
679
|
+
return true;
|
|
680
|
+
}
|
|
681
|
+
if (extTo !== "none" && path2.endsWith(`.${extTo}`)) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
if (extFrom === "none" && /\.[^/]+$/.test(path2)) {
|
|
685
|
+
return true;
|
|
686
|
+
}
|
|
637
687
|
return false;
|
|
638
688
|
}
|
|
639
689
|
try {
|
|
@@ -643,7 +693,9 @@ async function convertImportsExt({
|
|
|
643
693
|
entries.map(async (entry) => {
|
|
644
694
|
const fullPath = join(targetDir, entry.name);
|
|
645
695
|
if (entry.isDirectory()) {
|
|
646
|
-
if (entry.name === "node_modules")
|
|
696
|
+
if (entry.name === "node_modules") {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
647
699
|
const subdirResults = await convertImportsExt({
|
|
648
700
|
targetDir: fullPath,
|
|
649
701
|
extFrom,
|
|
@@ -660,7 +712,9 @@ async function convertImportsExt({
|
|
|
660
712
|
while (match !== null) {
|
|
661
713
|
const quote = match[1];
|
|
662
714
|
const importPath = match[2];
|
|
663
|
-
if (!importPath)
|
|
715
|
+
if (!importPath) {
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
664
718
|
if (shouldSkipPath(importPath)) {
|
|
665
719
|
match = importRegex.exec(updated);
|
|
666
720
|
continue;
|
|
@@ -673,7 +727,9 @@ async function convertImportsExt({
|
|
|
673
727
|
} else {
|
|
674
728
|
replacementPath = importPath.slice(0, -fromExtStr.length) + toExtStr;
|
|
675
729
|
}
|
|
676
|
-
if (importPath === replacementPath)
|
|
730
|
+
if (importPath === replacementPath) {
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
677
733
|
changes.push({ from: importPath, to: replacementPath });
|
|
678
734
|
const searchStr = `${quote}${importPath}${quote}`;
|
|
679
735
|
const replaceStr = `${quote}${replacementPath}${quote}`;
|
|
@@ -709,8 +765,12 @@ async function convertImportsExt({
|
|
|
709
765
|
}
|
|
710
766
|
}
|
|
711
767
|
function stripPathSegments(path2, count = 1, alias = "") {
|
|
712
|
-
if (typeof path2 !== "string" || path2.length === 0)
|
|
713
|
-
|
|
768
|
+
if (typeof path2 !== "string" || path2.length === 0) {
|
|
769
|
+
return path2;
|
|
770
|
+
}
|
|
771
|
+
if (count <= 0) {
|
|
772
|
+
return path2;
|
|
773
|
+
}
|
|
714
774
|
const normalizedPath = normalizeWindowsPath(path2);
|
|
715
775
|
const parsed = parse(normalizedPath);
|
|
716
776
|
let pathSegments = [];
|
|
@@ -741,10 +801,7 @@ function stripPathSegments(path2, count = 1, alias = "") {
|
|
|
741
801
|
const numToStrip = Math.min(count, pathSegments.length);
|
|
742
802
|
const remainingBodySegments = pathSegments.slice(numToStrip);
|
|
743
803
|
const pathRoot = parsed.root;
|
|
744
|
-
const effectiveSegments = [
|
|
745
|
-
...leadingPreservedSegments,
|
|
746
|
-
...remainingBodySegments
|
|
747
|
-
];
|
|
804
|
+
const effectiveSegments = [...leadingPreservedSegments, ...remainingBodySegments];
|
|
748
805
|
let result;
|
|
749
806
|
if (effectiveSegments.length === 0) {
|
|
750
807
|
result = normalize(pathRoot || DOT);
|
|
@@ -769,7 +826,9 @@ async function stripPathSegmentsInDirectory({
|
|
|
769
826
|
entries.map(async (entry) => {
|
|
770
827
|
const fullPath = join(targetDir, entry.name);
|
|
771
828
|
if (entry.isDirectory()) {
|
|
772
|
-
if (entry.name === "node_modules")
|
|
829
|
+
if (entry.name === "node_modules") {
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
773
832
|
const subdirResults = await stripPathSegmentsInDirectory({
|
|
774
833
|
targetDir: fullPath,
|
|
775
834
|
segmentsToStrip,
|
|
@@ -785,18 +844,16 @@ async function stripPathSegmentsInDirectory({
|
|
|
785
844
|
for (const match of matches) {
|
|
786
845
|
const originalQuote = match[1];
|
|
787
846
|
const importPath = match[2];
|
|
788
|
-
if (!importPath)
|
|
847
|
+
if (!importPath) {
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
789
850
|
if (!importPath.includes(SLASH)) {
|
|
790
851
|
continue;
|
|
791
852
|
}
|
|
792
853
|
if (alias && !importPath.startsWith(alias.replace("/*", ""))) {
|
|
793
854
|
continue;
|
|
794
855
|
}
|
|
795
|
-
const strippedPath = stripPathSegments(
|
|
796
|
-
importPath,
|
|
797
|
-
segmentsToStrip,
|
|
798
|
-
alias
|
|
799
|
-
);
|
|
856
|
+
const strippedPath = stripPathSegments(importPath, segmentsToStrip, alias);
|
|
800
857
|
if (importPath === strippedPath) {
|
|
801
858
|
continue;
|
|
802
859
|
}
|
|
@@ -836,7 +893,9 @@ async function stripPathSegmentsInDirectory({
|
|
|
836
893
|
}
|
|
837
894
|
}
|
|
838
895
|
function attachPathSegments(path2, segments, options = {}) {
|
|
839
|
-
if (typeof path2 !== "string" || path2.length === 0)
|
|
896
|
+
if (typeof path2 !== "string" || path2.length === 0) {
|
|
897
|
+
return path2;
|
|
898
|
+
}
|
|
840
899
|
const {
|
|
841
900
|
position = "after",
|
|
842
901
|
normalize: shouldNormalize = true,
|
|
@@ -846,14 +905,14 @@ function attachPathSegments(path2, segments, options = {}) {
|
|
|
846
905
|
} = options;
|
|
847
906
|
const segmentsArray = Array.isArray(segments) ? segments : [segments];
|
|
848
907
|
const validSegments = segmentsArray.filter((s) => s.length > 0);
|
|
849
|
-
if (validSegments.length === 0)
|
|
908
|
+
if (validSegments.length === 0) {
|
|
909
|
+
return path2;
|
|
910
|
+
}
|
|
850
911
|
const basePath = shouldNormalize ? normalizeWindowsPath(path2) : path2;
|
|
851
912
|
let alias = "";
|
|
852
913
|
let pathWithoutAlias = basePath;
|
|
853
914
|
if (preserveAlias && position === "before") {
|
|
854
|
-
const aliasMatch = new RegExp(`^${preserveAlias.replace("*", ".*")}`).exec(
|
|
855
|
-
basePath
|
|
856
|
-
);
|
|
915
|
+
const aliasMatch = new RegExp(`^${preserveAlias.replace("*", ".*")}`).exec(basePath);
|
|
857
916
|
if (aliasMatch) {
|
|
858
917
|
alias = aliasMatch[0];
|
|
859
918
|
pathWithoutAlias = basePath.slice(alias.length);
|
|
@@ -885,7 +944,9 @@ async function attachPathSegmentsInDirectory({
|
|
|
885
944
|
entries.map(async (entry) => {
|
|
886
945
|
const fullPath = join(targetDir, entry.name);
|
|
887
946
|
if (entry.isDirectory()) {
|
|
888
|
-
if (entry.name === "node_modules")
|
|
947
|
+
if (entry.name === "node_modules") {
|
|
948
|
+
return;
|
|
949
|
+
}
|
|
889
950
|
const subdirResults = await attachPathSegmentsInDirectory({
|
|
890
951
|
targetDir: fullPath,
|
|
891
952
|
segments,
|
|
@@ -901,14 +962,16 @@ async function attachPathSegmentsInDirectory({
|
|
|
901
962
|
for (const match of matches) {
|
|
902
963
|
const originalQuote = match[1];
|
|
903
964
|
const importPath = match[2];
|
|
904
|
-
if (!importPath)
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
);
|
|
911
|
-
if (importPath === modifiedPath)
|
|
965
|
+
if (!importPath) {
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
if (!importPath.includes(SLASH)) {
|
|
969
|
+
continue;
|
|
970
|
+
}
|
|
971
|
+
const modifiedPath = attachPathSegments(importPath, segments, options);
|
|
972
|
+
if (importPath === modifiedPath) {
|
|
973
|
+
continue;
|
|
974
|
+
}
|
|
912
975
|
changes.push({ from: importPath, to: modifiedPath });
|
|
913
976
|
const searchStr = `${originalQuote}${importPath}${originalQuote}`;
|
|
914
977
|
const replaceStr = `${originalQuote}${modifiedPath}${originalQuote}`;
|
|
@@ -925,9 +988,7 @@ async function attachPathSegmentsInDirectory({
|
|
|
925
988
|
})
|
|
926
989
|
);
|
|
927
990
|
if (results.length > 0 && displayLogs) {
|
|
928
|
-
regularLogger(
|
|
929
|
-
() => "\n[attachPathSegmentsInDirectory] Summary of changes:"
|
|
930
|
-
);
|
|
991
|
+
regularLogger(() => "\n[attachPathSegmentsInDirectory] Summary of changes:");
|
|
931
992
|
for (const { file, changes } of results) {
|
|
932
993
|
const displayPath = relative(targetDir, file) || basename(file);
|
|
933
994
|
regularLogger(() => ` in ${displayPath}:`);
|
|
@@ -966,11 +1027,15 @@ const _platforms = {
|
|
|
966
1027
|
sep: BACK_SLASH,
|
|
967
1028
|
delimiter: ";",
|
|
968
1029
|
isAbsolute: (p) => {
|
|
969
|
-
if (typeof p !== "string")
|
|
1030
|
+
if (typeof p !== "string") {
|
|
1031
|
+
return false;
|
|
1032
|
+
}
|
|
970
1033
|
return /^[a-zA-Z]:[/\\]/.test(p) || /^[/\\]{2}/.test(p);
|
|
971
1034
|
},
|
|
972
1035
|
toNamespacedPath: (p) => {
|
|
973
|
-
if (typeof p !== "string" || p.length === 0)
|
|
1036
|
+
if (typeof p !== "string" || p.length === 0) {
|
|
1037
|
+
return p;
|
|
1038
|
+
}
|
|
974
1039
|
const resolved = resolve(p);
|
|
975
1040
|
if (/^[a-zA-Z]:/.test(resolved)) {
|
|
976
1041
|
return `\\\\?\\${resolved.replace(/\//g, BACK_SLASH)}`;
|
|
@@ -987,9 +1052,15 @@ const mix = (platformDefault = "currentSystem") => {
|
|
|
987
1052
|
const defaultPathObject = actualDefault === "win32" ? _platforms.win32 : _platforms.posix;
|
|
988
1053
|
return new Proxy(defaultPathObject, {
|
|
989
1054
|
get(target, prop) {
|
|
990
|
-
if (prop === "delimiter")
|
|
991
|
-
|
|
992
|
-
|
|
1055
|
+
if (prop === "delimiter") {
|
|
1056
|
+
return actualDefault === "win32" ? ";" : ":";
|
|
1057
|
+
}
|
|
1058
|
+
if (prop === "posix") {
|
|
1059
|
+
return _platforms.posix;
|
|
1060
|
+
}
|
|
1061
|
+
if (prop === "win32") {
|
|
1062
|
+
return _platforms.win32;
|
|
1063
|
+
}
|
|
993
1064
|
if (prop in target) {
|
|
994
1065
|
return target[prop];
|
|
995
1066
|
}
|
|
@@ -6,7 +6,9 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
6
6
|
} = options;
|
|
7
7
|
const results = [];
|
|
8
8
|
function getImportExtension(path) {
|
|
9
|
-
if (!path)
|
|
9
|
+
if (!path) {
|
|
10
|
+
return "";
|
|
11
|
+
}
|
|
10
12
|
const lastDotIndex = path.lastIndexOf(".");
|
|
11
13
|
if (lastDotIndex <= 0 || lastDotIndex === path.length - 1) {
|
|
12
14
|
return "";
|
|
@@ -25,7 +27,9 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
25
27
|
return getImportExtension(path);
|
|
26
28
|
}
|
|
27
29
|
const pathExt = getImportExtension(path);
|
|
28
|
-
if (!pathExt)
|
|
30
|
+
if (!pathExt) {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
29
33
|
if (pathExt === ".js" || pathExt === ".jsx") {
|
|
30
34
|
return pathExt === ".jsx" ? ".tsx" : ".ts";
|
|
31
35
|
}
|
|
@@ -47,11 +51,18 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
47
51
|
namedExport: /export\s+(?:const|let|var|function|class|enum|interface|type)\s+(\w+)/g
|
|
48
52
|
};
|
|
49
53
|
function getPathType(path) {
|
|
50
|
-
if (path.startsWith("."))
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (path.startsWith("
|
|
54
|
+
if (path.startsWith(".")) {
|
|
55
|
+
return "relative";
|
|
56
|
+
}
|
|
57
|
+
if (path.startsWith("/")) {
|
|
58
|
+
return "absolute";
|
|
59
|
+
}
|
|
60
|
+
if (path.startsWith("@") || path.startsWith("~")) {
|
|
61
|
+
return "alias";
|
|
62
|
+
}
|
|
63
|
+
if (path.startsWith("http://") || path.startsWith("https://")) {
|
|
54
64
|
return "module";
|
|
65
|
+
}
|
|
55
66
|
return "bare";
|
|
56
67
|
}
|
|
57
68
|
function extractSpecifiers(statement) {
|
|
@@ -66,9 +77,7 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
66
77
|
});
|
|
67
78
|
return specifiers;
|
|
68
79
|
}
|
|
69
|
-
const defaultMatch = /import\s+(?:type\s+)?(\w+)(?:\s*,|\s+from)/.exec(
|
|
70
|
-
statement
|
|
71
|
-
);
|
|
80
|
+
const defaultMatch = /import\s+(?:type\s+)?(\w+)(?:\s*,|\s+from)/.exec(statement);
|
|
72
81
|
if (defaultMatch?.[1] && !statement.includes("{")) {
|
|
73
82
|
specifiers.push({
|
|
74
83
|
type: "default",
|
|
@@ -82,11 +91,15 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
82
91
|
for (const item of items) {
|
|
83
92
|
const typeMatch = /^type\s+(.+)/.exec(item);
|
|
84
93
|
const actualItem = typeMatch ? typeMatch[1] : item;
|
|
85
|
-
if (!actualItem)
|
|
94
|
+
if (!actualItem) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
86
97
|
const isItemType = !!typeMatch || isTypeOnly;
|
|
87
98
|
if (actualItem.includes(" as ")) {
|
|
88
99
|
const [name, alias] = actualItem.split(" as ").map((p) => p.trim());
|
|
89
|
-
if (!name)
|
|
100
|
+
if (!name) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
90
103
|
specifiers.push({
|
|
91
104
|
type: "named",
|
|
92
105
|
name,
|
|
@@ -108,9 +121,13 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
108
121
|
const importMatches = [...content.matchAll(patterns.staticImport)];
|
|
109
122
|
for (const match of importMatches) {
|
|
110
123
|
const source = match[1] || match[3];
|
|
111
|
-
if (!source)
|
|
124
|
+
if (!source) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
112
127
|
const pathType = getPathType(source);
|
|
113
|
-
if (!pathType
|
|
128
|
+
if (!(pathType && pathTypes.includes(pathType))) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
114
131
|
const info = {
|
|
115
132
|
statement: match[0],
|
|
116
133
|
type: "static",
|
|
@@ -130,9 +147,13 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
130
147
|
const dynamicMatches = [...content.matchAll(patterns.dynamicImport)];
|
|
131
148
|
for (const match of dynamicMatches) {
|
|
132
149
|
const source = match[1];
|
|
133
|
-
if (!source)
|
|
150
|
+
if (!source) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
134
153
|
const pathType = getPathType(source);
|
|
135
|
-
if (!pathType
|
|
154
|
+
if (!(pathType && pathTypes.includes(pathType))) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
136
157
|
const info = {
|
|
137
158
|
statement: match[0],
|
|
138
159
|
type: "dynamic",
|
|
@@ -154,9 +175,13 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
154
175
|
const exportMatches = [...content.matchAll(patterns.staticExport)];
|
|
155
176
|
for (const match of exportMatches) {
|
|
156
177
|
const source = match[1];
|
|
157
|
-
if (!source)
|
|
178
|
+
if (!source) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
158
181
|
const pathType = getPathType(source);
|
|
159
|
-
if (!pathType
|
|
182
|
+
if (!(pathType && pathTypes.includes(pathType))) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
160
185
|
const info = {
|
|
161
186
|
statement: match[0],
|
|
162
187
|
type: "static",
|
|
@@ -194,7 +219,9 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
194
219
|
const namedMatches = [...content.matchAll(patterns.namedExport)];
|
|
195
220
|
for (const match of namedMatches) {
|
|
196
221
|
const name = match[1];
|
|
197
|
-
if (!name)
|
|
222
|
+
if (!name) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
198
225
|
const info = {
|
|
199
226
|
statement: match[0],
|
|
200
227
|
type: "static",
|
|
@@ -213,18 +240,15 @@ export function getFileImportsExports(content, options = {}) {
|
|
|
213
240
|
}
|
|
214
241
|
}
|
|
215
242
|
if (limitPerType) {
|
|
216
|
-
const groupedByType = results.reduce(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
);
|
|
225
|
-
return Object.values(groupedByType).flatMap(
|
|
226
|
-
(group) => group.slice(0, limitPerType)
|
|
227
|
-
);
|
|
243
|
+
const groupedByType = results.reduce((acc, curr) => {
|
|
244
|
+
const key = `${curr.kind}-${curr.pathType || "none"}`;
|
|
245
|
+
if (!acc[key]) {
|
|
246
|
+
acc[key] = [];
|
|
247
|
+
}
|
|
248
|
+
acc[key].push(curr);
|
|
249
|
+
return acc;
|
|
250
|
+
}, {});
|
|
251
|
+
return Object.values(groupedByType).flatMap((group) => group.slice(0, limitPerType));
|
|
228
252
|
}
|
|
229
253
|
return results;
|
|
230
254
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reliverse/pathkit",
|
|
3
|
+
"version": "2.3.1",
|
|
4
|
+
"private": false,
|
|
3
5
|
"description": "Path manipulation and resolution utilities for Reliverse ecosystem",
|
|
6
|
+
"license": "MIT",
|
|
4
7
|
"author": "reliverse",
|
|
5
|
-
"
|
|
6
|
-
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"package.json"
|
|
11
|
+
],
|
|
7
12
|
"type": "module",
|
|
8
13
|
"exports": {
|
|
9
14
|
".": {
|
|
@@ -13,10 +18,5 @@
|
|
|
13
18
|
},
|
|
14
19
|
"publishConfig": {
|
|
15
20
|
"access": "public"
|
|
16
|
-
}
|
|
17
|
-
"files": [
|
|
18
|
-
"dist",
|
|
19
|
-
"package.json"
|
|
20
|
-
],
|
|
21
|
-
"license": "MIT"
|
|
21
|
+
}
|
|
22
22
|
}
|