mirror of
https://github.com/SukkaW/Surge.git
synced 2026-03-15 07:26:42 +08:00
Chore: update EOL writing
This commit is contained in:
@@ -11,8 +11,9 @@ import { promisify } from 'node:util';
|
|||||||
export const fileEqual = createCompareSource(fileEqualWithCommentComparator);
|
export const fileEqual = createCompareSource(fileEqualWithCommentComparator);
|
||||||
|
|
||||||
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
|
||||||
if (linesA[linesA.length - 1] !== '') {
|
// readFileByLine will not include last empty line. So we always pop the linesA for comparison purpose
|
||||||
linesA.push('');
|
if (linesA.length > 0 && linesA[linesA.length - 1] === '') {
|
||||||
|
linesA.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
|
const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
|
||||||
@@ -32,6 +33,8 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
return span.traceChildAsync<void>(`writing ${filePath}`, async () => {
|
return span.traceChildAsync<void>(`writing ${filePath}`, async () => {
|
||||||
const linesALen = linesA.length;
|
const linesALen = linesA.length;
|
||||||
|
|
||||||
|
console.log('writing', { linesA: fastStringArrayJoin(linesA, '\n').slice(-100) });
|
||||||
|
|
||||||
// The default highwater mark is normally 16384,
|
// The default highwater mark is normally 16384,
|
||||||
// So we make sure direct write to file if the content is
|
// So we make sure direct write to file if the content is
|
||||||
// most likely less than 250 lines
|
// most likely less than 250 lines
|
||||||
@@ -40,13 +43,15 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
|||||||
}
|
}
|
||||||
|
|
||||||
const writeStream = fs.createWriteStream(filePath);
|
const writeStream = fs.createWriteStream(filePath);
|
||||||
|
let p;
|
||||||
for (let i = 0; i < linesALen; i++) {
|
for (let i = 0; i < linesALen; i++) {
|
||||||
const p = asyncWriteToStream(writeStream, linesA[i] + '\n');
|
p = asyncWriteToStream(writeStream, linesA[i] + '\n');
|
||||||
// eslint-disable-next-line no-await-in-loop -- stream high water mark
|
// eslint-disable-next-line no-await-in-loop -- stream high water mark
|
||||||
if (p) await p;
|
if (p) await p;
|
||||||
}
|
}
|
||||||
await new Promise<void>(resolve => {
|
await new Promise<void>(resolve => {
|
||||||
writeStream.end(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('\n', resolve);
|
||||||
});
|
});
|
||||||
await promisify(writeStream.close.bind(writeStream))();
|
await promisify(writeStream.close.bind(writeStream))();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user