UNPKG

autofold

Version:

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

17 lines (14 loc) 1.74 kB
#!/usr/bin/env bun // @bun import{a as M,b as Q,c as R,d as V,e as W,f as X}from"./chunk-hkghdms7.js";import{existsSync as Y}from"fs";var K=` Usage: autofold [-f <format>] <tree-string-or-file> Options: -f <format> Specify input format: tree, nested, flat, segments, paths (default: tree) Examples: autofold ./tree.txt autofold -f tree "/autofold\\n\u251C\u2500\u2500 packages\\n\u2502 \u251C\u2500\u2500 core" autofold -f nested '{"autofold": {".bunfig.toml": null, "packages": {}}}' autofold -f flat ./flat.json `,C=(k)=>{try{return JSON.parse(k)}catch(q){console.error("\u274C Input format malformed")}};async function Z(){let k="tree",q,z=Bun.argv.slice(2);if(z[0]==="--help")console.log(K),process.exit(0);if(z[0]==="-f"&&z.length>=2)k=z[1],q=z[2];else if(z.length>=1)q=z[0];else console.log(K),console.error("\u274C Error: Missing input argument"),process.exit(1);let I=["tree","nested","flat","segments","compact","annotated"];if(!I.includes(k))console.error(`\u274C Error: Invalid format '${k}'. Supported formats: ${I.join(", ")}`),process.exit(1);let B;if(q&&Y(q))B=await Bun.file(q).text();else if(q)B=q;else console.error("\u274C Error: No input provided"),process.exit(1);let w;try{switch(k){case"tree":w=M(B);break;case"nested":w=Q(C(B));break;case"flat":w=R(C(B));break;case"segments":w=V(C(B));break;case"paths":w=W(C(B));break;default:throw new Error("Unexpected format")}}catch($){console.error(`\u274C Error parsing input as ${k}:`),process.exit(1)}if(!w||w.length===0)console.error("\u274C Error: No valid nodes parsed from input"),process.exit(1);let G=w[0],_=G.name.startsWith("/")?G.name.slice(1):"output";await X(G,".")}Z().catch((k)=>{console.error("\u274C Error:",k),process.exit(1)});