@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 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, (data: string) => any>;
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, (data: string) => Promise<string> | 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 g from"fs/promises";var l=async(e)=>{try{return await g.access(e,g.constants.R_OK),!0}catch{return!1}};import f from"path";var d=async(e,r)=>{let t=!f.isAbsolute(e)&&r?["",...r]:[""];for(let n of t){let o=f.join(n,e);if(await l(o))return f.resolve(o)}throw Error(`File not found: "${e}"`)};import F from"fs/promises";import y from"path";var G=async(e,r)=>{let t=await d(e,r?.dirs),n=await F.readFile(t,"utf-8"),o=y.dirname(t),i={text:(O)=>O,json:JSON.parse,...r?.parsers}[r?.format??y.extname(e).slice(1)];if(!i)throw Error(`Unhandled file type: ${e}`);return{content:i(n),fullPath:t,folderPath:o}};import A from"fs/promises";var u=async(e,r,t)=>{let n={text:(i)=>i,json:JSON.stringify,...t?.encoders},o=t?.format??"text",s=n[o];if(!s)throw Error(`Unsupported format: ${o}`);await A.writeFile(e,await s(r))};import v from"fs/promises";var U=async(e,r)=>{if(typeof e==="string"&&!e.includes("*"))return await r.map(e,0,[]);let t=await Array.fromAsync(v.glob(e,{cwd:r.cwd})),n=r.filter?t.filter(r.filter):t;return await Promise.all(n.map(r.map))};import w from"fs/promises";import x from"path";var H=async(e,r)=>{if(r?.targetDirectory&&r?.removeFirst)try{await w.rm(r?.targetDirectory,{recursive:!0,force:!0})}catch(t){throw Error(`Failed to clean the output directory: ${t.message}`)}for(let t of e)await E(t,r)},E=async(e,r)=>{let t=x.join(r?.targetDirectory??".",e.name),n=x.dirname(t);if(r?.classes){if(e.class!==void 0&&!r.classes.includes(e.class))return;if(r.classRequired&&e.class===void 0)throw Error(`Output "${e.name}" is missing class field`)}if(r?.writing?.(e)===!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 u(t,e.value,{format:e.type??"text"})}catch(o){throw Error(`Failed to write output "${t}": ${o.message}`)}};import $ from"fs/promises";import c from"path";import b from"path";var a=(e)=>e?b.isAbsolute(e)?e:b.join(process.cwd(),e):process.cwd();var j=async(e,r)=>{let t=[];for(let n of e)try{let o=!c.isAbsolute(n)?c.join(a(r?.cwd),n):n;if(r?.filter?.(o)===!1)continue;let s=await import(o);if(r?.resolveDefault==="only"&&!s.default)throw Error(`Module ${n} does not have a default export`);let i=r?.resolveDefault?s.default??s:s,m=r?.map?await r.map(i,n):i;if(m!==void 0)t.push(m)}catch(o){if(r?.fail?.(n,o)===!1)continue;throw Error(`Failed to import module ${n}: ${o.message??o}`)}return t},p=async(e,r)=>{let t=[];for(let n of e){let o=(await Array.fromAsync($.glob(n,{cwd:a(r?.cwd)}))).map((s)=>c.join(a(r?.cwd),s));t.push(...await j(o,r))}return t};export{H as writeOutputs,E as writeOutput,u as writeFormat,d as locate,G as loadFormat,j as importList,p as importGlob,U as globMap,l as exists};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@homedev/files",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "files utilities",
5
5
  "author": "julzor",
6
6
  "license": "ISC",