@hapico/cli 0.0.15 → 0.0.17

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.
@@ -0,0 +1,97 @@
1
+ class Path {
2
+ private static normalizeSlashes(path: string): string {
3
+ return path.replace(/\\/g, "/");
4
+ }
5
+
6
+ // Tương tự path.join()
7
+ public static join(...paths: string[]): string {
8
+ const normalizedPaths = paths
9
+ .filter((p) => p && typeof p === "string")
10
+ .map((p) => this.normalizeSlashes(p.trim()))
11
+ .map((p) => p.replace(/^\/+|\/+$/g, "")); // Loại bỏ slash đầu và cuối
12
+
13
+ return normalizedPaths.join("/");
14
+ }
15
+
16
+ // Tương tự path.resolve()
17
+ public static resolve(...paths: string[]): string {
18
+ let resolvedPath = "";
19
+ const segments: string[] = [];
20
+
21
+ paths
22
+ .filter((p) => p && typeof p === "string")
23
+ .map((p) => this.normalizeSlashes(p))
24
+ .forEach((path) => {
25
+ if (path.startsWith("/")) {
26
+ segments.length = 0; // Reset nếu gặp absolute path
27
+ segments.push(...path.split("/").filter(Boolean));
28
+ } else {
29
+ segments.push(...path.split("/").filter(Boolean));
30
+ }
31
+ });
32
+
33
+ const resolvedSegments: string[] = [];
34
+ for (const segment of segments) {
35
+ if (segment === "..") {
36
+ resolvedSegments.pop();
37
+ } else if (segment !== ".") {
38
+ resolvedSegments.push(segment);
39
+ }
40
+ }
41
+
42
+ resolvedPath = resolvedSegments.join("/");
43
+ return resolvedPath.startsWith("/") ? `/${resolvedPath}` : resolvedPath;
44
+ }
45
+
46
+ // Tương tự path.basename()
47
+ public static basename(path: string, ext?: string): string {
48
+ const normalized = this.normalizeSlashes(path);
49
+ const base = normalized.split("/").pop() || "";
50
+ return ext && base.endsWith(ext) ? base.slice(0, -ext.length) : base;
51
+ }
52
+
53
+ // Tương tự path.dirname()
54
+ public static dirname(path: string): string {
55
+ const normalized = this.normalizeSlashes(path);
56
+ const segments = normalized.split("/");
57
+ segments.pop();
58
+ return segments.join("/") || ".";
59
+ }
60
+
61
+ // Tương tự path.extname()
62
+ public static extname(path: string): string {
63
+ const base = this.basename(path);
64
+ const dotIndex = base.lastIndexOf(".");
65
+ return dotIndex > 0 ? base.slice(dotIndex) : "";
66
+ }
67
+
68
+ // Tương tự path.isAbsolute()
69
+ public static isAbsolute(path: string): boolean {
70
+ return this.normalizeSlashes(path).startsWith("/");
71
+ }
72
+
73
+ // Tương tự path.relative()
74
+ public static relative(from: string, to: string): string {
75
+ const fromParts = this.normalizeSlashes(from).split("/").filter(Boolean);
76
+ const toParts = this.normalizeSlashes(to).split("/").filter(Boolean);
77
+
78
+ // Tìm điểm chung
79
+ let i = 0;
80
+ while (
81
+ i < fromParts.length &&
82
+ i < toParts.length &&
83
+ fromParts[i] === toParts[i]
84
+ ) {
85
+ i++;
86
+ }
87
+
88
+ // Tính số bước đi lên
89
+ const upCount = fromParts.length - i;
90
+ const up = Array(upCount).fill("..");
91
+ const remaining = toParts.slice(i);
92
+
93
+ return [...up, ...remaining].join("/") || ".";
94
+ }
95
+ }
96
+
97
+ export default Path;