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 92c9481
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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
9 changes: 6 additions & 3 deletions node-src/lib/writeChromaticDiagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import jsonfile from 'jsonfile';
import { mkdirSync } from 'fs';
import { writeFile } from 'jsonfile';
import path from 'path';

import { Context } from '..';
import wroteReport from '../ui/messages/info/wroteReport';
import { redact } from './utils';

const { writeFile } = jsonfile;

/**
* Extract important information from ctx, sort it and output into a json file
*
Expand All @@ -17,6 +17,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 92c9481

Please sign in to comment.