Fix: write file properly

This commit is contained in:
SukkaW 2024-09-20 16:19:35 +08:00
parent 5c8636d7b3
commit 31136d8409
2 changed files with 17 additions and 4 deletions

View File

@ -50,4 +50,10 @@ describe('fileEqual', () => {
['A', 'B'], ['A', 'B'],
false false
)); ));
it('eol', () => test(
['A', 'B'],
['A', 'B', ''],
true
));
}); });

View File

@ -17,16 +17,21 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
return false; return false;
} }
let index = 0; let index = -1;
for await (const lineB of source) { for await (const lineB of source) {
const lineA = linesA[index] as string | undefined;
index++; index++;
if (lineA == null) { if (index > linesA.length - 1) {
if (index === linesA.length && lineB === '') {
index--;
continue;
}
// The file becomes smaller // The file becomes smaller
return false; return false;
} }
const lineA = linesA[index];
if (lineA[0] === '#' && lineB[0] === '#') { if (lineA[0] === '#' && lineB[0] === '#') {
continue; continue;
} }
@ -46,7 +51,7 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
} }
} }
if (index !== linesA.length) { if (index !== linesA.length - 1) {
// The file becomes larger // The file becomes larger
return false; return false;
} }
@ -84,7 +89,9 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
// 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 asyncWriteToStream(writeStream, '\n'); await asyncWriteToStream(writeStream, '\n');
writeStream.end(); writeStream.end();
}); });
} }