diff --git a/node-src/index.ts b/node-src/index.ts index 01c291c42..472482e5a 100644 --- a/node-src/index.ts +++ b/node-src/index.ts @@ -6,6 +6,7 @@ import { v4 as uuid } from 'uuid'; import { getBranch, getCommit, getSlug, getUncommittedHash, getUserEmail } from './git/git'; import GraphQLClient from './io/GraphQLClient'; import HTTPClient from './io/HTTPClient'; +import LoggingRenderer from './lib/LoggingRenderer'; import NonTTYRenderer from './lib/NonTTYRenderer'; import checkForUpdates from './lib/checkForUpdates'; import checkPackageJson from './lib/checkPackageJson'; @@ -136,6 +137,7 @@ export async function run({ export async function runAll(ctx: InitialContext) { ctx.log.info(''); ctx.log.info(intro(ctx)); + ctx.log.info(''); const onError = (e: Error | Error[]) => { ctx.log.info(''); @@ -188,9 +190,16 @@ export async function runAll(ctx: InitialContext) { async function runBuild(ctx: Context) { try { try { - ctx.log.info(''); - if (ctx.options.interactive) ctx.log.queue(); // queue up any log messages while Listr is running - const options = ctx.options.interactive ? {} : { renderer: NonTTYRenderer, log: ctx.log }; + const options = { + log: ctx.log, + renderer: NonTTYRenderer, + }; + if (ctx.options.interactive) { + // Use an enhanced version of Listr's default renderer, which also logs to a file + options.renderer = LoggingRenderer; + // Queue up any non-Listr log messages while Listr is running + ctx.log.queue(); + } await new Listr(getTasks(ctx.options), options).run(ctx); } catch (err) { endActivity(ctx); diff --git a/node-src/runBuild.ts b/node-src/runBuild.ts deleted file mode 100644 index b406348ad..000000000 --- a/node-src/runBuild.ts +++ /dev/null @@ -1,111 +0,0 @@ -import Listr from 'listr'; - -import GraphQLClient from './io/GraphQLClient'; -import { getConfiguration } from './lib/getConfiguration'; -import getOptions from './lib/getOptions'; -import LoggingRenderer from './lib/LoggingRenderer'; -import NonTTYRenderer from './lib/NonTTYRenderer'; -import { exitCodes, setExitCode } from './lib/setExitCode'; -import { rewriteErrorMessage } from './lib/utils'; -import getTasks from './tasks'; -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'; -import missingStories from './ui/messages/errors/missingStories'; -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'; - -export async function runBuild(ctx: Context) { - ctx.log.info(''); - ctx.log.info(intro(ctx)); - - try { - ctx.configuration = await getConfiguration( - ctx.extraOptions?.configFile || ctx.flags.configFile - ); - ctx.options = await getOptions(ctx); - } catch (e) { - ctx.log.info(''); - ctx.log.error(fatalError(ctx, [e])); - (ctx.options || ctx.extraOptions)?.experimental_onTaskError?.(ctx, { - formattedError: fatalError(ctx, [e]), - originalError: e, - }); - setExitCode(ctx, exitCodes.INVALID_OPTIONS, true); - return; - } - - try { - ctx.client = new GraphQLClient(ctx, `${ctx.env.CHROMATIC_INDEX_URL}/graphql`, { - headers: { - 'x-chromatic-session-id': ctx.sessionId, - 'x-chromatic-cli-version': ctx.pkg.version, - }, - retries: 3, - }); - - try { - ctx.log.info(''); - const options = { - log: ctx.log, - renderer: NonTTYRenderer, - }; - if (ctx.options.interactive) { - // Use an enhanced version of Listr's default renderer, which also logs to a file - options.renderer = LoggingRenderer; - // Queue up any non-Listr log messages while Listr is running - ctx.log.queue(); - } - await new Listr(getTasks(ctx.options), options).run(ctx); - } catch (err) { - endActivity(ctx); - if (err.code === 'ECONNREFUSED' || err.name === 'StatusCodeError') { - setExitCode(ctx, exitCodes.FETCH_ERROR); - throw rewriteErrorMessage(err, fetchError(ctx, err)); - } - if (err.name === 'GraphQLError') { - setExitCode(ctx, exitCodes.GRAPHQL_ERROR); - throw rewriteErrorMessage(err, graphqlError(ctx, err)); - } - if (err.message.startsWith('Cannot run a build with no stories')) { - setExitCode(ctx, exitCodes.BUILD_NO_STORIES); - throw rewriteErrorMessage(err, missingStories(ctx)); - } - if (ctx.options.experimental_abortSignal?.aborted) { - setExitCode(ctx, exitCodes.BUILD_WAS_CANCELED, true); - throw rewriteErrorMessage(err, buildCanceled()); - } - throw rewriteErrorMessage(err, taskError(ctx, err)); - } finally { - // Handle potential runtime errors from JSDOM - const { runtimeErrors, runtimeWarnings } = ctx; - if ((runtimeErrors && runtimeErrors.length) || (runtimeWarnings && runtimeWarnings.length)) { - ctx.log.info(''); - ctx.log.error(runtimeError(ctx)); - } - - ctx.log.flush(); - } - } catch (error) { - const errors = [].concat(error); // GraphQLClient might throw an array of errors - const formattedError = fatalError(ctx, errors); - - ctx.options.experimental_onTaskError?.(ctx, { - formattedError, - originalError: errors[0], - }); - - if (!ctx.userError) { - ctx.log.info(''); - ctx.log.error(formattedError); - } - - if (!ctx.exitCode) { - setExitCode(ctx, exitCodes.UNKNOWN_ERROR); - } - } -}