From c3398c33531279c25e56da65ff54c8411ffea9c4 Mon Sep 17 00:00:00 2001 From: Cody Kaup Date: Mon, 28 Oct 2024 16:07:36 -0500 Subject: [PATCH 1/7] Account for rsbuild stats JSON output and multiple locations --- node-src/lib/getDependentStoryFiles.test.ts | 5 ++++- node-src/lib/getDependentStoryFiles.ts | 9 ++++++++- node-src/types.ts | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/node-src/lib/getDependentStoryFiles.test.ts b/node-src/lib/getDependentStoryFiles.test.ts index 55fe8f790..2b3d73a24 100644 --- a/node-src/lib/getDependentStoryFiles.test.ts +++ b/node-src/lib/getDependentStoryFiles.test.ts @@ -179,7 +179,10 @@ describe('getDependentStoryFiles', () => { id: String.raw`./src lazy recursive ^\.\/.*$`, reasons: [ { - moduleName: './node_modules/.cache/storybook/default/dev-server/storybook-stories.js', + resolvedModule: + './node_modules/.cache/storybook/default/dev-server/storybook-stories.js', + moduleName: + './node_modules/.cache/storybook/default/dev-server/storybook-stories.js + 2 modules', }, ], }, diff --git a/node-src/lib/getDependentStoryFiles.ts b/node-src/lib/getDependentStoryFiles.ts index 94c44e493..d0940ca78 100644 --- a/node-src/lib/getDependentStoryFiles.ts +++ b/node-src/lib/getDependentStoryFiles.ts @@ -56,6 +56,7 @@ const getPackageName = (modulePath: string) => { */ export function normalizePath(posixPath: string, rootPath: string, baseDirectory = '') { if (!posixPath || posixPath.startsWith('/virtual:')) return posixPath; + return path.posix.isAbsolute(posixPath) ? path.posix.relative(rootPath, posixPath) : path.posix.join(baseDirectory, posixPath); @@ -140,6 +141,7 @@ export async function getDependentStoryFiles( `/virtual:/@storybook/builder-vite/vite-app.js`, // rspack builder `./node_modules/.cache/storybook/default/dev-server/storybook-stories.js`, + `./node_modules/.cache/storybook/storybook-rsbuild-builder/storybook-config-entry.js`, ].map((file) => normalize(file)) ); @@ -172,7 +174,12 @@ export async function getDependentStoryFiles( } const normalizedReasons = module_.reasons - ?.map((reason) => normalize(reason.moduleName)) + ?.map((reason) => + normalize( + reason.resolvedModule || // rspack sets a resolvedModule that holds the module name + reason.moduleName // vite, webpack, and default + ) + ) .filter((reasonName) => reasonName && reasonName !== normalizedName); if (normalizedReasons) { reasonsById.set(module_.id, normalizedReasons); diff --git a/node-src/types.ts b/node-src/types.ts index 8e34eab82..1e39a0f1d 100644 --- a/node-src/types.ts +++ b/node-src/types.ts @@ -352,6 +352,7 @@ export interface Task { } export interface Reason { + resolvedModule?: string; // rspack sets a resolvedModule field that holds the module name moduleName: string; } export interface Module { From d66dee9f8a206710352f0fea762b929ff76a287a Mon Sep 17 00:00:00 2001 From: Cody Kaup Date: Fri, 1 Nov 2024 12:05:55 -0500 Subject: [PATCH 2/7] Fix diagnostic flag parsing --- node-src/lib/getOptions.test.ts | 6 ++++++ node-src/lib/getOptions.ts | 6 ++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/node-src/lib/getOptions.test.ts b/node-src/lib/getOptions.test.ts index 1421544ea..a0c63869b 100644 --- a/node-src/lib/getOptions.test.ts +++ b/node-src/lib/getOptions.test.ts @@ -209,4 +209,10 @@ describe('getOptions', () => { uploadMetadata: true, }); }); + + it('allows you to specify a diagnostics file name', async () => { + expect(getOptions(getContext(['--diagnostics-file', 'output.json']))).toMatchObject({ + diagnosticsFile: 'output.json', + }); + }); }); diff --git a/node-src/lib/getOptions.ts b/node-src/lib/getOptions.ts index 947556fa0..c9dccf00b 100644 --- a/node-src/lib/getOptions.ts +++ b/node-src/lib/getOptions.ts @@ -141,11 +141,9 @@ export default function getOptions(ctx: InitialContext): Options { forceRebuild: trueIfSet(flags.forceRebuild), debug: flags.debug, diagnosticsFile: - defaultIfSet(flags.diagnosticsFile, DEFAULT_DIAGNOSTICS_FILE) || + defaultUnlessSetOrFalse(flags.diagnosticsFile, DEFAULT_DIAGNOSTICS_FILE) || // for backwards compatibility - flags.diagnostics - ? DEFAULT_DIAGNOSTICS_FILE - : undefined, + (flags.diagnostics ? DEFAULT_DIAGNOSTICS_FILE : undefined), junitReport: defaultIfSet(flags.junitReport, DEFAULT_REPORT_FILE), zip: flags.zip, skipUpdateCheck: flags.skipUpdateCheck, From 6cd81a7438ef13faa1ccea702d870d527b64e34a Mon Sep 17 00:00:00 2001 From: Cody Kaup Date: Fri, 1 Nov 2024 16:01:28 -0500 Subject: [PATCH 3/7] Ensure parent directory exists before writing log/diagnostics file --- node-src/index.test.ts | 1 + node-src/lib/log.ts | 12 +++++--- .../lib/writeChromaticDiagnostics.test.ts | 30 +++++++++++++++++-- node-src/lib/writeChromaticDiagnostics.ts | 5 ++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/node-src/index.test.ts b/node-src/index.test.ts index 0ba0b7c3c..a9f34a9c1 100644 --- a/node-src/index.test.ts +++ b/node-src/index.test.ts @@ -284,6 +284,7 @@ vi.mock('fs', async (importOriginal) => { const originalModule = (await importOriginal()) as any; return { pathExists: async () => true, + mkdirSync: vi.fn(), readFileSync: originalModule.readFileSync, writeFileSync: vi.fn(), createReadStream: vi.fn(() => mockStatsFile), diff --git a/node-src/lib/log.ts b/node-src/lib/log.ts index ffa999cab..4773e79af 100644 --- a/node-src/lib/log.ts +++ b/node-src/lib/log.ts @@ -1,6 +1,7 @@ import chalk from 'chalk'; import debug from 'debug'; -import { createWriteStream, rm } from 'fs'; +import { createWriteStream, mkdirSync, rm } from 'fs'; +import path from 'path'; import stripAnsi from 'strip-ansi'; import { format } from 'util'; @@ -96,13 +97,16 @@ const fileLogger = { this.append = () => {}; this.queue = []; }, - initialize(path: string, onError: LogFunction) { - rm(path, { force: true }, (err) => { + initialize(filepath: string, onError: LogFunction) { + rm(filepath, { force: true }, (err) => { if (err) { this.disable(); onError(err); } else { - const stream = createWriteStream(path, { flags: 'a' }); + // Ensure the parent directory exists before we create the stream + mkdirSync(path.dirname(filepath), { recursive: true }); + + const stream = createWriteStream(filepath, { flags: 'a' }); this.append = (...messages: string[]) => { stream?.write( messages diff --git a/node-src/lib/writeChromaticDiagnostics.test.ts b/node-src/lib/writeChromaticDiagnostics.test.ts index ecc8e4841..2dea57000 100644 --- a/node-src/lib/writeChromaticDiagnostics.test.ts +++ b/node-src/lib/writeChromaticDiagnostics.test.ts @@ -1,6 +1,13 @@ -import { describe, expect, it } from 'vitest'; +import { mkdirSync } from 'fs'; +import jsonfile from 'jsonfile'; +import path from 'path'; +import { describe, expect, it, vi } from 'vitest'; -import { getDiagnostics } from './writeChromaticDiagnostics'; +import { createLogger } from './log'; +import { getDiagnostics, writeChromaticDiagnostics } from './writeChromaticDiagnostics'; + +vi.mock('jsonfile'); +vi.mock('fs'); describe('getDiagnostics', () => { it('returns context object', () => { @@ -26,3 +33,22 @@ describe('getDiagnostics', () => { }); }); }); + +describe('writeChromaticDiagnostics', () => { + it('should create the parent directory if it does not exist', async () => { + const ctx = { + log: createLogger({}), + options: { diagnosticsFile: '/tmp/doesnotexist/diagnostics.json' }, + }; + await writeChromaticDiagnostics(ctx as any); + + expect(mkdirSync).toHaveBeenCalledWith(path.dirname(ctx.options.diagnosticsFile), { + recursive: true, + }); + expect(jsonfile.writeFile).toHaveBeenCalledWith( + ctx.options.diagnosticsFile, + expect.any(Object), + expect.any(Object) + ); + }); +}); diff --git a/node-src/lib/writeChromaticDiagnostics.ts b/node-src/lib/writeChromaticDiagnostics.ts index 8fcd3a8d6..25c9b0719 100644 --- a/node-src/lib/writeChromaticDiagnostics.ts +++ b/node-src/lib/writeChromaticDiagnostics.ts @@ -1,4 +1,6 @@ +import { mkdirSync } from 'fs'; import jsonfile from 'jsonfile'; +import path from 'path'; import { Context } from '..'; import wroteReport from '../ui/messages/info/wroteReport'; @@ -17,6 +19,9 @@ export async function writeChromaticDiagnostics(ctx: Context) { } try { + // Ensure the parent directory exists before writing file + mkdirSync(path.dirname(ctx.options.diagnosticsFile), { recursive: true }); + await writeFile(ctx.options.diagnosticsFile, getDiagnostics(ctx), { spaces: 2 }); ctx.log.info(wroteReport(ctx.options.diagnosticsFile, 'Chromatic diagnostics')); } catch (error) { From 66afb630b6d5886adb9ce6cb100320a7bd40479e Mon Sep 17 00:00:00 2001 From: Chromatic Date: Mon, 4 Nov 2024 16:01:09 +0000 Subject: [PATCH 4/7] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88336df74..7eac86828 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +# v11.16.4 (Mon Nov 04 2024) + +#### 🐛 Bug Fix + +- Ensure parent directory exists before writing log/diagnostics file [#1117](https://github.com/chromaui/chromatic-cli/pull/1117) ([@codykaup](https://github.com/codykaup)) +- Fix `--diagnostics-file` parsing [#1116](https://github.com/chromaui/chromatic-cli/pull/1116) ([@codykaup](https://github.com/codykaup)) +- Add steps for how to run builds against local CLI [#1113](https://github.com/chromaui/chromatic-cli/pull/1113) ([@codykaup](https://github.com/codykaup)) + +#### Authors: 1 + +- Cody Kaup ([@codykaup](https://github.com/codykaup)) + +--- + # v11.16.3 (Wed Oct 30 2024) #### ⚠️ Pushed to `main` From 6f84caf266c02c54d3172372de07ca7f0d4b0b01 Mon Sep 17 00:00:00 2001 From: Chromatic Date: Mon, 4 Nov 2024 16:01:10 +0000 Subject: [PATCH 5/7] Bump version to: 11.16.4 [skip ci] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 39f67eb26..89d0e92ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "11.16.3", + "version": "11.16.4", "description": "Automate visual testing across browsers. Gather UI feedback. Versioned documentation.", "keywords": [ "storybook-addon", From f57ab6bfe055628c2363247aec73d0c273f9e1ac Mon Sep 17 00:00:00 2001 From: Chromatic Date: Mon, 4 Nov 2024 21:40:36 +0000 Subject: [PATCH 6/7] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eac86828..a761ade82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +# v11.16.5 (Mon Nov 04 2024) + +#### 🐛 Bug Fix + +- Account for `rsbuild` stats JSON output and multiple locations [#1110](https://github.com/chromaui/chromatic-cli/pull/1110) ([@codykaup](https://github.com/codykaup)) + +#### Authors: 1 + +- Cody Kaup ([@codykaup](https://github.com/codykaup)) + +--- + # v11.16.4 (Mon Nov 04 2024) #### 🐛 Bug Fix From 89e6d25fe3cafbe07dbf4563264737691aef4a48 Mon Sep 17 00:00:00 2001 From: Chromatic Date: Mon, 4 Nov 2024 21:40:36 +0000 Subject: [PATCH 7/7] Bump version to: 11.16.5 [skip ci] --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 89d0e92ff..0f2b4200a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chromatic", - "version": "11.16.4", + "version": "11.16.5", "description": "Automate visual testing across browsers. Gather UI feedback. Versioned documentation.", "keywords": [ "storybook-addon",