create-file.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // @ts-check
  2. import { readFileByLine } from './fetch-text-by-line';
  3. import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
  4. import picocolors from 'picocolors';
  5. import type { Span } from '../trace';
  6. import path from 'path';
  7. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  8. let isEqual = true;
  9. const file = Bun.file(filePath);
  10. const linesALen = linesA.length;
  11. if (!(await file.exists())) {
  12. console.log(`${filePath} does not exists, writing...`);
  13. isEqual = false;
  14. } else if (linesALen === 0) {
  15. console.log(`Nothing to write to ${filePath}...`);
  16. isEqual = false;
  17. } else {
  18. isEqual = await span.traceChildAsync(`comparing ${filePath}`, async () => {
  19. let index = 0;
  20. for await (const lineB of readFileByLine(file)) {
  21. const lineA = linesA[index];
  22. index++;
  23. if (lineA == null) {
  24. // The file becomes smaller
  25. return false;
  26. }
  27. if (lineA[0] === '#' && lineB[0] === '#') {
  28. continue;
  29. }
  30. if (
  31. lineA[0] === '/'
  32. && lineA[1] === '/'
  33. && lineB[0] === '/'
  34. && lineB[1] === '/'
  35. && lineA[3] === '#'
  36. && lineB[3] === '#'
  37. ) {
  38. continue;
  39. }
  40. if (lineA !== lineB) {
  41. return false;
  42. }
  43. }
  44. if (index !== linesALen) {
  45. // The file becomes larger
  46. return false;
  47. }
  48. return true;
  49. });
  50. }
  51. if (isEqual) {
  52. console.log(picocolors.dim(`same content, bail out writing: ${filePath}`));
  53. return;
  54. }
  55. await span.traceChildAsync(`writing ${filePath}`, async () => {
  56. if (linesALen < 10000) {
  57. return Bun.write(file, `${linesA.join('\n')}\n`);
  58. }
  59. const writer = file.writer();
  60. for (let i = 0; i < linesALen; i++) {
  61. writer.write(linesA[i]);
  62. writer.write('\n');
  63. }
  64. return writer.end();
  65. });
  66. }
  67. export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
  68. return [
  69. '#########################################',
  70. `# ${title}`,
  71. `# Last Updated: ${date.toISOString()}`,
  72. `# Size: ${content.length}`,
  73. ...description.map(line => (line ? `# ${line}` : '#')),
  74. '#########################################',
  75. ...content,
  76. '################## EOF ##################'
  77. ];
  78. };
  79. const collectType = (rule: string) => {
  80. let buf = '';
  81. for (let i = 0, len = rule.length; i < len; i++) {
  82. if (rule[i] === ',') {
  83. return buf;
  84. }
  85. buf += rule[i];
  86. }
  87. return null;
  88. };
  89. const defaultSortTypeOrder = Symbol('defaultSortTypeOrder');
  90. const sortTypeOrder: Record<string | typeof defaultSortTypeOrder, number> = {
  91. DOMAIN: 1,
  92. 'DOMAIN-SUFFIX': 2,
  93. 'DOMAIN-KEYWORD': 10,
  94. 'USER-AGENT': 30,
  95. 'PROCESS-NAME': 40,
  96. [defaultSortTypeOrder]: 50, // default sort order for unknown type
  97. AND: 100,
  98. OR: 100,
  99. 'IP-CIDR': 200,
  100. 'IP-CIDR6': 200
  101. };
  102. // sort DOMAIN-SUFFIX and DOMAIN first, then DOMAIN-KEYWORD, then IP-CIDR and IP-CIDR6 if any
  103. export const sortRuleSet = (ruleSet: string[]) => ruleSet
  104. .map((rule) => {
  105. const type = collectType(rule);
  106. return [type ? (type in sortTypeOrder ? sortTypeOrder[type] : sortTypeOrder[defaultSortTypeOrder]) : 10, rule] as const;
  107. })
  108. .sort((a, b) => a[0] - b[0])
  109. .map(c => c[1]);
  110. const MARK = 'this_ruleset_is_made_by_sukkaw.ruleset.skk.moe';
  111. export const createRuleset = (
  112. parentSpan: Span,
  113. title: string, description: string[] | readonly string[], date: Date, content: string[],
  114. type: 'ruleset' | 'domainset', surgePath: string, clashPath: string
  115. ) => parentSpan.traceChild(`create ruleset: ${path.basename(surgePath, path.extname(surgePath))}`).traceAsyncFn((childSpan) => {
  116. const surgeContent = withBannerArray(
  117. title, description, date,
  118. sortRuleSet(type === 'domainset'
  119. ? [MARK, ...content]
  120. : [`DOMAIN,${MARK}`, ...content])
  121. );
  122. const clashContent = childSpan.traceChildSync('convert incoming ruleset to clash', () => {
  123. let _clashContent;
  124. switch (type) {
  125. case 'domainset':
  126. _clashContent = [MARK, ...surgeDomainsetToClashDomainset(content)];
  127. break;
  128. case 'ruleset':
  129. _clashContent = [`DOMAIN,${MARK}`, ...surgeRulesetToClashClassicalTextRuleset(content)];
  130. break;
  131. default:
  132. throw new TypeError(`Unknown type: ${type as any}`);
  133. }
  134. return withBannerArray(title, description, date, _clashContent);
  135. });
  136. return Promise.all([
  137. compareAndWriteFile(childSpan, surgeContent, surgePath),
  138. compareAndWriteFile(childSpan, clashContent, clashPath)
  139. ]);
  140. });