@toneflix/grithub 0.1.3 → 0.1.4

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/bin/cli.js +1 -1
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -16,7 +16,7 @@ Labels:`,[`white`,`bold`]],[e.labels.map(e=>e.name??e).join(`, `),[`blue`]],[`
16
16
  Assignees:`,[`white`,`bold`]],[e.assignees?.map(e=>e.login??e).join(`, `)||`N/A`,[`blue`]],[`
17
17
  Created at:`,[`white`,`bold`]],[new Date(e.created_at).toLocaleString(),[`blue`]],[`
18
18
  Updated at:`,[`white`,`bold`]],[new Date(e.updated_at).toLocaleString(),[`blue`]]],` `)},K=(e=B)=>{let t=e;for(;;){let e=o.join(t,`package.json`);if(n(e))return e;let r=o.dirname(t);if(r===t)break;t=r}return null},q=async e=>{let t=l.createInterface({input:process.stdin,output:process.stdout});await t.question(``),e(),t.close()};var se={issues:[{api:`create`,alias:void 0,endpoint:`/repos/{owner}/{repo}/issues`,description:`Create an issue`,params:[{parameter:`title`,required:!0,type:`String`,description:`The title of the issue`,paramType:`body`,flag:!0},{parameter:`body`,required:!1,type:`String`,description:`The contents of the issue`,paramType:`body`,flag:!0},{parameter:`owner`,required:!1,type:`String`,description:`The account owner of the repository`,paramType:`path`,arg:!0},{parameter:`repo`,required:!1,type:`String`,description:`The name of the repository`,paramType:`path`,arg:!0}]},{api:`listForRepo`,alias:`list`,endpoint:`/repos/{owner}/{repo}/issues`,description:`List repository issues`,params:[{parameter:`owner`,required:!1,type:`String`,description:`The account owner of the repository`,paramType:`path`},{parameter:`repo`,required:!1,type:`String`,description:`The name of the repository`,paramType:`path`},{parameter:`state`,required:!1,type:`String`,description:`Indicates the state of the issues to return. [open, closed]`,paramType:`query`}]},{api:`get`,alias:`get`,endpoint:`/repos/{owner}/{repo}/issues/{issue_number}`,description:`Get a single issue`,params:[{parameter:`issue_number`,required:!0,type:`Number`,description:`The number of the issue to get`,paramType:`path`},{parameter:`owner`,required:!1,type:`String`,description:`The account owner of the repository`,paramType:`path`},{parameter:`repo`,required:!1,type:`String`,description:`The name of the repository`,paramType:`path`}]}],orgs:[{api:`listForAuthenticatedUser`,alias:`list`,endpoint:`/user/orgs`,description:`List organizations for the authenticated user`,params:[{parameter:`page`,required:!1,type:`Number`,description:`Page number of the results to fetch`,paramType:`query`},{parameter:`per_page`,required:!1,type:`Number`,description:`Results per page (max 100)`,paramType:`query`}]}]};const ce=(e,t)=>{let[n,r]=L(),i=``;if((!e.required||e.default!==void 0||e.type===`Boolean`||e.options||e.flag===!0)&&e.paramType!==`path`&&e.arg!==!0){if(i+=`{--`,r(t+`:`+e.parameter.charAt(0).toLowerCase()))i+=`${e.parameter.charAt(0).toLowerCase()}|`;else{let n=e.parameter.split(/[_-\s]/);n.length>1&&r(t+`:`+n[1].charAt(0).toLowerCase())&&(i+=`${n[1].charAt(0).toLowerCase()}|`)}if(i+=`${e.parameter}`,e.type!==`Boolean`&&(i+=e.default?`=${e.default}`:`?`),e.description&&(i+=` : ${e.description}`),e.options){let t=e.options.join(`,`);i+=` : ${t}`}i+=`}`}else i+=`{${e.parameter}`,e.default&&(i+=`=${e.default}`),e.description&&(i+=` : ${e.description}`),i+=`}`;return i},le=e=>{let n=(e,r=0)=>{let i=` `.repeat(r);for(let a in e){let o=e[a];if(typeof o==`object`&&o)console.log(`${i}${J(a)}:`),n(o,r+2);else{let e;switch(typeof o){case`string`:e=t.log(o,`green`,!1);break;case`number`:e=t.log(String(o),`yellow`,!1);break;case`boolean`:e=t.log(String(o),`blue`,!1);break;case`object`:e=o===null?t.log(`null`,`gray`,!1):t.log(JSON.stringify(o),`cyan`,!1);break;default:e=o}console.log(`${i}${J(a)}: ${e}`)}}};n(e)},J=e=>e.replace(/([a-z])([A-Z])/g,`$1 $2`).replace(/[_-]+/g,` `).replace(/\s+/g,` `).split(` `).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(` `).trim().replace(/^(\w{2})$/,(e,t)=>t.toUpperCase()),ue=(e,t)=>ee(t,e).map(e=>{let[t,n]=e;return t===0?n:t===-1?W(n,[`red`,`strikethrough`],!1):W(n,[`green`,`underline`],!1)}).join(``);var de=()=>{let t=e(import.meta.url),r=[],i=se;!process.argv.includes(`generate:apis`)&&n(o.join(process.cwd(),`.grithub/apis.generated.js`))&&({APIs:i}=t(o.join(process.cwd(),`.grithub/apis.generated.js`)));let a=Object.entries(i).reduce((e,[t,n])=>(n.forEach(n=>{let r=t===n.api?t:`${t}:${(n.alias??n.api).toKebabCase()}`;e[r]=n}),e),{});for(let[e,t]of Object.entries(a)){let n=t.params.map(t=>ce(t,e)).join(`
