From a5c365aad805ca81693c5a7e7dccd43cd5298a48 Mon Sep 17 00:00:00 2001 From: Todd Evanoff Date: Tue, 5 Mar 2024 17:26:07 -0800 Subject: [PATCH 1/4] rename --- node-src/index.test.ts | 2 +- node-src/lib/{getE2eBuildCommand.ts => e2e.ts} | 0 node-src/tasks/build.ts | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename node-src/lib/{getE2eBuildCommand.ts => e2e.ts} (100%) diff --git a/node-src/index.test.ts b/node-src/index.test.ts index 3ff03bd68..533628ff5 100644 --- a/node-src/index.test.ts +++ b/node-src/index.test.ts @@ -41,7 +41,7 @@ vi.mock('execa'); // NOTE: we'd prefer to mock the require.resolve() of `@chromatic-com/playwright/..` but // vitest doesn't allow you to do that. const mockedBuildCommand = 'mocked build command'; -vi.mock('./lib/getE2eBuildCommand', () => ({ +vi.mock('./lib/e2e', () => ({ getE2eBuildCommand: () => mockedBuildCommand, })); diff --git a/node-src/lib/getE2eBuildCommand.ts b/node-src/lib/e2e.ts similarity index 100% rename from node-src/lib/getE2eBuildCommand.ts rename to node-src/lib/e2e.ts diff --git a/node-src/tasks/build.ts b/node-src/tasks/build.ts index 7b320491e..241b3ce70 100644 --- a/node-src/tasks/build.ts +++ b/node-src/tasks/build.ts @@ -11,7 +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 { buildBinName as e2EbuildBinName, getE2eBuildCommand } from '../lib/getE2eBuildCommand'; +import { buildBinName as e2EbuildBinName, getE2eBuildCommand } from '../lib/e2e'; import missingDependency from '../ui/messages/errors/missingDependency'; export const setSourceDir = async (ctx: Context) => { From 97ea0ac9cb38314068772bd97c916ce11d27b141 Mon Sep 17 00:00:00 2001 From: Todd Evanoff Date: Tue, 5 Mar 2024 17:27:04 -0800 Subject: [PATCH 2/4] rename --- node-src/index.test.ts | 5 +++-- node-src/lib/e2e.ts | 2 +- node-src/tasks/build.ts | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/node-src/index.test.ts b/node-src/index.test.ts index 533628ff5..555fa27f5 100644 --- a/node-src/index.test.ts +++ b/node-src/index.test.ts @@ -41,8 +41,9 @@ vi.mock('execa'); // NOTE: we'd prefer to mock the require.resolve() of `@chromatic-com/playwright/..` but // vitest doesn't allow you to do that. const mockedBuildCommand = 'mocked build command'; -vi.mock('./lib/e2e', () => ({ - getE2eBuildCommand: () => mockedBuildCommand, +vi.mock('./lib/e2e', async (importOriginal) => ({ + ...await importOriginal(), + getE2EBuildCommand: () => mockedBuildCommand, })); const execaCommand = vi.mocked(execaDefault); diff --git a/node-src/lib/e2e.ts b/node-src/lib/e2e.ts index d596fd7b6..8795d65e6 100644 --- a/node-src/lib/e2e.ts +++ b/node-src/lib/e2e.ts @@ -27,7 +27,7 @@ const parseNexec = ((agent, args) => { return command.replace('{0}', args.map(quote).join(' ')).trim(); }); -export async function getE2eBuildCommand( +export async function getE2EBuildCommand( ctx: Context, flag: 'playwright' | 'cypress', buildCommandOptions: string[] diff --git a/node-src/tasks/build.ts b/node-src/tasks/build.ts index 241b3ce70..a65d0c8f6 100644 --- a/node-src/tasks/build.ts +++ b/node-src/tasks/build.ts @@ -11,7 +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 { buildBinName as e2EbuildBinName, getE2eBuildCommand } from '../lib/e2e'; +import { buildBinName as e2EbuildBinName, getE2EBuildCommand } from '../lib/e2e'; import missingDependency from '../ui/messages/errors/missingDependency'; export const setSourceDir = async (ctx: Context) => { @@ -44,7 +44,7 @@ export const setBuildCommand = async (ctx: Context) => { ].filter(Boolean); if (ctx.options.playwright || ctx.options.cypress) { - ctx.buildCommand = await getE2eBuildCommand( + ctx.buildCommand = await getE2EBuildCommand( ctx, ctx.options.playwright ? 'playwright' : 'cypress', buildCommandOptions From cfeec9a1be803559eee4907795cc91c9fcd62d92 Mon Sep 17 00:00:00 2001 From: Todd Evanoff Date: Tue, 5 Mar 2024 17:32:03 -0800 Subject: [PATCH 3/4] add `isE2EBuild` helper --- node-src/lib/e2e.ts | 6 +++++- node-src/lib/getOptions.ts | 3 ++- node-src/tasks/build.ts | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/node-src/lib/e2e.ts b/node-src/lib/e2e.ts index 8795d65e6..4a512624b 100644 --- a/node-src/lib/e2e.ts +++ b/node-src/lib/e2e.ts @@ -1,6 +1,6 @@ import { getCliCommand, Runner, AGENTS } from '@antfu/ni'; -import { Context } from '../types'; +import { Context, Options } from '../types'; import missingDependency from '../ui/messages/errors/missingDependency'; import { exitCodes, setExitCode } from './setExitCode'; import { failed } from '../ui/tasks/build'; @@ -59,3 +59,7 @@ export async function getE2EBuildCommand( throw err; } } + +export function isE2EBuild(options: Options) { + return options.playwright || options.cypress; +} diff --git a/node-src/lib/getOptions.ts b/node-src/lib/getOptions.ts index e4a026a30..40240fc5f 100644 --- a/node-src/lib/getOptions.ts +++ b/node-src/lib/getOptions.ts @@ -14,6 +14,7 @@ import missingBuildScriptName from '../ui/messages/errors/missingBuildScriptName import missingProjectToken from '../ui/messages/errors/missingProjectToken'; import deprecatedOption from '../ui/messages/warnings/deprecatedOption'; import invalidPackageJson from '../ui/messages/errors/invalidPackageJson'; +import { isE2EBuild } from './e2e'; const takeLast = (input: string | string[]) => Array.isArray(input) ? input[input.length - 1] : input; @@ -263,7 +264,7 @@ export default function getOptions({ return options; } - if (options.playwright || options.cypress) { + if (isE2EBuild(options)) { return options; } diff --git a/node-src/tasks/build.ts b/node-src/tasks/build.ts index a65d0c8f6..33b258070 100644 --- a/node-src/tasks/build.ts +++ b/node-src/tasks/build.ts @@ -11,7 +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 { buildBinName as e2EbuildBinName, getE2EBuildCommand } from '../lib/e2e'; +import { buildBinName as e2EbuildBinName, getE2EBuildCommand, isE2EBuild } from '../lib/e2e'; import missingDependency from '../ui/messages/errors/missingDependency'; export const setSourceDir = async (ctx: Context) => { @@ -43,7 +43,7 @@ export const setBuildCommand = async (ctx: Context) => { ctx.git.changedFiles && webpackStatsSupported && ctx.sourceDir, ].filter(Boolean); - if (ctx.options.playwright || ctx.options.cypress) { + if (isE2EBuild(ctx.options)) { ctx.buildCommand = await getE2EBuildCommand( ctx, ctx.options.playwright ? 'playwright' : 'cypress', @@ -88,7 +88,7 @@ export const buildStorybook = async (ctx: Context) => { // installed the right dependency or run from the right directory if ( ctx.options.inAction && - (ctx.options.playwright || ctx.options.cypress) && + (isE2EBuild(ctx.options)) && e.message.match(e2EbuildBinName) ) { const flag = ctx.options.playwright ? 'playwright' : 'cypress'; From 0440f7a9ef9f14c0e57d6e1062b9df77c4351c5e Mon Sep 17 00:00:00 2001 From: Todd Evanoff Date: Wed, 6 Mar 2024 10:14:12 -0800 Subject: [PATCH 4/4] do not prompt to install chromatic script during E2E builds --- node-src/index.test.ts | 9 +++++++++ node-src/index.ts | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/node-src/index.test.ts b/node-src/index.test.ts index 555fa27f5..bb06011f1 100644 --- a/node-src/index.test.ts +++ b/node-src/index.test.ts @@ -12,6 +12,7 @@ import { DNSResolveAgent } from './io/getDNSResolveAgent'; import getEnv from './lib/getEnv'; import parseArgs from './lib/parseArgs'; import TestLogger from './lib/testLogger'; +import * as checkPackageJson from './lib/checkPackageJson'; import { uploadFiles } from './lib/uploadFiles'; import { writeChromaticDiagnostics } from './lib/writeChromaticDiagnostics'; import { Context } from './types'; @@ -712,6 +713,14 @@ it('prompts you to add a script to your package.json', async () => { expect(confirm).toHaveBeenCalled(); }); +it('does not propmpt you to add a script to your package.json for E2E builds', async () => { + const ctx = getContext(['--project-token=asdf1234', '--playwright']); + await runAll(ctx); + const spy = vi.spyOn(checkPackageJson, 'default'); + expect(spy).not.toHaveBeenCalled(); + expect(confirm).not.toHaveBeenCalled(); +}); + it('ctx should be JSON serializable', async () => { const ctx = getContext(['--project-token=asdf1234']); expect(() => writeChromaticDiagnostics(ctx)).not.toThrow(); diff --git a/node-src/index.ts b/node-src/index.ts index ed62df0c4..54c01e235 100644 --- a/node-src/index.ts +++ b/node-src/index.ts @@ -39,6 +39,7 @@ import noPackageJson from './ui/messages/errors/noPackageJson'; import runtimeError from './ui/messages/errors/runtimeError'; import taskError from './ui/messages/errors/taskError'; import intro from './ui/messages/info/intro'; +import { isE2EBuild } from './lib/e2e'; /** Make keys of `T` outside of `R` optional. @@ -176,7 +177,7 @@ export async function runAll(ctx: InitialContext) { // Run these in parallel; neither should ever reject await Promise.all([runBuild(ctx), checkForUpdates(ctx)]).catch(onError); - if ([0, 1].includes(ctx.exitCode)) { + if (!isE2EBuild(ctx.options) && [0, 1].includes(ctx.exitCode)) { await checkPackageJson(ctx); }