create-file.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // @ts-check
  2. import { readFileByLine } from './fetch-text-by-line';
  3. import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
  4. import { traceAsync } from './trace-runner';
  5. import picocolors from 'picocolors';
  6. export async function compareAndWriteFile(linesA: string[], filePath: string) {
  7. let isEqual = true;
  8. const file = Bun.file(filePath);
  9. const linesALen = linesA.length;
  10. if (!(await file.exists())) {
  11. console.log(`${filePath} does not exists, writing...`);
  12. isEqual = false;
  13. } else if (linesALen === 0) {
  14. console.log(`Nothing to write to ${filePath}...`);
  15. isEqual = false;
  16. } else {
  17. isEqual = await traceAsync(
  18. picocolors.gray(`Comparing ${filePath}`),
  19. async () => {
  20. let index = 0;
  21. for await (const lineB of readFileByLine(file)) {
  22. const lineA = linesA[index];
  23. index++;
  24. if (lineA == null) {
  25. // The file becomes smaller
  26. return false;
  27. }
  28. if (lineA[0] === '#' && lineB[0] === '#') {
  29. continue;
  30. }
  31. if (lineA !== lineB) {
  32. return false;
  33. }
  34. }
  35. if (index !== linesALen) {
  36. // The file becomes larger
  37. return false;
  38. }
  39. return true;
  40. },
  41. picocolors.gray
  42. );
  43. }
  44. if (isEqual) {
  45. console.log(picocolors.gray(`Same Content, bail out writing: ${filePath}`));
  46. return;
  47. }
  48. await traceAsync(picocolors.gray(`Writing ${filePath}`), async () => {
  49. if (linesALen < 10000) {
  50. return Bun.write(file, `${linesA.join('\n')}\n`);
  51. }
  52. const writer = file.writer();
  53. for (let i = 0; i < linesALen; i++) {
  54. writer.write(linesA[i]);
  55. writer.write('\n');
  56. }
  57. await writer.flush();
  58. return writer.end();
  59. }, picocolors.gray);
  60. }
  61. export const withBannerArray = (title: string, description: string[], date: Date, content: string[]) => {
  62. return [
  63. '#########################################',
  64. `# ${title}`,
  65. `# Last Updated: ${date.toISOString()}`,
  66. `# Size: ${content.length}`,
  67. ...description.map(line => (line ? `# ${line}` : '#')),
  68. '#########################################',
  69. ...content,
  70. '################## EOF ##################'
  71. ];
  72. };
  73. export const createRuleset = (
  74. title: string, description: string[], date: Date, content: string[],
  75. type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
  76. ) => {
  77. const surgeContent = withBannerArray(title, description, date, content);
  78. let _clashContent;
  79. switch (type) {
  80. case 'domainset':
  81. _clashContent = surgeDomainsetToClashDomainset(content);
  82. break;
  83. case 'ruleset':
  84. _clashContent = surgeRulesetToClashClassicalTextRuleset(content);
  85. break;
  86. default:
  87. throw new TypeError(`Unknown type: ${type as any}`);
  88. }
  89. const clashContent = withBannerArray(title, description, date, _clashContent);
  90. return [
  91. compareAndWriteFile(surgeContent, surgePath),
  92. compareAndWriteFile(clashContent, clashPath)
  93. ];
  94. };