From 76bb899102015dd672acc8ad4c2272cbc12e73a2 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 1 Aug 2026 13:22:58 +0800 Subject: [PATCH] Chore: skip file writing stream --- Build/lib/create-file.ts | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/Build/lib/create-file.ts b/Build/lib/create-file.ts index 52c9a22b..d81c14be 100644 --- a/Build/lib/create-file.ts +++ b/Build/lib/create-file.ts @@ -1,4 +1,3 @@ -import { asyncWriteToStream } from 'foxts/async-write-to-stream'; import { fastStringArrayJoin } from 'foxts/fast-string-array-join'; import fs from 'node:fs'; import picocolors from 'picocolors'; @@ -6,7 +5,6 @@ import type { Span } from '../trace'; import { readFileByLine } from './fetch-text-by-line'; import { writeFile } from './misc'; import { createCompareSource, fileEqualWithCommentComparator } from 'foxts/compare-source'; -import { promisify } from 'node:util'; import { extractContentHashFromFile } from './content-hash'; const fileEqual = createCompareSource(fileEqualWithCommentComparator); @@ -49,26 +47,12 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath return; } - return span.traceChildAsync(`writing ${filePath}`, async () => { - const linesALen = linesA.length; - - // The default highwater mark is normally 16384, - // So we make sure direct write to file if the content is - // most likely less than 250 lines - if (linesALen < 250) { - return writeFile(filePath, fastStringArrayJoin(linesA, '\n')); - } - - const writeStream = fs.createWriteStream(filePath); - for (let i = 0; i < linesALen; i++) { - const p = asyncWriteToStream(writeStream, linesA[i] + '\n'); - // eslint-disable-next-line no-await-in-loop -- stream high water mark - if (p) await p; - } - await new Promise(resolve => { - // Since we previously poped the last empty line for comparison, we need to add it back here to ensure final EOF line - writeStream.end(resolve); - }); - await promisify(writeStream.close.bind(writeStream))(); - }); + return writeFileLines(span, linesA, filePath); +} + +export function writeFileLines(span: Span, linesA: string[], filePath: string): Promise { + return span.traceChildAsync( + `writing ${filePath}`, + () => writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n') + ); }