UNPKG

gchcg-cli

Version:
1 lines 1.46 kB
const fse=require("fs-extra"),axios=require("axios"),{readFile:readFile,spinnerStart:spinnerStart,sleep:sleep}=require("./utils"),log=require("./log"),GIT_ROOT_DIR=".git",GIT_TOKEN_FILE=".git_token";function getGroups(e,t){return axios.get(`http://10.96.105.158/gitlab/api/v4/groups/${e}/subgroups?access_token=${t}`,{timeout:3e4}).then((e=>(e.data.forEach((e=>{e.type="group",e.children=[]})),e.data.sort(((e,t)=>e.id>t.id?1:-1)))))}module.exports=function(){return new Promise((async e=>{let t=process.env.CLI_HOME_PATH;if(t||(t=path.resolve(userHome,DEFAULT_CLI_HOME)),fse.ensureDirSync(t),!fs.existsSync(t))throw new Error("用户主目录获取失败!");const r=path.resolve(t,".git"),s=path.resolve(r,".git_token");let n=readFile(s),i=[],o=spinnerStart("正在获取项目分组信息...");await sleep();try{const e=await getGroups(401,n);e.length&&i.push(...e)}catch(e){}if(0===i.length)return o.stop(!0),void e();const l=[...i.filter((e=>"group"===e.type))];let a=0;for(;a<l.length;){const e=l[a];try{const t=await getGroups(e.id,n);e.len=t.length,t.length&&e.children.push(...t)}catch(e){}if(e.children.length){const t=e.children.filter((e=>"group"===e.type));t.length&&l.push(...t)}a++}o.stop(!0),log.success("项目分组信息获取成功");const p=[];for(;i.length;){const e=i.shift();for(Array.isArray(e.pName)||(e.pName=[]);e.children.length;){const t=e.children.shift();t.pName=[...e.pName,e.name],i.push(t)}delete e.children,p.push(e)}e(p)}))};