create-file.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 linesABound = linesA.length - 1;
  13. let index = -1;
  14. for await (const lineB of source) {
  15. index++;
  16. if (index > linesABound) {
  17. return (index === linesA.length && lineB.length === 0);
  18. }
  19. const lineA = linesA[index];
  20. if (lineA.length === 0) {
  21. if (lineB.length === 0) {
  22. // both lines are empty, check next line
  23. continue;
  24. }
  25. // lineA is empty but lineB is not
  26. return false;
  27. }
  28. // now lineA can not be empty
  29. if (lineB.length === 0) {
  30. // lineB is empty but lineA is not
  31. return false;
  32. }
  33. // now both lines can not be empty
  34. const firstCharA = lineA.charCodeAt(0);
  35. const firstCharB = lineB.charCodeAt(0);
  36. if (firstCharA !== firstCharB) {
  37. return false;
  38. }
  39. // now firstCharA is equal to firstCharB, we only need to check the first char
  40. if (firstCharA === 35 /* # */) {
  41. continue;
  42. }
  43. // adguard conf
  44. if (firstCharA === 33 /* ! */) {
  45. continue;
  46. }
  47. if (
  48. firstCharA === 47 /* / */
  49. && lineA[1] === '/' && lineB[1] === '/'
  50. && lineA[3] === '#' && lineB[3] === '#'
  51. ) {
  52. continue;
  53. }
  54. if (lineA !== lineB) {
  55. return false;
  56. }
  57. }
  58. // The file becomes larger
  59. return !(index < linesABound);
  60. }
  61. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  62. const linesALen = linesA.length;
  63. const isEqual = await span.traceChildAsync(`compare ${filePath}`, async () => {
  64. if (fs.existsSync(filePath)) {
  65. return fileEqual(linesA, readFileByLine(filePath));
  66. }
  67. console.log(`${filePath} does not exists, writing...`);
  68. return false;
  69. });
  70. if (isEqual) {
  71. console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
  72. return;
  73. }
  74. await span.traceChildAsync(`writing ${filePath}`, async () => {
  75. // The default highwater mark is normally 16384,
  76. // So we make sure direct write to file if the content is
  77. // most likely less than 500 lines
  78. if (linesALen < 500) {
  79. return writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n');
  80. }
  81. const writeStream = fs.createWriteStream(filePath);
  82. for (let i = 0; i < linesALen; i++) {
  83. const p = asyncWriteToStream(writeStream, linesA[i] + '\n');
  84. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  85. if (p) await p;
  86. }
  87. writeStream.end();
  88. });
  89. }