create-file.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. export async function fileEqual(linesA: string[], source: AsyncIterable<string> | Iterable<string>): Promise<boolean> {
  9. if (linesA.length === 0) {
  10. return false;
  11. }
  12. const maxIndexA = linesA.length - 1;
  13. let index = -1;
  14. for await (const lineB of source) {
  15. index++;
  16. // b become bigger
  17. if (index > maxIndexA) {
  18. return false;
  19. }
  20. const lineA = linesA[index];
  21. const aFirstChar = lineA.charCodeAt(0);
  22. if (aFirstChar !== lineB.charCodeAt(0)) {
  23. return false;
  24. }
  25. // Now both line has the same first char
  26. // We only need to compare one of them
  27. if (
  28. aFirstChar === 35 // #
  29. || aFirstChar === 33 // !
  30. ) {
  31. continue;
  32. }
  33. if (lineA !== lineB) {
  34. return false;
  35. }
  36. }
  37. // b is not smaller than a
  38. return index === maxIndexA;
  39. }
  40. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  41. const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
  42. if (fs.existsSync(filePath)) {
  43. return fileEqual(linesA, readFileByLine(filePath));
  44. }
  45. console.log(`${filePath} does not exists, writing...`);
  46. return false;
  47. });
  48. if (isEqual) {
  49. console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
  50. return;
  51. }
  52. await span.traceChildAsync(`writing ${filePath}`, async () => {
  53. const linesALen = linesA.length;
  54. // The default highwater mark is normally 16384,
  55. // So we make sure direct write to file if the content is
  56. // most likely less than 500 lines
  57. if (linesALen < 500) {
  58. return writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n');
  59. }
  60. const writeStream = fs.createWriteStream(filePath);
  61. for (let i = 0; i < linesALen; i++) {
  62. const p = asyncWriteToStream(writeStream, linesA[i] + '\n');
  63. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  64. if (p) await p;
  65. }
  66. writeStream.end();
  67. writeStream.close();
  68. });
  69. }