create-file.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @ts-check
  2. const fs = require('fs');
  3. const { readFileByLine } = require('./fetch-remote-text-by-line');
  4. const { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } = require('./clash');
  5. /**
  6. * @param {string[]} linesA
  7. * @param {string} filePath
  8. */
  9. async function compareAndWriteFile(linesA, filePath) {
  10. let isEqual = true;
  11. if (!fs.existsSync(filePath)) {
  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(filePath)) {
  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 file = Bun.file(filePath);
  41. const writer = file.writer();
  42. for (let i = 0, len = linesA.length; i < len; i++) {
  43. writer.write(`${linesA[i]}\n`);
  44. }
  45. await writer.end();
  46. return;
  47. }
  48. console.log(`Same Content, bail out writing: ${filePath}`);
  49. }
  50. module.exports.compareAndWriteFile = compareAndWriteFile;
  51. /**
  52. * @param {string} title
  53. * @param {string[]} description
  54. * @param {Date} date
  55. * @param {string[]} content
  56. * @returns {string[]}
  57. */
  58. const withBannerArray = (title, description, date, content) => {
  59. return [
  60. '########################################',
  61. `# ${title}`,
  62. `# Last Updated: ${date.toISOString()}`,
  63. `# Size: ${content.length}`,
  64. ...description.map(line => (line ? `# ${line}` : '#')),
  65. '########################################',
  66. ...content,
  67. '################# END ###################'
  68. ];
  69. };
  70. module.exports.withBannerArray = withBannerArray;
  71. /**
  72. * @param {string} title
  73. * @param {string[]} description
  74. * @param {Date} date
  75. * @param {string[]} content
  76. * @param {'ruleset' | 'domainset'} type
  77. * @param {string} surgePath
  78. * @param {string} clashPath
  79. */
  80. const createRuleset = (
  81. title, description, date, content,
  82. type, surgePath, clashPath
  83. ) => {
  84. const surgeContent = withBannerArray(title, description, date, content);
  85. let _clashContent;
  86. switch (type) {
  87. case 'domainset':
  88. _clashContent = surgeDomainsetToClashDomainset(content);
  89. break;
  90. case 'ruleset':
  91. _clashContent = surgeRulesetToClashClassicalTextRuleset(content);
  92. break;
  93. default:
  94. throw new TypeError(`Unknown type: ${type}`);
  95. }
  96. const clashContent = withBannerArray(title, description, date, _clashContent);
  97. return [
  98. compareAndWriteFile(surgeContent, surgePath),
  99. compareAndWriteFile(clashContent, clashPath)
  100. ];
  101. };
  102. module.exports.createRuleset = createRuleset;