path-class 0.6.0 → 0.7.0
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/lib/path-class/Path.d.ts +155 -0
- package/dist/lib/path-class/chunks/chunk-KSCIGSNU.js +317 -0
- package/dist/lib/path-class/chunks/chunk-KSCIGSNU.js.map +7 -0
- package/dist/lib/path-class/index.d.ts +1 -138
- package/dist/lib/path-class/index.js +7 -291
- package/dist/lib/path-class/index.js.map +3 -3
- package/dist/lib/path-class/sync/index.d.ts +57 -0
- package/dist/lib/path-class/sync/index.js +109 -0
- package/dist/lib/path-class/sync/index.js.map +7 -0
- package/dist/lib/path-class/sync/static.d.ts +6 -0
- package/package.json +5 -1
- package/src/{index.test.ts → Path.test.ts} +46 -18
- package/src/Path.ts +446 -0
- package/src/index.ts +1 -415
- package/src/sync/index.ts +235 -0
- package/src/sync/static.ts +14 -0
- package/src/sync/sync.test.ts +191 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { Path } from "../Path";
|
|
5
|
+
import "./index";
|
|
6
|
+
|
|
7
|
+
test(".existsAsFileSync()", () => {
|
|
8
|
+
const filePath = Path.makeTempDirSync().join("file.txt");
|
|
9
|
+
expect(filePath.existsSync()).toBe(false);
|
|
10
|
+
expect(filePath.existsSync({ mustBe: "file" })).toBe(false);
|
|
11
|
+
expect(filePath.existsSync({ mustBe: "directory" })).toBe(false);
|
|
12
|
+
expect(filePath.existsAsFileSync()).toBe(false);
|
|
13
|
+
filePath.writeSync("test");
|
|
14
|
+
expect(filePath.existsSync()).toBe(true);
|
|
15
|
+
expect(filePath.existsSync({ mustBe: "file" })).toBe(true);
|
|
16
|
+
expect(() => filePath.existsSync({ mustBe: "directory" })).toThrow(
|
|
17
|
+
/Path exists but is not a directory/,
|
|
18
|
+
);
|
|
19
|
+
expect(filePath.existsAsFileSync()).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test(".existsAsDir()", () => {
|
|
23
|
+
const filePath = Path.makeTempDirSync();
|
|
24
|
+
expect(filePath.existsSync()).toBe(true);
|
|
25
|
+
expect(() => filePath.existsSync({ mustBe: "file" })).toThrow(
|
|
26
|
+
/Path exists but is not a file/,
|
|
27
|
+
);
|
|
28
|
+
expect(filePath.existsSync({ mustBe: "directory" })).toBe(true);
|
|
29
|
+
expect(filePath.existsAsDirSync()).toBe(true);
|
|
30
|
+
filePath.rm_rfSync();
|
|
31
|
+
expect(filePath.existsSync()).toBe(false);
|
|
32
|
+
expect(filePath.existsSync({ mustBe: "file" })).toBe(false);
|
|
33
|
+
expect(filePath.existsSync({ mustBe: "directory" })).toBe(false);
|
|
34
|
+
expect(filePath.existsAsDirSync()).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test(".mkdirSync(…) (un-nested)", () => {
|
|
38
|
+
const dir = Path.makeTempDirSync().join("mkdir-test");
|
|
39
|
+
expect(dir.existsSync()).toBe(false);
|
|
40
|
+
dir.mkdirSync();
|
|
41
|
+
expect(dir.existsSync()).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test(".mkdirSync(…) (nested)", () => {
|
|
45
|
+
const dir = Path.makeTempDirSync().join("mkdir-test/nested");
|
|
46
|
+
expect(dir.existsSync()).toBe(false);
|
|
47
|
+
expect(() => dir.mkdirSync({ recursive: false })).toThrow("no such file");
|
|
48
|
+
dir.mkdirSync();
|
|
49
|
+
expect(dir.existsSync()).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test(".cpSync(…)", () => {
|
|
53
|
+
const parentDir = Path.makeTempDirSync();
|
|
54
|
+
const file1 = parentDir.join("file1.txt");
|
|
55
|
+
const file2 = parentDir.join("file2.txt");
|
|
56
|
+
|
|
57
|
+
file1.writeSync("hello world");
|
|
58
|
+
expect(file1.existsSync()).toBe(true);
|
|
59
|
+
expect(file2.existsSync()).toBe(false);
|
|
60
|
+
|
|
61
|
+
file1.cpSync(file2);
|
|
62
|
+
expect(file1.existsSync()).toBe(true);
|
|
63
|
+
expect(file2.existsSync()).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test(".renameSync(…)", () => {
|
|
67
|
+
const parentDir = Path.makeTempDirSync();
|
|
68
|
+
const file1 = parentDir.join("file1.txt");
|
|
69
|
+
const file2 = parentDir.join("file2.txt");
|
|
70
|
+
|
|
71
|
+
file1.writeSync("hello world");
|
|
72
|
+
expect(file1.existsSync()).toBe(true);
|
|
73
|
+
expect(file2.existsSync()).toBe(false);
|
|
74
|
+
|
|
75
|
+
file1.renameSync(file2);
|
|
76
|
+
expect(file1.existsSync()).toBe(false);
|
|
77
|
+
expect(file2.existsSync()).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test(".makeTempDirSync(…)", () => {
|
|
81
|
+
const tempDir = Path.makeTempDirSync();
|
|
82
|
+
expect(tempDir.path).toContain("/js-temp-");
|
|
83
|
+
expect(tempDir.basename.path).toStartWith("js-temp-");
|
|
84
|
+
expect(tempDir.existsAsDirSync()).toBe(true);
|
|
85
|
+
|
|
86
|
+
const tempDir2 = Path.makeTempDirSync("foo");
|
|
87
|
+
expect(tempDir2.path).not.toContain("/js-temp-");
|
|
88
|
+
expect(tempDir2.basename.path).toStartWith("foo");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test(".rmSync(…) (file)", () => {
|
|
92
|
+
const file = Path.makeTempDirSync().join("file.txt");
|
|
93
|
+
file.writeSync("");
|
|
94
|
+
expect(file.existsAsFileSync()).toBe(true);
|
|
95
|
+
file.rmSync();
|
|
96
|
+
expect(file.existsAsFileSync()).toBe(false);
|
|
97
|
+
expect(file.parent.existsAsDirSync()).toBe(true);
|
|
98
|
+
expect(() => file.rmSync()).toThrowError(/ENOENT/);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test(".rmSync(…) (folder)", () => {
|
|
102
|
+
const tempDir = Path.makeTempDirSync();
|
|
103
|
+
const file = tempDir.join("file.txt");
|
|
104
|
+
file.writeSync("");
|
|
105
|
+
expect(tempDir.existsAsDirSync()).toBe(true);
|
|
106
|
+
expect(() => tempDir.rmSync()).toThrowError(/EACCES|EFAULT/);
|
|
107
|
+
file.rmSync();
|
|
108
|
+
tempDir.rmSync({ recursive: true });
|
|
109
|
+
expect(tempDir.existsAsDirSync()).toBe(false);
|
|
110
|
+
expect(() => tempDir.rmSync()).toThrowError(/ENOENT/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test(".rm_rfSync(…) (file)", () => {
|
|
114
|
+
const file = Path.makeTempDirSync().join("file.txt");
|
|
115
|
+
file.writeSync("");
|
|
116
|
+
expect(file.existsAsFileSync()).toBe(true);
|
|
117
|
+
file.rm_rfSync();
|
|
118
|
+
expect(file.existsAsFileSync()).toBe(false);
|
|
119
|
+
expect(file.parent.existsAsDirSync()).toBe(true);
|
|
120
|
+
file.rm_rfSync();
|
|
121
|
+
expect(file.existsAsFileSync()).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test(".rm_rfSync(…) (folder)", () => {
|
|
125
|
+
const tempDir = Path.makeTempDirSync();
|
|
126
|
+
tempDir.join("file.txt").writeSync("");
|
|
127
|
+
expect(tempDir.path).toContain("/js-temp-");
|
|
128
|
+
expect(tempDir.existsSync()).toBe(true);
|
|
129
|
+
tempDir.rm_rfSync();
|
|
130
|
+
expect(tempDir.existsSync()).toBe(false);
|
|
131
|
+
tempDir.rm_rfSync();
|
|
132
|
+
expect(tempDir.existsSync()).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test(".readTextSync()", () => {
|
|
136
|
+
const file = Path.makeTempDirSync().join("file.txt");
|
|
137
|
+
file.writeSync("hi");
|
|
138
|
+
file.writeSync("bye");
|
|
139
|
+
|
|
140
|
+
expect(file.readTextSync()).toBe("bye");
|
|
141
|
+
expect(readFileSync(file.path, "utf-8")).toBe("bye");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test(".readJSONWync()", () => {
|
|
145
|
+
const file = Path.makeTempDirSync().join("file.json");
|
|
146
|
+
file.writeSync(JSON.stringify({ foo: "bar" }));
|
|
147
|
+
|
|
148
|
+
expect(file.readJSONSync()).toEqual<Record<string, string>>({ foo: "bar" });
|
|
149
|
+
expect(file.readJSONSync<Record<string, string>>()).toEqual({ foo: "bar" });
|
|
150
|
+
expect(JSON.parse(readFileSync(file.path, "utf-8"))).toEqual<
|
|
151
|
+
Record<string, string>
|
|
152
|
+
>({ foo: "bar" });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test(".writeSync(…)", () => {
|
|
156
|
+
const tempDir = Path.makeTempDirSync();
|
|
157
|
+
const file = tempDir.join("file.json");
|
|
158
|
+
expect(file.writeSync("foo")).toBe(file);
|
|
159
|
+
|
|
160
|
+
expect(readFileSync(join(tempDir.path, "./file.json"), "utf-8")).toEqual(
|
|
161
|
+
"foo",
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
const file2 = tempDir.join("nested/file2.json");
|
|
165
|
+
expect(file2.writeSync("bar")).toBe(file2);
|
|
166
|
+
expect(
|
|
167
|
+
readFileSync(join(tempDir.path, "./nested/file2.json"), "utf-8"),
|
|
168
|
+
).toEqual("bar");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test(".writeJSONSync(…)", () => {
|
|
172
|
+
const file = Path.makeTempDirSync().join("file.json");
|
|
173
|
+
expect(file.writeJSONSync({ foo: "bar" })).toBe(file);
|
|
174
|
+
|
|
175
|
+
expect(file.readJSONSync()).toEqual<Record<string, string>>({ foo: "bar" });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test(".readDirSync(…)", () => {
|
|
179
|
+
const dir = Path.makeTempDirSync();
|
|
180
|
+
dir.join("file.txt").writeSync("hello");
|
|
181
|
+
dir.join("dir/file.json").writeSync("hello");
|
|
182
|
+
|
|
183
|
+
const contentsAsStrings = dir.readDirSync();
|
|
184
|
+
expect(new Set(contentsAsStrings)).toEqual(new Set(["file.txt", "dir"]));
|
|
185
|
+
|
|
186
|
+
const contentsAsEntries = dir.readDirSync({ withFileTypes: true });
|
|
187
|
+
expect(contentsAsEntries.map((entry) => entry.name)).toEqual([
|
|
188
|
+
"file.txt",
|
|
189
|
+
"dir",
|
|
190
|
+
]);
|
|
191
|
+
});
|