#!/usr/bin/env node
import g from"prompts";import t from"fs-extra";import c from"path";import v from"lodash";import S from"ejs";import h from"minimist";import _ from"jiti";const k=_(__filename);g.override({cancelled:!0});const A=()=>{console.log("\u7528\u6237\u4E3B\u52A8\u9000\u51FA\u7A0B\u5E8F"),process.exit()};function w(e){return/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(e)}function B(e){return[".data.ts",".ejs"].some(F=>e.endsWith(F))}async function b(){const e=h(process.argv.slice(2),{alias:{template:["t"]}}),F=e._[0];if(w(F)){if(e.pkgName=F,e.template){const y={pkgName:F,config:["router","layout"],css:e.css||"unocss",electron:e.electron!==void 0?e.electron:!1,pwa:e.pwa!==void 0&&!e.electron?e.pwa:!1,cdn:e.cdn!==void 0?e.cdn:!1};await D(y),process.exit()}}else(F||Object.keys(e).length>0)&&(console.log("CLI\u53C2\u6570\u6709\u8BEF\uFF0C\u8BF7\u91CD\u65B0\u6267\u884CCLI\uFF0C\u5E76\u6838\u5BF9\u53C2\u6570\uFF0C\u6216\u8005\u76F4\u63A5\u4F7F\u7528CLI\u7684\u4EA4\u4E92\u547D\u4EE4\u521D\u59CB\u5316\uFF01"),process.exit());g.override(e);const m=await g([{type:"text",name:"pkgName",message:"\u8BF7\u8F93\u5165\u9879\u76EE\u7684\u540D\u79F0",validate:y=>w(y)?!0:"\u9879\u76EE\u540D\u79F0\u4E0D\u7B26\u5408npm\u5305\u89C4\u5219\uFF0C\u5EFA\u8BAE\u8BF7\u4F7F\u7528\u82F1\u6587\u5C0F\u5199\u52A0\u77ED\u6A2A\u7EBF"},{type:"multiselect",name:"config",message:"\u9009\u62E9\u9879\u76EE\u7684\u57FA\u7840\u914D\u7F6E\u6A21\u5757",choices:[{title:"unplugin-vue-router\uFF0C\u81EA\u52A8\u8DEF\u7531",value:"router"},{title:"vite-plugin-vue-layouts\uFF0C\u5168\u5C40layout\u5E03\u5C40\u6CE8\u518C\u7EC4\u4EF6",value:"layout"},{title:"vite-plugin-mock\uFF0Cmock\u63A5\u53E3",value:"mock"}],hint:"- \u7A7A\u683C\u7528\u4E8E \u9009\u62E9/\u53D6\u6D88\u9009\u62E9 \u5207\u6362\uFF0C\u56DE\u8F66\u7528\u4E8E\u786E\u8BA4\u9009\u62E9"},{type:"select",name:"css",message:"\u8BF7\u9009\u62E9\u4E00\u4E2A css Framework\uFF1A",choices:[{title:"unocss",description:"UnoCSS - Instant On-demand Atomic CSS Engine",value:"unocss"},{title:"tailwind",value:"tailwind",description:"TailWind CSS - A utility-first CSS framework"}],initial:0},{type:"toggle",name:"cdn",message:"\u662F\u5426\u9700\u8981\u914D\u7F6E cdn\u52A0\u901F\uFF1F",initial:!0,active:"yes",inactive:"no"},{type:"toggle",name:"electron",message:"\u662F\u5426\u9700\u8981\u914D\u7F6E electron\uFF1F",initial:!1,active:"yes",inactive:"no"},{type:y=>y===!1?"toggle":null,name:"pwa",message:"\u662F\u5426\u9700\u8981\u914D\u7F6E pwa\uFF1F",initial:!0,active:"yes",inactive:"no"}],{onCancel:A});await D(m)}async function O(e){await t.pathExists(e)&&((await t.readdir(e)).length===0||((await g({type:"toggle",name:"overwrite",message:"\u76EE\u6807\u76EE\u5F55\u5DF2\u5B58\u5728\u4E14\u4E0D\u4E3A\u7A7A\uFF0C\u662F\u5426\u8981\u8986\u76D6?",initial:!1,active:"yes",inactive:"no"},{onCancel:A})).overwrite?(t.removeSync(e),console.log(`\u5DF2\u8986\u76D6\u76EE\u5F55 ${e}`)):(console.log("\u64CD\u4F5C\u5DF2\u53D6\u6D88"),process.exit())))}async function D(e){const{pkgName:F,...m}=e,y=process.cwd(),j=c.join(__dirname,"../templates/base"),E=c.join(y,F);try{await O(E),t.removeSync(E),t.copySync(j,E),t.rename(c.join(E,"vite.config.ts.ejs"),c.join(E,"vite.config.ts"));const r={};let C=c.join(__dirname,"../templates");const p=async(o,d=0)=>{d===0&&(C=o);for(const f of t.readdirSync(o)){const i=c.join(o,f);if(t.statSync(i).isDirectory())await p(i,d+1);else{const s=i.replace(C,""),l=c.join(E,s);if(!t.existsSync(l)&&!B(i))t.ensureDirSync(c.dirname(l)),t.copyFileSync(i,l);else if(c.extname(i)===".json"){const a=t.readJSONSync(i),n=t.readJSONSync(l);for(const u of Object.keys(a))n[u]?n[u]instanceof Array?n[u]=v.uniq([...n[u],...a[u]]):typeof n[u]=="object"&&typeof a[u]=="object"?n[u]={...n[u],...a[u]}:n[u]=a[u]:n[u]=a[u];t.writeJSONSync(l,n)}else if(i.endsWith(".data.ts")){const a=(await k(i)).default();if(r[s]&&r[s].data){const n={...r[s].data};for(const u of Object.keys(a))n[u]&&n[u]instanceof Array?n[u]=v.uniq([...n[u],...a[u]]):n[u]=a[u];r[s].data=n}else r[s]={data:a};r[s].destPath=l.replace(".data",""),r[s].curPath=i}else if(i.endsWith(".ejs")){const a=t.readFileSync(i,"utf-8"),n=S.render(a,e);t.writeFileSync(l.replace(".ejs",""),n)}else if(i.endsWith(".d.ts")){const a=t.readFileSync(i,"utf-8"),n=t.readFileSync(l,"utf-8");t.writeFileSync(l,n+`
`+a)}else t.copyFileSync(i,l)}}};for(const o of Object.keys(m))if(m[o]instanceof Array){const d=c.join(__dirname,`../templates/${o}/base`);await p(d);const f=m[o];if(!f.length)continue;for(const i of f){const s=c.join(__dirname,`../templates/${o}/${i}`);await p(s)}}else if(m[o]){const d=c.join(__dirname,typeof m[o]=="string"?`../templates/${o}/${m[o]}`:`../templates/${o}`);await p(d)}for(const o of Object.keys(r)){const{data:d,destPath:f}=r[o],i=t.readFileSync(f,"utf-8"),s=S.render(i,d);t.writeFileSync(f,s)}console.log("\u590D\u5236\u6210\u529F")}catch(r){console.log("\u590D\u5236\u5931\u8D25",r)}}b().catch(e=>{console.log(e)});
