Skip to content

Commit

Permalink
Ensure parent directory exists before writing log/diagnostics file
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Nov 1, 2024
1 parent db3848b commit 6cd81a7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions node-src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 8 additions & 4 deletions node-src/lib/log.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions node-src/lib/writeChromaticDiagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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)
);
});
});
5 changes: 5 additions & 0 deletions node-src/lib/writeChromaticDiagnostics.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand Down

0 comments on commit 6cd81a7

Please sign in to comment.