cli-z-develop 0.0.33 → 0.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{program as e}from"commander";import t from"chalk";import i from"shelljs";import n from"fs-extra";import o from"minimist";import s from"node:fs";import r from"node:path";import a from"node:os";import c,{AxiosError as l}from"axios";import u from"dayjs";import p from"inquirer";import d from"inquirer/lib/prompts/base.js";import m from"inquirer/lib/utils/paginator.js";import f from"inquirer/lib/utils/events.js";import"inquirer/lib/objects/choices.js";import{map as h,takeUntil as g,filter as y}from"rxjs";import v from"figures";import b from"ora";import j from"child_process";import $ from"lint-staged";var w={name:"cli-z-develop",version:"0.0.33",description:"前端本地开发命令行工具",main:"dist/index.js",bin:{z:"./bin/z.js","z-develop":"./bin/z.js"},scripts:{prepare:"husky",dev:"rollup -c -w --environment DEBUG:true",build:"rollup -c",eslint:"eslint '**/*.{ts,js}' --fix",prettier:"prettier -wu .",upload:"npm version patch && npm run build && npm publish --access public --registry https://registry.npmjs.org/"},type:"module",repository:{type:"git",url:"http://git.cxlqd.com/fe-base/cli-z-develop.git"},author:"z",devDependencies:{"@commitlint/cli":"^19.3.0","@commitlint/config-conventional":"^19.2.2","@rollup/plugin-json":"^6.1.0","@rollup/plugin-node-resolve":"^15.2.3","@rollup/plugin-replace":"^5.0.7","@rollup/plugin-terser":"^0.4.4","@types/fs-extra":"^11.0.4","@types/inquirer":"^9.0.7","@types/minimist":"^1.2.5","@types/node":"^20.14.9","@types/shelljs":"^0.8.15","@types/tough-cookie":"^4.0.5","@typescript-eslint/eslint-plugin":"^7.14.1","@typescript-eslint/parser":"^7.14.1",eslint:"^8.57.0","eslint-config-alloy":"^5.1.2","eslint-config-prettier":"^9.1.0",husky:"^9.0.11",prettier:"^3.3.2",rollup:"^4.18.0","rollup-plugin-typescript2":"^0.36.0",tslib:"^2.6.3",typescript:"^5.5.2"},dependencies:{"@inquirer/prompts":"^5.1.0",axios:"^1.7.2",chalk:"^5.3.0",commander:"^12.1.0",dayjs:"^1.11.11",figures:"^6.1.0","fs-extra":"^11.2.0",inquirer:"9.3.1","lint-staged":"^15.2.7",minimist:"^1.2.8",ora:"^8.0.1",rxjs:"^7.8.1",shelljs:"^0.8.5"}};function k(e,t,i,n){return new(i||(i=Promise))((function(o,s){function r(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{red:x,green:S,blue:z,magenta:N}=t;function O(...e){console.log(S(...e))}function E(e,t=!1){let i=e;e instanceof Error?(i=e.message,c.isAxiosError(e)&&(i=`请求失败:${e.message}`),console.log(x(i)),console.log(N(e.stack))):console.log(x(e)),t||process.exit(1)}function T(e){return!!s.existsSync(e)&&s.lstatSync(e).isDirectory()}function q(e=process.cwd()){if(T(e)){return s.readdirSync(e).filter((t=>T(r.resolve(e,t))))}return[]}function D(e=process.cwd()){if(T(e)){return s.readdirSync(e).filter((t=>!T(r.resolve(e,t))))}return[]}function _(){return r.resolve(a.homedir(),".z")}function C(){return r.resolve(_(),"develop-config.json")}function P(){return s.existsSync(_())&&s.existsSync(C())}function I(e){return k(this,arguments,void 0,(function*(e,t={removeTailLinkBreak:!0,silent:!0}){let n=yield new Promise(((n,o)=>{try{n(i.exec(e,{silent:t.silent}))}catch(e){o(e)}}));return n=n.toString(),t.removeTailLinkBreak&&(n=n.replace(/\n$/,"")),n}))}function L(){return u(Date.now()).format("YYMMDD")}function U(){s.existsSync(r.resolve(".z"))||E("当前不在项目根目录。请切换到项目根目录")}function A(){s.existsSync(r.resolve(".git"))||E("当前不是git项目根目录,请先执行git init,或切换到根目录")}function J(){return k(this,void 0,void 0,(function*(){""!==(yield I("git status -s"))&&E("请先提交代码变动,再进行操作")}))}function B(e){return!["fe-tpl","fe-component"].includes(e)}const W={profile:{ldapAccount:"",ldapPassword:"",gitToken:"",gitUserId:0,gitName:"",gitEnglishName:"",gitEmail:"",weWorkName:"",weWorkUserId:"",jobType:"fe",zenTaoToken:""},main:{version:w.version,latestCheckVersionTimestamp:0,versionCheckDuring:3,weWorkListCache:[]},constants:{ZenTaoDomain:"",FEServerDomain:"",jenkinsDomain:"",jenkinsUsername:"",jenkinsPassword:"",OSSEndpoint:"",OSSAccessKeyID:"",OSSAccessKeySecret:"",OSSBucketName:""}};function R(e,t){void 0!==t?W.profile[e]=t:W.profile=Object.assign(Object.assign({},W.profile),e)}function K(e,t){W.constants=Object.assign(Object.assign({},W.constants),e)}function F(e,t){void 0!==t?W.main[e]=t:W.main=Object.assign(Object.assign({},W.main),e)}function M(e){return e?W.profile[e]:W.profile}function V(e){return e?W.main[e]:W.main}function H(e){return e?W.constants[e]:W.constants}const G='{\n "printWidth": 120,\n "tabWidth": 2,\n "useTabs": false,\n "semi": true,\n "singleQuote": false,\n "quoteProps": "as-needed",\n "jsxSingleQuote": false,\n "trailingComma": "all",\n "bracketSpacing": true,\n "bracketSameLine": false,\n "arrowParens": "always",\n "requirePragma": false,\n "insertPragma": false,\n "proseWrap": "preserve",\n "htmlWhitespaceSensitivity": "css",\n "vueIndentScriptAndStyle": false,\n "endOfLine": "lf",\n "embeddedLanguageFormatting": "auto",\n "singleAttributePerLine": false\n}\n',Q="# 系统 信息文件\n.DS_Store/\nThumbs.db\n\n# 编辑器配置文件\n.idea/\n.vscode/\n\n# 输出目录\ndist/\n\n# Log files\n*.log\n\n# 本地配置文件\n*.local\n\n# 缓存文件\n*.cache\n\n# 垃圾文件\n.Trashes\n",Z={Java:{"**/*.{java}":"./run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/quickstart.xml"},JavaScript:{"**/*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}":"eslint --fix","*":"prettier -wu"}},Y=[{name:"开发环境 - dev",value:"dev"},{name:"测试环境 - test",value:"test"},{name:"预发环境 - release",value:"release"},{name:"正式环境 - production",value:"production"}],X=[{name:"网页 - h5",value:"h5"},{name:"依赖包 - npm",value:"npm"}],ee=["main","master"],te=["dev","test","release"],ie=["fix","feat","refactor"],ne={1:"研发",2:"测试",3:"产品",4:"设计",5:"运营",6:"销售",7:"行政",8:"财务",9:"其他"},oe="http://git.cxlqd.com",se={PROJECT_NAME:"fe-pages-tpl",GIT_URL:`${oe}/fe-component/fe-pages-tpl.git`},re=["fe-biz","fe-base","fe-tpl","fe-component","fe-demo"];let ae=null,ce=null;function le(){if(ae)return ae;const e=r.resolve("package.json");try{ae=n.readJsonSync(e)}catch(t){E(`当前目录(${i.pwd()})不存在${e}文件,请在项目根目录执行该命令。`)}return ae}function ue(){if(ce)return ce;const e=r.resolve(".z","project.json");try{ce=n.readJsonSync(e)}catch(t){E(`当前目录(${i.pwd()})不存在${e}文件,请在项目根目录执行该命令,或者初始化项目(z init .)。`)}return ce}function pe(e){ce=ce?Object.assign(Object.assign({},ce),e):e,n.writeJSONSync(r.resolve(".z","project.json"),ce,{spaces:2})}const de={id:0,name:"",group:"",sourceBranch:"",mergeRequestUrl:""};function me(){return k(this,void 0,void 0,(function*(){if(!de.name){const e=le();de.name=e.name}if(!de.group){const e=ue();e.repository.group&&(de.group=e.repository.group)}if(!de.id)try{const e=de.group?`${de.group}/${de.name}`:de.name,[t]=yield Ne(e);de.mergeRequestUrl=t.web_url+"/merge_requests",de.id=t.id,de.group=t.namespace.path;const i=ue();i.repository.group||pe({repository:Object.assign(Object.assign({},i.repository),{group:t.namespace.path})})}catch(e){E(e)}return de.sourceBranch||(de.sourceBranch=yield I("git branch --show-current")),de}))}function fe(){n.writeJSONSync(C(),{main:V(),profile:M(),constants:H()},{spaces:2})}function he(e){return k(this,void 0,void 0,(function*(){const t=_(),n=process.cwd(),o=r.join(t,se.PROJECT_NAME);s.existsSync(o)?(i.cd(o),yield I("git pull"),i.cd(n)):(i.cd(t),yield I(`git clone ${se.GIT_URL} --depth=1`),i.cd(n));const a=r.join(o,"src",e);return s.existsSync(a)||E("模板库错误!"),a}))}function ge(e){return e.split("-").map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("")}function ye(e,t){const i=s.readFileSync(e,"utf-8"),n=t.replace(/\//g,"-"),o=i.replace(/name: "-_-NAME-_-"/g,`name: "${ge(n)}"`).replace(/class="-_-NAME-_-"/g,`class="${n}"`).replace(/.-_-NAME-_-/g,`.${n}`);s.writeFileSync(e,o,"utf-8")}function ve(e){return k(this,void 0,void 0,(function*(){var i;const n=e.method||"get",o=e.headers||{},s=e.data||{},r=e.dataKey||["GET","get"].includes(n)?"params":"data",a=e.auth||void 0;try{const i={url:e.url,method:n,[r]:s,headers:o,auth:a};t.magenta(i.method.toUpperCase()),t.yellow(i.url),JSON.stringify(i.headers),JSON.stringify(i.auth||{}),t.gray(JSON.stringify(i[r],null,2));const l=yield c(i);return t.green("Response"),t.grey(JSON.stringify(l.data,null,2)),Promise.resolve(l.data)}catch(e){return e instanceof l&&(t.red("Error"),t.grey(JSON.stringify(null===(i=null==e?void 0:e.response)||void 0===i?void 0:i.data))),Promise.reject(e)}}))}function be(){return k(this,void 0,void 0,(function*(){const e=M("gitToken");if(e)return e;{const{access_token:e}=yield ve({url:`${oe}/oauth/token`,method:"post",data:{grant_type:"password",username:M("ldapAccount"),password:M("ldapPassword")}}),t=`Bearer ${e}`;return R("gitToken",t),fe(),t}}))}function je(e){return k(this,void 0,void 0,(function*(){return ve(Object.assign(Object.assign({},e),{headers:{Authorization:yield be()}}))}))}function $e(){return k(this,void 0,void 0,(function*(){const e=M("zenTaoToken");if(e)return e;{const{token:e}=yield ve({url:`${H("ZenTaoDomain")}/api.php/v1/tokens`,method:"post",data:{account:M("ldapAccount"),password:M("ldapPassword")}});return R("zenTaoToken",e),fe(),e}}))}function we(e){return k(this,void 0,void 0,(function*(){const t=yield ve(Object.assign(Object.assign({},e),{headers:{Token:yield $e()}}));if("string"==typeof t){const e=JSON.parse(t.slice(0,t.indexOf("<script>")));return JSON.parse(e.data)}if("object"==typeof t)return"Unauthorized"===t.error||t.data.indexOf("<script>")<0?(R("zenTaoToken",""),we(Object.assign({},e))):t}))}const ke=()=>`${oe}/api/v4`,xe=e=>ke()+"/projects/100/repository/files/"+encodeURIComponent(e)+"/raw?ref=master";function Se(){return je({url:xe("src/data/template-projects.json")})}function ze(){return je({url:xe("src/data/z-develop-config.json")})}function Ne(e){return je({url:`${ke()}/projects`,data:{search:e,search_namespaces:!0}})}function Oe(e){return je({url:`${ke()}/projects/${e}/repository/branches`})}function Ee(e,i){let n="";return e.forEach(((e,o)=>{if(e.disabled)n=`${n} - ${e.name} (Disabled)`;else{n+=o===i?t.cyan(v.pointer)+" ":" ",n+=(e.checked||!1?t.green(v.radioOn):v.radioOff)+" "+e.name}n+="\n"})),n.replace(/\n$/,"")}class Te extends d{constructor(e,t,i){super(e,t,i),this.pointer=0,this.opt.choices||this.throwParamError("choices"),this.opt.choices.forEach(((e,t)=>{const i=e;i.checked=!1,i.id=t}));if(this.opt.choices.find((e=>"separator"===e.type)))throw new Error("Separator is not allowed in choices.");Array.isArray(this.opt.default)&&this.opt.choices.forEach((e=>{"separator"!==e.type&&this.opt.default.includes(e.value)&&(e.checked=!0)})),this.pointer=0,this.selection=[],this.done=e=>{console.log(e)},this.opt.default=null,this.paginator=new m(this.screen,{isInfinite:!0}),this.filterList=this.allList=this.opt.choices.choices}_run(e){this.done=e;const t=f(this.rl),i=this.handleSubmitEvents(t.line.pipe(h(this.getCurrentValue.bind(this))));i.success.forEach(this.onEnd.bind(this)),i.error.forEach(this.onError.bind(this)),t.normalizedUpKey.pipe(g(i.success)).forEach(this.onUpKey.bind(this)),t.normalizedDownKey.pipe(g(i.success)).forEach(this.onDownKey.bind(this)),t.spaceKey.pipe(g(i.success)).forEach(this.onSpaceKey.bind(this));const n=["up","down","space"];return t.keypress.pipe(y((e=>!e.key.ctrl&&!n.includes(e.key.name||"")))).pipe(g(i.success)).forEach(this.onKeyPress.bind(this)),this.render(),this}render(e){let i=this.getQuestion(),n="";if(this.getCurrentValue(),"answered"===this.status)i+=t.cyan(this.selection.join(","));else{this.selection.length?i+=t.magenta(`[已选: ${this.selection.join(",")}]`):i+="("+t.blue.bold("空格键")+"选择,"+t.blue.bold("回车键")+"提交,"+t.blue.bold("输入中文")+"搜索)",i+=t.bgYellowBright(t.black(this.rl.line));const e=Ee(this.filterList,this.pointer);n=this.paginator.paginate(e,this.pointer,this.opt.pageSize)}e&&(n=t.red(">> ")+e),this.screen.render(i,n)}getCurrentValue(){const e=this.allList.filter((e=>e.checked&&!e.disabled));return this.selection=e.map((e=>e.short)),e.map((e=>e.value))}toggleChoice(e){const t=this.filterList[e];t&&(this.allList[t.id].checked=!t.checked)}filterChoices(){this.filterList=this.allList.filter((e=>{var t;return null===(t=e.name)||void 0===t?void 0:t.includes(this.rl.line.trim())}))}onSpaceKey(){this.toggleChoice(this.pointer),this.render(),this.rl.resume()}onDownKey(){const e=this.filterList.length;this.pointer=this.pointer<e-1?this.pointer+1:0,this.render()}onUpKey(){const e=this.filterList.length;this.pointer=this.pointer>0?this.pointer-1:e-1,this.render()}onKeyPress(){this.pointer=0,this.filterChoices(),this.render()}onEnd(e){this.status="answered",this.render(),this.screen.done(),this.done(e.value)}onError(e){this.render(e.isValid)}}function qe(){return k(this,void 0,void 0,(function*(){const e=o(process.argv.slice(2));if(!(e.h||e.help||e.v||e.version||["init","i"].includes(e._[0])))if(P()){const e=n.readJSONSync(C());R(e.profile),F(e.main),K(e.constants),yield function(){return k(this,void 0,void 0,(function*(){const e=V("latestCheckVersionTimestamp"),i=V("versionCheckDuring");if(Date.now()-Number(e)>24*Number(i)*3600*1e3){K(yield ze());const e=yield I(`npm view ${w.name} version --registry https://registry.npmjs.org/`);if(e!==w.version){console.log(`${t.blue(w.name)}本地版本为${w.version},低于线上版本${e},开始升级`);try{yield I(`npm i -g ${w.name}@${e} --registry https://registry.npmjs.org/`),F("latestCheckVersionTimestamp",Date.now()),fe(),console.log(t.green("升级完成!")),process.exit(0)}catch(e){console.log("升级出错!请重试,或者手动升级"),E(e)}}}}))}()}else E("请先初始化z-develop(执行 z i)。更多见https://hxhtbr8t8uy.feishu.cn/docx/RnOhdtqJWo5UooxHTaUcQFwtnKg")}))}function De(){return k(this,void 0,void 0,(function*(){const e=yield je({url:`${ke()}/groups`}),t=re.map((t=>{const i=e.find((e=>e.name===t));return!!i&&{name:i.name,id:i.id,description:i.description}})).filter((e=>!!e)),i=r.join(_(),"fe-groups.json");n.writeJSONSync(i,{groups:t},{spaces:2})}))}function _e(e){return k(this,void 0,void 0,(function*(){const t=`${H("FEServerDomain")}/api`,{data:i}=yield ve({url:`${t}/auth/z-develop/login`,method:"post"});return ve({url:t+e.url,headers:{Authorization:`Bearer ${i}`},data:e.data,method:e.method||"post"})}))}function Ce(e,t){const i=[];t.forEach((t=>{e.includes(t.value)&&i.push(t)}));const n=V("weWorkListCache"),o=n.map((e=>e.value));i.forEach((e=>{const t=o.indexOf(e.value);t>-1?n[t].usageCount+=1:n.push(Object.assign(Object.assign({},e),{usageCount:1}))})),F({weWorkListCache:n.sort(((e,t)=>t.usageCount-e.usageCount))}),fe()}function Pe(){return k(this,void 0,void 0,(function*(){const{data:e}=yield _e({url:"/user/list"}),t=e.filter((e=>[1,2,3].includes(e.title))).map((e=>({name:`${e.nick} - ${ne[e.title]}`,value:e.weWorkUserId}))),i=V("weWorkListCache"),n=i.map((e=>e.value)),o=[];return t.forEach((e=>{const t=n.indexOf(e.value);o.push(Object.assign(Object.assign({},e),{usageCount:t>-1?i[t].usageCount:0}))})),o.sort(((e,t)=>t.usageCount-e.usageCount))}))}function Ie(){return k(this,void 0,void 0,(function*(){try{if(P()){const{isRemove:e}=yield p.prompt([{type:"confirm",name:"isRemove",message:"系统中已存在z的配置文件,确认重新配置?"}]);e?i.rm("-rf",C()):process.exit(0)}s.mkdirSync(_(),{recursive:!0});const{jobType:e,account:t,password:n}=yield p.prompt([{type:"list",name:"jobType",message:"请选择岗位类型",choices:[{name:"前端",value:"fe"},{name:"后端",value:"be"}]},{type:"input",name:"account",message:"请输入LDAP账号:"},{type:"input",name:"password",message:"请输入LDAP密码:"}]);R("jobType",e),R("ldapAccount",t),R("ldapPassword",n);K(yield ze());const o=yield Pe(),{weWork:r}=yield p.prompt([{type:"search-checkbox",name:"weWork",message:"请选择你自己(用于企微通知):",choices:o,validate:e=>e.length>1?"只能选一个":!(e.length<1)||"请选一个"}]),{name:a,value:c}=o.find((e=>e.value===r[0]));R({weWorkName:a,weWorkUserId:c})}catch(e){E(e)}const e=b("配置信息初始化中").start();try{const t=yield je({url:`${ke()}/user`});R({gitUserId:t.id,gitName:t.name,gitEnglishName:t.username,gitEmail:t.email}),F("latestCheckVersionTimestamp",Date.now()),fe(),yield De(),e.succeed("配置信息初始化完成")}catch(t){e.fail("配置信息初始化失败"),c.isAxiosError(t)&&E("请检查你的域名及令牌配置"),E(t)}}))}const Le=".z",Ue=(e="")=>r.join(Le,e);let Ae;function Je(e,t){return k(this,void 0,void 0,(function*(){if(e)i.rm("-rf",".z");else if(s.existsSync(Ue())){const{isRemove:e}=yield p.prompt([{type:"confirm",name:"isRemove",message:`当前项目中已存在配置文件夹${Le},确认重新配置?`}]);e?i.rm("-rf",".z"):process.exit(0)}s.mkdirSync(Ue(),{recursive:!0}),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=yield I("git remote get-url --push origin");i||E("获取项目远程地址失败,请配置后重试");let n=e;if(!n){const{language:e}=yield p.prompt([{type:"list",name:"language",message:"请选择项目语言",choices:[{name:"JavaScript",value:"JavaScript"},{name:"Java",value:"Java"}]}]);n=e}let o=t;if(!o){const e=le(),t=yield Ne(e.name);if(1===t.length)o=t[0].namespace.path;else if(t.length>1){const{groupName:e}=yield p.prompt([{type:"list",name:"groupName",message:"请选择当前项目所属分组",choices:t.map((e=>({name:e.namespace.path,value:e.namespace.path})))}]);o=e}}Ae=b("项目初始化中").start(),pe({language:n,"lint-staged":Z[n],repository:{url:i,group:o}})}))}(e,t),yield function(){return k(this,void 0,void 0,(function*(){j.spawnSync("git",["config","core.hooksPath",Le]),s.writeFileSync(Ue(".commit-msg-tpl"),"",{mode:493}),j.spawnSync("git",["config","commit.template",r.join(Le,".commit-msg-tpl")]),s.writeFileSync(Ue("commit-msg"),"#!/usr/bin/env sh\nz lint commit-msg",{mode:493}),s.writeFileSync(Ue("pre-commit"),"#!/usr/bin/env sh\nz lint commit-files",{mode:493}),s.writeFileSync(Ue(".prettierrc.json"),G,{mode:493}),s.writeFileSync(Ue(".prettierignore"),Q,{mode:493}),s.writeFileSync(Ue(".gitignore"),".commit-msg-tpl\n",{mode:493})}))}(),null==Ae||Ae.succeed("初始化完成。")}))}function Be(e){return k(this,void 0,void 0,(function*(){if(!e)return"不能为空";if(!/^[a-z0-9-]+$/.test(e))return"格式为小写字母、中横线(可选)、数字(不推荐)。如apple, apple-tree";if(q().includes(e))return"当前目录下已存在同名文件夹,请先处理。";return!(yield Ne(e)).find((t=>t.name===e))||"远程仓库中已存在同名项目"}))}function We(e){return!!e||"不能为空"}function Re(e){return e?!!/^[a-zA-Z0-9]+$/.test(e)||"格式为大小写字母、数字,小驼峰命名。如userInfo、systemRouter3":"不能为空"}function Ke(e){if(!e)return"目录名称不能为空";if(!/^[a-z][a-z0-9\-\/]*$/.test(e))return"支持小写字母、数字,连字符(-)命名,小写字母开头,多层目录使用/隔开。如user、modules-3、setting/profile";if(e.includes("/")){const t=e.split("/");for(const e of t){if(!e)return"子级目录/页面名称不能为空";if(!/^[a-z][a-z0-9\-]*$/.test(e))return"支持小写字母、数字,连字符(-)命名,小写字母开头。如user、modules-3"}}let t,i;t=e.includes("/")?r.resolve("src","pages",...e.split("/")):r.resolve("src","pages",e);const n=r.resolve(t,"..");return i=!!s.existsSync(n)&&s.readdirSync(n).map((e=>{const t=e.lastIndexOf(".");return t>-1?e.slice(0,t):e})).includes(e),!i||"本地目录已存在该文件"}function Fe(){return k(this,void 0,void 0,(function*(){var e,o,a,c,l,u;try{const d={},m=()=>k(this,void 0,void 0,(function*(){const e=q(),{projects:t}=yield Se(),i=t.map((t=>{const i={name:`${t.name} [${t.desc}]`,value:t.name,disabled:!1};return e.includes(t.name)&&(i.disabled="目录下已存在同名文件夹"),i})),{project:n}=yield p.prompt([{type:"list",name:"project",message:"请选择一个项目模板",choices:i}]),o=t.find((e=>e.name===n));d.tplName=o.name,d.tplUrl=o.url,d.tplLanguage=o.language})),f=()=>k(this,void 0,void 0,(function*(){var e;const{groups:t}=function(){const e=r.join(_(),"fe-groups.json");return s.existsSync(e)?n.readJSONSync(e):{groups:[]}}(),{group:i}=yield p.prompt([{type:"list",name:"group",message:"请选择一个分组",choices:t.map((e=>({name:`${e.name} [${e.description}]`,value:e.id,short:e.name})))}]);d.group={id:i,name:null===(e=t.find((e=>e.id===i)))||void 0===e?void 0:e.name}})),h=()=>k(this,void 0,void 0,(function*(){const{name:e}=yield p.prompt([{type:"input",name:"name",message:"请输入项目名称",validate:Be}]);d.projectName=e;const{desc:t}=yield p.prompt([{type:"input",name:"desc",message:"请输入项目描述",validate:We}]);d.projectDesc=t}));yield m(),yield f(),yield h();const g=`${oe}/${null===(e=d.group)||void 0===e?void 0:e.name}/${d.projectName}.git`,y=b("模版初始化中").start();yield I(`git clone --depth=1 ${d.tplUrl}`),i.mv(d.tplName,d.projectName),i.cd(d.projectName),i.rm("-rf",".git"),yield I("git init --initial-branch=master"),yield I(`git remote add origin ${g}`),yield I(`git config user.name ${M("gitName")}`),yield I(`git config user.email ${M("gitEmail")}`),u=Object.assign(Object.assign({},le()),{name:d.projectName,description:d.projectDesc}),ae=ae?Object.assign(Object.assign({},ae),u):u,n.writeJSONSync(r.resolve("package.json"),ae,{spaces:2}),yield n.writeFile(r.resolve("README.md"),function(e="项目中文名",t="项目描述"){return`\n# ${e}\n${t}\n\n## 快速开始\n### 安装依赖\n\`\`\`bash\n npm i\n\`\`\`\n\n### 本地开发\n\`\`\`bash\n npm run dev:{environment}:{platform}\n\`\`\`\n\n> 查看更多关于 [platform](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/nttvbeif4a62gtpz#faWqt) 和 [environment](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/nttvbeif4a62gtpz#faWqt)\n\n### 打包代码\n\`\`\`bash\n npm run build:{environment}:{platform}\n\`\`\`\n\n### 格式化、修复代码语法\n\`\`\`bash\n z lint prettier\n\`\`\`\n\n> [ESLint相关规范](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/ul914hdkqz8fr02g#AkvQ4)\n\n### 格式化文档\n\`\`\`bash\n z lint prettier\n\`\`\`\n\n> [Prettier相关规范](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/ul914hdkqz8fr02g#ojpwI)\n\n## 常见问题\n列出开发中常见的问题和解决方案\n\n## 了解更多\n在此处放入语雀文档链接。请在[前端团队-项目手册](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/bfrcicwzosoyaifg)中按照分类新建文档。\n`}(d.projectName,d.projectDesc)),y.succeed("模版初始化完成"),yield Je(d.tplLanguage,null===(o=d.group)||void 0===o?void 0:o.name);const v=b("依赖安装中").start();yield I("npm install"),v.succeed("依赖安装完成");const j=b("项目推送中").start();yield I('git add . && git commit -m "chore: init"');const $=yield(l={name:d.projectName,description:d.projectDesc,path:d.projectName,visibility:"private",namespace_id:null===(a=d.group)||void 0===a?void 0:a.id},je({url:`${ke()}/projects`,method:"post",data:l}));yield I("git push -u origin master"),j.succeed(`项目已推送到远程,地址: ${t.blue(g)}`);const w=b("初始化分支中").start(),x=`feat_init_${L()}`,S=B(null===(c=d.group)||void 0===c?void 0:c.name)?[...te,x]:[x],z=yield Promise.allSettled(S.map((e=>function(e){return je({url:`${ke()}/projects/${e.id}/repository/branches`,method:"post",data:e})}({id:$.id,branch:e,ref:"master"})))),N=[];z.forEach((({status:e},t)=>{"fulfilled"===e&&N.push(S[t])})),yield I("git pull"),N.includes(x)?(yield I(`git checkout -b ${x} origin/${x}`),w.succeed(`项目已切换到初始分支: ${t.blue(x)}`)):w.warn("开发分支检出失败!项目当前在主分支,请自行检出开发分支。");const O=`cd ${d.projectName} && z start`;console.log(`输入 ${t.green(O)} 开始开发吧~`),process.exit(0)}catch(e){E(e)}}))}e.command("init").alias("i").argument("[type]","非必填。不传为初始化工具配置;传 . 为初始化当前项目。").description("初始化工具配置、项目配置").action((function(e){return k(this,void 0,void 0,(function*(){e?"."===e?(A(),yield Je(void 0,void 0)):E("参数错误。执行 z init -h 查看帮助。"):yield Ie()}))}));const Me=e=>`${H("ZenTaoDomain")}${e}`;function Ve(e){return 0!==e.length||"请选择项"}function He(){return k(this,void 0,void 0,(function*(){const{type:e}=yield p.prompt({type:"list",name:"type",message:"请选择你要创建的模板类型",choices:[{name:"feat - 业务需求开发",value:"feat"},{name:"fix - bug修复",value:"fix"},{name:"refactor - 技术内部需求",value:"refactor"},{name:"chore - 其他",value:"chore"}]});if(["feat","refactor"].includes(e)){const e=yield function(){return k(this,void 0,void 0,(function*(){const e=yield we({url:Me("/my-work-task.json?tid=mrrferp8"),method:"get"});return null==e?void 0:e.tasks.map((({id:e,name:t})=>({value:{name:t,value:e},name:t})))}))}(),{tasks:t}=yield p.prompt({type:"checkbox",name:"tasks",message:"请关联开发任务(可多选):",validate:Ve,choices:e.map((e=>Object.assign(Object.assign({},e),{name:`[${e.value.value}]: ${e.name}`})))});Ge("feat",t)}else if("fix"===e){const e=yield function(){return k(this,void 0,void 0,(function*(){const e=yield we({url:Me("/my-work-bug.json?tid=mrrferp8"),method:"get"});return null==e?void 0:e.bugs.map((({id:e,title:t})=>({name:t,value:{name:t,value:e}})))}))}(),{bugs:t}=yield p.prompt({type:"checkbox",name:"bugs",message:"请关联Bug(可多选):",validate:Ve,choices:e.map((e=>Object.assign(Object.assign({},e),{name:`[${e.value.value}]: ${e.name}`})))});Ge("fix",t)}else if("chore"===e){const{msg:e}=yield p.prompt({type:"input",name:"msg",message:"请输入commit msg:",validate:e=>0!==e.length||"请输入commit msg"});Ge("chore",[e])}}))}function Ge(e,i){let n="";if(["feat","refactor","fix"].includes(e)){n=`${e}(${i.map((e=>e.value)).join(",")}): ${i.map((e=>e.name)).join(";")}`}"chore"===e&&(n=`${e}: ${i[0]}`),s.writeFileSync(r.resolve(".z",".commit-msg-tpl"),n,"utf-8"),console.log(t.yellow(n)),O("commit msg模板写入成功,可以进行提交了")}function Qe(e){return k(this,void 0,void 0,(function*(){try{const{originBranch:t,environment:i,platform:n,notify:o,processId:s}=e,r=ue();!function(e,t){var i,n,o,s,r;const a=le(),c=ue();(null===(i=null==c?void 0:c.repository)||void 0===i?void 0:i.url)||E("项目package.json文件中缺少repository.url");const l=`build:${e}`;(null==a?void 0:a.scripts[l])||E(`项目package.json文件scripts不存在命令${l}。`);["alipay","weapp"].includes(t)&&(a.mini||E("请在package.json配置mini字段"),"alipay"===t&&((null===(o=null===(n=a.mini)||void 0===n?void 0:n.alipay)||void 0===o?void 0:o.appid)||E("请在package.json配置mini.alipay.appid字段")),"weapp"===t&&((null===(r=null===(s=a.mini)||void 0===s?void 0:s.weapp)||void 0===r?void 0:r.appid)||E("请在package.json配置mini.weapp.appid字段")))}(i,n),yield I("git pull"),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=ue(),n=yield I(`git remote show ${i.repository.url}`),[,o]=n.match(/HEAD branch: (.+)\n/),s=yield I(`git log -b origin/${o} -1 --format=%H`),r=`origin/${e}`;if((yield I(`git branch --contains ${s} -r ${r}`))||E(`请先将最新的主分支(${o})代码合并到${r},再进行部署。`),"production"!==t)return;const a=yield I(`git log -b origin/${e} -1 --format=%H`),c=(yield I(`git log ${s}...${a} -b ${r}`)).split("\n");c.length||E(`分支${e}上没有要发布的commit`);const l=yield I("git log -b origin/test -1 --format=%H");(function(e,t){for(let i of t)if(!e.includes(i))return!1;return!0})((yield I(`git log ${s}...${l} -b origin/test`)).split("\n"),c)||E("请先在测试环境发布要部署的代码")}))}(t,i),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=function(e){const{pathname:t}=new URL(e),i=t.match(/\/(.+)\.git/);if(i)return i[1];E("GitLab URL有误")}(e),{originBranch:n,environment:o,platform:s,notify:r,processId:a}=t;var c;yield(c={params:{command:"build",params:JSON.stringify({originBranch:n,environment:o,platform:s,profile:{weWorkName:M("weWorkName"),weWorkUserId:M("weWorkUserId")}}),options:JSON.stringify({processId:a,notify:r||[]})},jobName:i},ve({url:`${H("jenkinsDomain")}/job/${c.jobName}/buildWithParameters`,method:"post",dataKey:"params",data:c.params,auth:{username:H("jenkinsUsername"),password:H("jenkinsPassword")}}))}))}(r.repository.url,{originBranch:t,environment:i,platform:n,notify:o,processId:s})}catch(e){E(e)}}))}function Ze(){if(s.existsSync(r.resolve(".z","project.json"))){const e=n.readJsonSync(r.resolve(".z","project.json"));if(e["lint-staged"])return e["lint-staged"];throw new Error("未找到lint配置")}throw new Error("请先初始化项目(z i .)。")}e.command("create").alias("c").argument("[type]","可选值为project|pr, branch|b, commit-msg|cm, page|p, component|co").description("创建项目/分支/提交信息/页面/组件").action((function(e){return k(this,void 0,void 0,(function*(){let t;if(e)["project","pr"].includes(e)?t="project":["branch","b"].includes(e)?t="branch":["page","pa"].includes(e)?t="page":["component","co"].includes(e)?t="component":["commit-msg","cm"].includes(e)?t="commit-msg":E("参数输入错误");else{t=(yield p.prompt({type:"list",name:"type",message:"请选择你要创建的类型",choices:[{name:"提交信息(commit message)",value:"commit-msg"},{name:"分支(branch)",value:"branch"},{name:"页面(page)",value:"page"},{name:"组件(component)",value:"component"},{name:"项目(project)",value:"project"}]})).type}"project"===t?yield Fe():"branch"===t?(U(),A(),yield J(),yield function(){return k(this,void 0,void 0,(function*(){const{type:e,purpose:t}=yield p.prompt([{name:"type",type:"list",message:"请选择创建分支的类型",choices:[{name:"开发新功能(feat)",value:"feat"},{name:"修复BUG(fix)",value:"fix"},{name:"重构/优化代码(refactor)",value:"refactor"}]},{name:"purpose",type:"input",message:"请输入创建分支的目的(大小写字母、数字,小驼峰式命名。如userInfo)",validate:Re}]),i=`${e}_${t}_${L()}`,n=ue(),o=yield I(`git remote show ${n.repository.url}`),[,s]=o.match(/HEAD branch: (.+)\n/);yield I("git pull"),yield I(`git checkout -b ${i} origin/${s}`)}))}()):"commit-msg"===t?(U(),A(),yield He()):"page"===t?(U(),A(),yield function(){return k(this,void 0,void 0,(function*(){const e="pages",t=yield he(e);s.mkdirSync(r.resolve("src",e),{recursive:!0});const{dirPageName:n}=yield p.prompt([{name:"dirPageName",type:"input",message:"请输入目录及页面名称(如user/list,不带文件后缀)",validate:Ke}]);let o;if(n.includes("/")){const t=n.split("/");t.pop(),o=r.resolve("src",e,...n.split("/")),s.mkdirSync(r.resolve("src",e,...t),{recursive:!0})}else o=r.resolve("src",e,n);const{pages:a}=yield Se(),c=s.readdirSync(t),{tplName:l}=yield p.prompt([{name:"tplName",type:"list",message:"请选择模版",choices:a.map((e=>!!c.includes(e.name)&&{name:`${e.name}: ${e.desc}(${e.author})`,value:e.name})).filter((e=>e))}]),u=b("页面生成中...").start(),d=o+r.extname(r.join(t,l));i.cp("-r",r.join(t,l),d),T(d)?D(d).forEach((e=>{ye(r.join(d,e),n)})):ye(d,n),u.succeed("页面生成成功")}))}()):"component"===t&&(U(),A(),yield function(){return k(this,void 0,void 0,(function*(){const e="components",t=yield he("components");s.mkdirSync(r.resolve("src",e),{recursive:!0});const{dirPageName:n}=yield p.prompt([{name:"dirPageName",type:"input",message:"请输入目录及组件名称(如user/list,不带文件后缀)",validate:Ke}]);let o;if(n.includes("/")){const t=n.split("/"),i=t.pop();o=r.resolve("src",e,...t.split("/"),ge(i)),s.mkdirSync(r.resolve("src",e,...t),{recursive:!0})}else o=r.resolve("src",e,ge(n));const{components:a}=yield Se(),c=s.readdirSync(t),{tplName:l}=yield p.prompt([{name:"tplName",type:"list",message:"请选择模版",choices:a.map((e=>!!c.includes(e.name)&&{name:`${e.name}: ${e.desc}(${e.author})`,value:e.name})).filter((e=>e))}]),u=b("组件生成中...").start(),d=o+r.extname(r.join(t,l));i.cp("-r",r.join(t,l),d),T(d)?D(d).forEach((e=>{ye(r.join(d,e),n)})):ye(d,n),u.succeed("组件生成成功")}))}())}))})),e.command("merge").alias("m").description("合并当前分支到指定远程分支,并部署。").action((function(){return k(this,void 0,void 0,(function*(){var e;U(),A(),yield J();const i=yield me(),n=yield Oe(i.id),o=[...te],s=B(i.group),r=n.filter((e=>s?![...ee,i.sourceBranch].includes(e.name):![i.sourceBranch].includes(e.name))).map((e=>{let i=e.name;return s&&o.includes(i)&&(i=t.bold.blue(i)),{name:i,value:e.name}})),{target:a}=yield p.prompt([{type:"list",name:"target",message:"请选择要合并到的目标分支:",choices:r}]),l=o.includes(a);let u="h5",d=[];if(l){const e=yield Pe(),t=yield p.prompt([{type:"list",name:"platform",message:"请选择平台",choices:[{name:"网页 - h5",value:"h5"},{name:"依赖包 - npm",value:"npm"}]},{type:"search-checkbox",name:"notify",message:"请选择部署成功要通知的人员:",choices:e,validate:e=>!(e.length<1)||"至少选一个"}]);Ce(d,e),u=t.platform,d=t.notify}const m=b(`${i.sourceBranch}分支推送中`).start();yield I("git pull"),yield I(`git push -u origin ${i.sourceBranch}`),m.succeed(`${i.sourceBranch}已推送到远程`);const f=b("分支合并中").start();let h=0;try{const{iid:e}=yield(g={title:"MR by z-develop",id:i.id,source_branch:i.sourceBranch,target_branch:a},je({url:`${ke()}/projects/${g.id}/merge_requests`,method:"post",data:g}));h=e}catch(t){if(c.isAxiosError(t)){409===(null===(e=null==t?void 0:t.response)||void 0===e?void 0:e.status)&&E(`存在重复的合并请求,前往查看${i.mergeRequestUrl}`)}E(t)}var g;const y=()=>k(this,void 0,void 0,(function*(){var e;try{yield new Promise((e=>{setTimeout(e,5e3)})),yield function(e){return je({url:`${ke()}/projects/${e.id}/merge_requests/${e.iid}/merge`,method:"put"})}({id:i.id,iid:h})}catch(t){if(c.isAxiosError(t)){406===(null===(e=null==t?void 0:t.response)||void 0===e?void 0:e.status)?yield y():(yield function(e){return je({url:`${ke()}/projects/${e.id}/merge_requests/${e.iid}`,method:"put",data:e})}({id:i.id,iid:h,state_event:"close"}),f.fail("合并过程中出现冲突,MR已关闭。建议在本地完成合并。"),process.exit(1))}else E(t)}}));yield y(),f.succeed("分支合并成功"),l&&(yield Qe({originBranch:a,environment:a,platform:u,notify:d,processId:""}))}))})),e.command("deploy").alias("d").description("发布指定远程分支").action((function(){return k(this,void 0,void 0,(function*(){const e=yield Pe(),{environment:t,platform:i,notify:n}=yield p.prompt([{type:"list",name:"environment",message:"请选择部署环境",choices:Y},{type:"list",name:"platform",message:"请选择平台",choices:X},{type:"search-checkbox",name:"notify",message:"请选择部署成功要通知的人员:",choices:e,validate:e=>!(e.length<1)||"至少选一个"}]);Ce(n,e);let o=t,s="";if("production"===t){const e=yield me(),t=(yield Oe(e.id)).filter((e=>![...ee,...te].includes(e.name))).map((e=>({name:e.name,value:e.name}))),n=yield p.prompt([{type:"list",name:"branchName",message:"请输入要部署的分支:",choices:t},{type:"input",name:"processId",message:"请输入本次部署的企微审批编号"}]);o=n.branchName,s=n.processId,function(e){const t=e.split("_");if(4!==t.length)return!1;const[i,n,o]=t;return!!ie.includes(i)&&!0===Re(n)&&8===o.length&&!isNaN(Number(o))}(o)||E("分支命名不符合规范,参见https://zhongshitech.yuque.com/nx4ugr/guide/mg8t44#bQhu4");const{data:{state:a,content:c}}=yield(r={businessId:s},_e({url:"/approval/findByBusinessId",data:r}));1===a&&E("当前审批流程状态为审批中,审批通过才能进行发布。"),3===a&&E("当前审批流程状态为已驳回,请重新提交审批。"),4===a&&E("当前审批流程状态为已发布,如需再次发布,请重新提交审批。");const{formComponentValues:l}=JSON.parse(c),u={};l.forEach((e=>{u[e.name]=e.value})),u["申请人"]!==M("weWorkName")&&E("发布审批申请人与当前发布人不一致!"),u["项目名称"]!==le().name&&E("发布审批项目名称与当前发布项目名称不一致!"),u["发布分支"]!==o&&E("发布审批发布分支与当前发布分支不一致!"),u["发布平台"]!==i&&E("发布审批发布平台与当前发布平台不一致!")}var r;yield Qe({originBranch:o,environment:t,platform:i,notify:n,processId:s})}))})),e.command("start").alias("s").description("启动本地开发环境").action((function(){return k(this,void 0,void 0,(function*(){const e=le(),{environment:t}=yield p.prompt([{type:"list",name:"environment",message:"请选择环境",choices:Y}]),n=`dev:${t}`;if(e.scripts[n])try{i.exec(`npm run ${n}`,{silent:!1})}catch(e){E(e)}else E(`项目中(package.josn > scripts)不存在命令${n},请先添加!`)}))})),e.command("lint").alias("l").argument("[type]","非必填。可选值为commit-msg|cm, commit-files|cf, prettier|p, eslint|e。").description("执行 lint 脚本。包含 lint staged 、 lint commit message、 prettier、 eslint。").action((function(e){return k(this,void 0,void 0,(function*(){let t;if(U(),A(),e)["commit-msg","cm"].includes(e)?t="commit-msg":["commit-files","cf"].includes(e)?t="commit-files":["prettier","p"].includes(e)?t="prettier":["eslint","e"].includes(e)?t="eslint":E("参数输入错误");else{t=(yield p.prompt({type:"list",name:"type",message:"请选择你要执行的操作",choices:[{name:"全局执行 prettier",value:"prettier"},{name:"全局执行 eslint",value:"eslint"}]})).type}if("commit-msg"===t)yield function(){return k(this,void 0,void 0,(function*(){const e=function(e){const t=s.readFileSync(r.resolve(".git","COMMIT_EDITMSG"),"utf-8").split(": ");if(1===t.length)return"body前缺少「:」";{const e=t[0];return/^(feat|fix|refactor)/.test(e)?!!/\(\d+(?:,\d+)*\)/.test(e)||"ID缺少或者格式不正确。":/^chore/.test(e)?"chore"===e||"chore类型无需填写ID。":"不存在的提交类型。"}}();!0===e?O("commit msg validate success."):E(`commit msg格式错误。${e}`)}))}();else if("commit-files"===t)yield function(){return k(this,void 0,void 0,(function*(){(yield $({concurrent:!1,debug:void 0,config:Ze()}))?O("代码风格检测通过!"):E("代码风格检测未通过!")}))}();else if("prettier"===t){s.existsSync(r.resolve("node_modules",".bin","prettier"))||E("该项目未安装prettier,请安装后重试");try{yield I("npx prettier --write . --config .z/.prettierrc.json --ignore-path .z/.prettierignore --ignore-unknown --no-error-on-unmatched-pattern",{silent:!1}),O("prettier执行成功")}catch(e){E("prettier校验出错")}}else if("eslint"===t){console.log(r.resolve("node_modules",".bin","eslint")),s.existsSync(r.resolve("node_modules",".bin","eslint"))||E("该项目未安装eslint,请安装后重试");try{let e="npx eslint '**/*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}' --fix";"win32"===process.platform&&(e="npx eslint '**\\*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}' --fix"),yield I(e,{silent:!1}),O("eslint执行成功")}catch(e){E("eslint校验出错")}}}))})),function(){k(this,void 0,void 0,(function*(){process.on("unhandledRejection",(e=>{})),process.on("uncaughtException",(e=>{})),i.config.fatal=!0,i.config.silent=!0,i.config.verbose=!1,yield qe(),p.registerPrompt("search-checkbox",Te)}))}(),e.name("z-develop").alias("z").description(`z-develop, 开发流程管理工具。\n了解更多: ${t.blue("https://hxhtbr8t8uy.feishu.cn/docx/RnOhdtqJWo5UooxHTaUcQFwtnKg")}`).usage("<command> [options]").version(w.version,"-v, --version","当前版本号").helpOption("-h, --help","帮助").showHelpAfterError("可以使用z -h查看帮助。"),e.parse();
1
+ import{program as e}from"commander";import t from"chalk";import i from"shelljs";import n from"fs-extra";import o from"minimist";import s from"node:fs";import r from"node:path";import a from"node:os";import c,{AxiosError as l}from"axios";import u from"dayjs";import p from"inquirer";import d from"inquirer/lib/prompts/base.js";import m from"inquirer/lib/utils/paginator.js";import f from"inquirer/lib/utils/events.js";import"inquirer/lib/objects/choices.js";import{map as h,takeUntil as g,filter as y}from"rxjs";import v from"figures";import b from"ora";import j from"child_process";import $ from"lint-staged";var w={name:"cli-z-develop",version:"0.0.35",description:"前端本地开发命令行工具",main:"dist/index.js",bin:{z:"./bin/z.js","z-develop":"./bin/z.js"},scripts:{prepare:"husky",dev:"rollup -c -w --environment DEBUG:true",build:"rollup -c",eslint:"eslint '**/*.{ts,js}' --fix",prettier:"prettier -wu .",upload:"npm version patch && npm run build && npm publish --access public --registry https://registry.npmjs.org/"},type:"module",repository:{type:"git",url:"http://git.cxlqd.com/fe-base/cli-z-develop.git"},author:"z",devDependencies:{"@commitlint/cli":"^19.3.0","@commitlint/config-conventional":"^19.2.2","@rollup/plugin-json":"^6.1.0","@rollup/plugin-node-resolve":"^15.2.3","@rollup/plugin-replace":"^5.0.7","@rollup/plugin-terser":"^0.4.4","@types/fs-extra":"^11.0.4","@types/inquirer":"^9.0.7","@types/minimist":"^1.2.5","@types/node":"^20.14.9","@types/shelljs":"^0.8.15","@types/tough-cookie":"^4.0.5","@typescript-eslint/eslint-plugin":"^7.14.1","@typescript-eslint/parser":"^7.14.1",eslint:"^8.57.0","eslint-config-alloy":"^5.1.2","eslint-config-prettier":"^9.1.0",husky:"^9.0.11",prettier:"^3.3.2",rollup:"^4.18.0","rollup-plugin-typescript2":"^0.36.0",tslib:"^2.6.3",typescript:"^5.5.2"},dependencies:{"@inquirer/prompts":"^5.1.0",axios:"^1.7.2",chalk:"^5.3.0",commander:"^12.1.0",dayjs:"^1.11.11",figures:"^6.1.0","fs-extra":"^11.2.0",inquirer:"9.3.1","lint-staged":"^15.2.7",minimist:"^1.2.8",ora:"^8.0.1",rxjs:"^7.8.1",shelljs:"^0.8.5"}};function k(e,t,i,n){return new(i||(i=Promise))((function(o,s){function r(e){try{c(n.next(e))}catch(e){s(e)}}function a(e){try{c(n.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const{red:x,green:S,blue:z,magenta:N}=t;function O(...e){console.log(S(...e))}function E(e,t=!1){let i=e;e instanceof Error?(i=e.message,c.isAxiosError(e)&&(i=`请求失败:${e.message}`),console.log(x(i)),console.log(N(e.stack))):console.log(x(e)),t||process.exit(1)}function T(e){return!!s.existsSync(e)&&s.lstatSync(e).isDirectory()}function _(e=process.cwd()){if(T(e)){return s.readdirSync(e).filter((t=>T(r.resolve(e,t))))}return[]}function q(e=process.cwd()){if(T(e)){return s.readdirSync(e).filter((t=>!T(r.resolve(e,t))))}return[]}function D(){return r.resolve(a.homedir(),".z")}function C(){return r.resolve(D(),"develop-config.json")}function I(){return s.existsSync(D())&&s.existsSync(C())}function U(e){return k(this,arguments,void 0,(function*(e,t={removeTailLinkBreak:!0,silent:!0}){let n=yield new Promise(((n,o)=>{try{n(i.exec(e,{silent:t.silent}))}catch(e){o(e)}}));return n=n.toString(),t.removeTailLinkBreak&&(n=n.replace(/\n$/,"")),n}))}function P(){return u(Date.now()).format("YYMMDD")}function L(){s.existsSync(r.resolve(".z"))||E("当前不在项目根目录。请切换到项目根目录")}function B(){s.existsSync(r.resolve(".git"))||E("当前不是git项目根目录,请先执行git init,或切换到根目录")}function J(){return k(this,void 0,void 0,(function*(){""!==(yield U("git status -s"))&&E("请先提交代码变动,再进行操作")}))}function A(e){return["fe-biz"].includes(e)}const W={profile:{ldapAccount:"",ldapPassword:"",gitToken:"",gitUserId:0,gitName:"",gitEnglishName:"",gitEmail:"",weWorkName:"",weWorkUserId:"",jobType:"fe",zenTaoToken:""},main:{version:w.version,latestCheckVersionTimestamp:0,versionCheckDuring:3,weWorkListCache:[]},constants:{ZenTaoDomain:"",FEServerDomain:"",jenkinsDomain:"",jenkinsUsername:"",jenkinsPassword:"",OSSEndpoint:"",OSSAccessKeyID:"",OSSAccessKeySecret:"",OSSBucketName:""}};function R(e,t){void 0!==t?W.profile[e]=t:W.profile=Object.assign(Object.assign({},W.profile),e)}function F(e,t){W.constants=Object.assign(Object.assign({},W.constants),e)}function K(e,t){void 0!==t?W.main[e]=t:W.main=Object.assign(Object.assign({},W.main),e)}function M(e){return e?W.profile[e]:W.profile}function V(e){return e?W.main[e]:W.main}function G(e){return e?W.constants[e]:W.constants}const Q='{\n "printWidth": 120,\n "tabWidth": 2,\n "useTabs": false,\n "semi": true,\n "singleQuote": false,\n "quoteProps": "as-needed",\n "jsxSingleQuote": false,\n "trailingComma": "all",\n "bracketSpacing": true,\n "bracketSameLine": false,\n "arrowParens": "always",\n "requirePragma": false,\n "insertPragma": false,\n "proseWrap": "preserve",\n "htmlWhitespaceSensitivity": "css",\n "vueIndentScriptAndStyle": false,\n "endOfLine": "lf",\n "embeddedLanguageFormatting": "auto",\n "singleAttributePerLine": false\n}\n',H="# 系统 信息文件\n.DS_Store/\nThumbs.db\n\n# 编辑器配置文件\n.idea/\n.vscode/\n\n# 输出目录\ndist/\n\n# Log files\n*.log\n\n# 本地配置文件\n*.local\n\n# 缓存文件\n*.cache\n\n# 垃圾文件\n.Trashes\n",Y={Java:{"**/*.{java}":"./run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/quickstart.xml"},JavaScript:{"**/*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}":"eslint --fix","*":"prettier -wu"}},Z=[{name:"开发环境 - dev",value:"dev"},{name:"测试环境 - test",value:"test"},{name:"预发环境 - release",value:"release"},{name:"正式环境 - production",value:"production"}],X=[{name:"网页 - h5",value:"h5"},{name:"依赖包 - npm",value:"npm"}],ee="master",te=["dev","test","release"],ie=[ee,...te],ne=["fix","feat","refactor"],oe={1:"研发",2:"测试",3:"产品",4:"设计",5:"运营",6:"销售",7:"行政",8:"财务",9:"其他"},se="http://git.cxlqd.com",re={PROJECT_NAME:"fe-pages-tpl",GIT_URL:`${se}/fe-component/fe-pages-tpl.git`},ae=["fe-biz","fe-base","fe-tpl","fe-component","fe-demo"];let ce=null,le=null;function ue(){if(ce)return ce;const e=r.resolve("package.json");try{ce=n.readJsonSync(e)}catch(t){E(`当前目录(${i.pwd()})不存在${e}文件,请在项目根目录执行该命令。`)}return ce}function pe(){if(le)return le;const e=r.resolve(".z","project.json");try{le=n.readJsonSync(e)}catch(t){E(`当前目录(${i.pwd()})不存在${e}文件,请在项目根目录执行该命令,或者初始化项目(z init .)。`)}return le}function de(e){le=le?Object.assign(Object.assign({},le),e):e,n.writeJSONSync(r.resolve(".z","project.json"),le,{spaces:2})}const me={id:0,name:"",group:"",sourceBranch:"",mergeRequestUrl:""};function fe(){return k(this,void 0,void 0,(function*(){if(!me.name){const e=ue();me.name=e.name}if(!me.group){const e=pe();e.repository.group&&(me.group=e.repository.group)}if(!me.id)try{const e=me.group?`${me.group}/${me.name}`:me.name,[t]=yield Oe(e);me.mergeRequestUrl=t.web_url+"/merge_requests",me.id=t.id,me.group=t.namespace.path;const i=pe();i.repository.group||de({repository:Object.assign(Object.assign({},i.repository),{group:t.namespace.path})})}catch(e){E(e)}return me.sourceBranch||(me.sourceBranch=yield U("git branch --show-current")),me}))}function he(){n.writeJSONSync(C(),{main:V(),profile:M(),constants:G()},{spaces:2})}function ge(e){return k(this,void 0,void 0,(function*(){const t=D(),n=process.cwd(),o=r.join(t,re.PROJECT_NAME);s.existsSync(o)?(i.cd(o),yield U("git pull"),i.cd(n)):(i.cd(t),yield U(`git clone ${re.GIT_URL} --depth=1`),i.cd(n));const a=r.join(o,"src",e);return s.existsSync(a)||E("模板库错误!"),a}))}function ye(e){return e.split("-").map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("")}function ve(e,t){const i=s.readFileSync(e,"utf-8"),n=t.replace(/\//g,"-"),o=i.replace(/name: "-_-NAME-_-"/g,`name: "${ye(n)}"`).replace(/class="-_-NAME-_-"/g,`class="${n}"`).replace(/.-_-NAME-_-/g,`.${n}`);s.writeFileSync(e,o,"utf-8")}function be(e){return k(this,void 0,void 0,(function*(){var i;const n=e.method||"get",o=e.headers||{},s=e.data||{},r=e.dataKey||["GET","get"].includes(n)?"params":"data",a=e.auth||void 0;try{const i={url:e.url,method:n,[r]:s,headers:o,auth:a};t.magenta(i.method.toUpperCase()),t.yellow(i.url),JSON.stringify(i.headers),JSON.stringify(i.auth||{}),t.gray(JSON.stringify(i[r],null,2));const l=yield c(i);return t.green("Response"),t.grey(JSON.stringify(l.data,null,2)),Promise.resolve(l.data)}catch(e){return e instanceof l&&(t.red("Error"),t.grey(JSON.stringify(null===(i=null==e?void 0:e.response)||void 0===i?void 0:i.data))),Promise.reject(e)}}))}function je(){return k(this,void 0,void 0,(function*(){const e=M("gitToken");if(e)return e;{const{access_token:e}=yield be({url:`${se}/oauth/token`,method:"post",data:{grant_type:"password",username:M("ldapAccount"),password:M("ldapPassword")}}),t=`Bearer ${e}`;return R("gitToken",t),he(),t}}))}function $e(e){return k(this,void 0,void 0,(function*(){return be(Object.assign(Object.assign({},e),{headers:{Authorization:yield je()}}))}))}function we(){return k(this,void 0,void 0,(function*(){const e=M("zenTaoToken");if(e)return e;{const{token:e}=yield be({url:`${G("ZenTaoDomain")}/api.php/v1/tokens`,method:"post",data:{account:M("ldapAccount"),password:M("ldapPassword")}});return R("zenTaoToken",e),he(),e}}))}function ke(e){return k(this,void 0,void 0,(function*(){const t=yield be(Object.assign(Object.assign({},e),{headers:{Token:yield we()}}));if("string"==typeof t){const e=JSON.parse(t.slice(0,t.indexOf("<script>")));return JSON.parse(e.data)}if("object"==typeof t)return"Unauthorized"===t.error||t.data.indexOf("<script>")<0?(R("zenTaoToken",""),ke(Object.assign({},e))):t}))}const xe=()=>`${se}/api/v4`,Se=e=>xe()+"/projects/100/repository/files/"+encodeURIComponent(e)+"/raw?ref=master";function ze(){return $e({url:Se("src/data/template-projects.json")})}function Ne(){return $e({url:Se("src/data/z-develop-config.json")})}function Oe(e){return $e({url:`${xe()}/projects`,data:{search:e,search_namespaces:!0}})}function Ee(e){return $e({url:`${xe()}/projects/${e}/repository/branches`})}function Te(e,i){let n="";return e.forEach(((e,o)=>{if(e.disabled)n=`${n} - ${e.name} (Disabled)`;else{n+=o===i?t.cyan(v.pointer)+" ":" ",n+=(e.checked||!1?t.green(v.radioOn):v.radioOff)+" "+e.name}n+="\n"})),n.replace(/\n$/,"")}class _e extends d{constructor(e,t,i){super(e,t,i),this.pointer=0,this.opt.choices||this.throwParamError("choices"),this.opt.choices.forEach(((e,t)=>{const i=e;i.checked=!1,i.id=t}));if(this.opt.choices.find((e=>"separator"===e.type)))throw new Error("Separator is not allowed in choices.");Array.isArray(this.opt.default)&&this.opt.choices.forEach((e=>{"separator"!==e.type&&this.opt.default.includes(e.value)&&(e.checked=!0)})),this.pointer=0,this.selection=[],this.done=e=>{console.log(e)},this.opt.default=null,this.paginator=new m(this.screen,{isInfinite:!0}),this.filterList=this.allList=this.opt.choices.choices}_run(e){this.done=e;const t=f(this.rl),i=this.handleSubmitEvents(t.line.pipe(h(this.getCurrentValue.bind(this))));i.success.forEach(this.onEnd.bind(this)),i.error.forEach(this.onError.bind(this)),t.normalizedUpKey.pipe(g(i.success)).forEach(this.onUpKey.bind(this)),t.normalizedDownKey.pipe(g(i.success)).forEach(this.onDownKey.bind(this)),t.spaceKey.pipe(g(i.success)).forEach(this.onSpaceKey.bind(this));const n=["up","down","space"];return t.keypress.pipe(y((e=>!e.key.ctrl&&!n.includes(e.key.name||"")))).pipe(g(i.success)).forEach(this.onKeyPress.bind(this)),this.render(),this}render(e){let i=this.getQuestion(),n="";if(this.getCurrentValue(),"answered"===this.status)i+=t.cyan(this.selection.join(","));else{this.selection.length?i+=t.magenta(`[已选: ${this.selection.join(",")}]`):i+="("+t.blue.bold("空格键")+"选择,"+t.blue.bold("回车键")+"提交,"+t.blue.bold("输入中文")+"搜索)",i+=t.bgYellowBright(t.black(this.rl.line));const e=Te(this.filterList,this.pointer);n=this.paginator.paginate(e,this.pointer,this.opt.pageSize)}e&&(n=t.red(">> ")+e),this.screen.render(i,n)}getCurrentValue(){const e=this.allList.filter((e=>e.checked&&!e.disabled));return this.selection=e.map((e=>e.short)),e.map((e=>e.value))}toggleChoice(e){const t=this.filterList[e];t&&(this.allList[t.id].checked=!t.checked)}filterChoices(){this.filterList=this.allList.filter((e=>{var t;return null===(t=e.name)||void 0===t?void 0:t.includes(this.rl.line.trim())}))}onSpaceKey(){this.toggleChoice(this.pointer),this.render(),this.rl.resume()}onDownKey(){const e=this.filterList.length;this.pointer=this.pointer<e-1?this.pointer+1:0,this.render()}onUpKey(){const e=this.filterList.length;this.pointer=this.pointer>0?this.pointer-1:e-1,this.render()}onKeyPress(){this.pointer=0,this.filterChoices(),this.render()}onEnd(e){this.status="answered",this.render(),this.screen.done(),this.done(e.value)}onError(e){this.render(e.isValid)}}function qe(){return k(this,void 0,void 0,(function*(){const e=o(process.argv.slice(2));if(!(e.h||e.help||e.v||e.version||["init","i"].includes(e._[0])))if(I()){const e=n.readJSONSync(C());R(e.profile),K(e.main),F(e.constants),yield function(){return k(this,void 0,void 0,(function*(){const e=V("latestCheckVersionTimestamp"),i=V("versionCheckDuring");if(Date.now()-Number(e)>24*Number(i)*3600*1e3){F(yield Ne());const e=yield U(`npm view ${w.name} version --registry https://registry.npmjs.org/`);if(e!==w.version){console.log(`${t.blue(w.name)}本地版本为${w.version},低于线上版本${e},开始升级`);try{yield U(`npm i -g ${w.name}@${e} --registry https://registry.npmjs.org/`),K("latestCheckVersionTimestamp",Date.now()),he(),console.log(t.green("升级完成!")),process.exit(0)}catch(e){console.log("升级出错!请重试,或者手动升级"),E(e)}}}}))}()}else E("请先初始化z-develop(执行 z i)。更多见https://hxhtbr8t8uy.feishu.cn/docx/RnOhdtqJWo5UooxHTaUcQFwtnKg")}))}function De(){return k(this,void 0,void 0,(function*(){const e=yield $e({url:`${xe()}/groups`}),t=ae.map((t=>{const i=e.find((e=>e.name===t));return!!i&&{name:i.name,id:i.id,description:i.description}})).filter((e=>!!e)),i=r.join(D(),"fe-groups.json");n.writeJSONSync(i,{groups:t},{spaces:2})}))}function Ce(e){return k(this,void 0,void 0,(function*(){const t=`${G("FEServerDomain")}/api`,{data:i}=yield be({url:`${t}/auth/z-develop/login`,method:"post"});return be({url:t+e.url,headers:{Authorization:`Bearer ${i}`},data:e.data,method:e.method||"post"})}))}function Ie(e,t){const i=[];t.forEach((t=>{e.includes(t.value)&&i.push(t)}));const n=V("weWorkListCache"),o=n.map((e=>e.value));i.forEach((e=>{const t=o.indexOf(e.value);t>-1?n[t].usageCount+=1:n.push(Object.assign(Object.assign({},e),{usageCount:1}))})),K({weWorkListCache:n.sort(((e,t)=>t.usageCount-e.usageCount))}),he()}function Ue(){return k(this,void 0,void 0,(function*(){const{data:e}=yield Ce({url:"/user/list"}),t=e.filter((e=>[1,2,3].includes(e.title))).map((e=>({name:`${e.nick} - ${oe[e.title]}`,value:e.weWorkUserId}))),i=V("weWorkListCache"),n=i.map((e=>e.value)),o=[];return t.forEach((e=>{const t=n.indexOf(e.value);o.push(Object.assign(Object.assign({},e),{usageCount:t>-1?i[t].usageCount:0}))})),o.sort(((e,t)=>t.usageCount-e.usageCount))}))}function Pe(){return k(this,void 0,void 0,(function*(){try{if(I()){const{isRemove:e}=yield p.prompt([{type:"confirm",name:"isRemove",message:"系统中已存在z的配置文件,确认重新配置?"}]);e?i.rm("-rf",C()):process.exit(0)}s.mkdirSync(D(),{recursive:!0});const{jobType:e,account:t,password:n}=yield p.prompt([{type:"list",name:"jobType",message:"请选择岗位类型",choices:[{name:"前端",value:"fe"},{name:"后端",value:"be"}]},{type:"input",name:"account",message:"请输入LDAP账号:"},{type:"input",name:"password",message:"请输入LDAP密码:"}]);R("jobType",e),R("ldapAccount",t),R("ldapPassword",n);F(yield Ne());const o=yield Ue(),{weWork:r}=yield p.prompt([{type:"search-checkbox",name:"weWork",message:"请选择你自己(用于企微通知):",choices:o,validate:e=>e.length>1?"只能选一个":!(e.length<1)||"请选一个"}]),{name:a,value:c}=o.find((e=>e.value===r[0]));R({weWorkName:a,weWorkUserId:c})}catch(e){E(e)}const e=b("配置信息初始化中").start();try{const t=yield $e({url:`${xe()}/user`});R({gitUserId:t.id,gitName:t.name,gitEnglishName:t.username,gitEmail:t.email}),K("latestCheckVersionTimestamp",Date.now()),he(),yield De(),e.succeed("配置信息初始化完成")}catch(t){e.fail("配置信息初始化失败"),c.isAxiosError(t)&&E("请检查你的域名及令牌配置"),E(t)}}))}const Le=".z",Be=(e="")=>r.join(Le,e);let Je;function Ae(e,t){return k(this,void 0,void 0,(function*(){if(e)i.rm("-rf",".z");else if(s.existsSync(Be())){const{isRemove:e}=yield p.prompt([{type:"confirm",name:"isRemove",message:`当前项目中已存在配置文件夹${Le},确认重新配置?`}]);e?i.rm("-rf",".z"):process.exit(0)}s.mkdirSync(Be(),{recursive:!0}),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=yield U("git remote get-url --push origin");i||E("获取项目远程地址失败,请配置后重试");let n=e;if(!n){const{language:e}=yield p.prompt([{type:"list",name:"language",message:"请选择项目语言",choices:[{name:"JavaScript",value:"JavaScript"},{name:"Java",value:"Java"}]}]);n=e}let o=t;if(!o){const e=ue(),t=yield Oe(e.name);if(1===t.length)o=t[0].namespace.path;else if(t.length>1){const{groupName:e}=yield p.prompt([{type:"list",name:"groupName",message:"请选择当前项目所属分组",choices:t.map((e=>({name:e.namespace.path,value:e.namespace.path})))}]);o=e}}Je=b("项目初始化中").start(),de({language:n,"lint-staged":Y[n],repository:{url:i,group:o}})}))}(e,t),yield function(){return k(this,void 0,void 0,(function*(){j.spawnSync("git",["config","core.hooksPath",Le]),s.writeFileSync(Be(".commit-msg-tpl"),"",{mode:493}),j.spawnSync("git",["config","commit.template",r.join(Le,".commit-msg-tpl")]),s.writeFileSync(Be("commit-msg"),"#!/usr/bin/env sh\nz lint commit-msg",{mode:493}),s.writeFileSync(Be("pre-commit"),"#!/usr/bin/env sh\nz lint commit-files",{mode:493}),s.writeFileSync(Be(".prettierrc.json"),Q,{mode:493}),s.writeFileSync(Be(".prettierignore"),H,{mode:493}),s.writeFileSync(Be(".gitignore"),".commit-msg-tpl\n",{mode:493})}))}(),null==Je||Je.succeed("初始化完成。")}))}function We(e){return k(this,void 0,void 0,(function*(){if(!e)return"不能为空";if(!/^[a-z0-9-]+$/.test(e))return"格式为小写字母、中横线(可选)、数字(不推荐)。如apple, apple-tree";if(_().includes(e))return"当前目录下已存在同名文件夹,请先处理。";return!(yield Oe(e)).find((t=>t.name===e))||"远程仓库中已存在同名项目"}))}function Re(e){return!!e||"不能为空"}function Fe(e){return e?!!/^[a-zA-Z0-9]+$/.test(e)||"格式为大小写字母、数字,小驼峰命名。如userInfo、systemRouter3":"不能为空"}function Ke(e){if(!e)return"目录名称不能为空";if(!/^[a-z][a-z0-9\-\/]*$/.test(e))return"支持小写字母、数字,连字符(-)命名,小写字母开头,多层目录使用/隔开。如user、modules-3、setting/profile";if(e.includes("/")){const t=e.split("/");for(const e of t){if(!e)return"子级目录/页面名称不能为空";if(!/^[a-z][a-z0-9\-]*$/.test(e))return"支持小写字母、数字,连字符(-)命名,小写字母开头。如user、modules-3"}}let t,i;t=e.includes("/")?r.resolve("src","pages",...e.split("/")):r.resolve("src","pages",e);const n=r.resolve(t,"..");return i=!!s.existsSync(n)&&s.readdirSync(n).map((e=>{const t=e.lastIndexOf(".");return t>-1?e.slice(0,t):e})).includes(e),!i||"本地目录已存在该文件"}function Me(){return k(this,void 0,void 0,(function*(){var e,o,a,c,l,u;try{const d={},m=()=>k(this,void 0,void 0,(function*(){const e=_(),{projects:t}=yield ze(),i=t.map((t=>{const i={name:`${t.name} [${t.desc}]`,value:t.name,disabled:!1};return e.includes(t.name)&&(i.disabled="目录下已存在同名文件夹"),i})),{project:n}=yield p.prompt([{type:"list",name:"project",message:"请选择一个项目模板",choices:i}]),o=t.find((e=>e.name===n));d.tplName=o.name,d.tplUrl=o.url,d.tplLanguage=o.language})),f=()=>k(this,void 0,void 0,(function*(){var e;const{groups:t}=function(){const e=r.join(D(),"fe-groups.json");return s.existsSync(e)?n.readJSONSync(e):{groups:[]}}(),{group:i}=yield p.prompt([{type:"list",name:"group",message:"请选择一个分组",choices:t.map((e=>({name:`${e.name} [${e.description}]`,value:e.id,short:e.name})))}]);d.group={id:i,name:null===(e=t.find((e=>e.id===i)))||void 0===e?void 0:e.name}})),h=()=>k(this,void 0,void 0,(function*(){const{name:e}=yield p.prompt([{type:"input",name:"name",message:"请输入项目名称",validate:We}]);d.projectName=e;const{desc:t}=yield p.prompt([{type:"input",name:"desc",message:"请输入项目描述",validate:Re}]);d.projectDesc=t}));yield m(),yield f(),yield h();const g=`${se}/${null===(e=d.group)||void 0===e?void 0:e.name}/${d.projectName}.git`,y=b("模版初始化中").start();yield U(`git clone --depth=1 ${d.tplUrl}`),i.mv(d.tplName,d.projectName),i.cd(d.projectName),i.rm("-rf",".git"),yield U(`git init --initial-branch=${ee}`),yield U(`git remote add origin ${g}`),yield U(`git config user.name ${M("gitName")}`),yield U(`git config user.email ${M("gitEmail")}`),u=Object.assign(Object.assign({},ue()),{name:d.projectName,description:d.projectDesc}),ce=ce?Object.assign(Object.assign({},ce),u):u,n.writeJSONSync(r.resolve("package.json"),ce,{spaces:2}),yield n.writeFile(r.resolve("README.md"),function(e="项目中文名",t="项目描述"){return`\n# ${e}\n${t}\n\n## 快速开始\n### 安装依赖\n\`\`\`bash\n npm i\n\`\`\`\n\n### 本地开发\n\`\`\`bash\n npm run dev:{environment}:{platform}\n\`\`\`\n\n> 查看更多关于 [platform](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/nttvbeif4a62gtpz#faWqt) 和 [environment](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/nttvbeif4a62gtpz#faWqt)\n\n### 打包代码\n\`\`\`bash\n npm run build:{environment}:{platform}\n\`\`\`\n\n### 格式化、修复代码语法\n\`\`\`bash\n z lint prettier\n\`\`\`\n\n> [ESLint相关规范](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/ul914hdkqz8fr02g#AkvQ4)\n\n### 格式化文档\n\`\`\`bash\n z lint prettier\n\`\`\`\n\n> [Prettier相关规范](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/ul914hdkqz8fr02g#ojpwI)\n\n## 常见问题\n列出开发中常见的问题和解决方案\n\n## 了解更多\n在此处放入语雀文档链接。请在[前端团队-项目手册](https://zxf-fe.yuque.com/org-wiki-zxf-fe-ex4bve/qi89vg/bfrcicwzosoyaifg)中按照分类新建文档。\n`}(d.projectName,d.projectDesc)),y.succeed("模版初始化完成"),yield Ae(d.tplLanguage,null===(o=d.group)||void 0===o?void 0:o.name);const v=b("依赖安装中").start();yield U("npm install"),v.succeed("依赖安装完成");const j=b("项目推送中").start();yield U('git add . && git commit -m "chore: init"');const $=yield(l={name:d.projectName,description:d.projectDesc,path:d.projectName,visibility:"private",namespace_id:null===(a=d.group)||void 0===a?void 0:a.id},$e({url:`${xe()}/projects`,method:"post",data:l}));yield U(`git push -u origin ${ee}`),j.succeed(`项目已推送到远程,地址: ${t.blue(g)}`);const w=b("初始化分支中").start(),x=`feat_init_${P()}`,S=A(null===(c=d.group)||void 0===c?void 0:c.name)?[...te,x]:[x],z=yield Promise.allSettled(S.map((e=>function(e){return $e({url:`${xe()}/projects/${e.id}/repository/branches`,method:"post",data:e})}({id:$.id,branch:e,ref:ee})))),N=[];z.forEach((({status:e},t)=>{"fulfilled"===e&&N.push(S[t])})),yield U("git pull"),N.includes(x)?(yield U(`git checkout -b ${x} origin/${x}`),w.succeed(`项目已切换到初始分支: ${t.blue(x)}`)):w.warn("开发分支检出失败!项目当前在主分支,请自行检出开发分支。");const O=`cd ${d.projectName} && z start`;console.log(`输入 ${t.green(O)} 开始开发吧~`),process.exit(0)}catch(e){E(e)}}))}e.command("init").alias("i").argument("[type]","非必填。不传为初始化工具配置;传 . 为初始化当前项目。").description("初始化工具配置、项目配置").action((function(e){return k(this,void 0,void 0,(function*(){e?"."===e?(B(),yield Ae(void 0,void 0)):E("参数错误。执行 z init -h 查看帮助。"):yield Pe()}))}));const Ve=e=>`${G("ZenTaoDomain")}${e}`;function Ge(e){return 0!==e.length||"请选择项"}function Qe(){return k(this,void 0,void 0,(function*(){const{type:e}=yield p.prompt({type:"list",name:"type",message:"请选择你要创建的模板类型",choices:[{name:"feat - 业务需求开发",value:"feat"},{name:"fix - bug修复",value:"fix"},{name:"refactor - 技术内部需求",value:"refactor"},{name:"chore - 其他",value:"chore"}]});if(["feat","refactor"].includes(e)){const e=yield function(){return k(this,void 0,void 0,(function*(){const e=yield ke({url:Ve("/my-work-task.json?tid=mrrferp8"),method:"get"});return null==e?void 0:e.tasks.map((({id:e,name:t})=>({value:{name:t,value:e},name:t})))}))}(),{tasks:t}=yield p.prompt({type:"checkbox",name:"tasks",message:"请关联开发任务(可多选):",validate:Ge,choices:e.map((e=>Object.assign(Object.assign({},e),{name:`[${e.value.value}]: ${e.name}`})))});He("feat",t)}else if("fix"===e){const e=yield function(){return k(this,void 0,void 0,(function*(){const e=yield ke({url:Ve("/my-work-bug.json?tid=mrrferp8"),method:"get"});return null==e?void 0:e.bugs.map((({id:e,title:t})=>({name:t,value:{name:t,value:e}})))}))}(),{bugs:t}=yield p.prompt({type:"checkbox",name:"bugs",message:"请关联Bug(可多选):",validate:Ge,choices:e.map((e=>Object.assign(Object.assign({},e),{name:`[${e.value.value}]: ${e.name}`})))});He("fix",t)}else if("chore"===e){const{msg:e}=yield p.prompt({type:"input",name:"msg",message:"请输入commit msg:",validate:e=>0!==e.length||"请输入commit msg"});He("chore",[e])}}))}function He(e,i){let n="";if(["feat","refactor","fix"].includes(e)){n=`${e}(${i.map((e=>e.value)).join(",")}): ${i.map((e=>e.name)).join(";")}`}"chore"===e&&(n=`${e}: ${i[0]}`),s.writeFileSync(r.resolve(".z",".commit-msg-tpl"),n,"utf-8"),console.log(t.yellow(n)),O("commit msg模板写入成功,可以进行提交了")}function Ye(e){return k(this,void 0,void 0,(function*(){try{const{originBranch:t,environment:i,platform:n,notify:o,processId:s}=e,r=pe();!function(e,t){var i,n,o,s,r;const a=ue(),c=pe();(null===(i=null==c?void 0:c.repository)||void 0===i?void 0:i.url)||E(".z/project.json中缺少repository.url");const l=`build:${e}`;(null==a?void 0:a.scripts[l])||E(`项目package.json文件scripts不存在命令${l}。`);["alipay","weapp"].includes(t)&&(a.mini||E("请在package.json配置mini字段"),"alipay"===t&&((null===(o=null===(n=a.mini)||void 0===n?void 0:n.alipay)||void 0===o?void 0:o.appid)||E("请在package.json配置mini.alipay.appid字段")),"weapp"===t&&((null===(r=null===(s=a.mini)||void 0===s?void 0:s.weapp)||void 0===r?void 0:r.appid)||E("请在package.json配置mini.weapp.appid字段")))}(i,n),yield U("git pull"),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=yield U(`git log -b origin/${ee} -1 --format=%H`),n=`origin/${e}`;if((yield U(`git branch --contains ${i} -r ${n}`))||E(`请先将最新的主分支(${ee})代码合并到${n},再进行部署。推荐命令:git merge origin/${ee}`),"production"!==t)return;const o=yield U(`git log -b origin/${e} -1 --format=%H`),s=(yield U(`git log ${i}...${o} -b ${n}`)).split("\n");s.length||E(`分支${e}上没有要发布的commit`);const r=yield U("git log -b origin/test -1 --format=%H");(function(e,t){for(let i of t)if(!e.includes(i))return!1;return!0})((yield U(`git log ${i}...${r} -b origin/test`)).split("\n"),s)||E("请先在测试环境发布要部署的代码")}))}(t,i),yield function(e,t){return k(this,void 0,void 0,(function*(){const i=function(e){const{pathname:t}=new URL(e),i=t.match(/\/(.+)\.git/);if(i)return i[1];E("GitLab URL有误")}(e),{originBranch:n,environment:o,platform:s,notify:r,processId:a}=t;var c;yield(c={params:{command:"build",params:JSON.stringify({originBranch:n,environment:o,platform:s,profile:{weWorkName:M("weWorkName"),weWorkUserId:M("weWorkUserId")}}),options:JSON.stringify({processId:a,notify:r?[...new Set([M("weWorkUserId"),...r])]:[M("weWorkUserId")]})},jobName:i},be({url:`${G("jenkinsDomain")}/job/${c.jobName}/buildWithParameters`,method:"post",dataKey:"params",data:c.params,auth:{username:G("jenkinsUsername"),password:G("jenkinsPassword")}}))}))}(r.repository.url,{originBranch:t,environment:i,platform:n,notify:o,processId:s})}catch(e){E(e)}}))}function Ze(){if(s.existsSync(r.resolve(".z","project.json"))){const e=n.readJsonSync(r.resolve(".z","project.json"));if(e["lint-staged"])return e["lint-staged"];throw new Error("未找到lint配置")}throw new Error("请先初始化项目(z i .)。")}e.command("create").alias("c").argument("[type]","可选值为project|pr, branch|b, commit-msg|cm, page|p, component|co").description("创建项目/分支/提交信息/页面/组件").action((function(e){return k(this,void 0,void 0,(function*(){let t;if(e)["project","pr"].includes(e)?t="project":["branch","b"].includes(e)?t="branch":["page","pa"].includes(e)?t="page":["component","co"].includes(e)?t="component":["commit-msg","cm"].includes(e)?t="commit-msg":E("参数输入错误");else{t=(yield p.prompt({type:"list",name:"type",message:"请选择你要创建的类型",choices:[{name:"提交信息(commit message)",value:"commit-msg"},{name:"分支(branch)",value:"branch"},{name:"页面(page)",value:"page"},{name:"组件(component)",value:"component"},{name:"项目(project)",value:"project"}]})).type}"project"===t?yield Me():"branch"===t?(L(),B(),yield J(),yield function(){return k(this,void 0,void 0,(function*(){L(),B(),yield J();const{type:e,purpose:t}=yield p.prompt([{name:"type",type:"list",message:"请选择创建分支的类型",choices:[{name:"开发新功能(feat)",value:"feat"},{name:"修复BUG(fix)",value:"fix"},{name:"重构/优化代码(refactor)",value:"refactor"}]},{name:"purpose",type:"input",message:"请输入创建分支的目的(大小写字母、数字,小驼峰式命名。如userInfo)",validate:Fe}]),i=`${e}_${t}_${P()}`;yield U(`git fetch origin ${ee}`),yield U(`git checkout -b ${i} origin/${ee}`),yield U("git push -u origin feat_new-feature")}))}()):"commit-msg"===t?(L(),B(),yield Qe()):"page"===t?(L(),B(),yield function(){return k(this,void 0,void 0,(function*(){const e="pages",t=yield ge(e);s.mkdirSync(r.resolve("src",e),{recursive:!0});const{dirPageName:n}=yield p.prompt([{name:"dirPageName",type:"input",message:"请输入目录及页面名称(如user/list,不带文件后缀)",validate:Ke}]);let o;if(n.includes("/")){const t=n.split("/");t.pop(),o=r.resolve("src",e,...n.split("/")),s.mkdirSync(r.resolve("src",e,...t),{recursive:!0})}else o=r.resolve("src",e,n);const{pages:a}=yield ze(),c=s.readdirSync(t),{tplName:l}=yield p.prompt([{name:"tplName",type:"list",message:"请选择模版",choices:a.map((e=>!!c.includes(e.name)&&{name:`${e.name}: ${e.desc}(${e.author})`,value:e.name})).filter((e=>e))}]),u=b("页面生成中...").start(),d=o+r.extname(r.join(t,l));i.cp("-r",r.join(t,l),d),T(d)?q(d).forEach((e=>{ve(r.join(d,e),n)})):ve(d,n),u.succeed("页面生成成功")}))}()):"component"===t&&(L(),B(),yield function(){return k(this,void 0,void 0,(function*(){const e="components",t=yield ge("components");s.mkdirSync(r.resolve("src",e),{recursive:!0});const{dirPageName:n}=yield p.prompt([{name:"dirPageName",type:"input",message:"请输入目录及组件名称(如user/list,不带文件后缀)",validate:Ke}]);let o;if(n.includes("/")){const t=n.split("/"),i=t.pop();o=r.resolve("src",e,...t.split("/"),ye(i)),s.mkdirSync(r.resolve("src",e,...t),{recursive:!0})}else o=r.resolve("src",e,ye(n));const{components:a}=yield ze(),c=s.readdirSync(t),{tplName:l}=yield p.prompt([{name:"tplName",type:"list",message:"请选择模版",choices:a.map((e=>!!c.includes(e.name)&&{name:`${e.name}: ${e.desc}(${e.author})`,value:e.name})).filter((e=>e))}]),u=b("组件生成中...").start(),d=o+r.extname(r.join(t,l));i.cp("-r",r.join(t,l),d),T(d)?q(d).forEach((e=>{ve(r.join(d,e),n)})):ve(d,n),u.succeed("组件生成成功")}))}())}))})),e.command("merge").alias("m").description("合并当前分支到指定远程分支,并部署。").action((function(){return k(this,void 0,void 0,(function*(){var e;L(),B(),yield J();const i=yield fe();ie.includes(i.sourceBranch)&&E(`当前分支${i.sourceBranch}不可作为源分支合并到目标分支。`);const n=yield Ee(i.id),o=[...te],s=A(i.group),r=n.filter((e=>![i.sourceBranch].includes(e.name))).map((e=>{let i=e.name;return s&&o.includes(i)&&(i=t.bold.blue(i)),{name:i,value:e.name}})),{target:a}=yield p.prompt([{type:"list",name:"target",message:"请选择要合并到的目标分支:",choices:r}]);s&&ee===a&&E("业务项目发布正式环境,请使用z deploy命令");const l=s&&o.includes(a);let u="h5",d=[];if(l){const e=yield Ue(),t=yield p.prompt([{type:"list",name:"platform",message:"请选择部署的目标平台",choices:[{name:"网页 - h5",value:"h5"},{name:"依赖包 - npm",value:"npm"}]},{type:"search-checkbox",name:"notify",message:"请选择部署成功要通知的人员:",choices:e,validate:e=>!(e.length<1)||"至少选一个"}]);Ie(d,e),u=t.platform,d=t.notify}const m=b(`${i.sourceBranch}分支推送中`).start();(yield U(`git ls-remote --heads origin ${i.sourceBranch}`))&&(yield U("git pull")),yield U(`git push -u origin ${i.sourceBranch}`),m.succeed(`${i.sourceBranch}已推送到远程`);const f=b("分支合并中").start();let h=0;try{const{iid:e}=yield(g={title:"MR by z-develop",id:i.id,source_branch:i.sourceBranch,target_branch:a,remove_source_branch:ee===a},$e({url:`${xe()}/projects/${g.id}/merge_requests`,method:"post",data:g}));h=e}catch(t){if(c.isAxiosError(t)){409===(null===(e=null==t?void 0:t.response)||void 0===e?void 0:e.status)&&E(`存在重复的合并请求,前往查看${i.mergeRequestUrl}`)}E(t)}var g;const y=()=>k(this,void 0,void 0,(function*(){var e;try{yield new Promise((e=>{setTimeout(e,5e3)})),yield function(e){return $e({url:`${xe()}/projects/${e.id}/merge_requests/${e.iid}/merge`,method:"put"})}({id:i.id,iid:h})}catch(t){if(c.isAxiosError(t)){406===(null===(e=null==t?void 0:t.response)||void 0===e?void 0:e.status)?yield y():(yield function(e){return $e({url:`${xe()}/projects/${e.id}/merge_requests/${e.iid}`,method:"put",data:e})}({id:i.id,iid:h,state_event:"close"}),f.fail("合并过程中出现冲突,MR已关闭。建议在本地完成合并。"),process.exit(1))}else E(t)}}));yield y(),f.succeed("分支合并成功"),ee===a&&(yield U(`git checkout ${ee}`),yield U("git pull"),yield U(`git branch -d ${i.sourceBranch}`),O(`\n${i.sourceBranch}分支已移除,当前已切换到最新的${ee}。\n如需继续开发,请检出新分支(z c b)。\n`)),l&&(yield Ye({originBranch:a,environment:a,platform:u,notify:d,processId:""}))}))})),e.command("deploy").alias("d").description("发布指定远程分支").action((function(){return k(this,void 0,void 0,(function*(){L(),B(),yield J();const e=yield Ue(),{environment:t,platform:i,notify:n}=yield p.prompt([{type:"list",name:"environment",message:"请选择部署环境",choices:Z},{type:"list",name:"platform",message:"请选择平台",choices:X},{type:"search-checkbox",name:"notify",message:"请选择部署成功要通知的人员:",choices:e,validate:e=>!(e.length<1)||"至少选一个"}]);Ie(n,e);let o=t,s="";if("production"===t){const e=yield fe(),t=(yield Ee(e.id)).filter((e=>!ie.includes(e.name))).map((e=>({name:e.name,value:e.name}))),n=yield p.prompt([{type:"list",name:"branchName",message:"请输入要部署的分支:",choices:t},{type:"input",name:"processId",message:"请输入本次部署的企微审批编号"}]);o=n.branchName,s=n.processId,function(e){const t=e.split("_");if(4!==t.length)return!1;const[i,n,o]=t;return!!ne.includes(i)&&!0===Fe(n)&&8===o.length&&!isNaN(Number(o))}(o)||E("分支命名不符合规范,参见https://hxhtbr8t8uy.feishu.cn/docx/Y9cSdowEQoNdzRxWtYScizMFnFh");const{data:{state:a,content:c}}=yield(r={businessId:s},Ce({url:"/approval/findByBusinessId",data:r}));1===a&&E("当前审批流程状态为审批中,审批通过才能进行发布。"),3===a&&E("当前审批流程状态为已驳回,请重新提交审批。"),4===a&&E("当前审批流程状态为已发布,如需再次发布,请重新提交审批。");const{formComponentValues:l}=JSON.parse(c),u={};l.forEach((e=>{u[e.name]=e.value})),u["申请人"]!==M("weWorkName")&&E("发布审批申请人与当前发布人不一致!"),u["项目名称"]!==ue().name&&E("发布审批项目名称与当前发布项目名称不一致!"),u["发布分支"]!==o&&E("发布审批发布分支与当前发布分支不一致!"),u["发布平台"]!==i&&E("发布审批发布平台与当前发布平台不一致!")}var r;yield Ye({originBranch:o,environment:t,platform:i,notify:n,processId:s})}))})),e.command("start").alias("s").description("启动本地开发环境").action((function(){return k(this,void 0,void 0,(function*(){const e=ue(),{environment:t}=yield p.prompt([{type:"list",name:"environment",message:"请选择环境",choices:Z}]),n=`dev:${t}`;if(e.scripts[n])try{i.exec(`npm run ${n}`,{silent:!1})}catch(e){E(e)}else E(`项目中(package.josn > scripts)不存在命令${n},请先添加!`)}))})),e.command("lint").alias("l").argument("[type]","非必填。可选值为commit-msg|cm, commit-files|cf, prettier|p, eslint|e。").description("执行 lint 脚本。包含 lint staged 、 lint commit message、 prettier、 eslint。").action((function(e){return k(this,void 0,void 0,(function*(){let t;if(L(),B(),e)["commit-msg","cm"].includes(e)?t="commit-msg":["commit-files","cf"].includes(e)?t="commit-files":["prettier","p"].includes(e)?t="prettier":["eslint","e"].includes(e)?t="eslint":E("参数输入错误");else{t=(yield p.prompt({type:"list",name:"type",message:"请选择你要执行的操作",choices:[{name:"全局执行 prettier",value:"prettier"},{name:"全局执行 eslint",value:"eslint"}]})).type}if("commit-msg"===t)yield function(){return k(this,void 0,void 0,(function*(){const e=function(e){const t=s.readFileSync(r.resolve(".git","COMMIT_EDITMSG"),"utf-8").split(": ");if(1===t.length)return"body前缺少「:」";{const e=t[0];return/^(feat|fix|refactor)/.test(e)?!!/\(\d+(?:,\d+)*\)/.test(e)||"ID缺少或者格式不正确。":/^chore/.test(e)?"chore"===e||"chore类型无需填写ID。":"不存在的提交类型。"}}();!0===e?O("commit msg validate success."):E(`commit msg格式错误。${e}`)}))}();else if("commit-files"===t)yield function(){return k(this,void 0,void 0,(function*(){(yield $({concurrent:!1,debug:void 0,config:Ze()}))?O("代码风格检测通过!"):E("代码风格检测未通过!")}))}();else if("prettier"===t){s.existsSync(r.resolve("node_modules",".bin","prettier"))||E("该项目未安装prettier,请安装后重试");try{yield U("npx prettier --write . --config .z/.prettierrc.json --ignore-path .z/.prettierignore --ignore-unknown --no-error-on-unmatched-pattern",{silent:!1}),O("prettier执行成功")}catch(e){E("prettier校验出错")}}else if("eslint"===t){console.log(r.resolve("node_modules",".bin","eslint")),s.existsSync(r.resolve("node_modules",".bin","eslint"))||E("该项目未安装eslint,请安装后重试");try{let e="npx eslint '**/*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}' --fix";"win32"===process.platform&&(e='npx eslint "**/*.{vue,js,jsx,cjs,mjs,ts,tsx,cts,mts}" --fix'),yield U(e,{silent:!1}),O("eslint执行成功")}catch(e){E("eslint校验出错")}}}))})),function(){k(this,void 0,void 0,(function*(){process.on("unhandledRejection",(e=>{})),process.on("uncaughtException",(e=>{})),i.config.fatal=!0,i.config.silent=!0,i.config.verbose=!1,yield qe(),p.registerPrompt("search-checkbox",_e)}))}(),e.name("z-develop").alias("z").description(`z-develop, 开发流程管理工具。\n了解更多: ${t.blue("https://hxhtbr8t8uy.feishu.cn/docx/RnOhdtqJWo5UooxHTaUcQFwtnKg")}`).usage("<command> [options]").version(w.version,"-v, --version","当前版本号").helpOption("-h, --help","帮助").showHelpAfterError("可以使用z -h查看帮助。"),e.parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-z-develop",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "前端本地开发命令行工具",
5
5
  "main": "dist/index.js",
6
6
  "bin": {