UNPKG

xzx-onion-cli

Version:
2 lines (1 loc) 6.42 kB
"use strict";var e=require("commander"),o=require("@inquirer/prompts"),n=require("simple-git"),i=require("progress-estimator"),t=require("chalk"),r=require("log-symbols"),c=require("node:path"),s=require("fs-extra"),l=require("axios"),a=require("lodash"),u=require("node:child_process"),d=require("ora");function p(e,o,n,i){return new(n||(n=Promise))((function(t,r){function c(e){try{l(i.next(e))}catch(e){r(e)}}function s(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var o;e.done?t(e.value):(o=e.value,o instanceof n?o:new n((function(e){e(o)}))).then(c,s)}l((i=i.apply(e,o||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;var m="0.1.11";const h=e=>{console.log(r.success,e)},g=e=>{console.log(r.error,e)},v=e=>{console.log(r.info,e)},f=require("figlet"),b=i({spinner:{interval:100,frames:["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"].map((e=>t.green(e)))}}),x={baseDir:process.cwd(),maxConcurrentProcesses:6,binary:"git"};function y(e,o,i){return p(this,void 0,void 0,(function*(){const r=n(x);try{yield b(r.clone(e,o,i),"代码下载中...",{estimate:1e4}),p(void 0,void 0,void 0,(function*(){const e=yield f("xzx-onion-cli");console.log(t.greenBright(`${e}`))})),console.log(t.blackBright("====================================")),console.log(t.blackBright("========欢迎使用 onion-cli 脚手架=====")),console.log(t.blackBright("====================================")),console.log(),h(`项目创建成功 ${t.blueBright(o)}`),h("执行一下命令启动项目"),v(`cd ${t.blueBright(o)}`),v(`${t.yellow("pnpm")} install`),v(`${t.yellow("pnpm")} run dev`)}catch(e){g(t.red("代码下载失败:",e))}}))}function w(e){return p(this,void 0,void 0,(function*(){var n;try{return yield o.select(e)}catch(e){throw(null===(n=null==e?void 0:e.message)||void 0===n?void 0:n.includes("User force closed"))&&(console.log("\n👋 感谢使用 onion-cli"),process.exit(0)),e}}))}function k(e){return p(this,void 0,void 0,(function*(){var n;try{return yield o.input(e)}catch(e){throw(null===(n=null==e?void 0:e.message)||void 0===n?void 0:n.includes("User force closed"))&&(console.log("\n👋 感谢使用 onion-cli"),process.exit(0)),e}}))}function q(e){return p(this,void 0,void 0,(function*(){return console.warn(`${e} 已存在,是否覆盖?`),yield w({message:"是否覆盖?",choices:[{name:"是",value:!0},{name:"否",value:!1}]})}))}process.on("SIGINT",(()=>{console.log("\n👋 感谢使用 onion-cli"),process.exit(0)}));const U=new Map([["vue3-Ts-web-page-template",{name:"web-template",downloadUrl:"https://gitee.com/xuzhenxin110/onion-vue-template.git",backupUrl:"https://github.com/Teernage/onion-vue-template.git",description:"Vue3技术栈开发web项目",branch:"feature_xzx_web_page"}],["vue3-Ts-chrome-newtab-extensions-template",{name:"chrome-newtab-template",downloadUrl:"https://gitee.com/xuzhenxin110/onion-vue-template.git",backupUrl:"https://github.com/Teernage/onion-vue-template.git",description:"Vue3技术栈开发chrome标签页模板",branch:"main"}],["vue3-Ts-chrome-sidebar-extensions-template",{name:"chrome-sidebar-template",downloadUrl:"https://gitee.com/xuzhenxin110/onion-vue-template.git",backupUrl:"https://github.com/Teernage/onion-vue-template.git",description:"Vue3技术栈开发chrome侧边栏模板",branch:"feature_xzx_chrome_sidebar_extension"}],["vue3-Ts-chrome-popup-extensions-template",{name:"chrome-popup-template",downloadUrl:"https://gitee.com/xuzhenxin110/onion-vue-template.git",backupUrl:"https://github.com/Teernage/onion-vue-template.git",description:"Vue3技术栈开发chrome弹窗模板",branch:"feature_xzx_chrome_popup_extension"}]]);function z(e){return p(this,void 0,void 0,(function*(){const{data:o}=yield function(e){return p(this,void 0,void 0,(function*(){const o=`https://registry.npmmirror.com/${e}`;let n={};try{n=yield l.get(o)}catch(e){console.error(e)}return n}))}(e);return o["dist-tags"].latest}))}function T(e,o){return p(this,void 0,void 0,(function*(){const n=yield z(e),i=a.gt(n,o);return i&&(console.warn(`发现当前脚手架的最新版本为${t.blackBright(n)},当前版本为${t.blackBright(o)}`),console.log(`可使用${t.yellow("npm install xzx-onion-cli@latest -g")},或者使用:${t.yellow("onion update")}指令更新!`)),i}))}const _=d({text:t.green("xzx-onion-cli 正在更新..."),spinner:{interval:100,frames:["⠋","⠙","⠹","⠸","⠼","⠴","⠦","⠧","⠇","⠏"].map((e=>t.blue(e)))}});const $=new e.Command("onion");function B(){console.log("\n👋 感谢使用 onion-cli"),process.exit(0)}process.on("SIGINT",B),process.on("SIGTERM",B),process.on("SIGQUIT",B),$.version(m,"-v, --version"),$.command("update").description("更新脚手架 xzx-onion 版本").action((()=>p(void 0,void 0,void 0,(function*(){yield(_.start(),void u.exec("npm install xzx-onion-cli@latest -g",(e=>{_.stop(),e?console.log(t.red(e)):console.log(t.green("更新成功"))})))})))),$.command("create").description("创建一个新的项目").argument("[name]","项目名称").action((e=>p(void 0,void 0,void 0,(function*(){!function(e){p(this,void 0,void 0,(function*(){var o;try{let o=1,n=e||"",i=!1;for(;;)if(1!==o)if(2!==o){if(3===o){const e=Array.from(U).filter((([e])=>e.includes("chrome"))).map((([e,o])=>({name:o.name,value:e,description:o.description})));e.push({name:"返回上一步",value:"back",description:"返回项目类型选择"});const i=yield w({message:"请选择Chrome插件类型",choices:e});if("back"===i){o=2;continue}const t=U.get(i);if(!t)throw new Error("未找到对应的模板信息");yield y(t.downloadUrl,n,["-b",t.branch]);break}}else{const e=yield w({message:"请选择项目类型",choices:[{name:"Web项目",value:"web"},{name:"Chrome插件",value:"chrome"},{name:"返回上一步",value:"back"}]});if("back"===e){o=1,n="";continue}if("web"===e){const e="vue3-Ts-web-page-template",o=U.get(e);if(!o)throw new Error("未找到对应的模板信息");yield y(o.downloadUrl,n,["-b",o.branch]);break}o=3}else{n||(n=yield k({message:"请输入项目名称",default:e}),i=!0);const t=c.resolve(process.cwd(),n);s.existsSync(t)&&((yield q(n))?yield s.remove(t):process.exit(0)),yield T("xzx-onion-cli",m),o=2}}catch(e){(null===(o=null==e?void 0:e.message)||void 0===o?void 0:o.includes("User force closed"))&&(console.log("\n👋 感谢使用 onion-cli"),process.exit(0)),console.error(t.red("❌ 发生错误:"),e.message),process.exit(1)}}))}(e)})))),$.parse();