Skip to content

Commit

Permalink
Added onTaskComplete option
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jun 6, 2023
1 parent c6fa415 commit 1358e1a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
61 changes: 31 additions & 30 deletions action-src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { error, getInput, setFailed, setOutput } from '@actions/core';
import { context } from '@actions/github';
import path from 'path';

import { run as runNode } from '../node-src';
import { runChromaticFull } from '../node-src';

const maybe = (a: string, b: any = undefined) => {
if (!a) {
Expand Down Expand Up @@ -108,35 +108,36 @@ async function run() {

process.chdir(path.join(process.cwd(), workingDir || ''));

const output = await runNode({
allowConsoleErrors: maybe(allowConsoleErrors, false),
autoAcceptChanges: maybe(autoAcceptChanges),
branchName: maybe(branchName),
buildScriptName: maybe(buildScriptName),
debug: maybe(debug),
diagnostics: maybe(diagnostics),
dryRun: maybe(dryRun),
exitOnceUploaded: maybe(exitOnceUploaded, false),
exitZeroOnChanges: maybe(exitZeroOnChanges, true),
externals: maybe(externals),
forceRebuild: maybe(forceRebuild),
ignoreLastBuildOnBranch: maybe(ignoreLastBuildOnBranch),
interactive: false,
only: maybe(only),
onlyChanged: maybe(onlyChanged),
onlyStoryFiles: maybe(onlyStoryFiles),
onlyStoryNames: maybe(onlyStoryNames),
preserveMissing: maybe(preserveMissing),
projectToken,
repositorySlug: maybe(repositorySlug),
skip: maybe(skip),
storybookBaseDir: maybe(storybookBaseDir),
storybookBuildDir: maybe(storybookBuildDir),
storybookConfigDir: maybe(storybookConfigDir),
traceChanged: maybe(traceChanged),
untraced: maybe(untraced),
zip: maybe(zip, false),
junitReport: maybe(junitReport, false),
const output = await runChromaticFull({
flags: {
allowConsoleErrors: maybe(allowConsoleErrors, false),
autoAcceptChanges: maybe(autoAcceptChanges),
branchName: maybe(branchName),
buildScriptName: maybe(buildScriptName),
debug: maybe(debug),
diagnostics: maybe(diagnostics),
dryRun: maybe(dryRun),
exitOnceUploaded: maybe(exitOnceUploaded, false),
exitZeroOnChanges: maybe(exitZeroOnChanges, true),
externals: maybe(externals),
forceRebuild: maybe(forceRebuild),
ignoreLastBuildOnBranch: maybe(ignoreLastBuildOnBranch),
interactive: false,
only: maybe(only),
onlyChanged: maybe(onlyChanged),
onlyStoryFiles: maybe(onlyStoryFiles),
onlyStoryNames: maybe(onlyStoryNames),
preserveMissing: maybe(preserveMissing),
projectToken,
repositorySlug: maybe(repositorySlug),
skip: maybe(skip),
storybookBaseDir: maybe(storybookBaseDir),
storybookBuildDir: maybe(storybookBuildDir),
storybookConfigDir: maybe(storybookConfigDir),
traceChanged: maybe(traceChanged),
untraced: maybe(untraced),
zip: maybe(zip, false),
},
});

Object.entries(output).forEach(([key, value]) => setOutput(key, String(value)));
Expand Down
6 changes: 5 additions & 1 deletion bin-src/lib/getOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const undefinedIfEmpty = <T>(array: T[]) => {
return filtered.length ? filtered : undefined;
};

export default function getOptions({ argv, env, flags, log, packageJson }: Context): Options {
export default function getOptions(
{ argv, env, flags, log, packageJson }: Context,
extraOptions: Partial<Options> = {}
): Options {
const fromCI = !!flags.ci || !!process.env.CI;
const [patchHeadRef, patchBaseRef] = (flags.patchBuild || '').split('...').filter(Boolean);
const [branchName, branchOwner] = (flags.branchName || '').split(':').reverse();
Expand Down Expand Up @@ -75,6 +78,7 @@ export default function getOptions({ argv, env, flags, log, packageJson }: Conte
branchName,
patchHeadRef,
patchBaseRef,
...extraOptions,
};

if (flags.debug) {
Expand Down
2 changes: 2 additions & 0 deletions bin-src/lib/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const createTask = ({ title, steps, ...config }): Listr.ListrTask<Context
// eslint-disable-next-line no-await-in-loop
await step(ctx, task);
}

ctx.options.onTaskComplete?.(ctx);
},
...config,
});
Expand Down
10 changes: 5 additions & 5 deletions bin-src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { exitCodes, setExitCode } from './lib/setExitCode';
import { rewriteErrorMessage } from './lib/utils';
import { writeChromaticDiagnostics } from './lib/writeChromaticDiagnostics';
import getTasks from './tasks';
import { Context } from './types';
import { Context, Options } from './types';
import fatalError from './ui/messages/errors/fatalError';
import fetchError from './ui/messages/errors/fetchError';
import graphqlError from './ui/messages/errors/graphqlError';
Expand Down Expand Up @@ -60,13 +60,13 @@ export async function main(argv: string[]) {
process.exit(ctx.exitCode);
}

export async function runAll(ctx) {
export async function runAll(ctx, extraOptions?: Partial<Options>) {
setExitCode(ctx, exitCodes.OK);

ctx.http = (ctx.http as HTTPClient) || new HTTPClient(ctx);

// Run these in parallel; neither should ever reject
await Promise.all([runBuild(ctx), checkForUpdates(ctx)]);
await Promise.all([runBuild(ctx, extraOptions), checkForUpdates(ctx)]);

if (ctx.exitCode === 0 || ctx.exitCode === 1) {
await checkPackageJson(ctx);
Expand All @@ -77,12 +77,12 @@ export async function runAll(ctx) {
}
}

export async function runBuild(ctx: Context) {
export async function runBuild(ctx: Context, extraOptions?: Partial<Options>) {
ctx.log.info('');
ctx.log.info(intro(ctx));

try {
ctx.options = await getOptions(ctx);
ctx.options = await getOptions(ctx, extraOptions);
} catch (e) {
ctx.log.info('');
ctx.log.error(fatalError(ctx, [e]));
Expand Down
3 changes: 3 additions & 0 deletions bin-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export interface Options {
branchName: string;
patchHeadRef: string;
patchBaseRef: string;

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

export interface Context {
Expand Down
16 changes: 13 additions & 3 deletions node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getEnv from '../bin-src/lib/getEnv';
import { createLogger } from '../bin-src/lib/log';
import parseArgs from '../bin-src/lib/parseArgs';
import { runAll } from '../bin-src/main';
import { Context, Flags } from '../bin-src/types';
import { Context, Flags, Options } from '../bin-src/types';

interface Output {
code: number;
Expand All @@ -25,7 +25,13 @@ interface Output {
inheritedCaptureCount: number;
}

export async function run(flags: Flags): Promise<Output> {
export async function runChromaticFull({
flags = {},
options = {},
}: {
flags?: Flags;
options?: Partial<Options>;
}): Promise<Output> {
const sessionId = uuid();
const env = getEnv();
const log = createLogger(sessionId, env);
Expand All @@ -41,7 +47,7 @@ export async function run(flags: Flags): Promise<Output> {
sessionId,
flags,
};
await runAll(ctx);
await runAll(ctx, options);

return {
// Keep this in sync with the configured outputs in action.yml
Expand All @@ -60,3 +66,7 @@ export async function run(flags: Flags): Promise<Output> {
inheritedCaptureCount: ctx.build?.inheritedCaptureCount,
};
}

export async function run(options: Partial<Options>): Promise<Output> {
return runChromaticFull({ options });
}

0 comments on commit 1358e1a

Please sign in to comment.