create-file.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. return writer.end();
  58. }, picocolors.gray);
  59. }
  60. export const withBannerArray = (title: string, description: string[], date: Date, content: string[]) => {
  61. return [
  62. '#########################################',
  63. `# ${title}`,
  64. `# Last Updated: ${date.toISOString()}`,
  65. `# Size: ${content.length}`,
  66. ...description.map(line => (line ? `# ${line}` : '#')),
  67. '#########################################',
  68. ...content,
  69. '################## EOF ##################'
  70. ];
  71. };
  72. export const createRuleset = (
  73. title: string, description: string[], date: Date, content: string[],
  74. type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
  75. ) => {
  76. const surgeContent = withBannerArray(title, description, date, content);
  77. let _clashContent;
  78. switch (type) {
  79. case 'domainset':
  80. _clashContent = surgeDomainsetToClashDomainset(content);
  81. break;
  82. case 'ruleset':
  83. _clashContent = surgeRulesetToClashClassicalTextRuleset(content);
  84. break;
  85. default:
  86. throw new TypeError(`Unknown type: ${type as any}`);
  87. }
  88. const clashContent = withBannerArray(title, description, date, _clashContent);
  89. return [
  90. compareAndWriteFile(surgeContent, surgePath),
  91. compareAndWriteFile(clashContent, clashPath)
  92. ];
  93. };