create-file.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // @ts-check
  2. const fs = require('fs');
  3. const fse = require('fs-extra');
  4. const { readFileByLine } = require('./fetch-remote-text-by-line');
  5. const { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } = require('./clash');
  6. /**
  7. * @param {string[]} linesA
  8. * @param {string} filePath
  9. */
  10. async function compareAndWriteFile(linesA, filePath) {
  11. await fse.ensureFile(filePath);
  12. let isEqual = true;
  13. let index = 0;
  14. for await (const lineB of readFileByLine(filePath)) {
  15. const lineA = linesA[index];
  16. index++;
  17. if (lineA[0] === '#' && lineB[0] === '#') {
  18. continue;
  19. }
  20. if (lineA !== lineB) {
  21. isEqual = false;
  22. break;
  23. }
  24. }
  25. if (!isEqual || index !== linesA.length) {
  26. const stream = fs.createWriteStream(filePath, { encoding: 'utf-8' });
  27. for (let i = 0, len = linesA.length; i < len; i++) {
  28. // eslint-disable-next-line no-await-in-loop -- backpressure
  29. await writeToStream(stream, linesA[i]);
  30. // eslint-disable-next-line no-await-in-loop -- backpressure
  31. await writeToStream(stream, '\n');
  32. }
  33. stream.end();
  34. } else {
  35. console.log(`Same Content, bail out writing: ${filePath}`);
  36. }
  37. }
  38. module.exports.compareAndWriteFile = compareAndWriteFile;
  39. /**
  40. * @param {import('fs').WriteStream} stream
  41. * @param {string} data
  42. */
  43. async function writeToStream(stream, data) {
  44. if (!stream.write(data)) {
  45. return /** @type {Promise<void>} */(new Promise((resolve) => {
  46. stream.once('drain', () => { resolve(); });
  47. }));
  48. }
  49. return Promise.resolve();
  50. }
  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;