Skip to content

Commit

Permalink
Add proper error and exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jan 13, 2024
1 parent 6155d8d commit 57321e9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions node-src/lib/setExitCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const exitCodes = {
// I/O errors
FETCH_ERROR: 201,
GRAPHQL_ERROR: 202,
MISSING_DEPENDENCY: 210,
INVALID_OPTIONS: 254,
};

Expand Down
19 changes: 9 additions & 10 deletions node-src/tasks/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { execaCommand } from 'execa';
import { createWriteStream, readFileSync } from 'fs';
import path, { dirname, resolve } from 'path';
import path, { dirname } from 'path';
import semver from 'semver';
import tmp from 'tmp-promise';

Expand All @@ -11,6 +11,7 @@ import { endActivity, startActivity } from '../ui/components/activity';
import buildFailed from '../ui/messages/errors/buildFailed';
import { failed, initial, pending, skipped, success } from '../ui/tasks/build';
import { getPackageManagerRunCommand } from '../lib/getPackageManager';
import missingDependency from '../ui/messages/errors/missingDependency';

export const setSourceDir = async (ctx: Context) => {
if (ctx.options.outputDir) {
Expand Down Expand Up @@ -41,19 +42,17 @@ export const setBuildCommand = async (ctx: Context) => {
ctx.git.changedFiles && webpackStatsSupported && ctx.sourceDir,
].filter(Boolean);

if (ctx.options.playwright) {
if (ctx.options.playwright || ctx.options.cypress) {
const flag = ctx.options.playwright ? 'playwright' : 'cypress';
const dependencyName = `chromatic-${flag}`;
try {
const archiveSBLocation = dirname(require.resolve('@chromaui/archive-storybook/package.json'));
console.log(archiveSBLocation);
const binPath = resolve(archiveSBLocation, './dist/bin/build-archive-storybook');
const binPath = dirname(require.resolve(`${dependencyName}/bin/build-archive-storybook`));
ctx.buildCommand = ['node', binPath, ...buildCommandOptions].join(' ');
console.log(ctx.buildCommand);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
// We should use a proper CLI error here.
throw new Error(
`It looks like you don't have '@chromaui/archive-storybook' installed, please install it!`
);
ctx.log.error(missingDependency({ dependencyName, flag }));
setExitCode(ctx, exitCodes.MISSING_DEPENDENCY, true);
throw new Error(failed(ctx).output);
}
throw err;
}
Expand Down
8 changes: 8 additions & 0 deletions node-src/ui/messages/errors/missingDependency.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import missingDependency from './missingDependency';

export default {
title: 'CLI/Messages/Errors',
};

export const MissingDependency = () =>
missingDependency({ dependencyName: 'chromatic-playwright', flag: 'playwright' });
11 changes: 11 additions & 0 deletions node-src/ui/messages/errors/missingDependency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import chalk from 'chalk';
import { dedent } from 'ts-dedent';
import { error, info } from '../../components/icons';

export default ({ dependencyName, flag }: { dependencyName: string; flag: string }) => {
return dedent(chalk`
${error} Failed to import \`${dependencyName}\`, is it installed in \`package.json\`?
${info} To run \`chromatic --${flag}\` you must have \`${dependencyName}\` installed.
`);
};

0 comments on commit 57321e9

Please sign in to comment.