create-file.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. let index = 0;
  18. for await (const lineB of readFileByLine(file)) {
  19. const lineA = linesA[index];
  20. index++;
  21. if (lineA == null) {
  22. // The file becomes smaller
  23. isEqual = false;
  24. break;
  25. }
  26. if (lineA[0] === '#' && lineB[0] === '#') {
  27. continue;
  28. }
  29. if (lineA !== lineB) {
  30. isEqual = false;
  31. break;
  32. }
  33. }
  34. if (isEqual && index !== linesALen) {
  35. // The file becomes larger
  36. isEqual = false;
  37. }
  38. }
  39. if (isEqual) {
  40. console.log(picocolors.gray(`Same Content, bail out writing: ${filePath}`));
  41. return;
  42. }
  43. await traceAsync(picocolors.gray(`Writing ${filePath}`), async () => {
  44. if (linesALen < 10000) {
  45. return Bun.write(file, `${linesA.join('\n')}\n`);
  46. }
  47. const writer = file.writer();
  48. for (let i = 0; i < linesALen; i++) {
  49. writer.write(linesA[i]);
  50. writer.write('\n');
  51. }
  52. await writer.flush();
  53. return writer.end();
  54. }, picocolors.gray);
  55. }
  56. export const withBannerArray = (title: string, description: string[], date: Date, content: string[]) => {
  57. return [
  58. '#########################################',
  59. `# ${title}`,
  60. `# Last Updated: ${date.toISOString()}`,
  61. `# Size: ${content.length}`,
  62. ...description.map(line => (line ? `# ${line}` : '#')),
  63. '#########################################',
  64. ...content,
  65. '################## EOF ##################'
  66. ];
  67. };
  68. export const createRuleset = (
  69. title: string, description: string[], date: Date, content: string[],
  70. type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
  71. ) => {
  72. const surgeContent = withBannerArray(title, description, date, content);
  73. let _clashContent;
  74. switch (type) {
  75. case 'domainset':
  76. _clashContent = surgeDomainsetToClashDomainset(content);
  77. break;
  78. case 'ruleset':
  79. _clashContent = surgeRulesetToClashClassicalTextRuleset(content);
  80. break;
  81. default:
  82. throw new TypeError(`Unknown type: ${type as any}`);
  83. }
  84. const clashContent = withBannerArray(title, description, date, _clashContent);
  85. return [
  86. compareAndWriteFile(surgeContent, surgePath),
  87. compareAndWriteFile(clashContent, clashPath)
  88. ];
  89. };