Skip to content

Commit

Permalink
Wrap publish in try-catch, exit on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-chernicki committed Jul 11, 2024
1 parent 3a4ce78 commit ee04815
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions apps/commonality/src/cli/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-process-exit */
import { getRootDirectory } from '@commonalityco/data-project';
import { getCodeownersData } from './../../../../../packages/data-codeowners/src/get-codeowners-data';
import { Command } from 'commander';
Expand Down Expand Up @@ -59,49 +60,58 @@ export const publish = command
process.env.COMMONALITY_PUBLISH_KEY,
)
.action(async (options) => {
publishSpinner.start('Publishing snapshot...');
try {
publishSpinner.start('Publishing snapshot...');

const { api, project, key } = options;
const { api, project, key } = options;

const rootDirectory = await getRootDirectory();
const blocks = await getPackages({ rootDirectory });
const rootDirectory = await getRootDirectory();
const blocks = await getPackages({ rootDirectory });

const codeowners = await getCodeownersData({
rootDirectory,
packages: blocks,
});
const dependencies = await getDependencies({ rootDirectory });
const data = {
publishKey: key,
projectId: project,
codeowners,
blocks,
dependencies,
};
const codeowners = await getCodeownersData({
rootDirectory,
packages: blocks,
});
const dependencies = await getDependencies({ rootDirectory });
const data = {
publishKey: key,
projectId: project,
codeowners,
blocks,
dependencies,
};

const result = createSnapshotSchema.safeParse(data);
const result = createSnapshotSchema.safeParse(data);

if (!result.success) {
publishSpinner.stop('Failed to publish snapshot');
prompts.log.error('Invalid snapshot data: ' + result.error);
return;
}
if (!result.success) {
publishSpinner.stop('Failed to publish snapshot');
prompts.log.error('Invalid snapshot data: ' + result.error);
return;
}

try {
const response = await ky
.post(api, {
json: result.data,
})
.json<{ message: string }>();
try {
const response = await ky
.post(api, {
json: result.data,
})
.json<{ message: string }>();

publishSpinner.stop(response.message);
} catch (error) {
if (error instanceof HTTPError) {
const errorJson = (await error.response.json()) as { message: string };
publishSpinner.stop('Failed to publish snapshot');
prompts.log.error(errorJson.message);
} else {
publishSpinner.stop('Failed to publish snapshot');
publishSpinner.stop(response.message);
} catch (error) {
if (error instanceof HTTPError) {
const errorJson = (await error.response.json()) as {
message: string;
};
publishSpinner.stop('Failed to publish snapshot');
prompts.log.error(errorJson.message);
} else {
publishSpinner.stop('Failed to publish snapshot');
}

process.exit(1);
}
} catch {
publishSpinner.stop('Failed to publish snapshot');
process.exit(1);
}
});

0 comments on commit ee04815

Please sign in to comment.