Skip to content

Commit 04631cf

Browse files
Refactor codebase: enforce double quotes, improve readability, integrate Biome config, and enhance tests with updated fixtures.
1 parent 7db4fe3 commit 04631cf

File tree

7 files changed

+208
-142
lines changed

7 files changed

+208
-142
lines changed

biome.json

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
3+
"vcs": {
4+
"enabled": false,
5+
"clientKind": "git",
6+
"useIgnoreFile": false
7+
},
8+
"files": {
9+
"ignoreUnknown": false,
10+
"includes": [
11+
"**",
12+
"!node_modules",
13+
"!vendor",
14+
"!composer.json",
15+
"!composer.lock",
16+
"!package.json",
17+
"!package-lock.json",
18+
"!analytics.*",
19+
"!metrics.*",
20+
"!coverage",
21+
"!dist"
22+
]
23+
},
24+
"formatter": {
25+
"enabled": true,
26+
"indentStyle": "space",
27+
"indentWidth": 4
28+
},
29+
"linter": {
30+
"enabled": true,
31+
"rules": {
32+
"recommended": true
33+
}
34+
},
35+
"javascript": {
36+
"formatter": {
37+
"quoteStyle": "double"
38+
}
39+
},
40+
"json": {
41+
"formatter": {
42+
"enabled": true,
43+
"bracketSpacing": true,
44+
"expand": "always"
45+
},
46+
"parser": {
47+
"allowComments": true
48+
}
49+
},
50+
"assist": {
51+
"enabled": true,
52+
"actions": {
53+
"source": {
54+
"organizeImports": "on"
55+
}
56+
}
57+
}
58+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"dev": "npm run build && node dist/index.js",
1010
"coverage": "jest --coverage",
1111
"test": "jest",
12-
"type-check": "tsc --noEmit"
12+
"type-check": "tsc --noEmit",
13+
"lint": "npx @biomejs/biome lint --write",
14+
"format": "npx @biomejs/biome format --write",
15+
"style": "npm run lint && npm run format"
1316
},
1417
"keywords": [
1518
"preview",

src/types/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { GitHub } from "@actions/github/lib/utils";
2+
13
export interface ImageParameters {
24
pattern: string;
35
style: string;
@@ -39,7 +41,7 @@ export interface PullRequest {
3941
export interface Repository {
4042
owner?: string;
4143
repo?: string;
42-
octokit?: any;
44+
octokit?: InstanceType<typeof GitHub>;
4345

4446
commit: Commit;
4547
pullRequest: PullRequest;

src/utils/preview.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ export const setPreview = (content: string, config: Config) => {
1919

2020
const images: string = getImages(config);
2121

22-
return cleanUp(content).replace(/^(#\s+.+[\n\s]+)/, "$1" + images + "\n\n");
22+
const replace = "$1";
23+
24+
return cleanUp(content).replace(
25+
/^(#\s+.+[\n\s]+)/,
26+
`${replace}${images}\n\n`,
27+
);
2328
};

src/utils/repository.ts

Lines changed: 74 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,190 +1,178 @@
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;
1412
}
1513

16-
async authenticate()
17-
{
14+
async authenticate() {
1815
try {
19-
const author = this._config.repository.commit.author
16+
const author = this._config.repository.commit.author;
2017

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}"`);
2320
} catch (error) {
2421
// @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}`;
2623

27-
throw error
24+
throw error;
2825
}
2926
}
3027

31-
async branchExists()
32-
{
28+
async branchExists() {
3329
try {
3430
const hasLocalBranch = async () => {
3531
const result = await exec(
36-
`git branch --list "${ this.branchName() }"`
37-
)
32+
`git branch --list "${this.branchName()}"`,
33+
);
3834

39-
return result.includes(this.branchName())
40-
}
35+
return result.includes(this.branchName());
36+
};
4137

4238
const hasRemoteBranch = async () => {
4339
const result = await exec(
44-
`git ls-remote --heads origin "${ this.branchName() }"`
45-
)
40+
`git ls-remote --heads origin "${this.branchName()}"`,
41+
);
4642

47-
return result.includes(this.branchName())
48-
}
43+
return result.includes(this.branchName());
44+
};
4945

50-
return (await hasLocalBranch()) || (await hasRemoteBranch())
46+
return (await hasLocalBranch()) || (await hasRemoteBranch());
5147
} catch (error) {
5248
// @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}`;
5450

55-
throw error
51+
throw error;
5652
}
5753
}
5854

59-
async checkoutBranch(isNew: boolean)
60-
{
55+
async checkoutBranch(isNew: boolean) {
6156
try {
62-
this._newBranch = isNew
57+
this._newBranch = isNew;
6358

6459
await exec(
65-
`git switch ${ isNew ? '-c' : '' } "${ this.branchName() }"`
66-
)
60+
`git switch ${isNew ? "-c" : ""} "${this.branchName()}"`,
61+
);
6762
} catch (error) {
6863
// @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}`;
7065

71-
throw error
66+
throw error;
7267
}
7368
}
7469

75-
async stage()
76-
{
70+
async stage() {
7771
try {
78-
await exec('git add ' + this._config.path.readme)
72+
await exec(`git add ${this._config.path.readme}`);
7973
} catch (error) {
8074
// @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}`;
8276

83-
throw error
77+
throw error;
8478
}
8579
}
8680

87-
async commit()
88-
{
81+
async commit() {
8982
try {
9083
const message =
9184
this._config.repository.commit.title +
92-
'\n' +
93-
this._config.repository.commit.body
85+
"\n" +
86+
this._config.repository.commit.body;
9487

95-
await exec(`git commit -m "${ message }"`)
88+
await exec(`git commit -m "${message}"`);
9689
} catch (error) {
9790
// @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}`;
9992

100-
throw error
93+
throw error;
10194
}
10295
}
10396

104-
async push()
105-
{
97+
async push() {
10698
try {
107-
let cmd = 'git push'
99+
let cmd = "git push";
108100

109101
if (this._newBranch) {
110-
cmd += ` --set-upstream origin ${ this.branchName() }`
102+
cmd += ` --set-upstream origin ${this.branchName()}`;
111103
}
112104

113-
await exec(cmd)
105+
await exec(cmd);
114106
} catch (error) {
115107
// @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}`;
117109

118-
throw error
110+
throw error;
119111
}
120112
}
121113

122-
async createPullRequest()
123-
{
114+
async createPullRequest() {
124115
try {
125116
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+
);
128119

129120
return this._config.repository.octokit.rest.pulls.create({
130121
owner: this._config.repository.owner,
131122
repo: this._config.repository.repo,
132123
title: this._config.repository.pullRequest.title,
133124
body: this._config.repository.pullRequest.body,
134125
head: this.branchName(),
135-
base: defaultBranch
136-
})
126+
base: defaultBranch,
127+
});
137128
} catch (error) {
138129
// @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}`;
140131

141-
throw error
132+
throw error;
142133
}
143134
}
144135

145-
async assignee(issueNumber: number, assignees: string[])
146-
{
136+
async assignee(issueNumber: number, assignees: string[]) {
147137
try {
148138
return this._config.repository.octokit.rest.issues.addAssignees({
149139
owner: this._config.repository.owner,
150140
repo: this._config.repository.repo,
151141
issue_number: issueNumber,
152-
assignees: assignees
153-
})
142+
assignees: assignees,
143+
});
154144
} catch (error) {
155145
// @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}`;
157147

158-
throw error
148+
throw error;
159149
}
160150
}
161151

162-
async addLabels(issueNumber: number, labels: string[])
163-
{
152+
async addLabels(issueNumber: number, labels: string[]) {
164153
try {
165154
return this._config.repository.octokit.rest.issues.addLabels({
166155
owner: this._config.repository.owner,
167156
repo: this._config.repository.repo,
168157
issue_number: issueNumber,
169-
labels
170-
})
158+
labels,
159+
});
171160
} catch (error) {
172161
// @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}`;
174163

175-
throw error
164+
throw error;
176165
}
177166
}
178167

179-
branchName(): string
180-
{
181-
if (this._currentBranch === '') {
168+
branchName(): string {
169+
if (this._currentBranch === "") {
182170
this._currentBranch = this._config.repository.commit.branch.replace(
183-
'{random}',
184-
randomizer()
185-
)
171+
"{random}",
172+
randomizer(),
173+
);
186174
}
187175

188-
return this._currentBranch
176+
return this._currentBranch;
189177
}
190178
}

tests/helpers/filesystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setPreview } from "../../src/utils/preview";
33
import { testConfig } from "./config";
44

55
export const getReadme = (filename: string): string => {
6-
const content = readFile(testConfig, "tests/fixtures/readme/" + filename);
6+
const content = readFile(testConfig, `tests/fixtures/readme/${filename}`);
77

88
return setPreview(content, testConfig);
99
};

0 commit comments

Comments
 (0)