@d0paminedriven/fs 5.0.0 → 6.0.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/README.md +105 -0
- package/dist/{chunk-HWF53VET.js → chunk-KZWE4E4I.js} +13 -7
- package/dist/fs/index.cjs +12 -6
- package/dist/fs/index.d.cts +6 -0
- package/dist/fs/index.d.ts +6 -0
- package/dist/fs/index.js +1 -1
- package/dist/index.cjs +12 -6
- package/dist/index.js +1 -1
- package/package.json +5 -5
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
|
+
---
|
|
@@ -69,16 +69,16 @@ var Fs = class extends MimeService {
|
|
|
69
69
|
existsSync(path) {
|
|
70
70
|
if (!/\//gm.test(path))
|
|
71
71
|
return fsSync.existsSync(resolve(join(this.cwd, path)));
|
|
72
|
-
return fsSync.existsSync(relative(this.cwd
|
|
72
|
+
return fsSync.existsSync(relative(this.cwd, path));
|
|
73
73
|
}
|
|
74
74
|
pathHandler(path) {
|
|
75
75
|
return /\//g.test(path) === true ? path.split(/([/])/gim).reverse().slice(2).reverse().join("") : path;
|
|
76
76
|
}
|
|
77
77
|
mkdirSync(path, options) {
|
|
78
|
-
return fsSync.mkdirSync(relative(this.cwd
|
|
78
|
+
return fsSync.mkdirSync(relative(this.cwd, path), options);
|
|
79
79
|
}
|
|
80
80
|
fileSizeMb(path) {
|
|
81
|
-
return fsSync.statSync(relative(this.cwd
|
|
81
|
+
return fsSync.statSync(relative(this.cwd, path)).size / (1024 * 1024);
|
|
82
82
|
}
|
|
83
83
|
generateDirIfDNE(path, options) {
|
|
84
84
|
const doesExist = this.existsSync(path);
|
|
@@ -290,12 +290,18 @@ var Fs = class extends MimeService {
|
|
|
290
290
|
return;
|
|
291
291
|
}
|
|
292
292
|
const result = await this.assetToBufferView(inputUrl);
|
|
293
|
-
const
|
|
294
|
-
this.withWs(formattedPath, Buffer.from(
|
|
293
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
294
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
295
295
|
} catch (err) {
|
|
296
|
-
console.error(`[
|
|
296
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
*
|
|
301
|
+
* @access package-private
|
|
302
|
+
*
|
|
303
|
+
* work in progress, for internal package maintainer use only
|
|
304
|
+
*/
|
|
299
305
|
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
300
306
|
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
301
307
|
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
|
@@ -319,4 +325,4 @@ var Fs = class extends MimeService {
|
|
|
319
325
|
export {
|
|
320
326
|
Fs
|
|
321
327
|
};
|
|
322
|
-
//# sourceMappingURL=chunk-
|
|
328
|
+
//# sourceMappingURL=chunk-KZWE4E4I.js.map
|
package/dist/fs/index.cjs
CHANGED
|
@@ -270,16 +270,16 @@ var Fs = class extends MimeService {
|
|
|
270
270
|
existsSync(path) {
|
|
271
271
|
if (!/\//gm.test(path))
|
|
272
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
|
|
273
|
+
return import_fs.default.existsSync((0, import_path.relative)(this.cwd, path));
|
|
274
274
|
}
|
|
275
275
|
pathHandler(path) {
|
|
276
276
|
return /\//g.test(path) === true ? path.split(/([/])/gim).reverse().slice(2).reverse().join("") : path;
|
|
277
277
|
}
|
|
278
278
|
mkdirSync(path, options) {
|
|
279
|
-
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd
|
|
279
|
+
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd, path), options);
|
|
280
280
|
}
|
|
281
281
|
fileSizeMb(path) {
|
|
282
|
-
return import_fs.default.statSync((0, import_path.relative)(this.cwd
|
|
282
|
+
return import_fs.default.statSync((0, import_path.relative)(this.cwd, path)).size / (1024 * 1024);
|
|
283
283
|
}
|
|
284
284
|
generateDirIfDNE(path, options) {
|
|
285
285
|
const doesExist = this.existsSync(path);
|
|
@@ -491,12 +491,18 @@ var Fs = class extends MimeService {
|
|
|
491
491
|
return;
|
|
492
492
|
}
|
|
493
493
|
const result = await this.assetToBufferView(inputUrl);
|
|
494
|
-
const
|
|
495
|
-
this.withWs(formattedPath, Buffer.from(
|
|
494
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
495
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
496
496
|
} catch (err) {
|
|
497
|
-
console.error(`[
|
|
497
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
498
498
|
}
|
|
499
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
*
|
|
502
|
+
* @access package-private
|
|
503
|
+
*
|
|
504
|
+
* work in progress, for internal package maintainer use only
|
|
505
|
+
*/
|
|
500
506
|
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
501
507
|
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
502
508
|
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
package/dist/fs/index.d.cts
CHANGED
|
@@ -55,6 +55,12 @@ declare class Fs extends MimeService {
|
|
|
55
55
|
*/
|
|
56
56
|
fetchRemoteWriteLocal<const I extends string, const O extends string>(inputUrl: I, outputPath: O, useDetectedExtension?: boolean): Promise<void>;
|
|
57
57
|
fetchRemoteWriteLocalLargeFiles<const I extends string, const O extends string>(inputUrl: I, outputPath: O, useDetectedExtension?: boolean): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @access package-private
|
|
61
|
+
*
|
|
62
|
+
* work in progress, for internal package maintainer use only
|
|
63
|
+
*/
|
|
58
64
|
imageTransform<const F extends "webp" | "avif" | "jpg" | "png" | "tif" | "tiff" | "jp2" | "jpeg">({ format, target, quality, tint, resize }: {
|
|
59
65
|
format: F;
|
|
60
66
|
target: Buffer<ArrayBuffer>;
|
package/dist/fs/index.d.ts
CHANGED
|
@@ -55,6 +55,12 @@ declare class Fs extends MimeService {
|
|
|
55
55
|
*/
|
|
56
56
|
fetchRemoteWriteLocal<const I extends string, const O extends string>(inputUrl: I, outputPath: O, useDetectedExtension?: boolean): Promise<void>;
|
|
57
57
|
fetchRemoteWriteLocalLargeFiles<const I extends string, const O extends string>(inputUrl: I, outputPath: O, useDetectedExtension?: boolean): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
*
|
|
60
|
+
* @access package-private
|
|
61
|
+
*
|
|
62
|
+
* work in progress, for internal package maintainer use only
|
|
63
|
+
*/
|
|
58
64
|
imageTransform<const F extends "webp" | "avif" | "jpg" | "png" | "tif" | "tiff" | "jp2" | "jpeg">({ format, target, quality, tint, resize }: {
|
|
59
65
|
format: F;
|
|
60
66
|
target: Buffer<ArrayBuffer>;
|
package/dist/fs/index.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -276,16 +276,16 @@ var Fs = class extends MimeService {
|
|
|
276
276
|
existsSync(path) {
|
|
277
277
|
if (!/\//gm.test(path))
|
|
278
278
|
return import_fs.default.existsSync((0, import_path.resolve)((0, import_path.join)(this.cwd, path)));
|
|
279
|
-
return import_fs.default.existsSync((0, import_path.relative)(this.cwd
|
|
279
|
+
return import_fs.default.existsSync((0, import_path.relative)(this.cwd, path));
|
|
280
280
|
}
|
|
281
281
|
pathHandler(path) {
|
|
282
282
|
return /\//g.test(path) === true ? path.split(/([/])/gim).reverse().slice(2).reverse().join("") : path;
|
|
283
283
|
}
|
|
284
284
|
mkdirSync(path, options) {
|
|
285
|
-
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd
|
|
285
|
+
return import_fs.default.mkdirSync((0, import_path.relative)(this.cwd, path), options);
|
|
286
286
|
}
|
|
287
287
|
fileSizeMb(path) {
|
|
288
|
-
return import_fs.default.statSync((0, import_path.relative)(this.cwd
|
|
288
|
+
return import_fs.default.statSync((0, import_path.relative)(this.cwd, path)).size / (1024 * 1024);
|
|
289
289
|
}
|
|
290
290
|
generateDirIfDNE(path, options) {
|
|
291
291
|
const doesExist = this.existsSync(path);
|
|
@@ -497,12 +497,18 @@ var Fs = class extends MimeService {
|
|
|
497
497
|
return;
|
|
498
498
|
}
|
|
499
499
|
const result = await this.assetToBufferView(inputUrl);
|
|
500
|
-
const
|
|
501
|
-
this.withWs(formattedPath, Buffer.from(
|
|
500
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
501
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
502
502
|
} catch (err) {
|
|
503
|
-
console.error(`[
|
|
503
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
504
504
|
}
|
|
505
505
|
}
|
|
506
|
+
/**
|
|
507
|
+
*
|
|
508
|
+
* @access package-private
|
|
509
|
+
*
|
|
510
|
+
* work in progress, for internal package maintainer use only
|
|
511
|
+
*/
|
|
506
512
|
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
507
513
|
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
508
514
|
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d0paminedriven/fs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "convenient fs helpers",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -53,15 +53,15 @@
|
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@microsoft/api-extractor": "^7.52.8",
|
|
56
|
-
"@swc/core": "^1.11.
|
|
57
|
-
"@swc/wasm": "^1.11.
|
|
58
|
-
"@types/node": "^22.15.
|
|
56
|
+
"@swc/core": "^1.11.31",
|
|
57
|
+
"@swc/wasm": "^1.11.31",
|
|
58
|
+
"@types/node": "^22.15.30",
|
|
59
59
|
"chokidar": "^4.0.3",
|
|
60
60
|
"dotenv-cli": "^8.0.0",
|
|
61
61
|
"eslint": "^9.28.0",
|
|
62
62
|
"jiti": "^2.4.2",
|
|
63
63
|
"prettier": "^3.5.3",
|
|
64
|
-
"terser": "^5.
|
|
64
|
+
"terser": "^5.41.0",
|
|
65
65
|
"tslib": "latest",
|
|
66
66
|
"tsup": "latest",
|
|
67
67
|
"tsx": "latest",
|