create-file.js 2.9 KB

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