@d0paminedriven/fs 1.0.0 → 2.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/dist/{chunk-MFKSUNKN.js → chunk-J3NUWUIN.js} +123 -25
- package/dist/fs/index.cjs +121 -23
- package/dist/fs/index.d.cts +74 -10
- package/dist/fs/index.d.ts +74 -10
- package/dist/fs/index.js +1 -1
- package/dist/index.cjs +121 -23
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/package.json +9 -9
|
@@ -87,8 +87,9 @@ var import_dotenv_expand = __toESM(require_main(), 1);
|
|
|
87
87
|
import { execSync } from "child_process";
|
|
88
88
|
import fsSync from "fs";
|
|
89
89
|
import fsAsync from "fs/promises";
|
|
90
|
-
import { relative } from "path";
|
|
90
|
+
import { join, relative, resolve } from "path";
|
|
91
91
|
import * as dotenv from "dotenv";
|
|
92
|
+
import sharp from "sharp";
|
|
92
93
|
dotenv.config();
|
|
93
94
|
var Fs = class {
|
|
94
95
|
constructor(cwd) {
|
|
@@ -108,20 +109,20 @@ var Fs = class {
|
|
|
108
109
|
);
|
|
109
110
|
}
|
|
110
111
|
excludeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
111
|
-
const
|
|
112
|
+
const resolve2 = Object.fromEntries(
|
|
112
113
|
Object.entries(obj).map(([key, val]) => {
|
|
113
114
|
if (props.includes(key)) {
|
|
114
115
|
return ["omit", "omit"];
|
|
115
116
|
} else return [key, val];
|
|
116
117
|
}).filter(([t, _v]) => /omit/.test(t) === false)
|
|
117
118
|
);
|
|
118
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
119
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
119
120
|
}, "excludeTargeted");
|
|
120
121
|
includeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
121
|
-
const
|
|
122
|
+
const resolve2 = Object.fromEntries(
|
|
122
123
|
props.map((val) => this.extractTuple(obj, val))
|
|
123
124
|
);
|
|
124
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
125
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
125
126
|
}, "includeTargeted");
|
|
126
127
|
get myEnv() {
|
|
127
128
|
return dotenv.config({ processEnv: {} });
|
|
@@ -145,6 +146,7 @@ var Fs = class {
|
|
|
145
146
|
return { ...this.omitFields(targ, keys) };
|
|
146
147
|
}
|
|
147
148
|
exists(target) {
|
|
149
|
+
if (!/\//gm.test(target)) return fsSync.existsSync(resolve(join(this.cwd, target)));
|
|
148
150
|
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), target));
|
|
149
151
|
}
|
|
150
152
|
arrToArrOfArrs = /* @__PURE__ */ __name(({
|
|
@@ -152,7 +154,7 @@ var Fs = class {
|
|
|
152
154
|
arrOfArrsAggregator = Array.of(),
|
|
153
155
|
interval
|
|
154
156
|
}) => new Promise(
|
|
155
|
-
(
|
|
157
|
+
(resolve2, _reject) => resolve2(
|
|
156
158
|
((interval2) => {
|
|
157
159
|
for (let i = 0; i <= arrToFragment.length; i++) {
|
|
158
160
|
if ((i % interval2 === 0 || i === 0) && i <= arrToFragment.length) {
|
|
@@ -164,7 +166,7 @@ var Fs = class {
|
|
|
164
166
|
)
|
|
165
167
|
).then((_) => arrOfArrsAggregator), "arrToArrOfArrs");
|
|
166
168
|
wait(ms) {
|
|
167
|
-
return new Promise((
|
|
169
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
168
170
|
}
|
|
169
171
|
countsSorter = /* @__PURE__ */ __name(({
|
|
170
172
|
counter,
|
|
@@ -182,8 +184,15 @@ var Fs = class {
|
|
|
182
184
|
(v) => Buffer.isBuffer(v) ? Buffer.from(v).toString("utf-8") : v
|
|
183
185
|
);
|
|
184
186
|
}
|
|
187
|
+
isRootPathTargeted(path) {
|
|
188
|
+
if (/^(?:\.\/|\.|\/|root|\.\.\/|~\/\.?|cwd|\.\/\.)$/g.test(path)) {
|
|
189
|
+
return true;
|
|
190
|
+
} else return false;
|
|
191
|
+
}
|
|
185
192
|
readDir(path, options) {
|
|
186
|
-
|
|
193
|
+
if (this.isRootPathTargeted(path)) {
|
|
194
|
+
return this.handleBuffStrArrUnion(fsSync.readdirSync(resolve(join(this.cwd, "./")), options));
|
|
195
|
+
} else return this.handleBuffStrArrUnion(
|
|
187
196
|
fsSync.readdirSync(
|
|
188
197
|
relative(this.cwd ??= process.cwd(), path),
|
|
189
198
|
options
|
|
@@ -193,9 +202,7 @@ var Fs = class {
|
|
|
193
202
|
executeCommand = /* @__PURE__ */ __name(({
|
|
194
203
|
command,
|
|
195
204
|
...options
|
|
196
|
-
}) => Buffer.from(
|
|
197
|
-
Buffer.from(execSync(command, { ...options }).toJSON().data)
|
|
198
|
-
).toString("utf-8"), "executeCommand");
|
|
205
|
+
}) => Buffer.from(execSync(command, { ...options }).toJSON().data).toString("utf-8"), "executeCommand");
|
|
199
206
|
fileGenTimestamp(d) {
|
|
200
207
|
const date = d.toISOString();
|
|
201
208
|
return `/* file-autogenerated by @d0paminedriven/fs on ${date.split(/([T])/gm)?.[0]} at ${date.split(/([T])/gm)[2]?.split(/([Z])/gm)?.[0]} UTC */`;
|
|
@@ -207,6 +214,7 @@ var Fs = class {
|
|
|
207
214
|
return this.fileGenTimestamp(d).replace("/*", "<!--").replace("*/", "-->");
|
|
208
215
|
}
|
|
209
216
|
existsSync(path) {
|
|
217
|
+
if (!/\//gm.test(path)) return fsSync.existsSync(resolve(join(this.cwd, path)));
|
|
210
218
|
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), path));
|
|
211
219
|
}
|
|
212
220
|
pathHandler(path) {
|
|
@@ -269,18 +277,11 @@ var Fs = class {
|
|
|
269
277
|
}
|
|
270
278
|
}, "writeFileAsync");
|
|
271
279
|
fileToBuffer = /* @__PURE__ */ __name((path) => Buffer.from(
|
|
272
|
-
|
|
273
|
-
fsSync.readFileSync(relative(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
274
|
-
)
|
|
280
|
+
fsSync.readFileSync(relative(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
275
281
|
), "fileToBuffer");
|
|
276
282
|
dirContainsDir(readDir, targetDir, options) {
|
|
277
283
|
return this.readDir(readDir, options).filter((t) => t.split(".").length === 1).includes(targetDir);
|
|
278
284
|
}
|
|
279
|
-
parseJsonBuffer(p) {
|
|
280
|
-
return JSON.parse(
|
|
281
|
-
Buffer.from(Buffer.from(p).toJSON().data).toString("utf-8")
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
285
|
/* begin url */
|
|
285
286
|
isServerSide() {
|
|
286
287
|
return typeof window === "undefined";
|
|
@@ -317,11 +318,21 @@ var Fs = class {
|
|
|
317
318
|
};
|
|
318
319
|
}
|
|
319
320
|
/* end url */
|
|
321
|
+
get cjsVal() {
|
|
322
|
+
return ["application/node", "text/javascript"].reduce(
|
|
323
|
+
(union) => union
|
|
324
|
+
);
|
|
325
|
+
}
|
|
320
326
|
get gzVal() {
|
|
321
327
|
return ["application/x-gzip", "application/gzip"].reduce(
|
|
322
328
|
(union) => union
|
|
323
329
|
);
|
|
324
330
|
}
|
|
331
|
+
get jsVal() {
|
|
332
|
+
return ["application/node", "text/javascript"].reduce(
|
|
333
|
+
(union) => union
|
|
334
|
+
);
|
|
335
|
+
}
|
|
325
336
|
get objVal() {
|
|
326
337
|
return ["application/octet-stream", "text/plain", "model/obj"].reduce((union) => union);
|
|
327
338
|
}
|
|
@@ -340,8 +351,10 @@ var Fs = class {
|
|
|
340
351
|
return {
|
|
341
352
|
aac: "audio/aac",
|
|
342
353
|
abw: "application/x-abiword",
|
|
354
|
+
aces: "image/aces",
|
|
343
355
|
apng: "image/apng",
|
|
344
356
|
arc: "application/x-freearc",
|
|
357
|
+
avci: "image/avci",
|
|
345
358
|
avif: "image/avif",
|
|
346
359
|
avi: "video/x-msvideo",
|
|
347
360
|
azw: "application/vnd.amazon.ebook",
|
|
@@ -350,26 +363,31 @@ var Fs = class {
|
|
|
350
363
|
bz: "application/x-bzip",
|
|
351
364
|
bz2: "application/x-bzip2",
|
|
352
365
|
cda: "application/x-cdf",
|
|
353
|
-
cjs:
|
|
366
|
+
cjs: this.cjsVal,
|
|
354
367
|
csh: "application/x-csh",
|
|
355
368
|
css: "text/css",
|
|
356
369
|
csv: "text/csv",
|
|
357
370
|
doc: "application/msword",
|
|
358
371
|
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
372
|
+
dpx: "image/dpx",
|
|
373
|
+
emf: "image/emf",
|
|
359
374
|
eot: "application/vnd.ms-fontobject",
|
|
360
375
|
epub: "application/epub+zip",
|
|
361
376
|
gif: "image/gif",
|
|
362
377
|
glb: "model/gltf-binary",
|
|
363
378
|
gltf: "model/gltf+json",
|
|
364
379
|
gz: this.gzVal,
|
|
380
|
+
hjif: "haptics/hjif",
|
|
381
|
+
hmpg: "haptics/hmpg",
|
|
365
382
|
htm: "text/html",
|
|
366
383
|
html: "text/html",
|
|
367
384
|
ico: "image/vnd.microsoft.icon",
|
|
368
385
|
ics: "text/calendar",
|
|
386
|
+
ivs: "haptics/ivs",
|
|
369
387
|
jar: "application/java-archive",
|
|
370
388
|
jpeg: "image/jpeg",
|
|
371
389
|
jpg: "image/jpeg",
|
|
372
|
-
js:
|
|
390
|
+
js: this.jsVal,
|
|
373
391
|
json: "application/json",
|
|
374
392
|
jsonld: "application/ld+json",
|
|
375
393
|
m3u8: "application/vnd.apple.mpegurl",
|
|
@@ -406,13 +424,16 @@ var Fs = class {
|
|
|
406
424
|
rar: "application/vnd.rar",
|
|
407
425
|
rtf: "application/rtf",
|
|
408
426
|
sh: "application/x-sh",
|
|
427
|
+
sql: "application/sql",
|
|
409
428
|
svg: "image/svg+xml",
|
|
410
429
|
tar: "application/x-tar",
|
|
411
430
|
tif: "image/tiff",
|
|
412
431
|
tiff: "image/tiff",
|
|
432
|
+
toml: "application/toml",
|
|
413
433
|
ts: this.tsVal,
|
|
414
434
|
ttf: this.ttfVal,
|
|
415
435
|
txt: "text/plain",
|
|
436
|
+
usdz: "model/vnd.usdz+zip",
|
|
416
437
|
vsd: "application/vnd.visio",
|
|
417
438
|
vtt: "text/vtt",
|
|
418
439
|
wasm: "application/wasm",
|
|
@@ -426,6 +447,8 @@ var Fs = class {
|
|
|
426
447
|
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
427
448
|
xml: "application/xml",
|
|
428
449
|
xul: "application/vnd.mozilla.xul+xml",
|
|
450
|
+
yaml: "application/yaml",
|
|
451
|
+
yml: "application/yaml",
|
|
429
452
|
zip: this.zipVal,
|
|
430
453
|
"3gp": "video/3gpp",
|
|
431
454
|
"3g2": "video/3gpp2",
|
|
@@ -454,6 +477,12 @@ var Fs = class {
|
|
|
454
477
|
};
|
|
455
478
|
}
|
|
456
479
|
async assetToBufferView(path) {
|
|
480
|
+
const p = this.parseUrl(path);
|
|
481
|
+
const doesUrlPathContainFileExtension = /\./g.test(
|
|
482
|
+
p.pathname.split(/\//g).reverse()[0] ?? ""
|
|
483
|
+
);
|
|
484
|
+
if (doesUrlPathContainFileExtension === false)
|
|
485
|
+
console.warn(`no file extension detected in input url ${path}, attempting fetch`);
|
|
457
486
|
const response = await fetch(path);
|
|
458
487
|
if (!response.ok) {
|
|
459
488
|
throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
@@ -530,7 +559,7 @@ var Fs = class {
|
|
|
530
559
|
{
|
|
531
560
|
const sliceSize = 512;
|
|
532
561
|
const typeMatch = b64Data.match(
|
|
533
|
-
/^data:(image|application|video|text|model|
|
|
562
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i
|
|
534
563
|
);
|
|
535
564
|
const type = typeMatch?.[1];
|
|
536
565
|
if (!typeMatch) {
|
|
@@ -554,10 +583,19 @@ var Fs = class {
|
|
|
554
583
|
}
|
|
555
584
|
cleanDataUrl(props) {
|
|
556
585
|
return props.replace(
|
|
557
|
-
/^data:(image|application|video|text|font|model|audio)\/[A-Za-z0-9+-.]
|
|
586
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
587
|
+
""
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* public cleanDataUrl<const C extends string>(props: C) {
|
|
592
|
+
return props.replace(
|
|
593
|
+
/^data:(?:image|application|video|text|font|model|audio|haptics|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
558
594
|
""
|
|
559
595
|
);
|
|
560
596
|
}
|
|
597
|
+
|
|
598
|
+
*/
|
|
561
599
|
/**
|
|
562
600
|
*
|
|
563
601
|
* @param inputUrl remote url to fetch data from
|
|
@@ -577,7 +615,7 @@ var Fs = class {
|
|
|
577
615
|
const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
|
|
578
616
|
if (/\./g.test(formattedPath) === false) {
|
|
579
617
|
throw new Error(
|
|
580
|
-
"either add false as the third argument in `fetchRemoteWriteLocal`
|
|
618
|
+
"either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension) or provide an output path without a file extension"
|
|
581
619
|
);
|
|
582
620
|
}
|
|
583
621
|
this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
@@ -585,9 +623,69 @@ var Fs = class {
|
|
|
585
623
|
return console.error(err);
|
|
586
624
|
}
|
|
587
625
|
}
|
|
626
|
+
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
627
|
+
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
628
|
+
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
|
629
|
+
async imageTransform({
|
|
630
|
+
format,
|
|
631
|
+
target,
|
|
632
|
+
quality = 80,
|
|
633
|
+
tint,
|
|
634
|
+
resize
|
|
635
|
+
}) {
|
|
636
|
+
if (tint && !resize) {
|
|
637
|
+
return await sharp(target).toFormat(format, { quality }).tint(tint).toBuffer();
|
|
638
|
+
} else if (!tint && resize) {
|
|
639
|
+
return await sharp(target).toFormat(format, { quality }).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
640
|
+
} else if (tint && resize) {
|
|
641
|
+
return await sharp(target).toFormat(format, { quality }).tint(tint).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
642
|
+
} else return await sharp(target).toFormat(format, { quality }).toBuffer();
|
|
643
|
+
}
|
|
644
|
+
// public async convertImage<
|
|
645
|
+
// const L extends "local" | "remote",
|
|
646
|
+
// const F extends "webp" | "avif" | "jpg" | "png",
|
|
647
|
+
// const I extends string,
|
|
648
|
+
// const O extends string
|
|
649
|
+
// >(isLocal: L, inputUrl: I, outputPath: O, useDetectedExtension = true) {
|
|
650
|
+
// if (isLocal === "remote") {
|
|
651
|
+
// try {
|
|
652
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
653
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
654
|
+
// const imageBuffer = Buffer.from(cleanData, "base64");
|
|
655
|
+
// const s = this.imageTransform({format: "webp", target: imageBuffer, quality: 100, resize: {options: {width: 1.2, height: 1.2}}} )
|
|
656
|
+
// const formattedPath = useDetectedExtension
|
|
657
|
+
// ? `${outputPath}.${result.extension}`
|
|
658
|
+
// : outputPath;
|
|
659
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
660
|
+
// throw new Error(
|
|
661
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
662
|
+
// );
|
|
663
|
+
// }
|
|
664
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
665
|
+
// } catch (err) {
|
|
666
|
+
// return console.error(err);
|
|
667
|
+
// }
|
|
668
|
+
// } else {
|
|
669
|
+
// try {
|
|
670
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
671
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
672
|
+
// const formattedPath = useDetectedExtension
|
|
673
|
+
// ? `${outputPath}.${result.extension}`
|
|
674
|
+
// : outputPath;
|
|
675
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
676
|
+
// throw new Error(
|
|
677
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
678
|
+
// );
|
|
679
|
+
// }
|
|
680
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
681
|
+
// } catch (err) {
|
|
682
|
+
// return console.error(err);
|
|
683
|
+
// }
|
|
684
|
+
// }
|
|
685
|
+
// }
|
|
588
686
|
};
|
|
589
687
|
|
|
590
688
|
export {
|
|
591
689
|
Fs
|
|
592
690
|
};
|
|
593
|
-
//# sourceMappingURL=chunk-
|
|
691
|
+
//# sourceMappingURL=chunk-J3NUWUIN.js.map
|
package/dist/fs/index.cjs
CHANGED
|
@@ -130,6 +130,7 @@ var import_promises = __toESM(require("fs/promises"), 1);
|
|
|
130
130
|
var import_path = require("path");
|
|
131
131
|
var dotenv = __toESM(require("dotenv"), 1);
|
|
132
132
|
var import_dotenv_expand = __toESM(require_main(), 1);
|
|
133
|
+
var import_sharp = __toESM(require("sharp"), 1);
|
|
133
134
|
dotenv.config();
|
|
134
135
|
var Fs = class {
|
|
135
136
|
constructor(cwd) {
|
|
@@ -149,20 +150,20 @@ var Fs = class {
|
|
|
149
150
|
);
|
|
150
151
|
}
|
|
151
152
|
excludeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
152
|
-
const
|
|
153
|
+
const resolve2 = Object.fromEntries(
|
|
153
154
|
Object.entries(obj).map(([key, val]) => {
|
|
154
155
|
if (props.includes(key)) {
|
|
155
156
|
return ["omit", "omit"];
|
|
156
157
|
} else return [key, val];
|
|
157
158
|
}).filter(([t, _v]) => /omit/.test(t) === false)
|
|
158
159
|
);
|
|
159
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
160
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
160
161
|
}, "excludeTargeted");
|
|
161
162
|
includeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
162
|
-
const
|
|
163
|
+
const resolve2 = Object.fromEntries(
|
|
163
164
|
props.map((val) => this.extractTuple(obj, val))
|
|
164
165
|
);
|
|
165
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
166
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
166
167
|
}, "includeTargeted");
|
|
167
168
|
get myEnv() {
|
|
168
169
|
return dotenv.config({ processEnv: {} });
|
|
@@ -186,6 +187,7 @@ var Fs = class {
|
|
|
186
187
|
return { ...this.omitFields(targ, keys) };
|
|
187
188
|
}
|
|
188
189
|
exists(target) {
|
|
190
|
+
if (!/\//gm.test(target)) return import_fs.default.existsSync((0, import_path.resolve)((0, import_path.join)(this.cwd, target)));
|
|
189
191
|
return import_fs.default.existsSync((0, import_path.relative)(this.cwd ?? process.cwd(), target));
|
|
190
192
|
}
|
|
191
193
|
arrToArrOfArrs = /* @__PURE__ */ __name(({
|
|
@@ -193,7 +195,7 @@ var Fs = class {
|
|
|
193
195
|
arrOfArrsAggregator = Array.of(),
|
|
194
196
|
interval
|
|
195
197
|
}) => new Promise(
|
|
196
|
-
(
|
|
198
|
+
(resolve2, _reject) => resolve2(
|
|
197
199
|
((interval2) => {
|
|
198
200
|
for (let i = 0; i <= arrToFragment.length; i++) {
|
|
199
201
|
if ((i % interval2 === 0 || i === 0) && i <= arrToFragment.length) {
|
|
@@ -205,7 +207,7 @@ var Fs = class {
|
|
|
205
207
|
)
|
|
206
208
|
).then((_) => arrOfArrsAggregator), "arrToArrOfArrs");
|
|
207
209
|
wait(ms) {
|
|
208
|
-
return new Promise((
|
|
210
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
209
211
|
}
|
|
210
212
|
countsSorter = /* @__PURE__ */ __name(({
|
|
211
213
|
counter,
|
|
@@ -223,8 +225,15 @@ var Fs = class {
|
|
|
223
225
|
(v) => Buffer.isBuffer(v) ? Buffer.from(v).toString("utf-8") : v
|
|
224
226
|
);
|
|
225
227
|
}
|
|
228
|
+
isRootPathTargeted(path) {
|
|
229
|
+
if (/^(?:\.\/|\.|\/|root|\.\.\/|~\/\.?|cwd|\.\/\.)$/g.test(path)) {
|
|
230
|
+
return true;
|
|
231
|
+
} else return false;
|
|
232
|
+
}
|
|
226
233
|
readDir(path, options) {
|
|
227
|
-
|
|
234
|
+
if (this.isRootPathTargeted(path)) {
|
|
235
|
+
return this.handleBuffStrArrUnion(import_fs.default.readdirSync((0, import_path.resolve)((0, import_path.join)(this.cwd, "./")), options));
|
|
236
|
+
} else return this.handleBuffStrArrUnion(
|
|
228
237
|
import_fs.default.readdirSync(
|
|
229
238
|
(0, import_path.relative)(this.cwd ??= process.cwd(), path),
|
|
230
239
|
options
|
|
@@ -234,9 +243,7 @@ var Fs = class {
|
|
|
234
243
|
executeCommand = /* @__PURE__ */ __name(({
|
|
235
244
|
command,
|
|
236
245
|
...options
|
|
237
|
-
}) => Buffer.from(
|
|
238
|
-
Buffer.from((0, import_child_process.execSync)(command, { ...options }).toJSON().data)
|
|
239
|
-
).toString("utf-8"), "executeCommand");
|
|
246
|
+
}) => Buffer.from((0, import_child_process.execSync)(command, { ...options }).toJSON().data).toString("utf-8"), "executeCommand");
|
|
240
247
|
fileGenTimestamp(d) {
|
|
241
248
|
const date = d.toISOString();
|
|
242
249
|
return `/* file-autogenerated by @d0paminedriven/fs on ${date.split(/([T])/gm)?.[0]} at ${date.split(/([T])/gm)[2]?.split(/([Z])/gm)?.[0]} UTC */`;
|
|
@@ -248,6 +255,7 @@ var Fs = class {
|
|
|
248
255
|
return this.fileGenTimestamp(d).replace("/*", "<!--").replace("*/", "-->");
|
|
249
256
|
}
|
|
250
257
|
existsSync(path) {
|
|
258
|
+
if (!/\//gm.test(path)) return import_fs.default.existsSync((0, import_path.resolve)((0, import_path.join)(this.cwd, path)));
|
|
251
259
|
return import_fs.default.existsSync((0, import_path.relative)(this.cwd ?? process.cwd(), path));
|
|
252
260
|
}
|
|
253
261
|
pathHandler(path) {
|
|
@@ -310,18 +318,11 @@ var Fs = class {
|
|
|
310
318
|
}
|
|
311
319
|
}, "writeFileAsync");
|
|
312
320
|
fileToBuffer = /* @__PURE__ */ __name((path) => Buffer.from(
|
|
313
|
-
|
|
314
|
-
import_fs.default.readFileSync((0, import_path.relative)(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
315
|
-
)
|
|
321
|
+
import_fs.default.readFileSync((0, import_path.relative)(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
316
322
|
), "fileToBuffer");
|
|
317
323
|
dirContainsDir(readDir, targetDir, options) {
|
|
318
324
|
return this.readDir(readDir, options).filter((t) => t.split(".").length === 1).includes(targetDir);
|
|
319
325
|
}
|
|
320
|
-
parseJsonBuffer(p) {
|
|
321
|
-
return JSON.parse(
|
|
322
|
-
Buffer.from(Buffer.from(p).toJSON().data).toString("utf-8")
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
326
|
/* begin url */
|
|
326
327
|
isServerSide() {
|
|
327
328
|
return typeof window === "undefined";
|
|
@@ -358,11 +359,21 @@ var Fs = class {
|
|
|
358
359
|
};
|
|
359
360
|
}
|
|
360
361
|
/* end url */
|
|
362
|
+
get cjsVal() {
|
|
363
|
+
return ["application/node", "text/javascript"].reduce(
|
|
364
|
+
(union) => union
|
|
365
|
+
);
|
|
366
|
+
}
|
|
361
367
|
get gzVal() {
|
|
362
368
|
return ["application/x-gzip", "application/gzip"].reduce(
|
|
363
369
|
(union) => union
|
|
364
370
|
);
|
|
365
371
|
}
|
|
372
|
+
get jsVal() {
|
|
373
|
+
return ["application/node", "text/javascript"].reduce(
|
|
374
|
+
(union) => union
|
|
375
|
+
);
|
|
376
|
+
}
|
|
366
377
|
get objVal() {
|
|
367
378
|
return ["application/octet-stream", "text/plain", "model/obj"].reduce((union) => union);
|
|
368
379
|
}
|
|
@@ -381,8 +392,10 @@ var Fs = class {
|
|
|
381
392
|
return {
|
|
382
393
|
aac: "audio/aac",
|
|
383
394
|
abw: "application/x-abiword",
|
|
395
|
+
aces: "image/aces",
|
|
384
396
|
apng: "image/apng",
|
|
385
397
|
arc: "application/x-freearc",
|
|
398
|
+
avci: "image/avci",
|
|
386
399
|
avif: "image/avif",
|
|
387
400
|
avi: "video/x-msvideo",
|
|
388
401
|
azw: "application/vnd.amazon.ebook",
|
|
@@ -391,26 +404,31 @@ var Fs = class {
|
|
|
391
404
|
bz: "application/x-bzip",
|
|
392
405
|
bz2: "application/x-bzip2",
|
|
393
406
|
cda: "application/x-cdf",
|
|
394
|
-
cjs:
|
|
407
|
+
cjs: this.cjsVal,
|
|
395
408
|
csh: "application/x-csh",
|
|
396
409
|
css: "text/css",
|
|
397
410
|
csv: "text/csv",
|
|
398
411
|
doc: "application/msword",
|
|
399
412
|
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
413
|
+
dpx: "image/dpx",
|
|
414
|
+
emf: "image/emf",
|
|
400
415
|
eot: "application/vnd.ms-fontobject",
|
|
401
416
|
epub: "application/epub+zip",
|
|
402
417
|
gif: "image/gif",
|
|
403
418
|
glb: "model/gltf-binary",
|
|
404
419
|
gltf: "model/gltf+json",
|
|
405
420
|
gz: this.gzVal,
|
|
421
|
+
hjif: "haptics/hjif",
|
|
422
|
+
hmpg: "haptics/hmpg",
|
|
406
423
|
htm: "text/html",
|
|
407
424
|
html: "text/html",
|
|
408
425
|
ico: "image/vnd.microsoft.icon",
|
|
409
426
|
ics: "text/calendar",
|
|
427
|
+
ivs: "haptics/ivs",
|
|
410
428
|
jar: "application/java-archive",
|
|
411
429
|
jpeg: "image/jpeg",
|
|
412
430
|
jpg: "image/jpeg",
|
|
413
|
-
js:
|
|
431
|
+
js: this.jsVal,
|
|
414
432
|
json: "application/json",
|
|
415
433
|
jsonld: "application/ld+json",
|
|
416
434
|
m3u8: "application/vnd.apple.mpegurl",
|
|
@@ -447,13 +465,16 @@ var Fs = class {
|
|
|
447
465
|
rar: "application/vnd.rar",
|
|
448
466
|
rtf: "application/rtf",
|
|
449
467
|
sh: "application/x-sh",
|
|
468
|
+
sql: "application/sql",
|
|
450
469
|
svg: "image/svg+xml",
|
|
451
470
|
tar: "application/x-tar",
|
|
452
471
|
tif: "image/tiff",
|
|
453
472
|
tiff: "image/tiff",
|
|
473
|
+
toml: "application/toml",
|
|
454
474
|
ts: this.tsVal,
|
|
455
475
|
ttf: this.ttfVal,
|
|
456
476
|
txt: "text/plain",
|
|
477
|
+
usdz: "model/vnd.usdz+zip",
|
|
457
478
|
vsd: "application/vnd.visio",
|
|
458
479
|
vtt: "text/vtt",
|
|
459
480
|
wasm: "application/wasm",
|
|
@@ -467,6 +488,8 @@ var Fs = class {
|
|
|
467
488
|
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
468
489
|
xml: "application/xml",
|
|
469
490
|
xul: "application/vnd.mozilla.xul+xml",
|
|
491
|
+
yaml: "application/yaml",
|
|
492
|
+
yml: "application/yaml",
|
|
470
493
|
zip: this.zipVal,
|
|
471
494
|
"3gp": "video/3gpp",
|
|
472
495
|
"3g2": "video/3gpp2",
|
|
@@ -495,6 +518,12 @@ var Fs = class {
|
|
|
495
518
|
};
|
|
496
519
|
}
|
|
497
520
|
async assetToBufferView(path) {
|
|
521
|
+
const p = this.parseUrl(path);
|
|
522
|
+
const doesUrlPathContainFileExtension = /\./g.test(
|
|
523
|
+
p.pathname.split(/\//g).reverse()[0] ?? ""
|
|
524
|
+
);
|
|
525
|
+
if (doesUrlPathContainFileExtension === false)
|
|
526
|
+
console.warn(`no file extension detected in input url ${path}, attempting fetch`);
|
|
498
527
|
const response = await fetch(path);
|
|
499
528
|
if (!response.ok) {
|
|
500
529
|
throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
@@ -571,7 +600,7 @@ var Fs = class {
|
|
|
571
600
|
{
|
|
572
601
|
const sliceSize = 512;
|
|
573
602
|
const typeMatch = b64Data.match(
|
|
574
|
-
/^data:(image|application|video|text|model|
|
|
603
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i
|
|
575
604
|
);
|
|
576
605
|
const type = typeMatch?.[1];
|
|
577
606
|
if (!typeMatch) {
|
|
@@ -595,10 +624,19 @@ var Fs = class {
|
|
|
595
624
|
}
|
|
596
625
|
cleanDataUrl(props) {
|
|
597
626
|
return props.replace(
|
|
598
|
-
/^data:(image|application|video|text|font|model|audio)\/[A-Za-z0-9+-.]
|
|
627
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
628
|
+
""
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* public cleanDataUrl<const C extends string>(props: C) {
|
|
633
|
+
return props.replace(
|
|
634
|
+
/^data:(?:image|application|video|text|font|model|audio|haptics|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
599
635
|
""
|
|
600
636
|
);
|
|
601
637
|
}
|
|
638
|
+
|
|
639
|
+
*/
|
|
602
640
|
/**
|
|
603
641
|
*
|
|
604
642
|
* @param inputUrl remote url to fetch data from
|
|
@@ -618,7 +656,7 @@ var Fs = class {
|
|
|
618
656
|
const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
|
|
619
657
|
if (/\./g.test(formattedPath) === false) {
|
|
620
658
|
throw new Error(
|
|
621
|
-
"either add false as the third argument in `fetchRemoteWriteLocal`
|
|
659
|
+
"either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension) or provide an output path without a file extension"
|
|
622
660
|
);
|
|
623
661
|
}
|
|
624
662
|
this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
@@ -626,5 +664,65 @@ var Fs = class {
|
|
|
626
664
|
return console.error(err);
|
|
627
665
|
}
|
|
628
666
|
}
|
|
667
|
+
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
668
|
+
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
669
|
+
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
|
670
|
+
async imageTransform({
|
|
671
|
+
format,
|
|
672
|
+
target,
|
|
673
|
+
quality = 80,
|
|
674
|
+
tint,
|
|
675
|
+
resize
|
|
676
|
+
}) {
|
|
677
|
+
if (tint && !resize) {
|
|
678
|
+
return await (0, import_sharp.default)(target).toFormat(format, { quality }).tint(tint).toBuffer();
|
|
679
|
+
} else if (!tint && resize) {
|
|
680
|
+
return await (0, import_sharp.default)(target).toFormat(format, { quality }).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
681
|
+
} else if (tint && resize) {
|
|
682
|
+
return await (0, import_sharp.default)(target).toFormat(format, { quality }).tint(tint).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
683
|
+
} else return await (0, import_sharp.default)(target).toFormat(format, { quality }).toBuffer();
|
|
684
|
+
}
|
|
685
|
+
// public async convertImage<
|
|
686
|
+
// const L extends "local" | "remote",
|
|
687
|
+
// const F extends "webp" | "avif" | "jpg" | "png",
|
|
688
|
+
// const I extends string,
|
|
689
|
+
// const O extends string
|
|
690
|
+
// >(isLocal: L, inputUrl: I, outputPath: O, useDetectedExtension = true) {
|
|
691
|
+
// if (isLocal === "remote") {
|
|
692
|
+
// try {
|
|
693
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
694
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
695
|
+
// const imageBuffer = Buffer.from(cleanData, "base64");
|
|
696
|
+
// const s = this.imageTransform({format: "webp", target: imageBuffer, quality: 100, resize: {options: {width: 1.2, height: 1.2}}} )
|
|
697
|
+
// const formattedPath = useDetectedExtension
|
|
698
|
+
// ? `${outputPath}.${result.extension}`
|
|
699
|
+
// : outputPath;
|
|
700
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
701
|
+
// throw new Error(
|
|
702
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
703
|
+
// );
|
|
704
|
+
// }
|
|
705
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
706
|
+
// } catch (err) {
|
|
707
|
+
// return console.error(err);
|
|
708
|
+
// }
|
|
709
|
+
// } else {
|
|
710
|
+
// try {
|
|
711
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
712
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
713
|
+
// const formattedPath = useDetectedExtension
|
|
714
|
+
// ? `${outputPath}.${result.extension}`
|
|
715
|
+
// : outputPath;
|
|
716
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
717
|
+
// throw new Error(
|
|
718
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
719
|
+
// );
|
|
720
|
+
// }
|
|
721
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
722
|
+
// } catch (err) {
|
|
723
|
+
// return console.error(err);
|
|
724
|
+
// }
|
|
725
|
+
// }
|
|
726
|
+
// }
|
|
629
727
|
};
|
|
630
728
|
//# sourceMappingURL=index.cjs.map
|