@d0paminedriven/fs 5.0.0 → 6.0.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/README.md +105 -0
- package/dist/chunk-4X5DVWVM.js +14 -0
- package/dist/{chunk-HWF53VET.js → chunk-73BXX52B.js} +162 -27
- package/dist/fs/index.cjs +168 -26
- package/dist/fs/index.d.cts +85 -2
- package/dist/fs/index.d.ts +85 -2
- package/dist/fs/index.js +2 -1
- package/dist/index.cjs +172 -28
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -2
- package/dist/types/index.cjs +19 -0
- package/dist/types/index.d.cts +19 -3
- package/dist/types/index.d.ts +19 -3
- package/dist/types/index.js +7 -0
- package/package.json +7 -7
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @d0paminedriven/fs
|
|
2
|
+
|
|
3
|
+
> *“Don’t sweat the small stuff; just write your damn files.”*
|
|
4
|
+
|
|
5
|
+
Let's be real, you likely already have a project manager breathing down your neck for updates on the hour, JIRA ticket blues bringing you down, Teams/Slack notification pings plaguing your dreams, the "hey, got five minutes?" guy breaking your flow state on the daily, and nth other pressing issues on your mind mid-development.
|
|
6
|
+
|
|
7
|
+
These situations are all annoying af, which is why this package seamlessly ensures that targeted output paths exist for you (if they don't exist yet, they will before your file is written). Whether you're using `writeFileAsync`, `fetchRemoteWriteLocalLargeFiles`, or `withWs`, this package has you covered no matter how deep your targeted output paths go. Unfortunately, this package *can't* help with that pesky PM or that flow-breaking co-worker (yet--PRs welcome).
|
|
8
|
+
|
|
9
|
+
Want to grab some remote files? Throw any file size its way — no problemo — just use the `fetchRemoteWriteLocalLargeFiles` method. (Proceed with caution in the >=gigabyte range. If your setup can handle it, then by all means.)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 🚀 Quickstart
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Fs } from "@d0paminedriven/fs";
|
|
17
|
+
|
|
18
|
+
// Set your cwd (project root recommended)
|
|
19
|
+
const fs = new Fs(process.cwd());
|
|
20
|
+
|
|
21
|
+
/** Write to a nested file path (no mkdirp needed) */
|
|
22
|
+
await fs.writeFileAsync("public/assets/images/foo/bar.jpg", myImageBuffer);
|
|
23
|
+
// No error, no “does this folder exist?”, no drama
|
|
24
|
+
|
|
25
|
+
/** Fetch and save a massive remote asset */
|
|
26
|
+
await fs.fetchRemoteWriteLocalLargeFiles(
|
|
27
|
+
"https://cdn.example.com/big-model.glb",
|
|
28
|
+
"public/models/my-big-model"
|
|
29
|
+
);
|
|
30
|
+
// Handles directories, streams file to disk, and never blows up your RAM
|
|
31
|
+
|
|
32
|
+
/** Read every file in a directory */
|
|
33
|
+
const files = fs.readDir("public/assets/images", { recursive: true });
|
|
34
|
+
|
|
35
|
+
/** Unlink a file (with existence check) */
|
|
36
|
+
await fs.unlink("public/assets/old/unused.txt");
|
|
37
|
+
|
|
38
|
+
/** Need a wait utility for your script? */
|
|
39
|
+
await fs.wait(2000); // waits 2 seconds
|
|
40
|
+
|
|
41
|
+
/** Get the MIME type for any file by extension */
|
|
42
|
+
const mime = fs.getMimeTypeForPath("foo.png"); // "image/png"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 🍳 Recipes / Real-World Scripts
|
|
48
|
+
|
|
49
|
+
Need to audit or analyze files, generate a TypeScript object from your assets, or automate asset metadata?
|
|
50
|
+
Here’s a dead-simple pattern to iterate off of:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
|
|
54
|
+
const files = fs.readDir("public/assets/images", { recursive: true });
|
|
55
|
+
|
|
56
|
+
/** Map those files to a typescript object with file names as keys + corresponding file sizes as values */
|
|
57
|
+
const arr = Array.of<[string, number]>();
|
|
58
|
+
|
|
59
|
+
function generateTsObjOfFileSizes(withExtensions = true) {
|
|
60
|
+
try {
|
|
61
|
+
files.forEach(function (file) {
|
|
62
|
+
const size = fs.fileSizeMb(`public/assets/images/${file}`);
|
|
63
|
+
if (withExtensions === false) {
|
|
64
|
+
arr.push([file.split(/\./g)[0] ?? file, size]);
|
|
65
|
+
} else {
|
|
66
|
+
arr.push([file, size]);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (err instanceof Error) throw new Error(err.message);
|
|
71
|
+
else console.error(`generateTsFileOfFileSizes`, err);
|
|
72
|
+
} finally {
|
|
73
|
+
const tupleArrToObj = Object.fromEntries(arr);
|
|
74
|
+
fs.withWs(
|
|
75
|
+
`src/utils/file-sizes.ts`,
|
|
76
|
+
`export const publicAssetsImagesFileSizesMb = ${JSON.stringify(tupleArrToObj, null, 2)} as const;`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
generateTsObjOfFileSizes();
|
|
82
|
+
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
*This outputs a TypeScript object you can use for dashboards, asset analytics, or CI/CD checks:*
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
export const fileSizesInMb = {
|
|
89
|
+
"chess-atb.png": 1.1277456283569336,
|
|
90
|
+
"elegant-stone-tiles-albedo.png": 53.93040370941162,
|
|
91
|
+
"port-40.avif": 1.7613801956176758,
|
|
92
|
+
"saeukkang.usdz": 3.6647157669067383
|
|
93
|
+
} as const;
|
|
94
|
+
```
|
|
95
|
+
<small>ⓘ assets used were pooled from repos across github via the `fetchRemoteWriteLocalLargeFiles` method</small>
|
|
96
|
+
|
|
97
|
+
<br>
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
|npm |downloads| github|
|
|
102
|
+
|:----:|:----:|:------:|
|
|
103
|
+
| [](https://www.npmjs.com/package/@d0paminedriven/fs) | [](https://www.npmjs.com/package/@d0paminedriven/fs) | <a style="fill:white;" href="https://github.com/DopamineDriven/d0paminedriven/tree/master/packages/fs"><svg height="24" aria-hidden="true" viewBox="0 0 24 24" version="1.1" width="24" data-view-component="true" class="octicon octicon-mark-github"><path d="M12 1C5.9225 1 1 5.9225 1 12C1 16.8675 4.14875 20.9787 8.52125 22.4362C9.07125 22.5325 9.2775 22.2025 9.2775 21.9137C9.2775 21.6525 9.26375 20.7862 9.26375 19.865C6.5 20.3737 5.785 19.1912 5.565 18.5725C5.44125 18.2562 4.905 17.28 4.4375 17.0187C4.0525 16.8125 3.5025 16.3037 4.42375 16.29C5.29 16.2762 5.90875 17.0875 6.115 17.4175C7.105 19.0812 8.68625 18.6137 9.31875 18.325C9.415 17.61 9.70375 17.1287 10.02 16.8537C7.5725 16.5787 5.015 15.63 5.015 11.4225C5.015 10.2262 5.44125 9.23625 6.1425 8.46625C6.0325 8.19125 5.6475 7.06375 6.2525 5.55125C6.2525 5.55125 7.17375 5.2625 9.2775 6.67875C10.1575 6.43125 11.0925 6.3075 12.0275 6.3075C12.9625 6.3075 13.8975 6.43125 14.7775 6.67875C16.8813 5.24875 17.8025 5.55125 17.8025 5.55125C18.4075 7.06375 18.0225 8.19125 17.9125 8.46625C18.6138 9.23625 19.04 10.2125 19.04 11.4225C19.04 15.6437 16.4688 16.5787 14.0213 16.8537C14.42 17.1975 14.7638 17.8575 14.7638 18.8887C14.7638 20.36 14.75 21.5425 14.75 21.9137C14.75 22.2025 14.9563 22.5462 15.5063 22.4362C19.8513 20.9787 23 16.8537 23 12C23 5.9225 18.0775 1 12 1Z"></path></svg></a>
|
|
104
|
+
|
|
105
|
+
---
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MimeService
|
|
3
3
|
} from "./chunk-EHOBCDHE.js";
|
|
4
|
+
import {
|
|
5
|
+
unitsObj
|
|
6
|
+
} from "./chunk-4X5DVWVM.js";
|
|
4
7
|
import {
|
|
5
8
|
__name
|
|
6
9
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -28,14 +31,84 @@ var Fs = class extends MimeService {
|
|
|
28
31
|
parseDotEnv() {
|
|
29
32
|
return expand.expand(this.myEnv);
|
|
30
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param target path to the file or directory to check for existence
|
|
37
|
+
* @returns true if the file or directory exists, false otherwise
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const fs = new Fs(process.cwd());
|
|
41
|
+
* const exists = fs.exists("my-file.txt");
|
|
42
|
+
* console.log(exists); // true if the file exists, false otherwise
|
|
43
|
+
* ```
|
|
44
|
+
* @description
|
|
45
|
+
* This method checks if a file or directory exists at the specified path input
|
|
46
|
+
*
|
|
47
|
+
* Additional Note:
|
|
48
|
+
*
|
|
49
|
+
* say you have a directory that begins with a dot, such as `.turbo`;
|
|
50
|
+
*
|
|
51
|
+
* in this scenario, both `./.turbo` and `.turbo` will return true if the directory exists
|
|
52
|
+
*
|
|
53
|
+
*/
|
|
31
54
|
exists(target) {
|
|
32
|
-
if (!/\//gm.test(target))
|
|
33
|
-
|
|
34
|
-
|
|
55
|
+
if (!/\//gm.test(target)) {
|
|
56
|
+
if (/\./g.test(target)) {
|
|
57
|
+
return fsSync.existsSync(resolve(join(this.cwd, target)));
|
|
58
|
+
} else {
|
|
59
|
+
const statsSync = fsSync.statSync(resolve(join(this.cwd, target)), {
|
|
60
|
+
throwIfNoEntry: false
|
|
61
|
+
});
|
|
62
|
+
const isDir = statsSync?.isDirectory();
|
|
63
|
+
if (isDir) {
|
|
64
|
+
return isDir;
|
|
65
|
+
} else return false;
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
if (/\./g.test(target)) {
|
|
69
|
+
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), target));
|
|
70
|
+
} else {
|
|
71
|
+
const statsSync = fsSync.statSync(relative(this.cwd, target), {
|
|
72
|
+
throwIfNoEntry: false
|
|
73
|
+
});
|
|
74
|
+
const isDir = statsSync?.isDirectory();
|
|
75
|
+
if (isDir) {
|
|
76
|
+
return isDir;
|
|
77
|
+
} else return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
35
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param ms milliseconds to wait
|
|
84
|
+
* @returns a promise that resolves after the specified number of milliseconds
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const fs = new Fs(process.cwd());
|
|
88
|
+
* await fs.wait(1000); // waits for 1 second
|
|
89
|
+
* console.log("Done waiting!");
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* Alternatively, you can use it like this:
|
|
93
|
+
* ```ts
|
|
94
|
+
* const fs = new Fs(process.cwd());
|
|
95
|
+
* fs.wait(1000).then(() => { // waits for 1 second
|
|
96
|
+
* console.log("Done waiting!");
|
|
97
|
+
* // do something after waiting
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @description
|
|
102
|
+
* This method is useful for delaying execution in asynchronous code, such as when you need to wait for a file operation to complete or when you want to introduce a delay in a loop.
|
|
103
|
+
*/
|
|
36
104
|
wait(ms) {
|
|
37
105
|
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
38
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Handles an array or readonly array of strings and/or Buffers, converting Buffers to strings
|
|
109
|
+
* @param arr An array or readonly array of strings and/or Buffers
|
|
110
|
+
* @returns An array of strings with Buffers converted to UTF-8 strings.
|
|
111
|
+
*/
|
|
39
112
|
handleBuffStrArrUnion(arr) {
|
|
40
113
|
return arr.map(
|
|
41
114
|
(v) => Buffer.isBuffer(v) ? Buffer.from(v).toString("utf-8") : v
|
|
@@ -46,10 +119,32 @@ var Fs = class extends MimeService {
|
|
|
46
119
|
return true;
|
|
47
120
|
} else return false;
|
|
48
121
|
}
|
|
49
|
-
|
|
122
|
+
/**
|
|
123
|
+
*
|
|
124
|
+
* @param path path to the directory relative to the cwd
|
|
125
|
+
* @param options options for reading the directory, defaults to recursive
|
|
126
|
+
*
|
|
127
|
+
* The following two examples are equivalent:
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const fs = new Fs(process.cwd());
|
|
132
|
+
* const files = fs.readDir("./my-directory", { recursive: true });
|
|
133
|
+
* console.log(files);
|
|
134
|
+
* ```
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* const fs = new Fs(process.cwd());
|
|
138
|
+
* const files = fs.readDir("my-directory", { recursive: true });
|
|
139
|
+
* console.log(files);
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @returns an array of strings representing the contents of the directory
|
|
143
|
+
*/
|
|
144
|
+
readDir(path, options = { recursive: true }) {
|
|
50
145
|
if (this.isRootPathTargeted(path)) {
|
|
51
146
|
return this.handleBuffStrArrUnion(
|
|
52
|
-
fsSync.readdirSync(resolve(join(this.cwd, "./")), options)
|
|
147
|
+
fsSync.readdirSync(resolve(join(this.cwd, "./")), { ...options })
|
|
53
148
|
);
|
|
54
149
|
} else {
|
|
55
150
|
return this.handleBuffStrArrUnion(
|
|
@@ -67,22 +162,64 @@ var Fs = class extends MimeService {
|
|
|
67
162
|
"utf-8"
|
|
68
163
|
), "executeCommand");
|
|
69
164
|
existsSync(path) {
|
|
70
|
-
|
|
71
|
-
return fsSync.existsSync(resolve(join(this.cwd, path)));
|
|
72
|
-
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), path));
|
|
165
|
+
return this.exists(path);
|
|
73
166
|
}
|
|
74
167
|
pathHandler(path) {
|
|
75
168
|
return /\//g.test(path) === true ? path.split(/([/])/gim).reverse().slice(2).reverse().join("") : path;
|
|
76
169
|
}
|
|
77
170
|
mkdirSync(path, options) {
|
|
78
|
-
return fsSync.mkdirSync(relative(this.cwd
|
|
171
|
+
return fsSync.mkdirSync(relative(this.cwd, path), options);
|
|
172
|
+
}
|
|
173
|
+
units = unitsObj;
|
|
174
|
+
u = ["B", "KB", "MB", "GB", "TB", "PB"];
|
|
175
|
+
autoFileSizeRaw(size) {
|
|
176
|
+
let s = typeof size === "bigint" ? Number(size) : size;
|
|
177
|
+
let i = 0;
|
|
178
|
+
while (s >= 1024 && i < this.u.length - 1) {
|
|
179
|
+
s /= 1024;
|
|
180
|
+
i++;
|
|
181
|
+
}
|
|
182
|
+
return { value: s, unit: this.u[i] };
|
|
183
|
+
}
|
|
184
|
+
getSize(size, target, opts = { decimals: 4, includeUnits: true }) {
|
|
185
|
+
const { decimals, includeUnits } = opts;
|
|
186
|
+
if (target === "auto") {
|
|
187
|
+
const { value, unit } = this.autoFileSizeRaw(size);
|
|
188
|
+
const rounded2 = value.toFixed(decimals);
|
|
189
|
+
return includeUnits ? `${rounded2} ${unit}` : Number.parseFloat(rounded2);
|
|
190
|
+
}
|
|
191
|
+
const key = target.toUpperCase();
|
|
192
|
+
const exp = this.units[key];
|
|
193
|
+
const divisor = typeof size === "bigint" ? 1024n ** BigInt(exp) : 1024 ** exp;
|
|
194
|
+
let v = 0;
|
|
195
|
+
if (typeof size === "bigint" || typeof divisor === "bigint") {
|
|
196
|
+
if (typeof size === "bigint" && typeof divisor === "bigint")
|
|
197
|
+
v = Number(size / divisor);
|
|
198
|
+
else if (typeof size !== "bigint" && typeof divisor === "bigint")
|
|
199
|
+
v = size / Number(divisor);
|
|
200
|
+
else if (typeof size === "bigint" && typeof divisor !== "bigint")
|
|
201
|
+
v = Number(size) / divisor;
|
|
202
|
+
} else if (typeof size === "number" && typeof divisor === "number") {
|
|
203
|
+
v = size / divisor;
|
|
204
|
+
}
|
|
205
|
+
const rounded = v.toFixed(decimals);
|
|
206
|
+
return includeUnits ? `${rounded} ${key}` : Number.parseFloat(rounded);
|
|
79
207
|
}
|
|
80
208
|
fileSizeMb(path) {
|
|
81
|
-
return fsSync.statSync(relative(this.cwd
|
|
209
|
+
return fsSync.statSync(relative(this.cwd, path)).size / (1024 * 1024);
|
|
210
|
+
}
|
|
211
|
+
fileSize(path, target, opts) {
|
|
212
|
+
if (!this.exists(path)) throw new Error(`path ${path} does not exist`);
|
|
213
|
+
else {
|
|
214
|
+
return this.getSize(
|
|
215
|
+
fsSync.statSync(relative(this.cwd, path)).size,
|
|
216
|
+
target,
|
|
217
|
+
opts
|
|
218
|
+
);
|
|
219
|
+
}
|
|
82
220
|
}
|
|
83
221
|
generateDirIfDNE(path, options) {
|
|
84
|
-
|
|
85
|
-
if (doesExist === true) return;
|
|
222
|
+
if (this.existsSync(path)) return;
|
|
86
223
|
else {
|
|
87
224
|
return this.mkdirSync(path, options);
|
|
88
225
|
}
|
|
@@ -128,14 +265,6 @@ var Fs = class extends MimeService {
|
|
|
128
265
|
);
|
|
129
266
|
}
|
|
130
267
|
}, "writeFileAsync");
|
|
131
|
-
dirExists(dir) {
|
|
132
|
-
const resolved = relative(this.cwd, dir);
|
|
133
|
-
try {
|
|
134
|
-
return fsSync.statSync(resolved).isDirectory();
|
|
135
|
-
} catch {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
268
|
async unlink(path) {
|
|
140
269
|
if (this.existsSync(path)) {
|
|
141
270
|
return await fsAsync.unlink(relative(this.cwd, path));
|
|
@@ -144,7 +273,7 @@ var Fs = class extends MimeService {
|
|
|
144
273
|
}
|
|
145
274
|
}
|
|
146
275
|
rmFile(filePath) {
|
|
147
|
-
if (this.
|
|
276
|
+
if (this.exists(filePath)) {
|
|
148
277
|
if (this.isRootPathTargeted(filePath)) {
|
|
149
278
|
fsSync.rmSync(resolve(join(this.cwd, filePath)));
|
|
150
279
|
} else fsSync.rmSync(resolve(this.cwd, filePath));
|
|
@@ -152,7 +281,7 @@ var Fs = class extends MimeService {
|
|
|
152
281
|
}
|
|
153
282
|
async rmdir(dir, options) {
|
|
154
283
|
const resolved = relative(this.cwd, dir);
|
|
155
|
-
if (this.
|
|
284
|
+
if (this.exists(dir)) {
|
|
156
285
|
return fsAsync.rmdir(relative(this.cwd, dir), options);
|
|
157
286
|
} else {
|
|
158
287
|
throw new Error(`directory ${resolved} does not exist`);
|
|
@@ -160,7 +289,7 @@ var Fs = class extends MimeService {
|
|
|
160
289
|
}
|
|
161
290
|
rmDirSync(dir) {
|
|
162
291
|
const resolved = resolve(this.cwd, dir);
|
|
163
|
-
if (this.
|
|
292
|
+
if (this.exists(dir)) {
|
|
164
293
|
return fsSync.rm(resolved, { force: true, recursive: true }, (err) => {
|
|
165
294
|
if (err?.message) {
|
|
166
295
|
console.error(err?.message);
|
|
@@ -290,12 +419,18 @@ var Fs = class extends MimeService {
|
|
|
290
419
|
return;
|
|
291
420
|
}
|
|
292
421
|
const result = await this.assetToBufferView(inputUrl);
|
|
293
|
-
const
|
|
294
|
-
this.withWs(formattedPath, Buffer.from(
|
|
422
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
423
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
295
424
|
} catch (err) {
|
|
296
|
-
console.error(`[
|
|
425
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
297
426
|
}
|
|
298
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
*
|
|
430
|
+
* @access package-private
|
|
431
|
+
*
|
|
432
|
+
* work in progress, for internal package maintainer use only
|
|
433
|
+
*/
|
|
299
434
|
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
300
435
|
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
301
436
|
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
|
@@ -319,4 +454,4 @@ var Fs = class extends MimeService {
|
|
|
319
454
|
export {
|
|
320
455
|
Fs
|
|
321
456
|
};
|
|
322
|
-
//# sourceMappingURL=chunk-
|
|
457
|
+
//# sourceMappingURL=chunk-73BXX52B.js.map
|
package/dist/fs/index.cjs
CHANGED
|
@@ -213,6 +213,16 @@ var MimeService = class extends UrlService {
|
|
|
213
213
|
}
|
|
214
214
|
};
|
|
215
215
|
|
|
216
|
+
// src/types/index.ts
|
|
217
|
+
var unitsObj = {
|
|
218
|
+
PB: 5,
|
|
219
|
+
TB: 4,
|
|
220
|
+
GB: 3,
|
|
221
|
+
MB: 2,
|
|
222
|
+
KB: 1,
|
|
223
|
+
B: 0
|
|
224
|
+
};
|
|
225
|
+
|
|
216
226
|
// src/fs/index.ts
|
|
217
227
|
dotenv.config();
|
|
218
228
|
var Fs = class extends MimeService {
|
|
@@ -229,14 +239,84 @@ var Fs = class extends MimeService {
|
|
|
229
239
|
parseDotEnv() {
|
|
230
240
|
return import_dotenv_expand.default.expand(this.myEnv);
|
|
231
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* @param target path to the file or directory to check for existence
|
|
245
|
+
* @returns true if the file or directory exists, false otherwise
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* const fs = new Fs(process.cwd());
|
|
249
|
+
* const exists = fs.exists("my-file.txt");
|
|
250
|
+
* console.log(exists); // true if the file exists, false otherwise
|
|
251
|
+
* ```
|
|
252
|
+
* @description
|
|
253
|
+
* This method checks if a file or directory exists at the specified path input
|
|
254
|
+
*
|
|
255
|
+
* Additional Note:
|
|
256
|
+
*
|
|
257
|
+
* say you have a directory that begins with a dot, such as `.turbo`;
|
|
258
|
+
*
|
|
259
|
+
* in this scenario, both `./.turbo` and `.turbo` will return true if the directory exists
|
|
260
|
+
*
|
|
261
|
+
*/
|
|
232
262
|
exists(target) {
|
|
233
|
-
if (!/\//gm.test(target))
|
|
234
|
-
|
|
235
|
-
|
|
263
|
+
if (!/\//gm.test(target)) {
|
|
264
|
+
if (/\./g.test(target)) {
|
|
265
|
+
return import_fs.default.existsSync((0, import_path.resolve)((0, import_path.join)(this.cwd, target)));
|
|
266
|
+
} else {
|
|
267
|
+
const statsSync = import_fs.default.statSync((0, import_path.resolve)((0, import_path.join)(this.cwd, target)), {
|
|
268
|
+
throwIfNoEntry: false
|
|
269
|
+
});
|
|
270
|
+
const isDir = statsSync?.isDirectory();
|
|
271
|
+
if (isDir) {
|
|
272
|
+
return isDir;
|
|
273
|
+
} else return false;
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
if (/\./g.test(target)) {
|
|
277
|
+
return import_fs.default.existsSync((0, import_path.relative)(this.cwd ?? process.cwd(), target));
|
|
278
|
+
} else {
|
|
279
|
+
const statsSync = import_fs.default.statSync((0, import_path.relative)(this.cwd, target), {
|
|
280
|
+
throwIfNoEntry: false
|
|
281
|
+
});
|
|
282
|
+
const isDir = statsSync?.isDirectory();
|
|
283
|
+
if (isDir) {
|
|
284
|
+
return isDir;
|
|
285
|
+
} else return false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
236
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
*
|
|
291
|
+
* @param ms milliseconds to wait
|
|
292
|
+
* @returns a promise that resolves after the specified number of milliseconds
|
|
293
|
+
* @example
|
|
294
|
+
* ```ts
|
|
295
|
+
* const fs = new Fs(process.cwd());
|
|
296
|
+
* await fs.wait(1000); // waits for 1 second
|
|
297
|
+
* console.log("Done waiting!");
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
* Alternatively, you can use it like this:
|
|
301
|
+
* ```ts
|
|
302
|
+
* const fs = new Fs(process.cwd());
|
|
303
|
+
* fs.wait(1000).then(() => { // waits for 1 second
|
|
304
|
+
* console.log("Done waiting!");
|
|
305
|
+
* // do something after waiting
|
|
306
|
+
* });
|
|
307
|
+
* ```
|
|
308
|
+
*
|
|
309
|
+
* @description
|
|
310
|
+
* This method is useful for delaying execution in asynchronous code, such as when you need to wait for a file operation to complete or when you want to introduce a delay in a loop.
|
|
311
|
+
*/
|
|
237
312
|
wait(ms) {
|
|
238
313
|
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
239
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Handles an array or readonly array of strings and/or Buffers, converting Buffers to strings
|
|
317
|
+
* @param arr An array or readonly array of strings and/or Buffers
|
|
318
|
+
* @returns An array of strings with Buffers converted to UTF-8 strings.
|
|
319
|
+
*/
|
|
240
320
|
handleBuffStrArrUnion(arr) {
|
|
241
321
|
return arr.map(
|
|
242
322
|
(v) => Buffer.isBuffer(v) ? Buffer.from(v).toString("utf-8") : v
|
|
@@ -247,10 +327,32 @@ var Fs = class extends MimeService {
|
|
|
247
327
|
return true;
|
|
248
328
|
} else return false;
|
|
249
329
|
}
|
|
250
|
-
|
|
330
|
+
/**
|
|
331
|
+
*
|
|
332
|
+
* @param path path to the directory relative to the cwd
|
|
333
|
+
* @param options options for reading the directory, defaults to recursive
|
|
334
|
+
*
|
|
335
|
+
* The following two examples are equivalent:
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```ts
|
|
339
|
+
* const fs = new Fs(process.cwd());
|
|
340
|
+
* const files = fs.readDir("./my-directory", { recursive: true });
|
|
341
|
+
* console.log(files);
|
|
342
|
+
* ```
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* const fs = new Fs(process.cwd());
|
|
346
|
+
* const files = fs.readDir("my-directory", { recursive: true });
|
|
347
|
+
* console.log(files);
|
|
348
|
+
* ```
|
|
349
|
+
*
|
|
350
|
+
* @returns an array of strings representing the contents of the directory
|
|
351
|
+
*/
|
|
352
|
+
readDir(path, options = { recursive: true }) {
|
|
251
353
|
if (this.isRootPathTargeted(path)) {
|
|
252
354
|
return this.handleBuffStrArrUnion(
|
|
253
|
-
import_fs.default.readdirSync((0, import_path.resolve)((0, import_path.join)(this.cwd, "./")), options)
|
|
355
|
+
import_fs.default.readdirSync((0, import_path.resolve)((0, import_path.join)(this.cwd, "./")), { ...options })
|
|
254
356
|
);
|
|
255
357
|
} else {
|
|
256
358
|
return this.handleBuffStrArrUnion(
|
|
@@ -268,22 +370,64 @@ var Fs = class extends MimeService {
|
|
|
268
370
|
"utf-8"
|
|
269
371
|
), "executeCommand");
|
|
270
372
|
existsSync(path) {
|
|
271
|
-
|
|
272
|
-
return import_fs.default.existsSync((0, import_path.resolve)((0, import_path.join)(this.cwd, path)));
|
|
273
|
-
return import_fs.default.existsSync((0, import_path.relative)(this.cwd ?? process.cwd(), path));
|
|
373
|
+
return this.exists(path);
|
|
274
374
|
}
|
|
275
375
|
pathHandler(path) {
|
|
276
376
|
return /\//g.test(path) === true ? path.split(/([/])/gim).reverse().slice(2).reverse().join("") : path;
|
|
277
377
|
}
|
|
278
378
|
mkdirSync(path, options) {
|
|
279
|
-
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd
|
|
379
|
+
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd, path), options);
|
|
380
|
+
}
|
|
381
|
+
units = unitsObj;
|
|
382
|
+
u = ["B", "KB", "MB", "GB", "TB", "PB"];
|
|
383
|
+
autoFileSizeRaw(size) {
|
|
384
|
+
let s = typeof size === "bigint" ? Number(size) : size;
|
|
385
|
+
let i = 0;
|
|
386
|
+
while (s >= 1024 && i < this.u.length - 1) {
|
|
387
|
+
s /= 1024;
|
|
388
|
+
i++;
|
|
389
|
+
}
|
|
390
|
+
return { value: s, unit: this.u[i] };
|
|
391
|
+
}
|
|
392
|
+
getSize(size, target, opts = { decimals: 4, includeUnits: true }) {
|
|
393
|
+
const { decimals, includeUnits } = opts;
|
|
394
|
+
if (target === "auto") {
|
|
395
|
+
const { value, unit } = this.autoFileSizeRaw(size);
|
|
396
|
+
const rounded2 = value.toFixed(decimals);
|
|
397
|
+
return includeUnits ? `${rounded2} ${unit}` : Number.parseFloat(rounded2);
|
|
398
|
+
}
|
|
399
|
+
const key = target.toUpperCase();
|
|
400
|
+
const exp = this.units[key];
|
|
401
|
+
const divisor = typeof size === "bigint" ? 1024n ** BigInt(exp) : 1024 ** exp;
|
|
402
|
+
let v = 0;
|
|
403
|
+
if (typeof size === "bigint" || typeof divisor === "bigint") {
|
|
404
|
+
if (typeof size === "bigint" && typeof divisor === "bigint")
|
|
405
|
+
v = Number(size / divisor);
|
|
406
|
+
else if (typeof size !== "bigint" && typeof divisor === "bigint")
|
|
407
|
+
v = size / Number(divisor);
|
|
408
|
+
else if (typeof size === "bigint" && typeof divisor !== "bigint")
|
|
409
|
+
v = Number(size) / divisor;
|
|
410
|
+
} else if (typeof size === "number" && typeof divisor === "number") {
|
|
411
|
+
v = size / divisor;
|
|
412
|
+
}
|
|
413
|
+
const rounded = v.toFixed(decimals);
|
|
414
|
+
return includeUnits ? `${rounded} ${key}` : Number.parseFloat(rounded);
|
|
280
415
|
}
|
|
281
416
|
fileSizeMb(path) {
|
|
282
|
-
return import_fs.default.statSync((0, import_path.relative)(this.cwd
|
|
417
|
+
return import_fs.default.statSync((0, import_path.relative)(this.cwd, path)).size / (1024 * 1024);
|
|
418
|
+
}
|
|
419
|
+
fileSize(path, target, opts) {
|
|
420
|
+
if (!this.exists(path)) throw new Error(`path ${path} does not exist`);
|
|
421
|
+
else {
|
|
422
|
+
return this.getSize(
|
|
423
|
+
import_fs.default.statSync((0, import_path.relative)(this.cwd, path)).size,
|
|
424
|
+
target,
|
|
425
|
+
opts
|
|
426
|
+
);
|
|
427
|
+
}
|
|
283
428
|
}
|
|
284
429
|
generateDirIfDNE(path, options) {
|
|
285
|
-
|
|
286
|
-
if (doesExist === true) return;
|
|
430
|
+
if (this.existsSync(path)) return;
|
|
287
431
|
else {
|
|
288
432
|
return this.mkdirSync(path, options);
|
|
289
433
|
}
|
|
@@ -329,14 +473,6 @@ var Fs = class extends MimeService {
|
|
|
329
473
|
);
|
|
330
474
|
}
|
|
331
475
|
}, "writeFileAsync");
|
|
332
|
-
dirExists(dir) {
|
|
333
|
-
const resolved = (0, import_path.relative)(this.cwd, dir);
|
|
334
|
-
try {
|
|
335
|
-
return import_fs.default.statSync(resolved).isDirectory();
|
|
336
|
-
} catch {
|
|
337
|
-
return false;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
476
|
async unlink(path) {
|
|
341
477
|
if (this.existsSync(path)) {
|
|
342
478
|
return await import_promises.default.unlink((0, import_path.relative)(this.cwd, path));
|
|
@@ -345,7 +481,7 @@ var Fs = class extends MimeService {
|
|
|
345
481
|
}
|
|
346
482
|
}
|
|
347
483
|
rmFile(filePath) {
|
|
348
|
-
if (this.
|
|
484
|
+
if (this.exists(filePath)) {
|
|
349
485
|
if (this.isRootPathTargeted(filePath)) {
|
|
350
486
|
import_fs.default.rmSync((0, import_path.resolve)((0, import_path.join)(this.cwd, filePath)));
|
|
351
487
|
} else import_fs.default.rmSync((0, import_path.resolve)(this.cwd, filePath));
|
|
@@ -353,7 +489,7 @@ var Fs = class extends MimeService {
|
|
|
353
489
|
}
|
|
354
490
|
async rmdir(dir, options) {
|
|
355
491
|
const resolved = (0, import_path.relative)(this.cwd, dir);
|
|
356
|
-
if (this.
|
|
492
|
+
if (this.exists(dir)) {
|
|
357
493
|
return import_promises.default.rmdir((0, import_path.relative)(this.cwd, dir), options);
|
|
358
494
|
} else {
|
|
359
495
|
throw new Error(`directory ${resolved} does not exist`);
|
|
@@ -361,7 +497,7 @@ var Fs = class extends MimeService {
|
|
|
361
497
|
}
|
|
362
498
|
rmDirSync(dir) {
|
|
363
499
|
const resolved = (0, import_path.resolve)(this.cwd, dir);
|
|
364
|
-
if (this.
|
|
500
|
+
if (this.exists(dir)) {
|
|
365
501
|
return import_fs.default.rm(resolved, { force: true, recursive: true }, (err) => {
|
|
366
502
|
if (err?.message) {
|
|
367
503
|
console.error(err?.message);
|
|
@@ -491,12 +627,18 @@ var Fs = class extends MimeService {
|
|
|
491
627
|
return;
|
|
492
628
|
}
|
|
493
629
|
const result = await this.assetToBufferView(inputUrl);
|
|
494
|
-
const
|
|
495
|
-
this.withWs(formattedPath, Buffer.from(
|
|
630
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
631
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
496
632
|
} catch (err) {
|
|
497
|
-
console.error(`[
|
|
633
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
498
634
|
}
|
|
499
635
|
}
|
|
636
|
+
/**
|
|
637
|
+
*
|
|
638
|
+
* @access package-private
|
|
639
|
+
*
|
|
640
|
+
* work in progress, for internal package maintainer use only
|
|
641
|
+
*/
|
|
500
642
|
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
501
643
|
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
502
644
|
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|