19
- `),i=class extends y{signature=`${e} \n${n}`;description=t.description||`No description available.`;handle=async()=>{let n=e.split(`:`).shift(),r={...this.arguments()??{},...this.options()??{}},[i,a]=P();if(a(this),!n)return void this.error(`Unknown command entry.`).newLine();for(let e of t.params)if(e.required&&!this.argument(e.parameter))return void this.newLine().error(`Missing required argument: ${e.parameter}`).newLine();let o=M(`default_repo`),s=M(`token`),c=([r.owner,r.repo].filter(Boolean).join(`/`)||o.full_name).split(`/`)??[``,``],l=t.params.some(e=>[`repo`,`user`].includes(e.parameter));if(l&&(!c[0]||!c[1]))return void this.error(`ERROR: No repository set. Please set a default repository using the [set-repo] command or provide one using the --repo option.`).newLine();if(!s)return void this.error(`ERROR: You're not signed in, please run the [login] command before you begin`).newLine();this.newLine();let u=this.spinner(`Loading...
19
+ `),i=class extends y{signature=`${e} \n${n}`;description=t.description||`No description available.`;handle=async()=>{let n=e.split(`:`).shift(),r={...this.arguments()??{},...this.options()??{}},[i,a]=P();if(a(this),!n)return void this.error(`Unknown command entry.`).newLine();for(let e of t.params)if(e.required&&!this.argument(e.parameter))return void this.newLine().error(`Missing required argument: ${e.parameter}`).newLine();let o=M(`default_repo`)??{},s=M(`token`),c=([r.owner,r.repo].filter(Boolean).join(`/`)||o.full_name).split(`/`)??[``,``],l=t.params.some(e=>[`repo`,`user`].includes(e.parameter));if(l&&(!c[0]||!c[1]))return void this.error(`ERROR: No repository set. Please set a default repository using the [set-repo] command or provide one using the --repo option.`).newLine();if(!s)return void this.error(`ERROR: You're not signed in, please run the [login] command before you begin`).newLine();this.newLine();let u=this.spinner(`Loading...
20
20
  `).start();l&&(r.owner=c[0],r.repo=c[1]);let[d,f]=await V(H(n,t,r));if(d||!f)return void u.fail((d||`An error occurred`)+`
