@homedev/files 1.0.7 → 1.0.8
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/index.d.ts +21 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -37,12 +37,20 @@ export declare interface ImportOptions {
|
|
|
37
37
|
*/
|
|
38
38
|
export declare const loadFormat: <T = any>(fileName: string, options?: LoadOptions) => Promise<LoadResult<T>>;
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
export declare interface LoadFormatParser {
|
|
44
|
+
matching?: string[];
|
|
45
|
+
fn: (data: string) => any;
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
/**
|
|
41
49
|
* @public
|
|
42
50
|
*/
|
|
43
51
|
export declare interface LoadOptions {
|
|
44
52
|
dirs?: string[];
|
|
45
|
-
parsers?: Record<string,
|
|
53
|
+
parsers?: Record<string, LoadFormatParser>;
|
|
46
54
|
format?: string;
|
|
47
55
|
}
|
|
48
56
|
|
|
@@ -63,9 +71,20 @@ export declare interface Output {
|
|
|
63
71
|
|
|
64
72
|
export declare const writeFormat: (filePath: string, content: any, options?: WriteFormatOptions) => Promise<void>;
|
|
65
73
|
|
|
74
|
+
/**
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
export declare interface WriteFormatEncoder {
|
|
78
|
+
matching?: string[];
|
|
79
|
+
fn: (data: any) => Promise<string> | string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
66
85
|
export declare interface WriteFormatOptions {
|
|
67
86
|
format: string;
|
|
68
|
-
encoders?: Record<string,
|
|
87
|
+
encoders?: Record<string, WriteFormatEncoder>;
|
|
69
88
|
}
|
|
70
89
|
|
|
71
90
|
export declare const writeOutput: (o: Output, options?: WriteOutputOption) => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import d from"fs/promises";var l=async(r)=>{try{return await d.access(r,d.constants.R_OK),!0}catch{return!1}};import s from"path";var y=async(r,e)=>{let t=!s.isAbsolute(r)&&e?["",...e]:[""];for(let n of t){let o=s.join(n,r);if(await l(o))return s.resolve(o)}throw Error(`File not found: "${r}"`)};import E from"fs/promises";import O from"path";var K=async(r,e)=>{let t=await y(r,e?.dirs),n=await E.readFile(t,"utf-8"),o=O.dirname(t),a={text:{fn:(f)=>f,matching:["\\.txt$","\\.text$"]},json:{fn:JSON.parse,matching:["\\.json$"]},...e?.parsers},i=e?.format?a[e.format]:Object.values(a).find((f)=>f.matching?.some((F)=>new RegExp(F).test(r)));if(!i)throw Error(`Unsupported format for file "${r}" and no matching parser found`);return{content:await i.fn(n),fullPath:t,folderPath:o}};import $ from"fs/promises";var x=async(r,e,t)=>{let n={text:{fn:(a)=>a,matching:["\\.txt$","\\.text$"]},json:{fn:JSON.stringify,matching:["\\.json$"]},...t?.encoders},o=t?.format?n[t.format]:Object.values(n).find((a)=>a.matching?.some((i)=>new RegExp(i).test(r)));if(!o)throw Error(`Unsupported format for file "${r}" and no matching encoder found`);await $.writeFile(r,await o.fn(e))};import j from"fs/promises";var q=async(r,e)=>{if(typeof r==="string"&&!r.includes("*"))return await e.map(r,0,[]);let t=await Array.fromAsync(j.glob(r,{cwd:e.cwd})),n=e.filter?t.filter(e.filter):t;return await Promise.all(n.map(e.map))};import w from"fs/promises";import u from"path";var Q=async(r,e)=>{if(e?.targetDirectory&&e?.removeFirst)try{await w.rm(e?.targetDirectory,{recursive:!0,force:!0})}catch(t){throw Error(`Failed to clean the output directory: ${t.message}`)}for(let t of r)await v(t,e)},v=async(r,e)=>{let t=u.join(e?.targetDirectory??".",r.name),n=u.dirname(t);if(e?.classes){if(r.class!==void 0&&!e.classes.includes(r.class))return;if(e.classRequired&&r.class===void 0)throw Error(`Output "${r.name}" is missing class field`)}if(e?.writing?.(r)===!1)return;try{await w.mkdir(n,{recursive:!0})}catch(o){throw Error(`Failed to create target directory "${n}" for output "${t}": ${o.message}`)}try{await x(t,r.value,{format:r.type??"text"})}catch(o){throw Error(`Failed to write output "${t}": ${o.message}`)}};import A from"fs/promises";import g from"path";import b from"path";var m=(r)=>r?b.isAbsolute(r)?r:b.join(process.cwd(),r):process.cwd();var W=async(r,e)=>{let t=[];for(let n of r)try{let o=!g.isAbsolute(n)?g.join(m(e?.cwd),n):n;if(e?.filter?.(o)===!1)continue;let a=await import(o);if(e?.resolveDefault==="only"&&!a.default)throw Error(`Module ${n} does not have a default export`);let i=e?.resolveDefault?a.default??a:a,c=e?.map?await e.map(i,n):i;if(c!==void 0)t.push(c)}catch(o){if(e?.fail?.(n,o)===!1)continue;throw Error(`Failed to import module ${n}: ${o.message??o}`)}return t},p=async(r,e)=>{let t=[];for(let n of r){let o=(await Array.fromAsync(A.glob(n,{cwd:m(e?.cwd)}))).map((a)=>g.join(m(e?.cwd),a));t.push(...await W(o,e))}return t};export{Q as writeOutputs,v as writeOutput,x as writeFormat,y as locate,K as loadFormat,W as importList,p as importGlob,q as globMap,l as exists};
|