#!/usr/bin/env node
"use strict";const j=require("prompts"),s=require("fs-extra"),o=require("path"),_=require("lodash"),x=require("ejs"),C=require("minimist"),O=require("jiti");function _interopDefaultCompat(e){return e&&typeof e=="object"&&"default"in e?e.default:e}const j__default=_interopDefaultCompat(j),s__default=_interopDefaultCompat(s),o__default=_interopDefaultCompat(o),___default=_interopDefaultCompat(_),x__default=_interopDefaultCompat(x),C__default=_interopDefaultCompat(C),O__default=_interopDefaultCompat(O),E=O__default(__filename);j__default.override({cancelled:!0});const F=()=>{console.log("\u7528\u6237\u4E3B\u52A8\u9000\u51FA\u7A0B\u5E8F"),process.exit()};function N(e){return/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(e)}function P(e){return[".data.ts",".ejs"].some(d=>e.endsWith(d))}async function $(){const e=C__default(process.argv.slice(2),{alias:{template:["t"]}}),d=e._[0];if(N(d)){if(e.pkgName=d,e.template){const m={pkgName:d,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 b(m),process.exit()}}else(d||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());j__default.override(e);const f=await j__default([{type:"text",name:"pkgName",message:"\u8BF7\u8F93\u5165\u9879\u76EE\u7684\u540D\u79F0",validate:m=>N(m)?!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:m=>m===!1?"toggle":null,name:"pwa",message:"\u662F\u5426\u9700\u8981\u914D\u7F6E pwa\uFF1F",initial:!0,active:"yes",inactive:"no"}],{onCancel:F});await b(f)}async function A(e){await s__default.pathExists(e)&&((await s__default.readdir(e)).length===0||((await j__default({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:F})).overwrite?(s__default.removeSync(e),console.log(`\u5DF2\u8986\u76D6\u76EE\u5F55 ${e}`)):(console.log("\u64CD\u4F5C\u5DF2\u53D6\u6D88"),process.exit())))}async function b(e){const{pkgName:d,...f}=e,m=process.cwd(),D=o__default.join(__dirname,"../templates/base"),g=o__default.join(m,d);try{await A(g),s__default.removeSync(g),s__default.copySync(D,g),s__default.rename(o__default.join(g,"vite.config.ts.ejs"),o__default.join(g,"vite.config.ts"));const r={};let S=o__default.join(__dirname,"../templates");const v=async(i,y=0)=>{y===0&&(S=i);for(const p of s__default.readdirSync(i)){const n=o__default.join(i,p);if(s__default.statSync(n).isDirectory())await v(n,y+1);else{const c=n.replace(S,""),l=o__default.join(g,c);if(!s__default.existsSync(l)&&!P(n))s__default.ensureDirSync(o__default.dirname(l)),s__default.copyFileSync(n,l);else if(o__default.extname(n)===".json"){const a=s__default.readJSONSync(n),t=s__default.readJSONSync(l);for(const u of Object.keys(a))t[u]?t[u]instanceof Array?t[u]=___default.uniq([...t[u],...a[u]]):typeof t[u]=="object"&&typeof a[u]=="object"?t[u]={...t[u],...a[u]}:t[u]=a[u]:t[u]=a[u];s__default.writeJSONSync(l,t)}else if(n.endsWith(".data.ts")){const a=(await E(n)).default();if(r[c]&&r[c].data){const t={...r[c].data};for(const u of Object.keys(a))t[u]&&t[u]instanceof Array?t[u]=___default.uniq([...t[u],...a[u]]):t[u]=a[u];r[c].data=t}else r[c]={data:a};r[c].destPath=l.replace(".data",""),r[c].curPath=n}else if(n.endsWith(".ejs")){const a=s__default.readFileSync(n,"utf-8"),t=x__default.render(a,e);s__default.writeFileSync(l.replace(".ejs",""),t)}else if(n.endsWith(".d.ts")){const a=s__default.readFileSync(n,"utf-8"),t=s__default.readFileSync(l,"utf-8");s__default.writeFileSync(l,t+`
`+a)}else s__default.copyFileSync(n,l)}}};for(const i of Object.keys(f))if(f[i]instanceof Array){const y=o__default.join(__dirname,`../templates/${i}/base`);await v(y);const p=f[i];if(!p.length)continue;for(const n of p){const c=o__default.join(__dirname,`../templates/${i}/${n}`);await v(c)}}else if(f[i]){const y=o__default.join(__dirname,typeof f[i]=="string"?`../templates/${i}/${f[i]}`:`../templates/${i}`);await v(y)}for(const i of Object.keys(r)){const{data:y,destPath:p}=r[i],n=s__default.readFileSync(p,"utf-8"),c=x__default.render(n,y);s__default.writeFileSync(p,c)}console.log("\u590D\u5236\u6210\u529F")}catch(r){console.log("\u590D\u5236\u5931\u8D25",r)}}$().catch(e=>{console.log(e)});
