create-file.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { asyncWriteToStream } from 'foxts/async-write-to-stream';
  2. import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
  3. import fs from 'node:fs';
  4. import picocolors from 'picocolors';
  5. import type { Span } from '../trace';
  6. import { readFileByLine } from './fetch-text-by-line';
  7. import { writeFile } from './misc';
  8. import { createCompareSource, fileEqualWithCommentComparator } from 'foxts/compare-source';
  9. import { promisify } from 'node:util';
  10. export const fileEqual = createCompareSource(fileEqualWithCommentComparator);
  11. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  12. // readFileByLine will not include last empty line. So we always pop the linesA for comparison purpose
  13. if (linesA.length > 0 && linesA[linesA.length - 1] === '') {
  14. linesA.pop();
  15. }
  16. const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
  17. if (fs.existsSync(filePath)) {
  18. return fileEqual(linesA, readFileByLine(filePath));
  19. }
  20. console.log(`${filePath} does not exists, writing...`);
  21. return false;
  22. });
  23. if (isEqual) {
  24. console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
  25. return;
  26. }
  27. return span.traceChildAsync<void>(`writing ${filePath}`, async () => {
  28. const linesALen = linesA.length;
  29. // The default highwater mark is normally 16384,
  30. // So we make sure direct write to file if the content is
  31. // most likely less than 250 lines
  32. if (linesALen < 250) {
  33. return writeFile(filePath, fastStringArrayJoin(linesA, '\n'));
  34. }
  35. const writeStream = fs.createWriteStream(filePath);
  36. let p;
  37. for (let i = 0; i < linesALen; i++) {
  38. p = asyncWriteToStream(writeStream, linesA[i] + '\n');
  39. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  40. if (p) await p;
  41. }
  42. await new Promise<void>(resolve => {
  43. // Since we previously poped the last empty line for comparison, we need to add it back here to ensure final EOF line
  44. writeStream.end('\n', resolve);
  45. });
  46. await promisify(writeStream.close.bind(writeStream))();
  47. });
  48. }