Skip to content

Commit

Permalink
refactor: remove slow types
Browse files Browse the repository at this point in the history
  • Loading branch information
syi0808 committed Oct 16, 2024
1 parent 0bb209a commit d350ae9
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 187 deletions.
95 changes: 50 additions & 45 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,59 +141,64 @@ function resolveCliOptions(options: CliOptions): Options {

cli
.command('[version]')
.action(async (nextVersion, options: Omit<CliOptions, 'version'>) => {
console.clear();

if (!isCI) {
await notifyNewVersion();
}

const context = {
version: nextVersion,
tag: options.tag,
};

try {
if (isCI) {
if (options.publishOnly) {
const git = new Git();
const latestVersion = (await git.latestTag())?.slice(1);

if (!latestVersion) {
throw new Error(
'Cannot find the latest tag. Please ensure tags exist in the repository.',
);
}
.action(
async (
nextVersion,
options: Omit<CliOptions, 'version'>,
): Promise<void> => {
console.clear();

if (!isCI) {
await notifyNewVersion();
}

if (!valid(latestVersion)) {
const context = {
version: nextVersion,
tag: options.tag,
};

try {
if (isCI) {
if (options.publishOnly) {
const git = new Git();
const latestVersion = (await git.latestTag())?.slice(1);

if (!latestVersion) {
throw new Error(
'Cannot find the latest tag. Please ensure tags exist in the repository.',
);
}

if (!valid(latestVersion)) {
throw new Error(
'Cannot parse the latest tag to a valid SemVer version. Please check the tag format.',
);
}

context.version = latestVersion;
} else {
throw new Error(
'Cannot parse the latest tag to a valid SemVer version. Please check the tag format.',
'Version must be set in the CI environment. Please define the version before proceeding.',
);
}

context.version = latestVersion;
} else {
throw new Error(
'Version must be set in the CI environment. Please define the version before proceeding.',
);
await requiredMissingInformationTasks().run(context);
}
} else {
await requiredMissingInformationTasks().run(context);

await pubm(
resolveCliOptions({
...options,
version: context.version,
tag: context.tag,
}),
);
} catch (e) {
consoleError(e as Error);
}
},
);

await pubm(
resolveCliOptions({
...options,
version: context.version,
tag: context.tag,
}),
);
} catch (e) {
consoleError(e as Error);
}
});

cli.help((sections) => {
cli.help((sections): void => {
sections[1].body += `\n\n Version can be:\n ${RELEASE_TYPES.join(' | ')} | 1.2.3`;
sections.splice(2, 2);
if (sections.at(2)) {
Expand Down
4 changes: 2 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AbstractError extends Error {
}
}

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

Expand Down Expand Up @@ -39,7 +39,7 @@ function formatError(error: AbstractError | string): string {
return stringifyError;
}

export function consoleError(error: string | Error) {
export function consoleError(error: string | Error): void {
let errorText = '\n';

if (typeof error === 'string') {
Expand Down
63 changes: 33 additions & 30 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class GitError extends AbstractError {
}

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

if (stderr) throw stderr;

return stdout;
}

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

async latestTag() {
async latestTag(): Promise<string | null> {
try {
return (await this.git(['describe', '--tags', '--abbrev=0'])).trim();
} catch {
return null;
}
}

async tags() {
async tags(): Promise<string[]> {
try {
return (await this.git(['tag', '-l']))
.trim()
Expand All @@ -47,17 +47,17 @@ export class Git {
}
}

async previousTag(tag: string) {
async previousTag(tag: string): Promise<string | null> {
try {
const tags = await this.tags();

return tags.at(tags.findIndex((t) => t === tag) - 1);
return tags.at(tags.findIndex((t) => t === tag) - 1) ?? null;
} catch {
return null;
}
}

async dryFetch() {
async dryFetch(): Promise<string> {
try {
return await this.git(['fetch', '--dry-run']);
} catch (error) {
Expand All @@ -67,7 +67,7 @@ export class Git {
}
}

async fetch() {
async fetch(): Promise<boolean> {
try {
await this.git(['fetch']);

Expand All @@ -79,7 +79,7 @@ export class Git {
}
}

async pull() {
async pull(): Promise<boolean> {
try {
await this.git(['pull']);

Expand All @@ -91,9 +91,9 @@ export class Git {
}
}

async revisionDiffsCount() {
async revisionDiffsCount(): Promise<number> {
try {
return await Number.parseInt(
return Number.parseInt(
await this.git(['rev-list', '@{u}...HEAD', '--count', '--left-only']),
);
} catch (error) {
Expand All @@ -104,7 +104,7 @@ export class Git {
}
}

async status() {
async status(): Promise<string> {
try {
return (await this.git(['status', '--porcelain'])).trim();
} catch (error) {
Expand All @@ -114,7 +114,10 @@ export class Git {
}
}

async commits(leftRev: string, rightRev: string) {
async commits(
leftRev: string,
rightRev: string,
): Promise<{ id: string; message: string }[]> {
try {
const logs = await this.git([
'log',
Expand All @@ -137,17 +140,17 @@ export class Git {
}
}

async version() {
async version(): Promise<string> {
try {
return (await this.git(['--version'])).trim().match(/\d+\.\d+\.\d+/)?.[0];
return `${(await this.git(['--version'])).trim().match(/\d+\.\d+\.\d+/)?.[0]}`;
} catch (error) {
throw new GitError('Failed to run `git --version`', {
cause: error,
});
}
}

async branch() {
async branch(): Promise<string> {
try {
return (await this.git(['rev-parse', '--abbrev-ref', 'HEAD'])).trim();
} catch (error) {
Expand All @@ -157,7 +160,7 @@ export class Git {
}
}

async switch(branch: string) {
async switch(branch: string): Promise<boolean> {
try {
await this.git(['switch', branch]);

Expand All @@ -169,7 +172,7 @@ export class Git {
}
}

async checkTagExist(tag: string) {
async checkTagExist(tag: string): Promise<boolean> {
try {
return (
(
Expand All @@ -186,7 +189,7 @@ export class Git {
}
}

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

Expand All @@ -198,7 +201,7 @@ export class Git {
}
}

async stageAll() {
async stageAll(): Promise<boolean> {
try {
await this.git(['add', '.']);

Expand All @@ -210,7 +213,7 @@ export class Git {
}
}

async stash() {
async stash(): Promise<boolean> {
try {
await this.git(['stash']);

Expand All @@ -222,7 +225,7 @@ export class Git {
}
}

async popStash() {
async popStash(): Promise<boolean> {
try {
await this.git(['stash', 'pop']);

Expand All @@ -234,7 +237,7 @@ export class Git {
}
}

async stage(file: string) {
async stage(file: string): Promise<boolean> {
try {
await this.git(['add', file]);

Expand All @@ -246,7 +249,7 @@ export class Git {
}
}

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

try {
Expand All @@ -260,7 +263,7 @@ export class Git {
}
}

async latestCommit() {
async latestCommit(): Promise<string> {
try {
return (await this.git(['rev-parse', 'HEAD'])).trim();
} catch (error) {
Expand All @@ -270,7 +273,7 @@ export class Git {
}
}

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

async commit(message: string) {
async commit(message: string): Promise<string> {
try {
await this.git(['commit', '-m', message]);

Expand All @@ -292,7 +295,7 @@ export class Git {
}
}

async repository() {
async repository(): Promise<string> {
try {
return (await this.git(['remote', 'get-url', 'origin'])).trim();
} catch (error) {
Expand All @@ -302,7 +305,7 @@ export class Git {
}
}

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

try {
Expand All @@ -316,7 +319,7 @@ export class Git {
}
}

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

try {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Options } from './types/options.js';
* @async
* @function
*/
export async function pubm(options: Options) {
export async function pubm(options: Options): Promise<void> {
const resolvedOptions = resolveOptions({ ...options });

await run(resolvedOptions);
Expand Down
Loading

0 comments on commit d350ae9

Please sign in to comment.