Skip to content

Commit d350ae9

Browse files
committed
refactor: remove slow types
1 parent 0bb209a commit d350ae9

23 files changed

+220
-187
lines changed

src/cli.ts

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -141,59 +141,64 @@ function resolveCliOptions(options: CliOptions): Options {
141141

142142
cli
143143
.command('[version]')
144-
.action(async (nextVersion, options: Omit<CliOptions, 'version'>) => {
145-
console.clear();
146-
147-
if (!isCI) {
148-
await notifyNewVersion();
149-
}
150-
151-
const context = {
152-
version: nextVersion,
153-
tag: options.tag,
154-
};
155-
156-
try {
157-
if (isCI) {
158-
if (options.publishOnly) {
159-
const git = new Git();
160-
const latestVersion = (await git.latestTag())?.slice(1);
161-
162-
if (!latestVersion) {
163-
throw new Error(
164-
'Cannot find the latest tag. Please ensure tags exist in the repository.',
165-
);
166-
}
144+
.action(
145+
async (
146+
nextVersion,
147+
options: Omit<CliOptions, 'version'>,
148+
): Promise<void> => {
149+
console.clear();
150+
151+
if (!isCI) {
152+
await notifyNewVersion();
153+
}
167154

168-
if (!valid(latestVersion)) {
155+
const context = {
156+
version: nextVersion,
157+
tag: options.tag,
158+
};
159+
160+
try {
161+
if (isCI) {
162+
if (options.publishOnly) {
163+
const git = new Git();
164+
const latestVersion = (await git.latestTag())?.slice(1);
165+
166+
if (!latestVersion) {
167+
throw new Error(
168+
'Cannot find the latest tag. Please ensure tags exist in the repository.',
169+
);
170+
}
171+
172+
if (!valid(latestVersion)) {
173+
throw new Error(
174+
'Cannot parse the latest tag to a valid SemVer version. Please check the tag format.',
175+
);
176+
}
177+
178+
context.version = latestVersion;
179+
} else {
169180
throw new Error(
170-
'Cannot parse the latest tag to a valid SemVer version. Please check the tag format.',
181+
'Version must be set in the CI environment. Please define the version before proceeding.',
171182
);
172183
}
173-
174-
context.version = latestVersion;
175184
} else {
176-
throw new Error(
177-
'Version must be set in the CI environment. Please define the version before proceeding.',
178-
);
185+
await requiredMissingInformationTasks().run(context);
179186
}
180-
} else {
181-
await requiredMissingInformationTasks().run(context);
187+
188+
await pubm(
189+
resolveCliOptions({
190+
...options,
191+
version: context.version,
192+
tag: context.tag,
193+
}),
194+
);
195+
} catch (e) {
196+
consoleError(e as Error);
182197
}
198+
},
199+
);
183200

184-
await pubm(
185-
resolveCliOptions({
186-
...options,
187-
version: context.version,
188-
tag: context.tag,
189-
}),
190-
);
191-
} catch (e) {
192-
consoleError(e as Error);
193-
}
194-
});
195-
196-
cli.help((sections) => {
201+
cli.help((sections): void => {
197202
sections[1].body += `\n\n Version can be:\n ${RELEASE_TYPES.join(' | ')} | 1.2.3`;
198203
sections.splice(2, 2);
199204
if (sections.at(2)) {

src/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class AbstractError extends Error {
1111
}
1212
}
1313

14-
function replaceCode(code: string) {
14+
function replaceCode(code: string): string {
1515
return code.replace(/`([^`].+)`/g, color.bold(color.underline('$1')));
1616
}
1717

@@ -39,7 +39,7 @@ function formatError(error: AbstractError | string): string {
3939
return stringifyError;
4040
}
4141

42-
export function consoleError(error: string | Error) {
42+
export function consoleError(error: string | Error): void {
4343
let errorText = '\n';
4444

4545
if (typeof error === 'string') {

src/git.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class GitError extends AbstractError {
77
}
88

99
export class Git {
10-
async git(args: string[]) {
10+
async git(args: string[]): Promise<string> {
1111
const { stdout, stderr } = await exec('git', args);
1212

1313
if (stderr) throw stderr;
1414

1515
return stdout;
1616
}
1717

18-
async userName() {
18+
async userName(): Promise<string> {
1919
try {
2020
return (await this.git(['config', '--get', 'user.name'])).trim();
2121
} catch (error) {
@@ -25,15 +25,15 @@ export class Git {
2525
}
2626
}
2727

28-
async latestTag() {
28+
async latestTag(): Promise<string | null> {
2929
try {
3030
return (await this.git(['describe', '--tags', '--abbrev=0'])).trim();
3131
} catch {
3232
return null;
3333
}
3434
}
3535

36-
async tags() {
36+
async tags(): Promise<string[]> {
3737
try {
3838
return (await this.git(['tag', '-l']))
3939
.trim()
@@ -47,17 +47,17 @@ export class Git {
4747
}
4848
}
4949

50-
async previousTag(tag: string) {
50+
async previousTag(tag: string): Promise<string | null> {
5151
try {
5252
const tags = await this.tags();
5353

54-
return tags.at(tags.findIndex((t) => t === tag) - 1);
54+
return tags.at(tags.findIndex((t) => t === tag) - 1) ?? null;
5555
} catch {
5656
return null;
5757
}
5858
}
5959

60-
async dryFetch() {
60+
async dryFetch(): Promise<string> {
6161
try {
6262
return await this.git(['fetch', '--dry-run']);
6363
} catch (error) {
@@ -67,7 +67,7 @@ export class Git {
6767
}
6868
}
6969

70-
async fetch() {
70+
async fetch(): Promise<boolean> {
7171
try {
7272
await this.git(['fetch']);
7373

@@ -79,7 +79,7 @@ export class Git {
7979
}
8080
}
8181

82-
async pull() {
82+
async pull(): Promise<boolean> {
8383
try {
8484
await this.git(['pull']);
8585

@@ -91,9 +91,9 @@ export class Git {
9191
}
9292
}
9393

94-
async revisionDiffsCount() {
94+
async revisionDiffsCount(): Promise<number> {
9595
try {
96-
return await Number.parseInt(
96+
return Number.parseInt(
9797
await this.git(['rev-list', '@{u}...HEAD', '--count', '--left-only']),
9898
);
9999
} catch (error) {
@@ -104,7 +104,7 @@ export class Git {
104104
}
105105
}
106106

107-
async status() {
107+
async status(): Promise<string> {
108108
try {
109109
return (await this.git(['status', '--porcelain'])).trim();
110110
} catch (error) {
@@ -114,7 +114,10 @@ export class Git {
114114
}
115115
}
116116

117-
async commits(leftRev: string, rightRev: string) {
117+
async commits(
118+
leftRev: string,
119+
rightRev: string,
120+
): Promise<{ id: string; message: string }[]> {
118121
try {
119122
const logs = await this.git([
120123
'log',
@@ -137,17 +140,17 @@ export class Git {
137140
}
138141
}
139142

140-
async version() {
143+
async version(): Promise<string> {
141144
try {
142-
return (await this.git(['--version'])).trim().match(/\d+\.\d+\.\d+/)?.[0];
145+
return `${(await this.git(['--version'])).trim().match(/\d+\.\d+\.\d+/)?.[0]}`;
143146
} catch (error) {
144147
throw new GitError('Failed to run `git --version`', {
145148
cause: error,
146149
});
147150
}
148151
}
149152

150-
async branch() {
153+
async branch(): Promise<string> {
151154
try {
152155
return (await this.git(['rev-parse', '--abbrev-ref', 'HEAD'])).trim();
153156
} catch (error) {
@@ -157,7 +160,7 @@ export class Git {
157160
}
158161
}
159162

160-
async switch(branch: string) {
163+
async switch(branch: string): Promise<boolean> {
161164
try {
162165
await this.git(['switch', branch]);
163166

@@ -169,7 +172,7 @@ export class Git {
169172
}
170173
}
171174

172-
async checkTagExist(tag: string) {
175+
async checkTagExist(tag: string): Promise<boolean> {
173176
try {
174177
return (
175178
(
@@ -186,7 +189,7 @@ export class Git {
186189
}
187190
}
188191

189-
async deleteTag(tag: string) {
192+
async deleteTag(tag: string): Promise<boolean> {
190193
try {
191194
await this.git(['tag', '--delete', tag]);
192195

@@ -198,7 +201,7 @@ export class Git {
198201
}
199202
}
200203

201-
async stageAll() {
204+
async stageAll(): Promise<boolean> {
202205
try {
203206
await this.git(['add', '.']);
204207

@@ -210,7 +213,7 @@ export class Git {
210213
}
211214
}
212215

213-
async stash() {
216+
async stash(): Promise<boolean> {
214217
try {
215218
await this.git(['stash']);
216219

@@ -222,7 +225,7 @@ export class Git {
222225
}
223226
}
224227

225-
async popStash() {
228+
async popStash(): Promise<boolean> {
226229
try {
227230
await this.git(['stash', 'pop']);
228231

@@ -234,7 +237,7 @@ export class Git {
234237
}
235238
}
236239

237-
async stage(file: string) {
240+
async stage(file: string): Promise<boolean> {
238241
try {
239242
await this.git(['add', file]);
240243

@@ -246,7 +249,7 @@ export class Git {
246249
}
247250
}
248251

249-
async reset(rev?: string, option?: string) {
252+
async reset(rev?: string, option?: string): Promise<boolean> {
250253
const args = ['reset', rev, option].filter((v) => v) as string[];
251254

252255
try {
@@ -260,7 +263,7 @@ export class Git {
260263
}
261264
}
262265

263-
async latestCommit() {
266+
async latestCommit(): Promise<string> {
264267
try {
265268
return (await this.git(['rev-parse', 'HEAD'])).trim();
266269
} catch (error) {
@@ -270,7 +273,7 @@ export class Git {
270273
}
271274
}
272275

273-
async firstCommit() {
276+
async firstCommit(): Promise<string> {
274277
try {
275278
return (await this.git(['rev-list', '--max-parents=0', 'HEAD'])).trim();
276279
} catch (error) {
@@ -280,7 +283,7 @@ export class Git {
280283
}
281284
}
282285

283-
async commit(message: string) {
286+
async commit(message: string): Promise<string> {
284287
try {
285288
await this.git(['commit', '-m', message]);
286289

@@ -292,7 +295,7 @@ export class Git {
292295
}
293296
}
294297

295-
async repository() {
298+
async repository(): Promise<string> {
296299
try {
297300
return (await this.git(['remote', 'get-url', 'origin'])).trim();
298301
} catch (error) {
@@ -302,7 +305,7 @@ export class Git {
302305
}
303306
}
304307

305-
async createTag(tag: string, commitRev?: string) {
308+
async createTag(tag: string, commitRev?: string): Promise<boolean> {
306309
const args = ['tag', tag, commitRev].filter((v) => v) as string[];
307310

308311
try {
@@ -316,7 +319,7 @@ export class Git {
316319
}
317320
}
318321

319-
async push(options?: string) {
322+
async push(options?: string): Promise<boolean> {
320323
const args = ['push', options].filter((v) => v) as string[];
321324

322325
try {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { Options } from './types/options.js';
1212
* @async
1313
* @function
1414
*/
15-
export async function pubm(options: Options) {
15+
export async function pubm(options: Options): Promise<void> {
1616
const resolvedOptions = resolveOptions({ ...options });
1717

1818
await run(resolvedOptions);

0 commit comments

Comments
 (0)