|
1 | | -import type { Config } from '../types/config' |
2 | | -import { exec } from './filesystem' |
3 | | -import { randomizer } from './randomizer' |
4 | | - |
5 | | -export class Repository |
6 | | -{ |
7 | | - private _config: Config |
8 | | - private _currentBranch: string = '' |
9 | | - private _newBranch: boolean = false |
10 | | - |
11 | | - constructor(config: Config) |
12 | | - { |
13 | | - this._config = config |
| 1 | +import type { Config } from "../types/config"; |
| 2 | +import { exec } from "./filesystem"; |
| 3 | +import { randomizer } from "./randomizer"; |
| 4 | + |
| 5 | +export class Repository { |
| 6 | + private _config: Config; |
| 7 | + private _currentBranch: string = ""; |
| 8 | + private _newBranch: boolean = false; |
| 9 | + |
| 10 | + constructor(config: Config) { |
| 11 | + this._config = config; |
14 | 12 | } |
15 | 13 |
|
16 | | - async authenticate() |
17 | | - { |
| 14 | + async authenticate() { |
18 | 15 | try { |
19 | | - const author = this._config.repository.commit.author |
| 16 | + const author = this._config.repository.commit.author; |
20 | 17 |
|
21 | | - await exec(`git config user.name "${ author.name }"`) |
22 | | - await exec(`git config user.email "${ author.email }"`) |
| 18 | + await exec(`git config user.name "${author.name}"`); |
| 19 | + await exec(`git config user.email "${author.email}"`); |
23 | 20 | } catch (error) { |
24 | 21 | // @ts-expect-error |
25 | | - error.message = `Error authenticating user "${ author.name }" with e-mail "${ author.email }": ${ error.message }` |
| 22 | + error.message = `Error authenticating user "${author.name}" with e-mail "${author.email}": ${error.message}`; |
26 | 23 |
|
27 | | - throw error |
| 24 | + throw error; |
28 | 25 | } |
29 | 26 | } |
30 | 27 |
|
31 | | - async branchExists() |
32 | | - { |
| 28 | + async branchExists() { |
33 | 29 | try { |
34 | 30 | const hasLocalBranch = async () => { |
35 | 31 | const result = await exec( |
36 | | - `git branch --list "${ this.branchName() }"` |
37 | | - ) |
| 32 | + `git branch --list "${this.branchName()}"`, |
| 33 | + ); |
38 | 34 |
|
39 | | - return result.includes(this.branchName()) |
40 | | - } |
| 35 | + return result.includes(this.branchName()); |
| 36 | + }; |
41 | 37 |
|
42 | 38 | const hasRemoteBranch = async () => { |
43 | 39 | const result = await exec( |
44 | | - `git ls-remote --heads origin "${ this.branchName() }"` |
45 | | - ) |
| 40 | + `git ls-remote --heads origin "${this.branchName()}"`, |
| 41 | + ); |
46 | 42 |
|
47 | | - return result.includes(this.branchName()) |
48 | | - } |
| 43 | + return result.includes(this.branchName()); |
| 44 | + }; |
49 | 45 |
|
50 | | - return (await hasLocalBranch()) || (await hasRemoteBranch()) |
| 46 | + return (await hasLocalBranch()) || (await hasRemoteBranch()); |
51 | 47 | } catch (error) { |
52 | 48 | // @ts-expect-error |
53 | | - error.message = `Error searching for branch "${ this.branchName() }": ${ error.message }` |
| 49 | + error.message = `Error searching for branch "${this.branchName()}": ${error.message}`; |
54 | 50 |
|
55 | | - throw error |
| 51 | + throw error; |
56 | 52 | } |
57 | 53 | } |
58 | 54 |
|
59 | | - async checkoutBranch(isNew: boolean) |
60 | | - { |
| 55 | + async checkoutBranch(isNew: boolean) { |
61 | 56 | try { |
62 | | - this._newBranch = isNew |
| 57 | + this._newBranch = isNew; |
63 | 58 |
|
64 | 59 | await exec( |
65 | | - `git switch ${ isNew ? '-c' : '' } "${ this.branchName() }"` |
66 | | - ) |
| 60 | + `git switch ${isNew ? "-c" : ""} "${this.branchName()}"`, |
| 61 | + ); |
67 | 62 | } catch (error) { |
68 | 63 | // @ts-expect-error |
69 | | - error.message = `Error checking out ${ isNew ? 'new' : 'existing' } branch "${ this.branchName() }": ${ error.message }` |
| 64 | + error.message = `Error checking out ${isNew ? "new" : "existing"} branch "${this.branchName()}": ${error.message}`; |
70 | 65 |
|
71 | | - throw error |
| 66 | + throw error; |
72 | 67 | } |
73 | 68 | } |
74 | 69 |
|
75 | | - async stage() |
76 | | - { |
| 70 | + async stage() { |
77 | 71 | try { |
78 | | - await exec('git add ' + this._config.path.readme) |
| 72 | + await exec("git add " + this._config.path.readme); |
79 | 73 | } catch (error) { |
80 | 74 | // @ts-expect-error |
81 | | - error.message = `Error staging file "${ this._config.path.readme }": ${ error.message }` |
| 75 | + error.message = `Error staging file "${this._config.path.readme}": ${error.message}`; |
82 | 76 |
|
83 | | - throw error |
| 77 | + throw error; |
84 | 78 | } |
85 | 79 | } |
86 | 80 |
|
87 | | - async commit() |
88 | | - { |
| 81 | + async commit() { |
89 | 82 | try { |
90 | 83 | const message = |
91 | 84 | this._config.repository.commit.title + |
92 | | - '\n' + |
93 | | - this._config.repository.commit.body |
| 85 | + "\n" + |
| 86 | + this._config.repository.commit.body; |
94 | 87 |
|
95 | | - await exec(`git commit -m "${ message }"`) |
| 88 | + await exec(`git commit -m "${message}"`); |
96 | 89 | } catch (error) { |
97 | 90 | // @ts-expect-error |
98 | | - error.message = `Error committing file "${ this._config.path.readme }": ${ error.message }` |
| 91 | + error.message = `Error committing file "${this._config.path.readme}": ${error.message}`; |
99 | 92 |
|
100 | | - throw error |
| 93 | + throw error; |
101 | 94 | } |
102 | 95 | } |
103 | 96 |
|
104 | | - async push() |
105 | | - { |
| 97 | + async push() { |
106 | 98 | try { |
107 | | - let cmd = 'git push' |
| 99 | + let cmd = "git push"; |
108 | 100 |
|
109 | 101 | if (this._newBranch) { |
110 | | - cmd += ` --set-upstream origin ${ this.branchName() }` |
| 102 | + cmd += ` --set-upstream origin ${this.branchName()}`; |
111 | 103 | } |
112 | 104 |
|
113 | | - await exec(cmd) |
| 105 | + await exec(cmd); |
114 | 106 | } catch (error) { |
115 | 107 | // @ts-expect-error |
116 | | - error.message = `Error pushing changes to "${ this.branchName() } branch": ${ error.message }` |
| 108 | + error.message = `Error pushing changes to "${this.branchName()} branch": ${error.message}`; |
117 | 109 |
|
118 | | - throw error |
| 110 | + throw error; |
119 | 111 | } |
120 | 112 | } |
121 | 113 |
|
122 | | - async createPullRequest() |
123 | | - { |
| 114 | + async createPullRequest() { |
124 | 115 | try { |
125 | 116 | const defaultBranch = await exec( |
126 | | - `git remote show origin | grep 'HEAD branch' | cut -d ' ' -f5` |
127 | | - ) |
| 117 | + `git remote show origin | grep 'HEAD branch' | cut -d ' ' -f5`, |
| 118 | + ); |
128 | 119 |
|
129 | 120 | return this._config.repository.octokit.rest.pulls.create({ |
130 | 121 | owner: this._config.repository.owner, |
131 | 122 | repo: this._config.repository.repo, |
132 | 123 | title: this._config.repository.pullRequest.title, |
133 | 124 | body: this._config.repository.pullRequest.body, |
134 | 125 | head: this.branchName(), |
135 | | - base: defaultBranch |
136 | | - }) |
| 126 | + base: defaultBranch, |
| 127 | + }); |
137 | 128 | } catch (error) { |
138 | 129 | // @ts-expect-error |
139 | | - error.message = `Error when creating pull request from ${ this.branchName() }: ${ error.message }` |
| 130 | + error.message = `Error when creating pull request from ${this.branchName()}: ${error.message}`; |
140 | 131 |
|
141 | | - throw error |
| 132 | + throw error; |
142 | 133 | } |
143 | 134 | } |
144 | 135 |
|
145 | | - async assignee(issueNumber: number, assignees: string[]) |
146 | | - { |
| 136 | + async assignee(issueNumber: number, assignees: string[]) { |
147 | 137 | try { |
148 | 138 | return this._config.repository.octokit.rest.issues.addAssignees({ |
149 | 139 | owner: this._config.repository.owner, |
150 | 140 | repo: this._config.repository.repo, |
151 | 141 | issue_number: issueNumber, |
152 | | - assignees: assignees |
153 | | - }) |
| 142 | + assignees: assignees, |
| 143 | + }); |
154 | 144 | } catch (error) { |
155 | 145 | // @ts-expect-error |
156 | | - error.message = `Error when adding assignees to issue ${ issueNumber }: ${ error.message }` |
| 146 | + error.message = `Error when adding assignees to issue ${issueNumber}: ${error.message}`; |
157 | 147 |
|
158 | | - throw error |
| 148 | + throw error; |
159 | 149 | } |
160 | 150 | } |
161 | 151 |
|
162 | | - async addLabels(issueNumber: number, labels: string[]) |
163 | | - { |
| 152 | + async addLabels(issueNumber: number, labels: string[]) { |
164 | 153 | try { |
165 | 154 | return this._config.repository.octokit.rest.issues.addLabels({ |
166 | 155 | owner: this._config.repository.owner, |
167 | 156 | repo: this._config.repository.repo, |
168 | 157 | issue_number: issueNumber, |
169 | | - labels |
170 | | - }) |
| 158 | + labels, |
| 159 | + }); |
171 | 160 | } catch (error) { |
172 | 161 | // @ts-expect-error |
173 | | - error.message = `Error when adding labels to issue ${ issueNumber }: ${ error.message }` |
| 162 | + error.message = `Error when adding labels to issue ${issueNumber}: ${error.message}`; |
174 | 163 |
|
175 | | - throw error |
| 164 | + throw error; |
176 | 165 | } |
177 | 166 | } |
178 | 167 |
|
179 | | - branchName(): string |
180 | | - { |
181 | | - if (this._currentBranch === '') { |
| 168 | + branchName(): string { |
| 169 | + if (this._currentBranch === "") { |
182 | 170 | this._currentBranch = this._config.repository.commit.branch.replace( |
183 | | - '{random}', |
184 | | - randomizer() |
185 | | - ) |
| 171 | + "{random}", |
| 172 | + randomizer(), |
| 173 | + ); |
186 | 174 | } |
187 | 175 |
|
188 | | - return this._currentBranch |
| 176 | + return this._currentBranch; |
189 | 177 | } |
190 | 178 | } |
0 commit comments