build-public.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import path from 'node:path';
  2. import fs from 'node:fs';
  3. import fsp from 'node:fs/promises';
  4. import { task } from './trace';
  5. import { treeDir, TreeFileType } from './lib/tree-dir';
  6. import type { TreeType, TreeTypeArray } from './lib/tree-dir';
  7. import { OUTPUT_MOCK_DIR, OUTPUT_MODULES_DIR, OUTPUT_MODULES_RULES_DIR, PUBLIC_DIR, ROOT_DIR } from './constants/dir';
  8. import { fastStringCompare, mkdirp, writeFile } from './lib/misc';
  9. import picocolors from 'picocolors';
  10. import { tagged as html } from 'foxts/tagged';
  11. import { compareAndWriteFile } from './lib/create-file';
  12. const mockDir = path.join(ROOT_DIR, 'Mock');
  13. const modulesDir = path.join(ROOT_DIR, 'Modules');
  14. async function copyDirContents(srcDir: string, destDir: string) {
  15. const promises: Array<Promise<void>> = [];
  16. for await (const entry of await fsp.opendir(srcDir)) {
  17. const src = path.join(srcDir, entry.name);
  18. const dest = path.join(destDir, entry.name);
  19. if (entry.isDirectory()) {
  20. console.warn(picocolors.red('[build public] cant copy directory'), src);
  21. } else {
  22. promises.push(fsp.copyFile(src, dest, fs.constants.COPYFILE_FICLONE));
  23. }
  24. }
  25. return Promise.all(promises);
  26. }
  27. export const buildPublic = task(require.main === module, __filename)(async (span) => {
  28. await span.traceChildAsync('copy rest of the files', async () => {
  29. await Promise.all([
  30. // mkdirp(OUTPUT_MODULES_DIR),
  31. mkdirp(OUTPUT_MODULES_RULES_DIR),
  32. mkdirp(OUTPUT_MOCK_DIR)
  33. ]);
  34. await Promise.all([
  35. copyDirContents(modulesDir, OUTPUT_MODULES_DIR),
  36. copyDirContents(mockDir, OUTPUT_MOCK_DIR)
  37. ]);
  38. });
  39. const html = await span
  40. .traceChild('generate index.html')
  41. .traceAsyncFn(() => treeDir(PUBLIC_DIR).then(generateHtml));
  42. await Promise.all([
  43. compareAndWriteFile(
  44. span,
  45. [
  46. '/*',
  47. ' cache-control: public, max-age=240, stale-while-revalidate=60, stale-if-error=15',
  48. 'https://:project.pages.dev/*',
  49. ' X-Robots-Tag: noindex',
  50. '/Modules/*',
  51. ' content-type: text/plain; charset=utf-8',
  52. '/List/*',
  53. ' content-type: text/plain; charset=utf-8',
  54. '/Internal/*',
  55. ' content-type: text/plain; charset=utf-8'
  56. ],
  57. path.join(PUBLIC_DIR, '_headers')
  58. ),
  59. compareAndWriteFile(
  60. span,
  61. [
  62. '# <pre>',
  63. '#########################################',
  64. '# Sukka\'s Ruleset - 404 Not Found',
  65. '################## EOF ##################</pre>'
  66. ],
  67. path.join(PUBLIC_DIR, '404.html')
  68. ),
  69. compareAndWriteFile(
  70. span,
  71. [
  72. '# The source code is located at [Sukkaw/Surge](https://github.com/Sukkaw/Surge)',
  73. '',
  74. '![GitHub repo size](https://img.shields.io/github/repo-size/sukkalab/ruleset.skk.moe?style=flat-square)'
  75. ],
  76. path.join(PUBLIC_DIR, 'README.md')
  77. )
  78. ]);
  79. return writeFile(path.join(PUBLIC_DIR, 'index.html'), html);
  80. });
  81. const priorityOrder: Record<'default' | string & {}, number> = {
  82. domainset: 1,
  83. non_ip: 2,
  84. ip: 3,
  85. List: 10,
  86. Surge: 11,
  87. Clash: 12,
  88. 'sing-box': 13,
  89. Modules: 20,
  90. Script: 30,
  91. Mock: 40,
  92. Assets: 50,
  93. Internal: 60,
  94. LICENSE: 70,
  95. default: Number.MAX_VALUE
  96. };
  97. const prioritySorter = (a: TreeType, b: TreeType) => ((priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default)) || fastStringCompare(a.name, b.name);
  98. function walk(tree: TreeTypeArray) {
  99. let result = '';
  100. tree.sort(prioritySorter);
  101. for (let i = 0, len = tree.length; i < len; i++) {
  102. const entry = tree[i];
  103. if (entry.type === TreeFileType.DIRECTORY) {
  104. result += html`
  105. <li class="folder">
  106. ${entry.name}
  107. <ul>
  108. ${walk(entry.children)}
  109. </ul>
  110. </li>
  111. `;
  112. } else if (/* entry.type === 'file' && */ entry.name !== 'index.html') {
  113. result += html`<li><a class="file directory-list-file" href="${entry.path}">${entry.name}</a></li>`;
  114. }
  115. }
  116. return result;
  117. }
  118. function generateHtml(tree: TreeTypeArray) {
  119. return html`
  120. <!DOCTYPE html>
  121. <html lang="en">
  122. <head>
  123. <meta charset="utf-8">
  124. <title>Surge Ruleset Server | Sukka (@SukkaW)</title>
  125. <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
  126. <link href="https://cdn.skk.moe/favicon.ico" rel="icon" type="image/ico">
  127. <link href="https://cdn.skk.moe/favicon/apple-touch-icon.png" rel="apple-touch-icon" sizes="180x180">
  128. <link href="https://cdn.skk.moe/favicon/android-chrome-192x192.png" rel="icon" type="image/png" sizes="192x192">
  129. <link href="https://cdn.skk.moe/favicon/favicon-32x32.png" rel="icon" type="image/png" sizes="32x32">
  130. <link href="https://cdn.skk.moe/favicon/favicon-16x16.png" rel="icon" type="image/png" sizes="16x16">
  131. <meta name="description" content="Sukka 自用的 Surge / Clash Premium 规则组">
  132. <link rel="stylesheet" href="https://cdn.skk.moe/ruleset/css/21d8777a.css" />
  133. <meta property="og:title" content="Surge Ruleset | Sukka (@SukkaW)">
  134. <meta property="og:type" content="Website">
  135. <meta property="og:url" content="https://ruleset.skk.moe/">
  136. <meta property="og:image" content="https://cdn.skk.moe/favicon/android-chrome-192x192.png">
  137. <meta property="og:description" content="Sukka 自用的 Surge / Clash Premium 规则组">
  138. <meta name="twitter:card" content="summary">
  139. <link rel="canonical" href="https://ruleset.skk.moe/">
  140. </head>
  141. <body>
  142. <main class="container">
  143. <h1>Sukka Ruleset Server</h1>
  144. <p>
  145. Made by <a href="https://skk.moe">Sukka</a> | <a href="https://github.com/SukkaW/Surge/">Source @ GitHub</a> | Licensed under <a href="/LICENSE" target="_blank">AGPL-3.0</a>
  146. </p>
  147. <p>Last Build: ${new Date().toISOString()}</p>
  148. <br>
  149. <ul class="directory-list">
  150. ${walk(tree)}
  151. </ul>
  152. </main>
  153. </body>
  154. </html>
  155. `;
  156. }