16
16
// and GitHub Releases:
17
17
18
18
import { ReleasePR } from './release-pr' ;
19
- import { GitHubRelease , ReleaseResponse } from './github-release' ;
19
+ import { GitHubRelease , GitHubReleaseResponse } from './github-release' ;
20
20
import { ReleaseType , getReleasers } from './releasers' ;
21
- import { GitHub } from './github' ;
21
+ import { GitHub , GitHubTag } from './github' ;
22
22
import {
23
23
ReleasePRFactoryOptions ,
24
24
GitHubReleaseFactoryOptions ,
@@ -28,26 +28,98 @@ import {DEFAULT_LABELS} from './constants';
28
28
// eslint-disable-next-line @typescript-eslint/no-var-requires
29
29
const parseGithubRepoUrl = require ( 'parse-github-repo-url' ) ;
30
30
31
+ // types defining methods available to call on instances
32
+ export type ReleasePRMethod = 'run' | 'latestTag' ;
33
+ export type GitHubReleaseMethod = 'run' ;
34
+ export type Method = ReleasePRMethod | GitHubReleaseMethod ;
35
+
36
+ // types defining cli commands and their options
37
+ export type ReleasePRCommands = 'release-pr' | 'latest-tag' ;
38
+ export type GitHubReleaseCommands = 'github-release' ;
39
+ type Command = ReleasePRCommands | GitHubReleaseCommands ;
40
+ type IsReleasePRCmd = {
41
+ command : Command ;
42
+ options : ReleasePRFactoryOptions ;
43
+ } ;
44
+ type IsGitHubReleaseCmd = {
45
+ command : Command ;
46
+ options : GitHubReleaseFactoryOptions ;
47
+ } ;
48
+
49
+ // shorthand aliases for instance/method call return types
50
+ export type ReleasePRRunResult = Promise < number | GitHubTag | undefined > ;
51
+ export type GitHubReleaseRunResult = Promise < GitHubReleaseResponse | undefined > ;
52
+ export type RunResult = ReleasePRRunResult | GitHubReleaseRunResult ;
53
+
54
+ function isGitHubReleaseCmd (
55
+ cmdOpts : IsReleasePRCmd | IsGitHubReleaseCmd
56
+ ) : cmdOpts is IsGitHubReleaseCmd {
57
+ const { command, options} = cmdOpts ;
58
+ return command === 'github-release' && typeof options === 'object' ;
59
+ }
60
+
61
+ function isReleasePRCmd (
62
+ cmdOpts : IsReleasePRCmd | IsGitHubReleaseCmd
63
+ ) : cmdOpts is IsReleasePRCmd {
64
+ const { command, options} = cmdOpts ;
65
+ return (
66
+ ( command === 'release-pr' || command === 'latest-tag' ) &&
67
+ typeof options === 'object'
68
+ ) ;
69
+ }
70
+
31
71
function runCommand (
32
- command : string ,
72
+ command : ReleasePRCommands ,
73
+ options : ReleasePRFactoryOptions
74
+ ) : ReleasePRRunResult ;
75
+ function runCommand (
76
+ command : GitHubReleaseCommands ,
77
+ options : GitHubReleaseFactoryOptions
78
+ ) : GitHubReleaseRunResult ;
79
+ function runCommand (
80
+ command : Command ,
33
81
options : GitHubReleaseFactoryOptions | ReleasePRFactoryOptions
34
- ) : Promise < number | undefined | ReleaseResponse > {
35
- if ( isGitHubRelease ( command , options ) ) {
36
- return factory . run ( githubRelease ( options ) ) ;
82
+ ) : RunResult {
83
+ let result : RunResult ;
84
+ const cmdOpts = { command, options} ;
85
+ if ( isReleasePRCmd ( cmdOpts ) ) {
86
+ let method : ReleasePRMethod = 'run' ;
87
+ if ( command === 'latest-tag' ) {
88
+ method = 'latestTag' ;
89
+ }
90
+ result = factory . call ( releasePR ( cmdOpts . options ) , method ) ;
91
+ } else if ( isGitHubReleaseCmd ( { command, options} ) ) {
92
+ result = factory . call ( githubRelease ( cmdOpts . options ) , 'run' ) ;
37
93
} else {
38
- return factory . run ( releasePR ( options ) ) ;
94
+ throw new Error (
95
+ `Invalid command(${ cmdOpts . command } ) with options(${ JSON . stringify (
96
+ cmdOpts . options
97
+ ) } )`
98
+ ) ;
39
99
}
100
+ return result ;
40
101
}
41
102
42
- function isGitHubRelease (
43
- command : string ,
44
- options : GitHubReleaseFactoryOptions | ReleasePRFactoryOptions
45
- ) : options is GitHubReleaseFactoryOptions {
46
- return command === 'github-release' && typeof options === 'object' ;
47
- }
48
-
49
- function run ( runnable : ReleasePR | GitHubRelease ) {
50
- return runnable . run ( ) ;
103
+ function call ( instance : ReleasePR , method : ReleasePRMethod ) : ReleasePRRunResult ;
104
+ function call (
105
+ instance : GitHubRelease ,
106
+ method : GitHubReleaseMethod
107
+ ) : GitHubReleaseRunResult ;
108
+ function call ( instance : ReleasePR | GitHubRelease , method : Method ) : RunResult {
109
+ if ( ! ( method in instance ) ) {
110
+ throw new Error (
111
+ `No such method(${ method } ) on ${ instance . constructor . name } `
112
+ ) ;
113
+ }
114
+ let result : RunResult ;
115
+ if ( instance instanceof ReleasePR ) {
116
+ result = instance [ method as ReleasePRMethod ] ( ) ;
117
+ } else if ( instance instanceof GitHubRelease ) {
118
+ result = instance [ method as GitHubReleaseMethod ] ( ) ;
119
+ } else {
120
+ throw new Error ( 'Unknown instance.' ) ;
121
+ }
122
+ return result ;
51
123
}
52
124
53
125
function getLabels ( label ?: string ) : string [ ] {
@@ -157,6 +229,6 @@ export const factory = {
157
229
githubRelease,
158
230
releasePR,
159
231
releasePRClass,
160
- run ,
232
+ call ,
161
233
runCommand,
162
234
} ;
0 commit comments