shadcn
Version:
Add components to your apps.
3 lines • 3.13 kB
JavaScript
import {a}from'./chunk-BFNVF2P5.js';import {SyntaxKind}from'ts-morph';var O=async({sourceFile:s,config:N})=>{let p=N.iconLibrary;if(!p||!(p in a))return s;let f=p,m=a[f],g=[];for(let e of s.getDescendantsOfKind(SyntaxKind.JsxSelfClosingElement)){if(e.getTagNameNode()?.getText()!=="IconPlaceholder")continue;let c=e.getAttributes().find(r=>r.getKind()!==SyntaxKind.JsxAttribute?false:r.asKindOrThrow(SyntaxKind.JsxAttribute).getNameNode().getText()===f);if(!c)continue;let t=c.asKindOrThrow(SyntaxKind.JsxAttribute),n=t.getInitializer()?.getText().replace(/^["']|["']$/g,"");if(!n)continue;g.includes(n)||g.push(n);let d=m.usage.match(/<(\w+)([^>]*)\s*\/>/);t.remove();for(let r of e.getAttributes()){if(r.getKind()!==SyntaxKind.JsxAttribute)continue;let a$1=r.asKindOrThrow(SyntaxKind.JsxAttribute);a$1.getNameNode().getText()in a&&a$1.remove();}if(!d){e.getTagNameNode()?.replaceWithText(n);continue}let[,x,T]=d;if(x==="ICON"){let r=e.getAttributes().filter(a$1=>a$1.getKind()!==SyntaxKind.JsxAttribute?true:!(a$1.asKindOrThrow(SyntaxKind.JsxAttribute).getNameNode().getText()in a)).map(a=>a.getText()).join(" ");r.trim()?e.replaceWithText(`<${n} ${r} />`):e.getTagNameNode()?.replaceWithText(n);}else {let r=new Set(e.getAttributes().filter(o=>o.getKind()===SyntaxKind.JsxAttribute).map(o=>o.asKindOrThrow(SyntaxKind.JsxAttribute).getNameNode().getText())),A=T.replace(/\{ICON\}/g,`{${n}}`).trim().split(/\s+(?=\w+=)/).filter(o=>o).map(o=>{let h=o.split("=")[0];return h&&!r.has(h)?o:null}).filter(Boolean),b=e.getAttributes().filter(o=>o.getKind()!==SyntaxKind.JsxAttribute?true:!(o.asKindOrThrow(SyntaxKind.JsxAttribute).getNameNode().getText()in a)).map(o=>o.getText()).join(" "),I=[...A,b].filter(Boolean).join(" ");e.replaceWithText(`<${x} ${I} />`);}}for(let e of s.getImportDeclarations()??[])if(e.getModuleSpecifier()?.getText()?.includes("icon-placeholder")){let n=(e.getNamedImports()??[]).find(u=>u.getName()==="IconPlaceholder");n&&n.remove(),e.getNamedImports()?.length===0&&e.remove();}if(g.length>0){let e=m.import.split(`
`),c=[];for(let t of e){let n=t.match(/import\s+{([^}]+)}\s+from\s+['"]([^'"]+)['"]/);if(!n)continue;let[,u,d]=n,x=u.split(",").map(r=>r.trim()).map(r=>r==="ICON"?g.map(a=>({name:a})):{name:r}).flat(),T=s.addImportDeclaration({moduleSpecifier:d,namedImports:x});c.push(T);}if(!K(s))for(let t of c)t.replaceWithText(t.getText().replace(";",""));}return s};function K(s){return s.getImportDeclarations()?.[0]?.getText().endsWith(";")??false}var W=async({sourceFile:s,config:N})=>{let f=N.menuColor==="inverted"?"dark":"";for(let m of s.getDescendantsOfKind(SyntaxKind.JsxAttribute)){if(m.getNameNode().getText()!=="className")continue;let e=m.getInitializer();if(!e)continue;let c=e.getText();if(!c.includes("cn-menu-target"))continue;let t=c.replace(/cn-menu-target/g,f);f||(t=t.replace(/\s{2,}/g," "),t=t.replace(/"\s+/g,'"'),t=t.replace(/\s+"/g,'"'),t=t.replace(/,\s*""\s*,/g,","),t=t.replace(/\(\s*""\s*,/g,"("),t=t.replace(/,\s*""\s*\)/g,")")),m.setInitializer(t);}return s};export{O as a,W as b};//# sourceMappingURL=chunk-PMFK2ZSQ.js.map
//# sourceMappingURL=chunk-PMFK2ZSQ.js.map