Skip to content

Commit

Permalink
Add experimental_abortSignal to Node API
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Sep 20, 2023
1 parent e396476 commit eaba5ab
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions node-src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const createTask = ({

// eslint-disable-next-line no-restricted-syntax
for (const step of steps) {
ctx.options.experimental_abortSignal?.throwIfAborted();
// eslint-disable-next-line no-await-in-loop
await step(ctx, task);
}
Expand Down
9 changes: 9 additions & 0 deletions node-src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ it('should exit with code 0 when the current branch is skipped', async () => {
expect(ctx.exitCode).toBe(0);
});

it('should exit with code 6 and stop the build when abortSignal is aborted', async () => {
const abortSignal = AbortSignal.abort(new Error('Build canceled'));
const ctx = getContext(['--project-token=asdf1234']);
ctx.extraOptions = { experimental_abortSignal: abortSignal };
await runBuild(ctx);
expect(ctx.exitCode).toBe(6);
expect(uploadFiles).not.toHaveBeenCalled();
});

it('calls out to npm build script passed and uploads files', async () => {
const ctx = getContext(['--project-token=asdf1234', '--build-script-name=build-storybook']);
await runBuild(ctx);
Expand Down
10 changes: 8 additions & 2 deletions node-src/runBuild.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Listr from 'listr';

import GraphQLClient from './io/GraphQLClient';
import { getConfiguration } from './lib/getConfiguration';
import getOptions from './lib/getOptions';
import NonTTYRenderer from './lib/NonTTYRenderer';
import { exitCodes, setExitCode } from './lib/setExitCode';
import { rewriteErrorMessage } from './lib/utils';
import getTasks from './tasks';
import { Context, Options } from './types';
import { Context } from './types';
import buildCanceled from './ui/messages/errors/buildCanceled';
import fatalError from './ui/messages/errors/fatalError';
import fetchError from './ui/messages/errors/fetchError';
import graphqlError from './ui/messages/errors/graphqlError';
Expand All @@ -15,7 +17,6 @@ import runtimeError from './ui/messages/errors/runtimeError';
import taskError from './ui/messages/errors/taskError';
import intro from './ui/messages/info/intro';
import { endActivity } from './ui/components/activity';
import { getConfiguration } from './lib/getConfiguration';

export async function runBuild(ctx: Context) {
ctx.log.info('');
Expand Down Expand Up @@ -65,6 +66,11 @@ export async function runBuild(ctx: Context) {
setExitCode(ctx, exitCodes.BUILD_NO_STORIES);
throw rewriteErrorMessage(err, missingStories(ctx));
}
if (ctx.extraOptions.experimental_abortSignal?.aborted) {
ctx.userError = true;
setExitCode(ctx, exitCodes.BUILD_WAS_CANCELED);
throw rewriteErrorMessage(err, buildCanceled());
}
throw rewriteErrorMessage(err, taskError(ctx, err));
} finally {
// Handle potential runtime errors from JSDOM
Expand Down
3 changes: 3 additions & 0 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export interface Options {

/** A callback that is called at the completion of each task */
experimental_onTaskComplete?: (ctx: Context) => void;

/** An AbortSignal that terminates the build if aborted */
experimental_abortSignal?: AbortSignal;
}

export { Configuration };
Expand Down
7 changes: 7 additions & 0 deletions node-src/ui/messages/errors/buildCanceled.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import buildCanceled from './buildCanceled';

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

export const BuildCanceled = () => buildCanceled();
10 changes: 10 additions & 0 deletions node-src/ui/messages/errors/buildCanceled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import chalk from 'chalk';
import { dedent } from 'ts-dedent';

import { error } from '../../components/icons';

export default () =>
dedent(chalk`
${error} {bold Build canceled}
The build was canceled before it completed.
`);

0 comments on commit eaba5ab

Please sign in to comment.