Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getGitInfo function exported by the Node API #794

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import checkPackageJson from './lib/checkPackageJson';
import { writeChromaticDiagnostics } from './lib/writeChromaticDiagnostics';
import invalidPackageJson from './ui/messages/errors/invalidPackageJson';
import noPackageJson from './ui/messages/errors/noPackageJson';
import { getBranch, getCommit, getSlug } from './git/git';

/**
Make keys of `T` outside of `R` optional.
Expand Down Expand Up @@ -110,3 +111,20 @@ export async function runAll(ctx, options?: Options) {
await writeChromaticDiagnostics(ctx);
}
}

export type GitInfo = {
branch: string;
commit: string;
slug: string;
};

export async function getGitInfo(): Promise<GitInfo> {
const branch = await getBranch();
const { commit } = await getCommit();
const slug = await getSlug();

const [ownerName, repoName, ...rest] = slug ? slug.split('/') : [];
const isValidSlug = !!ownerName && !!repoName && !rest.length;

return { branch, commit, slug: isValidSlug ? slug : '' };
}