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

Expose repositoryRootDir, configFile and fix diagnosticsFile #913

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import Listr from 'listr';
import readPkgUp from 'read-pkg-up';
import { v4 as uuid } from 'uuid';

import { getBranch, getCommit, getSlug, getUncommittedHash, getUserEmail } from './git/git';
import {
getBranch,
getCommit,
getRepositoryRoot,
getSlug,
getUncommittedHash,
getUserEmail,
} from './git/git';
import GraphQLClient from './io/GraphQLClient';
import HTTPClient from './io/HTTPClient';
import LoggingRenderer from './lib/LoggingRenderer';
Expand Down Expand Up @@ -85,7 +92,7 @@ export async function run({
flags?: Flags;
options?: Partial<Options>;
}): Promise<Output> {
const { sessionId = uuid(), env = getEnv(), log = createLogger() } = extraOptions;
const { sessionId = uuid(), env = getEnv(), log = createLogger() } = extraOptions || {};

const pkgInfo = await readPkgUp({ cwd: process.cwd() });
if (!pkgInfo) {
Expand Down Expand Up @@ -255,19 +262,21 @@ export type GitInfo = {
uncommittedHash: string;
userEmail: string;
userEmailHash: string;
repositoryRootDir: string;
};

export async function getGitInfo(): Promise<GitInfo> {
let slug: string;
try {
slug = await getSlug();
} catch {
slug = ''
slug = '';
}
const branch = await getBranch();
const commitInfo = await getCommit();
const userEmail = await getUserEmail();
const userEmailHash = emailHash(userEmail);
const repositoryRootDir = await getRepositoryRoot();

const [ownerName, repoName, ...rest] = slug ? slug.split('/') : [];
const isValidSlug = !!ownerName && !!repoName && !rest.length;
Expand All @@ -280,6 +289,7 @@ export async function getGitInfo(): Promise<GitInfo> {
uncommittedHash,
userEmail,
userEmailHash,
repositoryRootDir,
};
}

Expand Down
10 changes: 6 additions & 4 deletions node-src/lib/getConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ it('reads configuration successfully', async () => {
untraced: ['untraced'],
externals: ['externals'],
debug: true,
diagnosticFile: 'diagnostic-file',
diagnosticsFile: 'diagnostics-file',
fileHashing: true,
junitReport: 'junit-report',
zip: true,
Expand All @@ -45,6 +45,7 @@ it('reads configuration successfully', async () => {
);

expect(await getConfiguration()).toEqual({
configFile: 'chromatic.config.json',
projectId: 'project-id',
projectToken: 'project-token',

Expand All @@ -54,7 +55,7 @@ it('reads configuration successfully', async () => {
untraced: ['untraced'],
externals: ['externals'],
debug: true,
diagnosticFile: 'diagnostic-file',
diagnosticsFile: 'diagnostics-file',
fileHashing: true,
junitReport: 'junit-report',
zip: true,
Expand All @@ -80,7 +81,7 @@ it('handles other side of union options', async () => {
mockedReadFile.mockReturnValue(
JSON.stringify({
onlyChanged: true,
diagnosticFile: true,
diagnosticsFile: true,
junitReport: true,
autoAcceptChanges: true,
exitZeroOnChanges: true,
Expand All @@ -92,8 +93,9 @@ it('handles other side of union options', async () => {
);

expect(await getConfiguration()).toEqual({
configFile: 'chromatic.config.json',
onlyChanged: true,
diagnosticFile: true,
diagnosticsFile: true,
junitReport: true,
autoAcceptChanges: true,
exitZeroOnChanges: true,
Expand Down
10 changes: 6 additions & 4 deletions node-src/lib/getConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const configurationSchema = z
untraced: z.array(z.string()),
externals: z.array(z.string()),
debug: z.boolean(),
diagnosticFile: z.union([z.string(), z.boolean()]),
diagnosticsFile: z.union([z.string(), z.boolean()]),
fileHashing: z.boolean().default(true),
junitReport: z.union([z.string(), z.boolean()]),
zip: z.boolean(),
Expand All @@ -42,12 +42,14 @@ const configurationSchema = z

export type Configuration = z.infer<typeof configurationSchema>;

export async function getConfiguration(configFile?: string) {
export async function getConfiguration(
configFile?: string
): Promise<Configuration & { configFile?: string }> {
const usedConfigFile = configFile || 'chromatic.config.json';
try {
const rawJson = readFileSync(usedConfigFile, 'utf8');

return configurationSchema.parse(JSON.parse(rawJson));
const configuration = configurationSchema.parse(JSON.parse(rawJson));
return { configFile: usedConfigFile, ...configuration };
} catch (err) {
// Config file does not exist
if (err.message.match(/ENOENT/)) {
Expand Down
Loading