21
21
  `);u.succeed(f.message),this.newLine(),le(f.data),this.newLine()}};r.push(i)}return r};const fe=e=>[{name:`Debug Mode`,value:`debug`,description:`Enable or disable debug mode (${e.debug?`Enabled`:`Disabled`})`},{name:`API Base URL`,value:`apiBaseURL`,description:`Set the base URL for the API (${e.apiBaseURL})`},{name:`Timeout Duration`,value:`timeoutDuration`,description:`Set the timeout duration for API requests (${e.timeoutDuration} ms)`},{name:`Skip Long Command Generation`,value:`skipLongCommandGeneration`,description:`Enable or disable skipping of long command generation when calling ${W(`generate:apis`,[`grey`,`italic`])} (${e.skipLongCommandGeneration?`Enabled`:`Disabled`})`},{name:`Ngrok Auth Token`,value:`ngrokAuthToken`,description:`Set the Ngrok Auth Token - will default to environment variable if not set (${e.ngrokAuthToken?`************`:`Not Set`})`},{name:`Reset Configuration`,value:`reset`,description:`Reset all configurations to default values`}],pe=async e=>{let[t,n]=F(),[r]=P(),i=t();if(e===`debug`){let e=await r().confirm(`${i.debug?`Dis`:`En`}able debug mode?`,i.debug===!0);i.debug=i.debug!==e}else if(e===`apiBaseURL`)i.apiBaseURL=await r().ask(`Enter API Base URL`,i.apiBaseURL);else if(e===`ngrokAuthToken`)i.ngrokAuthToken=await r().ask(`Enter Ngrok Auth Token`,i.ngrokAuthToken||``);else if(e===`timeoutDuration`){let e=await r().ask(`Enter Timeout Duration (in ms)`,i.timeoutDuration.toString());i.timeoutDuration=parseInt(e)}else e===`skipLongCommandGeneration`?i.skipLongCommandGeneration=await r().confirm(`${i.skipLongCommandGeneration?`Dis`:`En`}able skipping of long command generation?`,i.skipLongCommandGeneration===!0):e===`reset`&&(i={debug:!1,apiBaseURL:`https://api.github.com`,timeoutDuration:3e3,skipLongCommandGeneration:!0});n(i)};var me=class extends y{signature=`config`;description=`Configure Grithub`;async handle(){let[e,t]=P();t(this);let[n,r]=F(),i=n();i||(i={debug:!1,apiBaseURL:`https://api.github.com`,timeoutDuration:3e3,skipLongCommandGeneration:!0},r(i)),await pe(await this.choice(`Select configuration to set`,fe(i))),this.info(`Configuration updated successfully!`).newLine()}},he=class e{spec;config;openapi;skipApis=new Set([`issues:list`,`issues:update`,`issues:seed`,`issues:delete`]);skipParams=new Set([`s`]);PARAM_LOCATIONS=new Set([`path`,`query`,`header`]);constructor(e,t=`api.github.com.deref`){let[n]=F();if(this.openapi=e,this.spec=this.openapi.schemas[t],this.config=n(),!this.spec||!this.spec.paths)throw Error(`Could not find ${t} schema`)}static async installOctokitOpenapi(){let e=P()[0]().spinner(`Installing @octokit/openapi...`).start(),t=s(a(import.meta.url));return await te(`@octokit/openapi`,{cwd:o.normalize(o.join(t,`../..`)),silent:!0,dev:!0}),e.succeed(`@octokit/openapi installed successfully.`),(await import(`@octokit/openapi`)).default}skipParam(e){return this.skipParams.has(e)||e.length>20||e.length<=2}skipApi(e,t){let n=(t?t+`:`:``)+e.toCamelCase();return this.skipApis.has(n)||this.skipApis.has(e.toCamelCase())||n.length>(this.config.skipLongCommandGeneration?23:1/0)}normalizeType(e){let t={integer:`Number`,number:`Number`,string:`String`,boolean:`Boolean`,array:`Array`,object:`Object`,enum:`String`,oneOf:`String`,anyOf:`String`,allOf:`String`},n=t[e?.type]||`any`;return Array.isArray(e?.type)&&(n=e.type.map(e=>t[e]||`any`).join(`|`)),n===`any`?(e||(n=`any`),Array.isArray(e.type)?e.type.join(`|`):(e.type&&(n=e.type),e.enum&&(n=`enum`),e.oneOf&&(n=`oneOf`),e.anyOf&&(n=`anyOf`),e.allOf&&(n=`allOf`),t[n]||`any`)):n}gatherParams(e){let t=[];for(let n of e.parameters??[]){let e=this.PARAM_LOCATIONS.has(n.in)?n.in:`query`;this.skipParam(n.name)||t.push({parameter:n.name,required:!!n.required,type:this.normalizeType(n.schema).toPascalCase(),description:n.description,paramType:e})}let n=e.requestBody?.content?.[`application/json`],r=n?.schema,i=r?.properties??{},a=r?.required??[];for(let[e,r]of Object.entries(i))this.skipParam(e)||t.push({parameter:e,required:a.includes(e)||!!n?.required,type:this.normalizeType(r).toPascalCase(),description:r.description,paramType:`body`});return t}buildTree(){let e={};for(let[t,n]of Object.entries(this.spec.paths))for(let[r,i]of Object.entries(n??{})){let n=i,r=n?.operationId;if(!r)continue;let[a,o]=r.split(`/`);if(!a||!o||this.skipApi(o,a))continue;let s=this.gatherParams(n);e[a.toCamelCase()]||(e[a.toCamelCase()]=[]),e[a.toCamelCase()].push({api:o.toCamelCase(),endpoint:t,description:n.summary??n.description??void 0,alias:n[`x-github`]?.alias??n[`x-octokit`]?.alias??void 0,params:s})}return e}static async run(){let[t]=P(),n=t(),a,s=n.spinner(`Checking if @octokit/openapi Installed...`).start();try{({default:a}=await import(`@octokit/openapi`)),s.succeed(`@octokit/openapi is already installed.`)}catch{s.fail(`@octokit/openapi is not installed.`),a=await e.installOctokitOpenapi()}s.start(`Generating Extended APIs...`);let c=new e(a,`api.github.com.deref`).buildTree(),l=o.join(process.cwd(),`.grithub/apis.generated.js`),u=`// Auto-generated from @octokit/openapi. Do not edit directly.
22
22
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@toneflix/grithub",
3
3
  "type": "module",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "description": "Command-line toolkit that blends hand-crafted workflows with OpenAPI-generated commands for GitHub.",
6
6
  "main": "bin/cli.js",
7
7
  "private": false,