@@ -19,53 +19,64 @@ const __filename = fileURLToPath(import.meta.url);
1919export class GenerateCommand extends Command {
2020 config = Option . String ( `-c,--config` , {
2121 description : "Configuration file path" ,
22+ env : "OPENAPI_CODEGEN_CONFIG" ,
2223 } ) ;
2324
2425 namespace = Option . String ( ) ;
2526
2627 source = Option . String ( `--source` , {
2728 description : "Source of the spec (file, url or github)" ,
29+ env : "OPENAPI_CODEGEN_SOURCE" ,
2830 validator : t . isEnum ( [ "file" , "url" , "github" ] ) ,
2931 } ) ;
3032
3133 // source=file options
3234 relativePath = Option . String ( `--relativePath` , {
3335 description : "[source=file] Relative path of the spec file" ,
36+ env : "OPENAPI_CODEGEN_FILE_PATH" ,
3437 } ) ;
3538
3639 // source=url options
3740 url = Option . String ( "--url" , {
3841 description : "[source=url] URL of the spec file" ,
42+ env : "OPENAPI_CODEGEN_URL" ,
3943 } ) ;
4044 method = Option . String ( "--method" , {
4145 description : "[source=url] HTTP Method" ,
46+ env : "OPENAPI_CODEGEN_URL_METHOD" ,
4247 validator : t . isEnum ( [ "get" , "post" ] ) ,
4348 } ) ;
4449
4550 // source=github options
4651 owner = Option . String ( "--owner" , {
4752 description : "[source=github] Owner of the repository" ,
53+ env : "OPENAPI_CODEGEN_GITHUB_OWNER" ,
4854 } ) ;
49- repository = Option . String ( "--repository --repo" , {
55+ repository = Option . String ( "--repository, --repo" , {
5056 description : "[source=github] Repository name" ,
57+ env : "OPENAPI_CODEGEN_GITHUB_REPOSITORY" ,
5158 } ) ;
52- branch = Option . String ( "-b --branch" , {
53- description : "[source=github] Branch name" ,
59+ ref = Option . String ( "--ref" , {
60+ description : "[source=github] Git reference (commit sha, branch or tag)" ,
61+ env : "OPENAPI_CODEGEN_GITHUB_REF" ,
5462 } ) ;
5563 specPath = Option . String ( "--specPath" , {
5664 description : "[source=github] OpenAPI specs file path" ,
65+ env : "OPENAPI_CODEGEN_GITHUB_SPEC_PATH" ,
5766 } ) ;
58- pullRequest = Option . String ( "--pr --pull-request" , {
59- description :
60- "[source=github] Select a specific pull-request instead of a branch" ,
67+ pullRequest = Option . String ( "--pr,--pull-request" , {
68+ description : "[source=github] Select a specific pull-request as ref" ,
69+ env : "OPENAPI_CODEGEN_GITHUB_PULL_REQUEST" ,
70+ validator : t . isNumber ( ) ,
71+ tolerateBoolean : true ,
6172 } ) ;
6273
6374 static paths = [ [ "gen" ] , [ "generate" ] , Command . Default ] ;
6475 static usage = Command . Usage ( {
6576 description : "Generate types & components from an OpenAPI file" ,
6677 examples : [
6778 [ `From a config key` , `$0 gen myapi` ] ,
68- [ `With some override` , `$0 gen myapi --branch awesome-feature` ] ,
79+ [ `With some override` , `$0 gen myapi --ref awesome-feature` ] ,
6980 ] ,
7081 } ) ;
7182
@@ -159,16 +170,16 @@ export class GenerateCommand extends Command {
159170 return {
160171 ...config . from ,
161172 owner : this . owner ?? config . from . owner ,
162- branch : this . branch ?? config . from . branch ,
173+ ref : this . ref ?? config . from . ref ,
163174 repository : this . repository ?? config . from . repository ,
164175 specPath : this . specPath ?? config . from . specPath ,
165176 } ;
166177 } else {
167178 if ( ! this . owner ) {
168179 throw new UsageError ( "--owner argument is missing" ) ;
169180 }
170- if ( ! this . branch ) {
171- throw new UsageError ( "--branch argument is missing" ) ;
181+ if ( ! this . ref && ! this . pullRequest ) {
182+ throw new UsageError ( "--ref argument is missing" ) ;
172183 }
173184 if ( ! this . repository ) {
174185 throw new UsageError ( "--repository argument is missing" ) ;
@@ -179,7 +190,7 @@ export class GenerateCommand extends Command {
179190
180191 return {
181192 source : "github" ,
182- branch : this . branch ,
193+ ref : this . ref || "main" , // Fallback for --pr mode
183194 owner : this . owner ,
184195 repository : this . repository ,
185196 specPath : this . specPath ,
@@ -197,7 +208,24 @@ export class GenerateCommand extends Command {
197208 }
198209
199210 const config = configs [ this . namespace ] ;
200- const sourceFile = await getOpenAPISourceFile ( this . getFromOptions ( config ) ) ;
211+ const options = this . getFromOptions ( config ) ;
212+ if ( options . source === "github" && this . pullRequest ) {
213+ const { Prompt } = await import ( "../prompts/Prompt.js" ) ;
214+ const prompt = new Prompt ( ) ;
215+ const token = await prompt . githubToken ( ) ;
216+ const pullRequest = await prompt . githubPullRequest ( {
217+ ...options ,
218+ token,
219+ pullRequestNumber :
220+ typeof this . pullRequest === "number" ? this . pullRequest : undefined ,
221+ } ) ;
222+
223+ options . ref = pullRequest . ref ;
224+ options . owner = pullRequest . owner ;
225+ options . repository = pullRequest . repository ;
226+ }
227+
228+ const sourceFile = await getOpenAPISourceFile ( options ) ;
201229 const openAPIDocument = await parseOpenAPISourceFile ( sourceFile ) ;
202230 const prettierConfig = await prettier . resolveConfig ( process . cwd ( ) ) ;
203231
0 commit comments