Skip to content

Commit 1f6e326

Browse files
author
John Doe
committed
refactor: wip
1 parent b0c9cc4 commit 1f6e326

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

packages/utils/src/lib/file-sink-text.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'node:fs';
22
import { existsSync, mkdirSync } from 'node:fs';
33
import path from 'node:path';
4+
import { PROFILER_FILE_BASE_NAME, PROFILER_OUT_DIR } from './profiler';
45
import type {
56
RecoverOptions,
67
RecoverResult,
@@ -60,6 +61,42 @@ export const stringRecover = function <I, O extends FileOutput = FileOutput>(
6061
return { records, errors, partialTail };
6162
};
6263

64+
export type FileNameOptions = {
65+
fileBaseName: string;
66+
outDir: string;
67+
fileName?: string;
68+
};
69+
70+
export function getFilenameParts(options: FileNameOptions): {
71+
outDir: string;
72+
fileName: string;
73+
} {
74+
const { fileName, fileBaseName, outDir } = options;
75+
76+
if (fileName) {
77+
return {
78+
outDir,
79+
fileName,
80+
};
81+
}
82+
83+
const baseName = fileBaseName;
84+
const DATE_LENGTH = 10;
85+
const TIME_SEGMENTS = 3;
86+
const COLON_LENGTH = 1;
87+
const TOTAL_TIME_LENGTH =
88+
TIME_SEGMENTS * 2 + (TIME_SEGMENTS - 1) * COLON_LENGTH; // HH:MM:SS = 8 chars
89+
const id = new Date()
90+
.toISOString()
91+
.slice(0, DATE_LENGTH + TOTAL_TIME_LENGTH)
92+
.replace(/:/g, '-');
93+
94+
return {
95+
outDir,
96+
fileName: `${baseName}.${id}`,
97+
};
98+
}
99+
63100
export type FileSinkOptions = {
64101
filePath: string;
65102
recover?: () => RecoverResult;

0 commit comments

Comments
 (0)