UNPKG

autofold

Version:

A lightweight utility for creating directory structures and files from a tree-like string representation.

3 lines (2 loc) 4.43 kB
var M=Object.create;var{getPrototypeOf:j,defineProperty:W,getOwnPropertyNames:A,getOwnPropertyDescriptor:w}=Object,E=Object.prototype.hasOwnProperty;var C=(q,B,G)=>{G=q!=null?M(j(q)):{};let z=B||!q||!q.__esModule?W(G,"default",{value:q,enumerable:!0}):G;for(let D of A(q))if(!E.call(z,D))W(z,D,{get:()=>q[D],enumerable:!0});return z},$=new WeakMap,x=(q)=>{var B=$.get(q),G;if(B)return B;if(B=W({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function")A(q).map((z)=>!E.call(B,z)&&W(B,z,{get:()=>q[z],enumerable:!(G=w(q,z))||G.enumerable}));return $.set(q,B),B};var y=(q,B)=>{for(var G in B)W(q,G,{get:B[G],enumerable:!0,configurable:!0,set:(z)=>B[G]=()=>z})};var L=require("fs"),X=require("fs/promises"),Z=C(require("path"));var I=new Set([".js",".ts",".json",".toml",".txt",".md",".yml",".yaml",".css",".html",".xml",".csv",".log",".ini",".conf",".sh",".py",".java",".c",".cpp",".h",".hpp",".sql",".rb",".go",".php",".swift",".kt",".rs",".jsx",".tsx",".scss",".sass"]);function F(q){if(!q||typeof q!=="string"||q.trim()==="")return[];let B=q.split(` `).map((D)=>D.replace(/\r$/,"")).map((D)=>{let K=D.search(/(?<!\/)\/\/|(?<!\w)#/);if(K!==-1)return D.slice(0,K).trimEnd();return D}).filter((D)=>D.trim()!==""),G=[],z=[];for(let D of B){let H=D.replace(/^([\u251C\u2514\u2502\u2500\s]*)/,(R)=>R.replace(/[\u251C\u2514\u2502\u2500]/g," ")).trim();if(!H)continue;let J=Math.floor(D.search(/[^\s\u2502\u251C\u2514\u2500]/)/4),O=H.includes("."),Q={type:O?"file":"folder",name:H.replace(/^\//,""),...O?{}:{children:[]}};while(z.length>0&&z[z.length-1].depth>=J)z.pop();if(z.length===0)G.push(Q);else{let R=z[z.length-1].node;if(R.children)R.children.push(Q)}if(!O)z.push({depth:J,node:Q})}return Y(G),G}function g(q){if(!q||typeof q!=="object")return[];function B(z,D=""){let K=[];for(let[H,J]of Object.entries(z)){if(!H)continue;let O=J===null||Object.keys(J).length===0&&H.includes("."),Q={type:O?"file":"folder",name:H,...O?{}:{children:[]}};if(!O&&J&&typeof J==="object")Q.children=B(J,H);K.push(Q)}return K.sort((H,J)=>{if(H.type!==J.type)return H.type==="folder"?-1:1;return H.name.localeCompare(J.name)}),K}return B(q)}function b(q){if(!q||typeof q!=="object"||Object.keys(q).length===0)return[];let B=new Map,G=[];for(let z of Object.keys(q)){if(!z||typeof z!=="string")continue;let D=z.split("/");if(D.length===0)continue;let K="",H=null;for(let J=0;J<D.length;J++){let O=D[J];if(!O)continue;K=K?`${K}/${O}`:O;let Q=B.get(K);if(!Q){let R=J===D.length-1&&O.includes(".");if(Q={type:R?"file":"folder",name:O,...R?{}:{children:[]}},B.set(K,Q),J===0)G.push(Q);else if(H&&!!H.children)H.children.push(Q)}H=Q.type==="folder"?Q:null}}return Y(G),G}function P(q){if(!q||q.length===0)return[];let B=new Map,G=[];for(let z of q){if(!z||z.length===0)continue;let D="",K=null;for(let H=0;H<z.length;H++){let J=z[H];if(!J)continue;D=D?`${D}/${J}`:J;let O=B.get(D);if(!O){let Q=H===z.length-1&&J.includes(".");if(O={type:Q?"file":"folder",name:J,...Q?{}:{children:[]}},B.set(D,O),H===0)G.push(O);else if(K&&!!K.children)K.children?.push(O)}K=O.type==="folder"?O:null}}return Y(G),G}function N(q){if(!q||q.length===0)return[];let B={type:"folder",name:"",children:[]},G=[...q].sort();for(let z of G){if(!z||typeof z!=="string"||z.trim()==="")continue;let D=z.split("/").filter((H)=>H&&H.trim()!=="");if(D.length===0)continue;let K=B;for(let H=0;H<D.length;H++){let J=D[H],O=H===D.length-1,Q=T(J),R=O&&Q?"file":"folder",V=K.children?.find((U)=>U.name===J&&U.type===R);if(!V){if(V={type:R,name:J},R==="folder")V.children=[];if(!K.children)K.children=[];K.children.push(V),K.children.sort((U,_)=>{if(U.type===_.type)return U.name.localeCompare(_.name);return U.type==="folder"?-1:1})}K=V}}if(B.children?.length===1&&B.children[0].type==="folder")return[B.children[0]];if(B.children?.length===0)return[];return B.children||[]}function Y(q){for(let B of q)if(B.type==="folder"&&B.children)B.children.sort((G,z)=>{if(G.type!==z.type)return G.type==="folder"?-1:1;return G.name.localeCompare(z.name)}),Y(B.children)}function T(q){for(let B of I)if(q.endsWith(B))return!0;return!1}async function v(q,B=".",G=""){let z=Z.default.join(B,G,q.name);if(q.type==="file")await X.writeFile(z,"","utf8"),console.log(`\uD83D\uDCDD Created file: ${z}`);else{if(!L.existsSync(z))await X.mkdir(z,{recursive:!0}),console.log(`\uD83D\uDCC1 Created directory: ${z}`);if(q.children)for(let D of q.children)await v(D,B,Z.default.join(G,q.name))}}