UNPKG

autofold

Version:

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

4 lines (3 loc) 3.8 kB
import{existsSync as _}from"fs";import{mkdir as $,writeFile as A}from"fs/promises";import Z from"path";var Y=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 T(z){if(!z||typeof z!=="string"||z.trim()==="")return[];let G=z.split(` `).map((D)=>D.replace(/\r$/,"")).map((D)=>{let J=D.search(/(?<!\/)\/\/|(?<!\w)#/);if(J!==-1)return D.slice(0,J).trimEnd();return D}).filter((D)=>D.trim()!==""),O=[],q=[];for(let D of G){let B=D.replace(/^([\u251C\u2514\u2502\u2500\s]*)/,(R)=>R.replace(/[\u251C\u2514\u2502\u2500]/g," ")).trim();if(!B)continue;let H=Math.floor(D.search(/[^\s\u2502\u251C\u2514\u2500]/)/4),K=B.includes("."),Q={type:K?"file":"folder",name:B.replace(/^\//,""),...K?{}:{children:[]}};while(q.length>0&&q[q.length-1].depth>=H)q.pop();if(q.length===0)O.push(Q);else{let R=q[q.length-1].node;if(R.children)R.children.push(Q)}if(!K)q.push({depth:H,node:Q})}return W(O),O}function v(z){if(!z||typeof z!=="object")return[];function G(q,D=""){let J=[];for(let[B,H]of Object.entries(q)){if(!B)continue;let K=H===null||Object.keys(H).length===0&&B.includes("."),Q={type:K?"file":"folder",name:B,...K?{}:{children:[]}};if(!K&&H&&typeof H==="object")Q.children=G(H,B);J.push(Q)}return J.sort((B,H)=>{if(B.type!==H.type)return B.type==="folder"?-1:1;return B.name.localeCompare(H.name)}),J}return G(z)}function x(z){if(!z||typeof z!=="object"||Object.keys(z).length===0)return[];let G=new Map,O=[];for(let q of Object.keys(z)){if(!q||typeof q!=="string")continue;let D=q.split("/");if(D.length===0)continue;let J="",B=null;for(let H=0;H<D.length;H++){let K=D[H];if(!K)continue;J=J?`${J}/${K}`:K;let Q=G.get(J);if(!Q){let R=H===D.length-1&&K.includes(".");if(Q={type:R?"file":"folder",name:K,...R?{}:{children:[]}},G.set(J,Q),H===0)O.push(Q);else if(B&&!!B.children)B.children.push(Q)}B=Q.type==="folder"?Q:null}}return W(O),O}function y(z){if(!z||z.length===0)return[];let G=new Map,O=[];for(let q of z){if(!q||q.length===0)continue;let D="",J=null;for(let B=0;B<q.length;B++){let H=q[B];if(!H)continue;D=D?`${D}/${H}`:H;let K=G.get(D);if(!K){let Q=B===q.length-1&&H.includes(".");if(K={type:Q?"file":"folder",name:H,...Q?{}:{children:[]}},G.set(D,K),B===0)O.push(K);else if(J&&!!J.children)J.children?.push(K)}J=K.type==="folder"?K:null}}return W(O),O}function S(z){if(!z||z.length===0)return[];let G={type:"folder",name:"",children:[]},O=[...z].sort();for(let q of O){if(!q||typeof q!=="string"||q.trim()==="")continue;let D=q.split("/").filter((B)=>B&&B.trim()!=="");if(D.length===0)continue;let J=G;for(let B=0;B<D.length;B++){let H=D[B],K=B===D.length-1,Q=E(H),R=K&&Q?"file":"folder",V=J.children?.find((U)=>U.name===H&&U.type===R);if(!V){if(V={type:R,name:H},R==="folder")V.children=[];if(!J.children)J.children=[];J.children.push(V),J.children.sort((U,X)=>{if(U.type===X.type)return U.name.localeCompare(X.name);return U.type==="folder"?-1:1})}J=V}}if(G.children?.length===1&&G.children[0].type==="folder")return[G.children[0]];if(G.children?.length===0)return[];return G.children||[]}function W(z){for(let G of z)if(G.type==="folder"&&G.children)G.children.sort((O,q)=>{if(O.type!==q.type)return O.type==="folder"?-1:1;return O.name.localeCompare(q.name)}),W(G.children)}function E(z){for(let G of Y)if(z.endsWith(G))return!0;return!1}async function I(z,G=".",O=""){let q=Z.join(G,O,z.name);if(z.type==="file")await A(q,"","utf8"),console.log(`\uD83D\uDCDD Created file: ${q}`);else{if(!_(q))await $(q,{recursive:!0}),console.log(`\uD83D\uDCC1 Created directory: ${q}`);if(z.children)for(let D of z.children)await I(D,G,Z.join(O,z.name))}} export{T as a,v as b,x as c,y as d,S as e,I as f};